Skip to content

Commit

Permalink
Refactor: message origin instead of safeAppId
Browse files Browse the repository at this point in the history
  • Loading branch information
katspaugh committed Dec 17, 2024
1 parent b942226 commit 0f71b7a
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@safe-global/protocol-kit": "^4.1.3",
"@safe-global/safe-apps-sdk": "^9.1.0",
"@safe-global/safe-client-gateway-sdk": "v1.60.1",
"@safe-global/safe-gateway-typescript-sdk": "3.22.4",
"@safe-global/safe-gateway-typescript-sdk": "3.22.6-beta.0",
"@safe-global/safe-modules-deployments": "^2.2.1",
"@sentry/react": "^7.91.0",
"@spindl-xyz/attribution-lite": "^1.4.0",
Expand Down
12 changes: 6 additions & 6 deletions src/components/tx-flow/flows/SignMessage/SignMessage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
safeAppId={25}
origin="http://localhost:3000"
/>,
)

Expand Down Expand Up @@ -253,7 +253,7 @@ describe('SignMessage', () => {
expect.objectContaining({
safe: extendedSafeInfo,
message: 'Hello world!',
safeAppId: 25,
origin: 'http://localhost:3000',
//onboard: expect.anything(),
}),
)
Expand Down Expand Up @@ -362,7 +362,7 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
safeAppId={25}
origin="http://localhost:3000"
/>,
)

Expand All @@ -384,7 +384,7 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
safeAppId={25}
origin="http://localhost:3000"
/>,
)

Expand All @@ -409,7 +409,7 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
safeAppId={25}
origin="http://localhost:3000"
/>,
)

Expand Down Expand Up @@ -491,7 +491,7 @@ describe('SignMessage', () => {
name="Test App"
message="Hello world!"
requestId="123"
safeAppId={25}
origin="http://localhost:3000"
/>,
)

Expand Down
15 changes: 4 additions & 11 deletions src/components/tx-flow/flows/SignMessage/SignMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -242,19 +242,12 @@ const SuccessCard = ({ safeMessage, onContinue }: { safeMessage: SafeMessage; on

type BaseProps = Pick<SafeMessage, 'logoUri' | 'name' | 'message'>

// Custom Safe Apps do not have a `safeAppId`
export type ProposeProps = BaseProps & {
safeAppId?: number
requestId: RequestId
}

// A proposed message does not return the `safeAppId` but the `logoUri` and `name` of the Safe App that proposed it
export type ConfirmProps = BaseProps & {
safeAppId?: never
export type SignMessageProps = BaseProps & {
origin?: string
requestId?: RequestId
}

const SignMessage = ({ message, safeAppId, requestId }: ProposeProps | ConfirmProps): ReactElement => {
const SignMessage = ({ message, origin, requestId }: SignMessageProps): ReactElement => {
// Hooks & variables
const { setTxFlow } = useContext(TxModalContext)
const { setSafeMessage: setContextSafeMessage } = useContext(SafeTxContext)
Expand Down Expand Up @@ -283,7 +276,7 @@ const SignMessage = ({ message, safeAppId, requestId }: ProposeProps | ConfirmPr
decodedMessage,
safeMessageHash,
requestId,
safeAppId,
origin,
() => setTxFlow(undefined),
)

Expand Down
4 changes: 2 additions & 2 deletions src/components/tx-flow/flows/SignMessage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import TxLayout from '@/components/tx-flow/common/TxLayout'
import SignMessage, { type ConfirmProps, type ProposeProps } from '@/components/tx-flow/flows/SignMessage/SignMessage'
import SignMessage, { type SignMessageProps } from '@/components/tx-flow/flows/SignMessage/SignMessage'
import { getSwapTitle } from '@/features/swap'
import { selectSwapParams } from '@/features/swap/store/swapParamsSlice'
import { useAppSelector } from '@/store'
Expand Down Expand Up @@ -47,7 +47,7 @@ export const AppTitle = ({
)
}

const SignMessageFlow = ({ ...props }: ProposeProps | ConfirmProps) => {
const SignMessageFlow = ({ ...props }: SignMessageProps) => {
return (
<TxLayout
title="Confirm message"
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/messages/useSyncSafeMessageSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const useSyncSafeMessageSigner = (
decodedMessage: string | EIP712TypedData,
safeMessageHash: string,
requestId: string | undefined,
safeAppId: number | undefined,
origin: string | undefined,
onClose: () => void,
) => {
const [submitError, setSubmitError] = useState<Error | undefined>()
Expand All @@ -60,7 +60,7 @@ const useSyncSafeMessageSigner = (
try {
// When collecting the first signature
if (!message) {
await dispatchSafeMsgProposal({ provider: wallet.provider, safe, message: decodedMessage, safeAppId })
await dispatchSafeMsgProposal({ provider: wallet.provider, safe, message: decodedMessage, origin })

// Fetch updated message
const updatedMsg = await fetchSafeMessage(safeMessageHash, safe.chainId)
Expand All @@ -86,7 +86,7 @@ const useSyncSafeMessageSigner = (
} catch (e) {
setSubmitError(asError(e))
}
}, [wallet, safe, message, decodedMessage, safeAppId, safeMessageHash, onClose, requestId])
}, [wallet, safe, message, decodedMessage, origin, safeMessageHash, onClose, requestId])

return { submitError, onSign }
}
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/safe-apps/useCustomAppCommunicator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const useCustomAppCommunicator = (
logoUri={appData?.iconUrl || ''}
name={appData?.name || ''}
message={message}
safeAppId={appData?.id}
origin={appData?.url}
requestId={requestId}
/>,
onTxFlowClose,
Expand Down
24 changes: 12 additions & 12 deletions src/services/safe-messages/__tests__/safeMsgSender.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ describe('safeMsgSender', () => {
},
} as unknown as gateway.SafeInfo
const message = 'Hello world'
const safeAppId = 1
const origin = 'http://example.com'

await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, safeAppId })
await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, origin })

expect(proposeSafeMessageSpy).toHaveBeenCalledWith('5', zeroPadValue('0x0789', 20), {
message,
signature: mockValidSignature,
safeAppId,
origin,
})

expect(safeMsgDispatchSpy).toHaveBeenCalledWith(events.SafeMsgEvent.PROPOSE, {
Expand Down Expand Up @@ -88,9 +88,9 @@ describe('safeMsgSender', () => {
test: 'Hello World!',
},
}
const safeAppId = 1
const origin = 'http://example.com'

await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, safeAppId })
await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, origin })

// Normalize message manually
message.types['EIP712Domain'] = [
Expand All @@ -103,7 +103,7 @@ describe('safeMsgSender', () => {
expect(proposeSafeMessageSpy).toHaveBeenCalledWith('5', zeroPadValue('0x0789', 20), {
message,
signature: mockValidSignature,
safeAppId,
origin,
})
})

Expand All @@ -125,15 +125,15 @@ describe('safeMsgSender', () => {
},
} as unknown as gateway.SafeInfo
const message = 'Hello world'
const safeAppId = 1
const origin = 'http://example.com'

await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, safeAppId })
await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, origin })

