Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: support confirm safe message #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/actions/confirmSafeMessage/__snapshots__/test.ts.snap
Original file line number Diff line number Diff line change
@@ -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",
}
`;
25 changes: 25 additions & 0 deletions src/actions/confirmSafeMessage/fetchData.ts
Original file line number Diff line number Diff line change
@@ -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;
};
6 changes: 6 additions & 0 deletions src/actions/confirmSafeMessage/formatSecurityEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { FormatSecurityEngineContext } from '../../types';

export const formatSecurityEngineConfirmSafeMessage: FormatSecurityEngineContext =
async () => {
return {};
};
39 changes: 39 additions & 0 deletions src/actions/confirmSafeMessage/mocks.ts
Original file line number Diff line number Diff line change
@@ -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',
},
};
15 changes: 15 additions & 0 deletions src/actions/confirmSafeMessage/parseAction.ts
Original file line number Diff line number Diff line change
@@ -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,
};
};
56 changes: 56 additions & 0 deletions src/actions/confirmSafeMessage/test.ts
Original file line number Diff line number Diff line change
@@ -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');
});
2 changes: 2 additions & 0 deletions src/types/parsedActionData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ConfirmSafeMessageActions } from './../../node_modules/@rabby-wallet/rabby-api/dist/types.d';
import {
TokenItem,
NFTItem,
Expand Down Expand Up @@ -154,6 +155,7 @@ export type ParsedTypedDataActionData = ParsedTransactionActionData & {
sellNFT?: SellNFTOrderAction;
batchSellNFT?: BatchSellNFTOrderAction;
signMultiSig?: SignMultiSigActions;
confirmSafeMessage?: ConfirmSafeMessageActions;
buyNFT?: BuyNFTOrderAction;
permit?: PermitActionData;
permit2?: Permit2ActionData;
Expand Down