Skip to content

Commit

Permalink
Merge branch 'feat/safe-sign-text' into tmp/20241101
Browse files Browse the repository at this point in the history
  • Loading branch information
cs1707 committed Nov 1, 2024
2 parents 37c4c81 + 75ecc6e commit 8d732fd
Show file tree
Hide file tree
Showing 41 changed files with 2,451 additions and 313 deletions.
9 changes: 7 additions & 2 deletions _raw/locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,8 @@
"createKey": {
"interactDapp": "Interact Dapp",
"description": "Description"
}
},
"sameSafeMessageAlert": "The same message is confirmed; no additional signature is required."
},
"securityEngine": {
"yes": "Yes",
Expand Down Expand Up @@ -1867,7 +1868,7 @@
"requestBtn": "Request"
},
"safeQueue": {
"title": "Queue",
"title": "Queue ({{total}})",
"sameNonceWarning": "These transactions conflict as they use the same nonce. Executing one will automatically replace the other(s).",
"loading": "Loading pending transactions",
"noData": "No pending transactions",
Expand Down Expand Up @@ -2264,6 +2265,10 @@
"title": "Switch address before deposit",
"desc": "Please switch to your login address."
}
},
"safeMessageQueue": {
"loading": "Loading messages",
"noData": "No messages"
}
},
"component": {
Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"@rabby-wallet/eth-trezor-keyring": "2.6.1",
"@rabby-wallet/eth-walletconnect-keyring": "2.1.3",
"@rabby-wallet/eth-watch-keyring": "1.0.0",
"@rabby-wallet/gnosis-sdk": "1.3.8",
"@rabby-wallet/gnosis-sdk": "1.3.9-alpha.0",
"@rabby-wallet/page-provider": "0.4.2",
"@rabby-wallet/rabby-action": "0.1.4",
"@rabby-wallet/rabby-api": "0.8.3",
Expand All @@ -67,6 +67,10 @@
"@rabby-wallet/widgets": "1.0.9",
"@rematch/core": "2.2.0",
"@rematch/select": "3.1.2",
"@safe-global/api-kit": "^2.5.3",
"@safe-global/protocol-kit": "^5.0.3",
"@safe-global/sdk-starter-kit": "^1.0.3",
"@safe-global/types-kit": "^1.0.0",
"@scure/bip39": "1.2.1",
"@sentry/browser": "6.7.2",
"@sentry/react": "6.7.2",
Expand Down
44 changes: 42 additions & 2 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
CustomTestnetTokenBase,
customTestnetService,
} from '@/background/service/customTestnet';
import { isString } from 'lodash';

