From a06fc01f448f5a05aba24a7dc72dab416f407e95 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Tue, 2 Jul 2024 09:59:27 +0200 Subject: [PATCH] feat: remove isEnabled check and use fetchMetaMaskSnapWallet helper function --- packages/core/src/wallet/metamaskBridge.ts | 76 +++++++++------------- 1 file changed, 31 insertions(+), 45 deletions(-) diff --git a/packages/core/src/wallet/metamaskBridge.ts b/packages/core/src/wallet/metamaskBridge.ts index 6c82575..63375d4 100644 --- a/packages/core/src/wallet/metamaskBridge.ts +++ b/packages/core/src/wallet/metamaskBridge.ts @@ -7,12 +7,37 @@ import type { import wallets, { WalletProvider } from "../discovery" import { init, loadRemote } from "@module-federation/runtime" +async function fetchMetaMaskSnapWallet(windowObject: Record) { + await init({ + name: "MetaMaskStarknetSnapWallet", + remotes: [ + { + name: "MetaMaskStarknetSnapWallet", + alias: "MetaMaskStarknetSnapWallet", + entry: + "https://snaps.consensys.io/starknet/get-starknet/v1/remoteEntry.js", //"http://localhost:8082/remoteEntry.js", + }, + ], + }) + + const result = await loadRemote("MetaMaskStarknetSnapWallet/index") + + const { MetaMaskSnapWallet } = result as { + MetaMaskSnapWallet: any + MetaMaskSnap: any + } + + const metaMaskSnapWallet = new MetaMaskSnapWallet("*") + await (metaMaskSnapWallet as any).init(windowObject) + return metaMaskSnapWallet as IStarknetWindowObject +} + function createMetaMaskProviderWrapper( walletInfo: WalletProvider, windowObject: Record, ): StarknetWindowObject { let metaMaskSnapWallet: IStarknetWindowObject | undefined - let isEnabling = false + let fetchPromise: Promise | undefined = undefined const metaMaskProviderWrapper: IStarknetWindowObject = { id: walletInfo.id, name: walletInfo.name, @@ -44,52 +69,13 @@ function createMetaMaskProviderWrapper( return metaMaskSnapWallet.request(call) }, async enable(): Promise { - if (isEnabling) { - // If enable is already being called, return a promise that waits for the current enable call to finish - return new Promise((resolve, reject) => { - const checkInterval = setInterval(() => { - if (!isEnabling) { - clearInterval(checkInterval) - if (metaMaskSnapWallet) { - resolve(metaMaskSnapWallet.enable()) - } else { - reject(new Error("Wallet not enabled")) - } - } - }, 100) - }) + if (!metaMaskSnapWallet) { + fetchPromise = fetchPromise || fetchMetaMaskSnapWallet(windowObject) + metaMaskSnapWallet = await fetchPromise } - isEnabling = true // Set the guard flag - try { - if (!metaMaskSnapWallet) { - await init({ - name: "MetaMaskStarknetSnapWallet", - remotes: [ - { - name: "MetaMaskStarknetSnapWallet", - alias: "MetaMaskStarknetSnapWallet", - entry: - "https://snaps.consensys.io/starknet/get-starknet/v1/remoteEntry.js", //"http://localhost:8082/remoteEntry.js", - }, - ], - }) - - const result = await loadRemote("MetaMaskStarknetSnapWallet/index") - - const { MetaMaskSnapWallet } = result as { - MetaMaskSnapWallet: any - MetaMaskSnap: any - } - metaMaskSnapWallet = new MetaMaskSnapWallet("*") - await (metaMaskSnapWallet as any).init(windowObject) - } - - const accounts = await metaMaskSnapWallet!.enable() - return accounts - } finally { - isEnabling = false // Reset the guard flag - } + const accounts = await metaMaskSnapWallet!.enable() + return accounts }, isPreauthorized() { return metaMaskSnapWallet?.isPreauthorized() ?? Promise.resolve(false)