From 34e44b6c1eb94124747347babdd93de441b8a5aa Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Thu, 5 Dec 2024 10:55:23 +0100 Subject: [PATCH 1/4] fix: allows discovery of virtual wallet support independently --- packages/core/src/main.ts | 9 +++++++++ packages/core/src/types.ts | 1 + packages/core/src/wallet/virtualWallets/index.ts | 10 ++++++---- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index b6c70695..41119b4e 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,14 @@ export function getStarknet( return firstAuthorizedWallet }, + discoverVirtualWallets: async () => { + virtualWallets.forEach(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 } From 0263b88530db48ba3f517c7f54c541c26972cc99 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Thu, 5 Dec 2024 16:29:16 +0100 Subject: [PATCH 2/4] chore: added changeset --- .changeset/eleven-buttons-unite.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/eleven-buttons-unite.md 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 From 7144af2c574fd88536efc839f0cbf97efca617a2 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Fri, 6 Dec 2024 09:58:23 +0100 Subject: [PATCH 3/4] fix: await for all hasSupport to haapen --- packages/core/src/main.ts | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index 41119b4e..bcbe8f28 100644 --- a/packages/core/src/main.ts +++ b/packages/core/src/main.ts @@ -117,13 +117,15 @@ export function getStarknet( return firstAuthorizedWallet }, - discoverVirtualWallets: async () => { - virtualWallets.forEach(async (virtualWallet) => { - const hasSupport = await virtualWallet.hasSupport(windowObject) - if (hasSupport) { - windowObject[virtualWallet.windowKey] = virtualWallet - } - }) + discoverVirtualWallets: async (): Promise => { + await Promise.all( + virtualWallets.map(async (virtualWallet) => { + const hasSupport = await virtualWallet.hasSupport(windowObject) + if (hasSupport) { + windowObject[virtualWallet.windowKey] = virtualWallet + } + }), + ) }, enable: async (inputWallet, options) => { let wallet: StarknetWindowObject From 2cccbbbbff75304b7b86ec962764ad02e26f0ad6 Mon Sep 17 00:00:00 2001 From: Florin Dzeladini Date: Fri, 6 Dec 2024 10:06:06 +0100 Subject: [PATCH 4/4] feat: allow filtering by ids or names --- packages/core/src/main.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/core/src/main.ts b/packages/core/src/main.ts index bcbe8f28..242f11a1 100644 --- a/packages/core/src/main.ts +++ b/packages/core/src/main.ts @@ -117,9 +117,22 @@ export function getStarknet( return firstAuthorizedWallet }, - discoverVirtualWallets: async (): Promise => { + 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( - virtualWallets.map(async (virtualWallet) => { + virtualWalletToDiscover.map(async (virtualWallet) => { const hasSupport = await virtualWallet.hasSupport(windowObject) if (hasSupport) { windowObject[virtualWallet.windowKey] = virtualWallet