Skip to content

Commit

Permalink
metamask: only detectEthereumProvider() once
Browse files Browse the repository at this point in the history
  • Loading branch information
mrnerdhair committed Mar 23, 2022
1 parent 35ee044 commit e1fe00c
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 46 deletions.
4 changes: 0 additions & 4 deletions integration/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ import { terraTests } from "./terra";
import { kavaTests } from "./kava";
import { WalletSuite } from "./wallets/suite";

import { ethereum } from "./wallets/mocks/@metamask/detect-provider";

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
Expand Down
25 changes: 23 additions & 2 deletions integration/src/wallets/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,32 @@ export function name(): string {
}

export function createInfo(): core.HDWalletInfo {
return metamask.info();
return new metamask.MetaMaskHDWalletInfo();
}

export async function createWallet(): Promise<core.HDWallet> {
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;
}
Expand Down
21 changes: 0 additions & 21 deletions integration/src/wallets/mocks/@metamask/detect-provider.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/hdwallet-metamask/src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,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);
Expand Down
22 changes: 4 additions & 18 deletions packages/hdwallet-metamask/src/metamask.ts
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -53,8 +52,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<Record<string, any>> {
Expand All @@ -77,14 +77,8 @@ export class MetaMaskHDWallet implements core.HDWallet, core.ETHWallet {
return Promise.resolve("MetaMask");
}

public async initialize(): Promise<any> {
try {
this.provider = await detectEthereumProvider({ mustBeMetaMask: true, silent: false, timeout: 3000 });
} catch (e) {
console.error(e);
}

return Promise.resolve();
public async initialize(): Promise<void> {
// nothing to initialize
}

public hasOnDevicePinEntry(): boolean {
Expand Down Expand Up @@ -330,11 +324,3 @@ export class MetaMaskHDWalletInfo implements core.HDWalletInfo, core.ETHWalletIn
return eth.ethGetAccountPaths(msg);
}
}

export function info() {
return new MetaMaskHDWalletInfo();
}

export function create(): MetaMaskHDWallet {
return new MetaMaskHDWallet();
}

0 comments on commit e1fe00c

Please sign in to comment.