diff --git a/.changeset/eleven-buttons-unite.md b/.changeset/eleven-buttons-unite.md new file mode 100644 index 00000000..9e06eade --- /dev/null +++ b/.changeset/eleven-buttons-unite.md @@ -0,0 +1,5 @@ +--- +"@starknet-io/get-starknet-core": patch +--- + +Decouple Virtual Wallet Discovery for async workflow diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index b6c70695..242f11a1 100644 --- a/packages/core/src/main.ts +++ b/packages/core/src/main.ts @@ -13,6 +13,7 @@ import { sortBy } from "./wallet/sort" import { initiateVirtualWallets, resolveVirtualWallet, + virtualWallets, } from "./wallet/virtualWallets" import { Permission, type StarknetWindowObject } from "@starknet-io/types-js" @@ -116,6 +117,29 @@ export function getStarknet( return firstAuthorizedWallet }, + discoverVirtualWallets: async ( + walletNamesOrIds: string[] = [], + ): Promise => { + const walletNamesOrIdsSet = new Set(walletNamesOrIds) + + const virtualWalletToDiscover = + walletNamesOrIdsSet.size > 0 + ? virtualWallets.filter( + (virtualWallet) => + walletNamesOrIdsSet.has(virtualWallet.name) || + walletNamesOrIdsSet.has(virtualWallet.id), + ) + : virtualWallets + + await Promise.all( + virtualWalletToDiscover.map(async (virtualWallet) => { + const hasSupport = await virtualWallet.hasSupport(windowObject) + if (hasSupport) { + windowObject[virtualWallet.windowKey] = virtualWallet + } + }), + ) + }, enable: async (inputWallet, options) => { let wallet: StarknetWindowObject if (isVirtualWallet(inputWallet)) { diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 3fb0c9e9..49e81bfb 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -69,6 +69,7 @@ export interface GetStarknetResult { ) => Promise // Returns only preauthorized wallets available in the window object getDiscoveryWallets: (options?: GetWalletOptions) => Promise // Returns all wallets in existence (from discovery file) getLastConnectedWallet: () => Promise // Returns the last wallet connected when it's still connected + discoverVirtualWallets: () => Promise // Discovers the virtual wallets by calling their hasSupport methods enable: ( wallet: StarknetWindowObject | VirtualWallet, options?: RequestAccountsParameters, diff --git a/packages/core/src/wallet/virtualWallets/index.ts b/packages/core/src/wallet/virtualWallets/index.ts index a7d5288f..5924500c 100644 --- a/packages/core/src/wallet/virtualWallets/index.ts +++ b/packages/core/src/wallet/virtualWallets/index.ts @@ -6,9 +6,11 @@ const virtualWallets: VirtualWallet[] = [metaMaskVirtualWallet] function initiateVirtualWallets(windowObject: Record) { virtualWallets.forEach(async (virtualWallet) => { - const hasSupport = await virtualWallet.hasSupport(windowObject) - if (hasSupport) { - windowObject[virtualWallet.windowKey] = virtualWallet + if (!(virtualWallet.windowKey in windowObject)) { + const hasSupport = await virtualWallet.hasSupport(windowObject) + if (hasSupport) { + windowObject[virtualWallet.windowKey] = virtualWallet + } } }) } @@ -28,4 +30,4 @@ async function resolveVirtualWallet( return wallet } -export { initiateVirtualWallets, resolveVirtualWallet } +export { initiateVirtualWallets, resolveVirtualWallet, virtualWallets }