Skip to content

Commit

Permalink
- Cache resolved virtual wallets
Browse files Browse the repository at this point in the history
  • Loading branch information
naorye2 committed Apr 11, 2024
1 parent f65736a commit 490fd56
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
7 changes: 5 additions & 2 deletions packages/core/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
} from "./wallet/isWalletObject"
import { scanObjectForWallets } from "./wallet/scan"
import { sortBy } from "./wallet/sort"
import { initiateVirtualWallets } from "./wallet/virtualWallets"
import {
initiateVirtualWallets,
resolveVirtualWallet,
} from "./wallet/virtualWallets"
import { Permission, type StarknetWindowObject } from "starknet-types"

export type {
Expand Down Expand Up @@ -118,7 +121,7 @@ export function getStarknet(
enable: async (inputWallet, options) => {
let wallet: StarknetWindowObject
if (isVirtualWallet(inputWallet)) {
wallet = await inputWallet.loadWallet()
wallet = await resolveVirtualWallet(inputWallet)
} else if (isFullWallet(inputWallet)) {
wallet = inputWallet
} else {
Expand Down
35 changes: 23 additions & 12 deletions packages/core/src/wallet/virtualWallets/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import type { VirtualWallet } from "../../types";
import { metaMaskVirtualWallet } from "./metaMaskVirtualWallet";
import type { StarknetWindowObject } from "starknet-types"
import type { VirtualWallet } from "../../types"
import { metaMaskVirtualWallet } from "./metaMaskVirtualWallet"

const virtualWallets: VirtualWallet[] = [
metaMaskVirtualWallet
];
const virtualWallets: VirtualWallet[] = [metaMaskVirtualWallet]

function initiateVirtualWallets() {
virtualWallets.forEach(async virtualWallet => {
const hasSupport = await virtualWallet.hasSupport()
if (hasSupport) {
window[virtualWallet.windowKey] = virtualWallet;
}
});
virtualWallets.forEach(async (virtualWallet) => {
const hasSupport = await virtualWallet.hasSupport()
if (hasSupport) {
window[virtualWallet.windowKey] = virtualWallet
}
})
}

export { initiateVirtualWallets, }
const virtualWalletsMap: Record<string, StarknetWindowObject> = {}

async function resolveVirtualWallet(virtualWallet: VirtualWallet) {
let wallet: StarknetWindowObject = virtualWalletsMap[virtualWallet.id]
if (!wallet) {
wallet = await virtualWallet.loadWallet()
virtualWalletsMap[virtualWallet.id] = wallet
}

return wallet
}

export { initiateVirtualWallets, resolveVirtualWallet }

0 comments on commit 490fd56

Please sign in to comment.