const reportSignText = (params: {
method: string;
Expand Down Expand Up @@ -138,8 +139,9 @@ const signTypedDataVlidation = ({
} catch (e) {
throw ethErrors.rpc.invalidParams('data is not a validate JSON string');
}
const currentChain = permissionService.getConnectedSite(session.origin)
?.chain;
const currentChain = permissionService.isInternalOrigin(session.origin)
? findChain({ id: jsonData.domain.chainId })?.enum
: permissionService.getConnectedSite(session.origin)?.chain;
if (jsonData.domain.chainId) {
const chainItem = findChainByEnum(currentChain);
if (
Expand Down Expand Up @@ -813,18 +815,32 @@ class ProviderController extends BaseController {
},
])
personalSign = async ({ data, approvalRes, session }) => {
console.log('personal sign', {
data,
approvalRes,
session,
});

if (!data.params) return;

const currentAccount = preferenceService.getCurrentAccount()!;
if (
currentAccount.type === KEYRING_TYPE.GnosisKeyring &&
isString(approvalRes)
) {
return approvalRes;
}
try {
const [string, from] = data.params;
const hex = isHexString(string) ? string : stringToHex(string);
const keyring = await this._checkAddress(from);
// todo
const result = await keyringService.signPersonalMessage(
keyring,
{ data: hex, from },
approvalRes?.extra
);

signTextHistoryService.createHistory({
address: from,
text: string,
Expand Down Expand Up @@ -873,6 +889,12 @@ class ProviderController extends BaseController {
approvalRes,
}) => {
const currentAccount = preferenceService.getCurrentAccount()!;
if (
currentAccount.type === KEYRING_TYPE.GnosisKeyring &&
isString(approvalRes)
) {
return approvalRes;
}
try {
const result = await this._signTypedData(
from,
Expand Down Expand Up @@ -911,6 +933,12 @@ class ProviderController extends BaseController {
approvalRes,
}) => {
const currentAccount = preferenceService.getCurrentAccount()!;
if (
currentAccount.type === KEYRING_TYPE.GnosisKeyring &&
isString(approvalRes)
) {
return approvalRes;
}
try {
const result = await this._signTypedData(
from,
Expand Down Expand Up @@ -949,6 +977,12 @@ class ProviderController extends BaseController {
approvalRes,
}) => {
const currentAccount = preferenceService.getCurrentAccount()!;
if (
currentAccount.type === KEYRING_TYPE.GnosisKeyring &&
isString(approvalRes)
) {
return approvalRes;
}
try {
const result = await this._signTypedData(
from,
Expand Down Expand Up @@ -987,6 +1021,12 @@ class ProviderController extends BaseController {
approvalRes,
}) => {
const currentAccount = preferenceService.getCurrentAccount()!;
if (
currentAccount.type === KEYRING_TYPE.GnosisKeyring &&
isString(approvalRes)
) {
return approvalRes;
}
try {
const result = await this._signTypedData(
from,
Expand Down
39 changes: 39 additions & 0 deletions src/background/controller/provider/gnosisController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { createSafeService } from '@/background/utils/safe';
import { poll } from '@/utils/poll';

export const gnosisController = {
watchMessage: async ({
address,
chainId,
safeMessageHash,
pollingInterval = 10000,
}: {
address: string;
chainId: number;
safeMessageHash: string;
pollingInterval?: number;
}) => {
const safe = await createSafeService({
address,
networkId: String(chainId),
});
const threshold = await safe.getThreshold();

// todo: manual destroy
return new Promise((resolve, reject) => {
poll(
async ({ unpoll }) => {
const res = await safe.apiKit.getMessage(safeMessageHash);
if (res.confirmations.length >= threshold) {
resolve(res.preparedSignature);
unpoll();
}
},
{
interval: pollingInterval,
emitOnBegin: true,
}
);
});
},
};
31 changes: 29 additions & 2 deletions src/background/controller/provider/rpcFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import stats from '@/stats';
import { addHexPrefix, stripHexPrefix } from 'ethereumjs-util';
import { findChain } from '@/utils/chain';
import { waitSignComponentAmounted } from '@/utils/signEvent';
import { gnosisController } from './gnosisController';

const isSignApproval = (type: string) => {
const SIGN_APPROVALS = ['SignText', 'SignTypedData', 'SignTx'];
Expand Down Expand Up @@ -234,7 +235,12 @@ const flowContext = flow
waitSignComponentPromise = waitSignComponentAmounted();
}

if (approvalRes?.isGnosis) return resolve(undefined);
// if (approvalRes?.isGnosis && !approvalRes.safeMessage) {
// return resolve(undefined);
// }
if (approvalRes?.isGnosis) {
return resolve(undefined);
}

return waitSignComponentPromise.then(() =>
Promise.resolve(
Expand Down Expand Up @@ -290,11 +296,32 @@ const flowContext = flow
return res;
}
}
console.log({ approvalRes, mapMethod, approvalType, request });
if (uiRequestComponent) {
ctx.request.requestedApproval = true;
const result = await requestApprovalLoop({ uiRequestComponent, ...rest });
reportStatsData();
return result;
if (rest.safeMessage) {
const safeMessage: {
safeAddress: string;
message: string | Record<string, any>;
chainId: number;
safeMessageHash: string;
} = rest.safeMessage;
if (ctx.request.requestedApproval) {
flow.requestedApproval = false;
// only unlock notification if current flow is an approval flow
notificationService.unLock();
keyringService.resetResend();
}
return gnosisController.watchMessage({
address: safeMessage.safeAddress,
chainId: safeMessage.chainId,
safeMessageHash: safeMessage.safeMessageHash,
});
} else {
return result;
}
}

return requestDefer;
Expand Down
Loading

0 comments on commit 8d732fd

Please sign in to comment.