Skip to content

Commit

Permalink
fix: rollback provider detection back to metamaskBridge
Browse files Browse the repository at this point in the history
  • Loading branch information
khanti42 committed Jul 3, 2024
1 parent a06fc01 commit 602219f
Showing 1 changed file with 85 additions and 6 deletions.
91 changes: 85 additions & 6 deletions packages/core/src/wallet/metamaskBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,82 @@ import type {
import wallets, { WalletProvider } from "../discovery"
import { init, loadRemote } from "@module-federation/runtime"

async function fetchMetaMaskSnapWallet(windowObject: Record<string, unknown>) {
interface MetaMaskProvider {
isMetaMask: boolean
request(options: { method: string }): Promise<void>
}

function isMetaMaskProvider(obj: unknown): obj is MetaMaskProvider {
return (
obj !== null &&
typeof obj === "object" &&
obj.hasOwnProperty("isMetaMask") &&
obj.hasOwnProperty("request")
)
}

function detectMetaMaskProvider(
windowObject: Record<string, unknown>,
{ timeout = 3000 } = {},
): Promise<MetaMaskProvider | null> {
let handled = false
return new Promise<MetaMaskProvider | null>((resolve) => {
const handleEIP6963Provider = (event: CustomEvent) => {
const { info, provider } = event.detail
if (
["io.metamask", "io.metamask.flask"].includes(info.rdns) &&
isMetaMaskProvider(provider)
) {
resolve(provider)
handled = true
}
}

if (typeof windowObject.addEventListener === "function") {
windowObject.addEventListener(
"eip6963:announceProvider",
handleEIP6963Provider,
)
}

setTimeout(() => {
if (!handled) {
resolve(null)
}
}, timeout)

// Notify event listeners and other parts of the dapp that a provider is requested.
if (typeof windowObject.dispatchEvent === "function") {
windowObject.dispatchEvent(new Event("eip6963:requestProvider"))
}
})
}

async function waitForMetaMaskProvider(
windowObject: Record<string, unknown>,
{ timeout = 3000, retries = 0 } = {},
): Promise<MetaMaskProvider | null> {
return detectMetaMaskProvider(windowObject, { timeout })
.catch(function () {
return null
})
.then(function (provider) {
if (provider || retries === 0) {
return provider
}
return waitForMetaMaskProvider(windowObject, {
timeout,
retries: retries - 1,
})
})
}

async function detectMetamaskSupport(windowObject: Record<string, unknown>) {
const provider = await waitForMetaMaskProvider(windowObject, { retries: 3 })
return provider
}

async function fetchMetaMaskSnapWallet(provider: unknown) {
await init({
name: "MetaMaskStarknetSnapWallet",
remotes: [
Expand All @@ -27,14 +102,13 @@ async function fetchMetaMaskSnapWallet(windowObject: Record<string, unknown>) {
MetaMaskSnap: any
}

const metaMaskSnapWallet = new MetaMaskSnapWallet("*")
await (metaMaskSnapWallet as any).init(windowObject)
const metaMaskSnapWallet = new MetaMaskSnapWallet(provider, "*")
return metaMaskSnapWallet as IStarknetWindowObject
}

function createMetaMaskProviderWrapper(
walletInfo: WalletProvider,
windowObject: Record<string, unknown>,
provider: unknown,
): StarknetWindowObject {
let metaMaskSnapWallet: IStarknetWindowObject | undefined
let fetchPromise: Promise<IStarknetWindowObject> | undefined = undefined
Expand Down Expand Up @@ -70,7 +144,7 @@ function createMetaMaskProviderWrapper(
},
async enable(): Promise<string[]> {
if (!metaMaskSnapWallet) {
fetchPromise = fetchPromise || fetchMetaMaskSnapWallet(windowObject)
fetchPromise = fetchPromise || fetchMetaMaskSnapWallet(provider)
metaMaskSnapWallet = await fetchPromise
}

Expand Down Expand Up @@ -112,9 +186,14 @@ async function injectMetamaskBridge(windowObject: Record<string, unknown>) {
return
}

const provider = await detectMetamaskSupport(windowObject)
if (!provider) {
return
}

windowObject.starknet_metamask = createMetaMaskProviderWrapper(
metamaskWalletInfo,
windowObject,
provider,
)
}

Expand Down

0 comments on commit 602219f

Please sign in to comment.