Skip to content

Commit

Permalink
chore: update ensureWallet method for injected to be compliant with s…
Browse files Browse the repository at this point in the history
…tarknet react
  • Loading branch information
bluecco committed Nov 13, 2023
1 parent a8ca44e commit 504a6a7
Showing 1 changed file with 50 additions and 9 deletions.
59 changes: 50 additions & 9 deletions src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { StarknetWindowObject } from "get-starknet-core"
import { getStarknet } from "get-starknet-core"
import { AccountInterface, constants } from "starknet"
import {
ConnectorNotConnectedError,
Expand Down Expand Up @@ -43,7 +42,7 @@ export class InjectedConnector extends Connector {
}

async ready(): Promise<boolean> {
await this.ensureWallet()
this.ensureWallet()

if (!this._wallet) {
return false
Expand All @@ -52,7 +51,7 @@ export class InjectedConnector extends Connector {
}

async chainId(): Promise<bigint> {
await this.ensureWallet()
this.ensureWallet()

if (!this._wallet) {
throw new ConnectorNotConnectedError()
Expand Down Expand Up @@ -110,7 +109,7 @@ export class InjectedConnector extends Connector {
}

async connect(): Promise<ConnectorData> {
await this.ensureWallet()
this.ensureWallet()

if (!this._wallet) {
throw new ConnectorNotFoundError()
Expand Down Expand Up @@ -151,7 +150,7 @@ export class InjectedConnector extends Connector {
}

async disconnect(): Promise<void> {
await this.ensureWallet()
this.ensureWallet()

if (!this.available()) {
throw new ConnectorNotFoundError()
Expand All @@ -163,7 +162,7 @@ export class InjectedConnector extends Connector {
}

async account(): Promise<AccountInterface> {
await this.ensureWallet()
this.ensureWallet()

if (!this._wallet || !this._wallet.account) {
throw new ConnectorNotConnectedError()
Expand Down Expand Up @@ -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<string, any>): StarknetWindowObject[] {
return Object.values(
Object.getOwnPropertyNames(obj).reduce<
Record<string, StarknetWindowObject>
>((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
}

0 comments on commit 504a6a7

Please sign in to comment.