From e615e8f1700b2e42c25d8ba3d286f386fe3c27a0 Mon Sep 17 00:00:00 2001 From: MrNerdHair Date: Fri, 11 Feb 2022 16:55:47 -0500 Subject: [PATCH] metamask: only detectEthereumProvider() once --- integration/src/wallets/metamask.ts | 5 +++-- packages/hdwallet-metamask/src/adapter.ts | 2 +- packages/hdwallet-metamask/src/metamask.ts | 13 +++++-------- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/integration/src/wallets/metamask.ts b/integration/src/wallets/metamask.ts index f866f5198..f63aac2b4 100644 --- a/integration/src/wallets/metamask.ts +++ b/integration/src/wallets/metamask.ts @@ -10,8 +10,9 @@ export function createInfo(): core.HDWalletInfo { } export async function createWallet(): Promise { - const wallet = new metamask.MetaMaskHDWallet(); - await wallet.initialize(); + const keyring = new core.Keyring(); + const adapter = metamask.MetaMaskAdapter.useKeyring(keyring); + const wallet = await adapter.pairDevice() return wallet; } diff --git a/packages/hdwallet-metamask/src/adapter.ts b/packages/hdwallet-metamask/src/adapter.ts index f1d674dd9..b5422813e 100644 --- a/packages/hdwallet-metamask/src/adapter.ts +++ b/packages/hdwallet-metamask/src/adapter.ts @@ -35,7 +35,7 @@ export class MetaMaskAdapter { throw error; } const wallet = new MetaMaskHDWallet(); - await wallet.initialize(); + await wallet.initialize(provider); const deviceID = await wallet.getDeviceID(); this.keyring.add(wallet, deviceID); this.currentDeviceID = deviceID; diff --git a/packages/hdwallet-metamask/src/metamask.ts b/packages/hdwallet-metamask/src/metamask.ts index 73440a9c3..8f38c8139 100644 --- a/packages/hdwallet-metamask/src/metamask.ts +++ b/packages/hdwallet-metamask/src/metamask.ts @@ -77,14 +77,11 @@ export class MetaMaskHDWallet implements core.HDWallet, core.ETHWallet { return Promise.resolve("MetaMask"); } - public async initialize(): Promise { - try { - this.provider = await detectEthereumProvider({ mustBeMetaMask: true, silent: false, timeout: 3000 }); - } catch (e) { - console.error(e); - } - - return Promise.resolve(); + public initialize(): never; + public initialize(provider: unknown): Promise; + public async initialize(provider?: unknown): Promise { + if (!provider) throw new Error("provider is required"); + this.provider = provider; } public hasOnDevicePinEntry(): boolean {