Skip to content

Commit

Permalink
Merge pull request #280 from Consensys/fix/virtual-wallet-discovery
Browse files Browse the repository at this point in the history
fix: Decouple Virtual Wallet Discovery in `get-starknet`
  • Loading branch information
fracek authored Dec 9, 2024
2 parents f7131db + 2cccbbb commit 187bf96
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/eleven-buttons-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@starknet-io/get-starknet-core": patch
---

Decouple Virtual Wallet Discovery for async workflow
24 changes: 24 additions & 0 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -116,6 +117,29 @@ export function getStarknet(

return firstAuthorizedWallet
},
discoverVirtualWallets: async (
walletNamesOrIds: string[] = [],
): Promise<void> => {
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)) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export interface GetStarknetResult {
) => Promise<StarknetWindowObject[]> // Returns only preauthorized wallets available in the window object
getDiscoveryWallets: (options?: GetWalletOptions) => Promise<WalletProvider[]> // Returns all wallets in existence (from discovery file)
getLastConnectedWallet: () => Promise<StarknetWindowObject | null | undefined> // Returns the last wallet connected when it's still connected
discoverVirtualWallets: () => Promise<void> // Discovers the virtual wallets by calling their hasSupport methods
enable: (
wallet: StarknetWindowObject | VirtualWallet,
options?: RequestAccountsParameters,
Expand Down
10 changes: 6 additions & 4 deletions packages/core/src/wallet/virtualWallets/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ const virtualWallets: VirtualWallet[] = [metaMaskVirtualWallet]

function initiateVirtualWallets(windowObject: Record<string, unknown>) {
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
}
}
})
}
Expand All @@ -28,4 +30,4 @@ async function resolveVirtualWallet(
return wallet
}

export { initiateVirtualWallets, resolveVirtualWallet }
export { initiateVirtualWallets, resolveVirtualWallet, virtualWallets }

0 comments on commit 187bf96

Please sign in to comment.