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

fix: receive last transaction in AirGap when signing #2202

Merged
merged 2 commits into from
Apr 16, 2024
Merged
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
7 changes: 4 additions & 3 deletions src/background/controller/provider/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
13 changes: 6 additions & 7 deletions src/background/service/keyring/eth-keystone-keyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
}

Expand Down
5 changes: 5 additions & 0 deletions src/constant/timeout.ts
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -95,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);
Expand Down Expand Up @@ -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(() => {
Expand Down
Loading