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: Decouple Virtual Wallet Discovery in get-starknet #280

Merged
merged 4 commits into from
Dec 9, 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
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 }
Loading