From c72db194c4aabd7feee99c49d03690804a4b38a6 Mon Sep 17 00:00:00 2001 From: heisenberg Date: Mon, 15 Apr 2024 12:26:56 +0800 Subject: [PATCH 1/2] fix: receive last transaction in AirGap when signing --- src/background/controller/provider/controller.ts | 7 ++++--- src/constant/timeout.ts | 5 +++++ .../components/QRHardWareWaiting/QRHardWareWaiting.tsx | 6 +++++- 3 files changed, 14 insertions(+), 4 deletions(-) create mode 100644 src/constant/timeout.ts diff --git a/src/background/controller/provider/controller.ts b/src/background/controller/provider/controller.ts index e34777b2ef1..b3a6e45ebf8 100644 --- a/src/background/controller/provider/controller.ts +++ b/src/background/controller/provider/controller.ts @@ -56,6 +56,7 @@ import eventBus from '@/eventBus'; import { StatsData } from '../../service/notification'; import { customTestnetService } from '@/background/service/customTestnet'; import { sendTransaction } from 'viem/actions'; +import { SIGN_TIMEOUT } from '@/constant/timeout'; // import { customTestnetService } from '@/background/service/customTestnet'; const reportSignText = (params: { @@ -440,7 +441,7 @@ class ProviderController extends BaseController { const chainItem = findChainByEnum(chain); // wait ui - await new Promise((r) => setTimeout(r, 100)); + await new Promise((r) => setTimeout(r, SIGN_TIMEOUT)); const statsData: StatsData = { signed: false, @@ -777,7 +778,7 @@ class ProviderController extends BaseController { if (!data.params) return; // wait ui - await new Promise((r) => setTimeout(r, 100)); + await new Promise((r) => setTimeout(r, SIGN_TIMEOUT)); const currentAccount = preferenceService.getCurrentAccount()!; try { @@ -822,7 +823,7 @@ class ProviderController extends BaseController { } // wait ui - await new Promise((r) => setTimeout(r, 100)); + await new Promise((r) => setTimeout(r, SIGN_TIMEOUT)); return keyringService.signTypedMessage( keyring, diff --git a/src/constant/timeout.ts b/src/constant/timeout.ts new file mode 100644 index 00000000000..b39d1470f09 --- /dev/null +++ b/src/constant/timeout.ts @@ -0,0 +1,5 @@ +/** + * When calling signatures, it is not possible to receive event messages in time, + if the UI has not been initialized + */ +export const SIGN_TIMEOUT = 100; diff --git a/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx b/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx index b05e8dca0d1..00dcf41b368 100644 --- a/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx +++ b/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx @@ -25,6 +25,7 @@ import { KeystoneWiredWaiting, } from './KeystoneWaiting'; import clsx from 'clsx'; +import { SIGN_TIMEOUT } from '@/constant/timeout'; const KEYSTONE_TYPE = HARDWARE_KEYRING_TYPES.Keystone.type; enum QRHARDWARE_STATUS { @@ -147,7 +148,10 @@ const QRHardWareWaiting = ({ params }) => { // rejectApproval(data.errorMsg); } }); - await wallet.acquireKeystoneMemStoreData(); + // Wait for the keyring to have called the signature method + setTimeout(() => { + wallet.acquireKeystoneMemStoreData(); + }, SIGN_TIMEOUT); }, []); React.useEffect(() => { From 26dc658a35d92e6c9483f39dd757f1eaecc5ffb1 Mon Sep 17 00:00:00 2001 From: heisenberg Date: Tue, 16 Apr 2024 16:41:14 +0800 Subject: [PATCH 2/2] fix: keystone close #2203 --- .../service/keyring/eth-keystone-keyring.ts | 13 ++++++------ .../QRHardWareWaiting/QRHardWareWaiting.tsx | 20 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/background/service/keyring/eth-keystone-keyring.ts b/src/background/service/keyring/eth-keystone-keyring.ts index b66a1e33e12..592e941c3ba 100644 --- a/src/background/service/keyring/eth-keystone-keyring.ts +++ b/src/background/service/keyring/eth-keystone-keyring.ts @@ -47,13 +47,12 @@ export default class KeystoneKeyring extends MetaMaskKeyring { constructor() { super(); - this.getMemStore().subscribe((data) => { - const request = data.sign?.request; - this.getInteraction().on(AcquireMemeStoreData, () => { - if (request) { - this.getInteraction().emit(MemStoreDataReady, request); - } - }); + this.getInteraction().on(AcquireMemeStoreData, () => { + const request = this.getMemStore().getState().sign?.request; + + if (request) { + this.getInteraction().emit(MemStoreDataReady, request); + } }); } diff --git a/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx b/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx index 00dcf41b368..2887ccfb7c9 100644 --- a/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx +++ b/src/ui/views/Approval/components/QRHardWareWaiting/QRHardWareWaiting.tsx @@ -96,18 +96,18 @@ const QRHardWareWaiting = ({ params }) => { params.isGnosis ? true : approval?.data.approvalType !== 'SignTx' ); - let currentSignId = null; - if (account.brandName === WALLET_BRAND_TYPES.KEYSTONE) { - currentSignId = await wallet.requestKeyring( - KEYSTONE_TYPE, - 'exportCurrentSignRequestIdIfExist', - null - ); - } - eventBus.addEventListener( EVENTS.QRHARDWARE.ACQUIRE_MEMSTORE_SUCCEED, - ({ request }) => { + async ({ request }) => { + let currentSignId = null; + if (account.brandName === WALLET_BRAND_TYPES.KEYSTONE) { + currentSignId = await wallet.requestKeyring( + KEYSTONE_TYPE, + 'exportCurrentSignRequestIdIfExist', + null + ); + } + if (currentSignId) { if (currentSignId === request.requestId) { setSignPayload(request);