From 6beeb13f1c8172bff833994507f37a406b6f3d45 Mon Sep 17 00:00:00 2001 From: MrNerdHair Date: Tue, 22 Mar 2022 21:22:27 -0400 Subject: [PATCH] metamask: only detectEthereumProvider() once --- integration/src/integration.ts | 3 --- integration/src/wallets/metamask.ts | 27 +++++++++++++++++-- .../mocks/@metamask/detect-provider.ts | 23 ---------------- packages/hdwallet-metamask/src/adapter.ts | 2 +- packages/hdwallet-metamask/src/metamask.ts | 22 +++------------ 5 files changed, 30 insertions(+), 47 deletions(-) delete mode 100644 integration/src/wallets/mocks/@metamask/detect-provider.ts diff --git a/integration/src/integration.ts b/integration/src/integration.ts index 8535ac086..841ab953c 100644 --- a/integration/src/integration.ts +++ b/integration/src/integration.ts @@ -19,11 +19,8 @@ import { rippleTests } from "./ripple"; import { secretTests } from "./secret"; import { terraTests } from "./terra"; import { thorchainTests } from "./thorchain"; -import { ethereum } from "./wallets/mocks/@metamask/detect-provider"; import { WalletSuite } from "./wallets/suite"; -jest.mock("@metamask/detect-provider", () => async () => Promise.resolve(ethereum)); - /** * We run all the integration tests against every device, even though some * devices might not support a given Coin mixin. Tests in the various suites diff --git a/integration/src/wallets/metamask.ts b/integration/src/wallets/metamask.ts index 560096e5a..caae2f682 100644 --- a/integration/src/wallets/metamask.ts +++ b/integration/src/wallets/metamask.ts @@ -6,11 +6,34 @@ export function name(): string { } export function createInfo(): core.HDWalletInfo { - return metamask.info(); + return new metamask.MetaMaskHDWalletInfo(); } export async function createWallet(): Promise { - const wallet = new metamask.MetaMaskHDWallet(); + const provider = { + request: jest.fn(({ method, params }: any) => { + switch (method) { + case "eth_accounts": + return ["0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8"]; + case "personal_sign": { + const [message] = params; + + if (message === "48656c6c6f20576f726c64") + return "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b"; + + throw new Error("unknown message"); + } + case "eth_sendTransaction": { + const [{ to }] = params; + + return `txHash-${to}`; + } + default: + throw new Error(`ethereum: Unkown method ${method}`); + } + }), + }; + const wallet = new metamask.MetaMaskHDWallet(provider); await wallet.initialize(); return wallet; } diff --git a/integration/src/wallets/mocks/@metamask/detect-provider.ts b/integration/src/wallets/mocks/@metamask/detect-provider.ts deleted file mode 100644 index 72b204a75..000000000 --- a/integration/src/wallets/mocks/@metamask/detect-provider.ts +++ /dev/null @@ -1,23 +0,0 @@ -export const ethereum = { - request: jest.fn(({ method, params }: any) => { - switch (method) { - case "eth_accounts": - return ["0x3f2329C9ADFbcCd9A84f52c906E936A42dA18CB8"]; - case "personal_sign": { - const [message] = params; - - if (message === "48656c6c6f20576f726c64") - return "0x29f7212ecc1c76cea81174af267b67506f754ea8c73f144afa900a0d85b24b21319621aeb062903e856352f38305710190869c3ce5a1425d65ef4fa558d0fc251b"; - - throw new Error("unknown message"); - } - case "eth_sendTransaction": { - const [{ to }] = params; - - return `txHash-${to}`; - } - default: - throw new Error(`ethereum: Unkown method ${method}`); - } - }), -}; diff --git a/packages/hdwallet-metamask/src/adapter.ts b/packages/hdwallet-metamask/src/adapter.ts index 9e9ea6461..20f26e264 100644 --- a/packages/hdwallet-metamask/src/adapter.ts +++ b/packages/hdwallet-metamask/src/adapter.ts @@ -35,7 +35,7 @@ export class MetaMaskAdapter { console.error("Could not get MetaMask accounts. "); throw error; } - const wallet = new MetaMaskHDWallet(); + const wallet = new MetaMaskHDWallet(provider); await wallet.initialize(); const deviceID = await wallet.getDeviceID(); this.keyring.add(wallet, deviceID); diff --git a/packages/hdwallet-metamask/src/metamask.ts b/packages/hdwallet-metamask/src/metamask.ts index 71822c09f..223962155 100644 --- a/packages/hdwallet-metamask/src/metamask.ts +++ b/packages/hdwallet-metamask/src/metamask.ts @@ -1,4 +1,3 @@ -import detectEthereumProvider from "@metamask/detect-provider"; import * as core from "@shapeshiftoss/hdwallet-core"; import _ from "lodash"; @@ -135,8 +134,9 @@ export class MetaMaskHDWallet implements core.HDWallet, core.ETHWallet { ethAddress?: string | null; provider: any; - constructor() { + constructor(provider: unknown) { this.info = new MetaMaskHDWalletInfo(); + this.provider = provider; } async getFeatures(): Promise> { @@ -159,14 +159,8 @@ 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 async initialize(): Promise { + // nothing to initialize } public hasOnDevicePinEntry(): boolean { @@ -341,11 +335,3 @@ export class MetaMaskHDWallet implements core.HDWallet, core.ETHWallet { return "metaMask"; } } - -export function info() { - return new MetaMaskHDWalletInfo(); -} - -export function create(): MetaMaskHDWallet { - return new MetaMaskHDWallet(); -}