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

metamask: only detectEthereumProvider() once #417

Merged
merged 1 commit into from
Apr 8, 2022
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
3 changes: 0 additions & 3 deletions integration/src/integration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 25 additions & 2 deletions integration/src/wallets/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
23 changes: 0 additions & 23 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 @@ -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);
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,4 +1,3 @@
import detectEthereumProvider from "@metamask/detect-provider";
import * as core from "@shapeshiftoss/hdwallet-core";
import _ from "lodash";

Expand Down Expand Up @@ -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<Record<string, any>> {
Expand All @@ -159,14 +159,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 @@ -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();
}