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 0ea93d9 + efcb5d0 commit 412def1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/background/controller/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2462,7 +2462,7 @@ export class WalletController extends BaseController {
signerAddress: string;
signature: string;
}) => {
const sigs = await this.getGnosisTransactionSignatures();
const sigs = await this.getGnosisMessageSignatures();
if (sigs.length > 0) {
await wallet.addGnosisMessageSignature({
signature: signature,
Expand Down
18 changes: 13 additions & 5 deletions src/ui/views/Approval/components/SignText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,15 @@ const SignText = ({ params }: { params: SignTextProps }) => {
chainId,
message: signText,
});
useCheckCurrentSafeMessage(
const { data: currentSafeMessage } = useCheckCurrentSafeMessage(
{
chainId,
safeMessageHash,
threshold: safeInfo?.threshold,
},
{
onSuccess(safeMessage) {
if (safeMessage) {
onSuccess(res) {
if (res?.isFinished) {
const modal = Modal.info({
maskClosable: false,
closable: false,
Expand All @@ -339,7 +339,7 @@ const SignText = ({ params }: { params: SignTextProps }) => {
block
onClick={() => {
modal.destroy();
resolveApproval(safeMessage.preparedSignature);
resolveApproval(res.safeMessage.preparedSignature);
}}
className="text-[15px] h-[40px] rounded-[6px]"
>
Expand Down Expand Up @@ -493,6 +493,14 @@ const SignText = ({ params }: { params: SignTextProps }) => {
networkId: chainId + '',
message: signText,
});
await Promise.all(
(currentSafeMessage?.safeMessage?.confirmations || []).map((item) => {
return wallet.addPureGnosisMessageSignature({
signerAddress: item.owner,
signature: item.signature,
});
})
);
}

const typedData = generateTypedData({
Expand Down Expand Up @@ -584,10 +592,10 @@ const SignText = ({ params }: { params: SignTextProps }) => {
maskClosable
>
<GnosisDrawer
type="message"
safeInfo={safeInfo}
onCancel={handleDrawerCancel}
onConfirm={handleGnosisConfirm}
confirmations={currentSafeMessage?.safeMessage?.confirmations}
/>
</Drawer>
)}
Expand Down
19 changes: 13 additions & 6 deletions src/ui/views/Approval/components/SignTypedData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,16 +278,15 @@ const SignTypedData = ({ params }: { params: SignTypedDataProps }) => {
chainId: currentChainId,
message: signTypedData,
});

useCheckCurrentSafeMessage(
const { data: currentSafeMessage } = useCheckCurrentSafeMessage(
{
chainId: currentChainId,
safeMessageHash,
threshold: safeInfo?.threshold,
},
{
onSuccess(safeMessage) {
if (safeMessage) {
onSuccess(res) {
if (res?.isFinished) {
const modal = Modal.info({
maskClosable: false,
closable: false,
Expand All @@ -305,7 +304,7 @@ const SignTypedData = ({ params }: { params: SignTypedDataProps }) => {
block
onClick={() => {
modal.destroy();
resolveApproval(safeMessage.preparedSignature);
resolveApproval(res.safeMessage.preparedSignature);
}}
className="text-[15px] h-[40px] rounded-[6px]"
>
Expand Down Expand Up @@ -568,6 +567,14 @@ const SignTypedData = ({ params }: { params: SignTypedDataProps }) => {
networkId: currentChainId + '',
message: signTypedData,
});
await Promise.all(
(currentSafeMessage?.safeMessage?.confirmations || []).map((item) => {
return wallet.addPureGnosisMessageSignature({
signerAddress: item.owner,
signature: item.signature,
});
})
);
}

const typedData = generateTypedData({
Expand Down Expand Up @@ -738,10 +745,10 @@ const SignTypedData = ({ params }: { params: SignTypedDataProps }) => {
maskClosable
>
<GnosisDrawer
type="message"
safeInfo={safeInfo}
onCancel={handleDrawerCancel}
onConfirm={handleGnosisConfirm}
confirmations={currentSafeMessage?.safeMessage?.confirmations}
/>
</Drawer>
)}
Expand Down
38 changes: 24 additions & 14 deletions src/ui/views/Approval/components/TxComponents/GnosisDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { groupBy } from 'lodash';
import { BasicSafeInfo } from '@rabby-wallet/gnosis-sdk';
import { SafeMessage } from '@safe-global/api-kit';
import { Button } from 'antd';
import { Account } from 'background/service/preference';
import { useWallet, isSameAddress } from 'ui/utils';
import { BasicSafeInfo } from '@rabby-wallet/gnosis-sdk';
import { AddressItem, ownerPriority } from './DrawerAddressItem';
import clsx from 'clsx';
import { groupBy } from 'lodash';
import React, { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { isSameAddress, useWallet } from 'ui/utils';
import { AddressItem, ownerPriority } from './DrawerAddressItem';

interface GnosisDrawerProps {
// safeInfo: SafeInfo;
safeInfo: BasicSafeInfo;
onCancel(): void;
onConfirm(account: Account, isNew?: boolean): Promise<void> | void;
type?: 'transaction' | 'message';
confirmations?: SafeMessage['confirmations'];
}

interface Signature {
Expand All @@ -25,7 +26,7 @@ const GnosisDrawer = ({
safeInfo,
onCancel,
onConfirm,
type = 'transaction',
confirmations,
}: GnosisDrawerProps) => {
const wallet = useWallet();
const { t } = useTranslation();
Expand Down Expand Up @@ -91,19 +92,28 @@ const GnosisDrawer = ({
};

const init = async () => {
const sigs =
type === 'transaction'
? await wallet.getGnosisTransactionSignatures()
: await wallet.getGnosisMessageSignatures();

setSignatures(sigs);
sortOwners();
};

useEffect(() => {
init();
}, []);

useEffect(() => {
if (confirmations) {
setSignatures(
confirmations.map((item) => {
return {
signer: item.owner,
data: item.signature,
};
})
);
} else {
wallet.getGnosisTransactionSignatures().then(setSignatures);
}
}, [confirmations]);

return (
<div className="gnosis-drawer-container">
<div className="text-[18px] leading-[21px] font-medium text-r-neutral-title1 text-center mb-[16px]">
Expand Down
17 changes: 15 additions & 2 deletions src/ui/views/Approval/hooks/useCheckCurrentSafeMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export const useCheckCurrentSafeMessage = (
safeMessageHash?: string;
threshold?: number;
},
options?: Options<SafeMessage | undefined, any>
options?: Options<
| { safeMessage: SafeMessage; threshold: number; isFinished: boolean }
| undefined,
any
>
) => {
const wallet = useWallet();
const { t } = useTranslation();
Expand All @@ -35,8 +39,17 @@ export const useCheckCurrentSafeMessage = (
messageHash: safeMessageHash,
});
if (res.confirmations.length >= threshold) {
return res;
return {
safeMessage: res,
threshold,
isFinished: true,
};
}
return {
safeMessage: res,
threshold,
isFinished: false,
};
},
{
refreshDeps: [chainId, threshold, safeMessageHash],
Expand Down

0 comments on commit 412def1

Please sign in to comment.