diff --git a/src/connectors/injected/index.ts b/src/connectors/injected/index.ts index 5f191f0..fb29653 100644 --- a/src/connectors/injected/index.ts +++ b/src/connectors/injected/index.ts @@ -1,5 +1,4 @@ import type { StarknetWindowObject } from "get-starknet-core" -import { getStarknet } from "get-starknet-core" import { AccountInterface, constants } from "starknet" import { ConnectorNotConnectedError, @@ -43,7 +42,7 @@ export class InjectedConnector extends Connector { } async ready(): Promise { - await this.ensureWallet() + this.ensureWallet() if (!this._wallet) { return false @@ -52,7 +51,7 @@ export class InjectedConnector extends Connector { } async chainId(): Promise { - await this.ensureWallet() + this.ensureWallet() if (!this._wallet) { throw new ConnectorNotConnectedError() @@ -110,7 +109,7 @@ export class InjectedConnector extends Connector { } async connect(): Promise { - await this.ensureWallet() + this.ensureWallet() if (!this._wallet) { throw new ConnectorNotFoundError() @@ -151,7 +150,7 @@ export class InjectedConnector extends Connector { } async disconnect(): Promise { - await this.ensureWallet() + this.ensureWallet() if (!this.available()) { throw new ConnectorNotFoundError() @@ -163,7 +162,7 @@ export class InjectedConnector extends Connector { } async account(): Promise { - await this.ensureWallet() + this.ensureWallet() if (!this._wallet || !this._wallet.account) { throw new ConnectorNotConnectedError() @@ -208,12 +207,54 @@ export class InjectedConnector extends Connector { return this._wallet } - private async ensureWallet() { - const starknet = getStarknet() - const installed = await starknet.getAvailableWallets() + private ensureWallet() { + const installed = getAvailableWallets(globalThis) const wallet = installed.filter((w) => w.id === this._options.id)[0] if (wallet) { this._wallet = wallet } } } + +function getAvailableWallets(obj: Record): StarknetWindowObject[] { + return Object.values( + Object.getOwnPropertyNames(obj).reduce< + Record + >((wallets, key) => { + if (key.startsWith("starknet")) { + const wallet = obj[key] + + if (isWalletObject(wallet) && !wallets[wallet.id]) { + wallets[wallet.id] = wallet as StarknetWindowObject + } + } + return wallets + }, {}), + ) +} + +// biome-ignore lint: wallet could be anything +function isWalletObject(wallet: any): boolean { + try { + return ( + wallet && + [ + // wallet's must have methods/members, see IStarknetWindowObject + "request", + "isConnected", + "provider", + "enable", + "isPreauthorized", + "on", + "off", + "version", + "id", + "name", + "icon", + ].every((key) => key in wallet) + ) + } catch (err) { + /* empty */ + } + return false +}