From c6f9a9f352ccca5520262551887c2253394df9bf Mon Sep 17 00:00:00 2001 From: Hodor Date: Fri, 1 Nov 2024 17:02:21 +0800 Subject: [PATCH] wip: support confirm safe message --- .../__snapshots__/test.ts.snap | 60 +++++++++++++++++++ src/actions/confirmSafeMessage/fetchData.ts | 25 ++++++++ .../formatSecurityEngine.ts | 6 ++ src/actions/confirmSafeMessage/mocks.ts | 39 ++++++++++++ src/actions/confirmSafeMessage/parseAction.ts | 15 +++++ src/actions/confirmSafeMessage/test.ts | 56 +++++++++++++++++ src/types/parsedActionData.ts | 2 + 7 files changed, 203 insertions(+) create mode 100644 src/actions/confirmSafeMessage/__snapshots__/test.ts.snap create mode 100644 src/actions/confirmSafeMessage/fetchData.ts create mode 100644 src/actions/confirmSafeMessage/formatSecurityEngine.ts create mode 100644 src/actions/confirmSafeMessage/mocks.ts create mode 100644 src/actions/confirmSafeMessage/parseAction.ts create mode 100644 src/actions/confirmSafeMessage/test.ts diff --git a/src/actions/confirmSafeMessage/__snapshots__/test.ts.snap b/src/actions/confirmSafeMessage/__snapshots__/test.ts.snap new file mode 100644 index 0000000..051e3d0 --- /dev/null +++ b/src/actions/confirmSafeMessage/__snapshots__/test.ts.snap @@ -0,0 +1,60 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Confirm Safe Message: fetchDataConfirmSafeMessage 1`] = ` +{ + "contract": {}, + "id": "0x89f4a8e75da4b985bae9798162073fe5c037e9dd", +} +`; + +exports[`Confirm Safe Message: fetchDataConfirmSafeMessage 2`] = ` +{ + "bornAt": 1591388241, + "hasInteraction": true, + "id": "0x89f4a8e75da4b985bae9798162073fe5c037e9dd", + "protocol": { + "id": "uniswap2", + "logo_url": "https://static.debank.com/image/project/logo_url/uniswap2/87a541b3b83b041c8d12119e5a0d19f0.png", + "name": "Uniswap V2", + }, + "rank": 65, + "receiverInWallet": false, + "sender": "0x5853ed4f26a3fcea565b3fbc698bb19cdf6deb85", + "unexpectedAddr": null, +} +`; + +exports[`Confirm Safe Message: formatSecurityEngineConfirmSafeMessage 1`] = `{}`; + +exports[`Confirm Safe Message: formatSecurityEngineConfirmSafeMessage 2`] = ` +{ + "contractCall": { + "chainId": "eth", + "id": "0x89f4a8e75da4b985bae9798162073fe5c037e9dd", + }, +} +`; + +exports[`Confirm Safe Message: parseActionConfirmSafeMessage 1`] = ` +{ + "actionType": "confirm_safe_message", + "brand": undefined, + "chainId": "0x1", + "confirmSafeMessage": { + "multisig_id": "0x89f4a8e75da4b985bae9798162073fe5c037e9dd", + }, + "contractId": "0x89f4a8e75da4b985bae9798162073fe5c037e9dd", + "sender": "0x5853ed4f26a3fcea565b3fbc698bb19cdf6deb85", +} +`; + +exports[`Confirm Safe Message: parseActionConfirmSafeMessage 2`] = ` +{ + "actionType": "contractCall", + "brand": undefined, + "chainId": "0x1", + "contractCall": {}, + "contractId": "0x89f4a8e75da4b985bae9798162073fe5c037e9dd", + "sender": "0x5853ed4f26a3fcea565b3fbc698bb19cdf6deb85", +} +`; diff --git a/src/actions/confirmSafeMessage/fetchData.ts b/src/actions/confirmSafeMessage/fetchData.ts new file mode 100644 index 0000000..5930b83 --- /dev/null +++ b/src/actions/confirmSafeMessage/fetchData.ts @@ -0,0 +1,25 @@ +import { FetchActionRequiredData, MultiSigRequireData } from '../../types'; + +export const fetchDataConfirmSafeMessage: FetchActionRequiredData = async ( + options +) => { + if (options.type !== 'typed_data') { + return {}; + } + const { actionData, apiProvider } = options; + if (!actionData.confirmSafeMessage) { + return {}; + } + + const result: MultiSigRequireData = { + contract: null, + id: actionData.confirmSafeMessage.multisig_id, + }; + const { desc } = await apiProvider.addrDesc( + actionData.confirmSafeMessage.multisig_id + ); + if (desc.contract) { + result.contract = desc.contract; + } + return result; +}; diff --git a/src/actions/confirmSafeMessage/formatSecurityEngine.ts b/src/actions/confirmSafeMessage/formatSecurityEngine.ts new file mode 100644 index 0000000..198a3d3 --- /dev/null +++ b/src/actions/confirmSafeMessage/formatSecurityEngine.ts @@ -0,0 +1,6 @@ +import { FormatSecurityEngineContext } from '../../types'; + +export const formatSecurityEngineConfirmSafeMessage: FormatSecurityEngineContext = + async () => { + return {}; + }; diff --git a/src/actions/confirmSafeMessage/mocks.ts b/src/actions/confirmSafeMessage/mocks.ts new file mode 100644 index 0000000..0ca35e3 --- /dev/null +++ b/src/actions/confirmSafeMessage/mocks.ts @@ -0,0 +1,39 @@ +export const parseTxData = { + action: { + type: 'confirm_safe_message', + data: { + multisig_id: '0x89f4a8e75da4b985bae9798162073fe5c037e9dd', + }, + }, + log_id: 10317007, +} as any; + +export const txData = { + types: { + SafeMessage: [ + { + name: 'message', + type: 'bytes', + }, + ], + EIP712Domain: [ + { + name: 'chainId', + type: 'uint256', + }, + { + name: 'verifyingContract', + type: 'address', + }, + ], + }, + primaryType: 'SafeMessage', + domain: { + chainId: '0x1', + verifyingContract: '0x89f4a8e75da4b985bae9798162073fe5c037e9dd', + }, + message: { + message: + '0x2bf01edd92e8a198c95b799831abdae4f93a2428674d06dcf39b4a3e80b38a09', + }, +}; diff --git a/src/actions/confirmSafeMessage/parseAction.ts b/src/actions/confirmSafeMessage/parseAction.ts new file mode 100644 index 0000000..f72fa99 --- /dev/null +++ b/src/actions/confirmSafeMessage/parseAction.ts @@ -0,0 +1,15 @@ +import { ConfirmSafeMessageActions } from '@rabby-wallet/rabby-api/dist/types'; +import { PartialParseAction } from '../../types'; + +export const parseActionConfirmSafeMessage: PartialParseAction<'typed_data'> = ( + options +) => { + const { data } = options; + + if (data?.type !== 'confirm_safe_message') { + return {}; + } + return { + confirmSafeMessage: data.data as ConfirmSafeMessageActions, + }; +}; diff --git a/src/actions/confirmSafeMessage/test.ts b/src/actions/confirmSafeMessage/test.ts new file mode 100644 index 0000000..4462075 --- /dev/null +++ b/src/actions/confirmSafeMessage/test.ts @@ -0,0 +1,56 @@ +import { + apiProvider, + ETH_CHAIN_ID, + formatProvider, + ORIGIN, + SENDER, + walletProvider, +} from '../../../__mocks__'; +import { fetchDataConfirmSafeMessage } from './fetchData'; +import { formatSecurityEngineConfirmSafeMessage } from './formatSecurityEngine'; +import { parseTxData, txData } from './mocks'; +import { parseActionConfirmSafeMessage } from './parseAction'; +import { parseTypedDataAction } from '../../utils/parseTypedDataAction'; +import { + parseAction, + fetchActionRequiredData, + formatSecurityEngineContext, +} from '../..'; + +test.each([ + [ + parseTypedDataAction(parseActionConfirmSafeMessage), + fetchDataConfirmSafeMessage, + formatSecurityEngineConfirmSafeMessage, + ], + [parseAction, fetchActionRequiredData, formatSecurityEngineContext], +])('Confirm Safe Message', async (_parseAction, _fetchData, _format) => { + const actionData = _parseAction({ + type: 'typed_data', + data: parseTxData['action'], + typedData: txData, + sender: SENDER, + }); + expect(actionData).toMatchSnapshot('parseActionConfirmSafeMessage'); + + const requireData = await _fetchData({ + type: 'typed_data', + actionData, + chainId: ETH_CHAIN_ID, + walletProvider, + apiProvider, + sender: SENDER, + }); + expect(requireData).toMatchSnapshot('fetchDataConfirmSafeMessage'); + + const ctx = await _format({ + type: 'typed_data', + actionData, + requireData, + chainId: ETH_CHAIN_ID, + isTestnet: false, + provider: formatProvider, + origin: ORIGIN, + }); + expect(ctx).toMatchSnapshot('formatSecurityEngineConfirmSafeMessage'); +}); diff --git a/src/types/parsedActionData.ts b/src/types/parsedActionData.ts index f6b260b..ad41812 100644 --- a/src/types/parsedActionData.ts +++ b/src/types/parsedActionData.ts @@ -1,3 +1,4 @@ +import { ConfirmSafeMessageActions } from './../../node_modules/@rabby-wallet/rabby-api/dist/types.d'; import { TokenItem, NFTItem, @@ -154,6 +155,7 @@ export type ParsedTypedDataActionData = ParsedTransactionActionData & { sellNFT?: SellNFTOrderAction; batchSellNFT?: BatchSellNFTOrderAction; signMultiSig?: SignMultiSigActions; + confirmSafeMessage?: ConfirmSafeMessageActions; buyNFT?: BuyNFTOrderAction; permit?: PermitActionData; permit2?: Permit2ActionData;