expect(proposeSafeMessageSpy).toHaveBeenCalledWith('5', zeroPadValue('0x0789', 20), {
message,
// Even though the mock returns the signature with invalid V, the valid signature should get dispatched as we adjust invalid Vs
signature: mockValidSignature,
safeAppId,
origin,
})

expect(safeMsgDispatchSpy).toHaveBeenCalledWith(events.SafeMsgEvent.PROPOSE, {
Expand All @@ -155,17 +155,17 @@ describe('safeMsgSender', () => {
},
} as unknown as gateway.SafeInfo
const message = 'Hello world'
const safeAppId = 1
const origin = 'http://example.com'

try {
await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, safeAppId })
await dispatchSafeMsgProposal({ provider: MockEip1193Provider, safe, message, origin })
} catch (e) {
expect((e as Error).message).toBe('Example error')

expect(proposeSafeMessageSpy).toHaveBeenCalledWith('5', zeroPadValue('0x0789', 20), {
message,
signature: mockValidSignature,
safeAppId,
origin,
})

expect(safeMsgDispatchSpy).toHaveBeenCalledWith(events.SafeMsgEvent.PROPOSE_FAILED, {
Expand Down
6 changes: 3 additions & 3 deletions src/services/safe-messages/safeMsgSender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export const dispatchSafeMsgProposal = async ({
provider,
safe,
message,
safeAppId,
origin = '',
}: {
provider: Eip1193Provider
safe: SafeInfo
message: SafeMessage['message']
safeAppId?: number
origin: string | undefined
}): Promise<void> => {
const messageHash = generateSafeMessageHash(safe, message)

Expand All @@ -33,7 +33,7 @@ export const dispatchSafeMsgProposal = async ({
await proposeSafeMessage(safe.chainId, safe.address.value, {
message: normalizedMessage,
signature,
safeAppId,
origin,
})
} catch (error) {
safeMsgDispatch(SafeMsgEvent.PROPOSE_FAILED, {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ export const useTxFlowApi = (chainId: string, safeAddress: string): WalletSDK |
<SignMessageFlow
logoUri={appInfo.iconUrl}
name={appInfo.name}
origin={appInfo.url}
message={message}
requestId={id}
safeAppId={appInfo.id}
/>,
onClose,
)
Expand Down
4 changes: 2 additions & 2 deletions src/tests/mocks/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const contractAddresses = {
simulateTxAccessorAddress: null,
}

const CONFIG_SERVICE_CHAINS: ChainInfo[] = [
const CONFIG_SERVICE_CHAINS = [
{
transactionService: 'https://safe-transaction.mainnet.gnosis.io',
contractAddresses,
Expand Down Expand Up @@ -648,6 +648,6 @@ const CONFIG_SERVICE_CHAINS: ChainInfo[] = [
enabled: false,
},
},
]
] as unknown as ChainInfo[]

export { CONFIG_SERVICE_CHAINS }
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4598,10 +4598,10 @@
dependencies:
semver "^7.6.2"

"@safe-global/[email protected].4":
version "3.22.4"
resolved "https://registry.yarnpkg.com/@safe-global/safe-gateway-typescript-sdk/-/safe-gateway-typescript-sdk-3.22.4.tgz#9109a538df40f778666a3e6776e7a08c757e893d"
integrity sha512-Z7Z8w3GEJdJ/paF+NK23VN4AwqWPadq0AeRYjYLjIBiPWpRB2UO/FKq7ONABEq0YFgNPklazIV4IExQU1gavXA==
"@safe-global/[email protected].6-beta.0":
version "3.22.6-beta.0"
resolved "https://registry.yarnpkg.com/@safe-global/safe-gateway-typescript-sdk/-/safe-gateway-typescript-sdk-3.22.6-beta.0.tgz#a71085f4084201bd0782f2e35f1222d9458b7dcc"
integrity sha512-Uix0cLfPahy1nGlfda5yA4vC/XS1yeKyQJQF9W2KE4kuCzSegLtdYWkBqCPQtRQj2snSlWk511paRzfa7QtGvA==

"@safe-global/safe-gateway-typescript-sdk@^3.5.3":
version "3.21.2"
Expand Down

0 comments on commit 0f71b7a

Please sign in to comment.