From ec28a5c4c3798b9660af6de8cf9b47a92a8fe62d Mon Sep 17 00:00:00 2001 From: gomes <17035424+gomesalexandre@users.noreply.github.com> Date: Mon, 7 Oct 2024 15:52:21 +0200 Subject: [PATCH] fix: snap ensure proper detection when multiple wallets installed --- packages/adapter/package.json | 3 ++- packages/adapter/src/utils.ts | 32 +++++++++++--------------------- yarn.lock | 13 +++++++++++++ 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/packages/adapter/package.json b/packages/adapter/package.json index 423feba..01fa678 100644 --- a/packages/adapter/package.json +++ b/packages/adapter/package.json @@ -1,6 +1,6 @@ { "name": "@shapeshiftoss/metamask-snaps-adapter", - "version": "1.0.10", + "version": "1.0.11", "description": "", "author": "pastaghost (http://www.github.com/pastaghost)", "homepage": "https://github.com/shapeshift/metamask-snaps/tree/main/packages/adapter", @@ -30,6 +30,7 @@ "@shapeshiftoss/logger": "^1.1.3", "@shapeshiftoss/metamask-snaps": "^1.0.10", "@shapeshiftoss/metamask-snaps-types": "^1.0.10", + "mipd": "^0.0.7", "p-queue": "^7.4.1" }, "peerDependencies": { diff --git a/packages/adapter/src/utils.ts b/packages/adapter/src/utils.ts index 5b1d255..0699ad3 100644 --- a/packages/adapter/src/utils.ts +++ b/packages/adapter/src/utils.ts @@ -1,5 +1,4 @@ import type { ExternalProvider } from '@ethersproject/providers' -import detectEthereumProvider from '@metamask/detect-provider' import type { EnableShapeShiftMultichainResult, RPCHandlerResponse, @@ -7,6 +6,7 @@ import type { ShapeShiftSnapRPCResponse, } from '@shapeshiftoss/metamask-snaps-types' import assert from 'assert' +import { createStore } from 'mipd' import PQueue from 'p-queue' import { logger } from './lib/logger' @@ -18,26 +18,16 @@ export type Provider = Omit & { request?: (request: { method: string; params?: any }) => Promise } -export const getMetaMaskProvider = async (): Promise => { - try { - const provider = await (detectEthereumProvider({ mustBeMetaMask: true }) as Promise< - Provider | undefined - >) - if (provider === undefined) { - throw new Error('Could not get MetaMask provider') - } - if (provider.request === undefined) { - throw new Error('MetaMask provider does not define a .request() method') - } - return provider - } catch (error) { - moduleLogger.error( - error, - { fn: 'getMetaMaskProvider' }, - 'Please install MetaMask browser extension.', - ) - } - return undefined +export const mipdStore = createStore() + +const METAMASK_RDNS = 'io.metamask' + +// Detects MM explicitly using its rdns - no weird cases e.g Brave https://github.com/MetaMask/detect-provider/pull/87 +// and ensures the correct EIP-1193 provider is used even with many, many wallets installed +export const getMetaMaskProvider = (): Promise => { + const maybeEip6963Provider = mipdStore.findProvider({ rdns: METAMASK_RDNS }) + + return maybeEip6963Provider?.provider } export const shapeShiftSnapInstalled = async (snapId: string): Promise => { diff --git a/yarn.lock b/yarn.lock index 6ce560c..3d668f3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -7286,6 +7286,7 @@ __metadata: eslint-plugin-prettier: ^5.0.0 eslint-plugin-react: ^7.33.2 eslint-plugin-simple-import-sort: ^10.0.0 + mipd: ^0.0.7 p-queue: ^7.4.1 prettier: ^2.8.8 prettier-package-json: ^2.8.0 @@ -19532,6 +19533,18 @@ __metadata: languageName: node linkType: hard +"mipd@npm:^0.0.7": + version: 0.0.7 + resolution: "mipd@npm:0.0.7" + peerDependencies: + typescript: ">=5.0.4" + peerDependenciesMeta: + typescript: + optional: true + checksum: 14526f78d6d1bc8580898922508d64714f5abc7293b5998fe93c54237fd1cea120dc98674fe2b329ba3803bda5a85f3e442c3b1fa880e4c6b443bf73018514a8 + languageName: node + linkType: hard + "mkdirp-classic@npm:^0.5.2": version: 0.5.3 resolution: "mkdirp-classic@npm:0.5.3"