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

fix: snap ensure proper detection when multiple wallets installed #124

Merged
merged 1 commit into from
Oct 7, 2024
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: 2 additions & 1 deletion packages/adapter/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@shapeshiftoss/metamask-snaps-adapter",
"version": "1.0.10",
"version": "1.0.11",
gomesalexandre marked this conversation as resolved.
Show resolved Hide resolved
"description": "",
"author": "pastaghost <pastaghost@2520.io> (http://www.github.com/pastaghost)",
"homepage": "https://github.com/shapeshift/metamask-snaps/tree/main/packages/adapter",
Expand Down Expand Up @@ -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": {
Expand Down
32 changes: 11 additions & 21 deletions packages/adapter/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ExternalProvider } from '@ethersproject/providers'
import detectEthereumProvider from '@metamask/detect-provider'
import type {
EnableShapeShiftMultichainResult,
RPCHandlerResponse,
ShapeShiftSnapRPCRequest,
ShapeShiftSnapRPCResponse,
} from '@shapeshiftoss/metamask-snaps-types'
import assert from 'assert'
import { createStore } from 'mipd'
import PQueue from 'p-queue'

import { logger } from './lib/logger'
Expand All @@ -18,26 +18,16 @@ export type Provider = Omit<ExternalProvider, 'request'> & {
request?: (request: { method: string; params?: any }) => Promise<any>
}

export const getMetaMaskProvider = async (): Promise<Provider | undefined> => {
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<Provider | undefined> => {
const maybeEip6963Provider = mipdStore.findProvider({ rdns: METAMASK_RDNS })

return maybeEip6963Provider?.provider
}

export const shapeShiftSnapInstalled = async (snapId: string): Promise<boolean> => {
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down
Loading