From 3ed5407b48b9142c9be385daacbc924d2ccc6b93 Mon Sep 17 00:00:00 2001 From: MrNerdHair Date: Fri, 11 Feb 2022 21:48:58 -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 | 14 +++++--------- 3 files changed, 9 insertions(+), 12 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 56696a8ac..7902056c2 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..c097c9481 100644 --- a/packages/hdwallet-metamask/src/metamask.ts +++ b/packages/hdwallet-metamask/src/metamask.ts @@ -1,7 +1,6 @@ import * as core from "@shapeshiftoss/hdwallet-core"; import * as eth from "./ethereum"; import _ from "lodash"; -import detectEthereumProvider from "@metamask/detect-provider"; class MetaMaskTransport extends core.Transport { public async getDeviceID() { @@ -77,14 +76,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 {