From ab4b79b28e29764b6dcbf144a13c26f2820b63cb Mon Sep 17 00:00:00 2001 From: Dhruv Kelawala Date: Sat, 11 Nov 2023 11:08:40 +0300 Subject: [PATCH 1/2] fix: connector connect --- src/connectors/argentMobile/index.ts | 29 ++++++++++++++++-- src/connectors/connector.ts | 46 +++++++++++++++------------- src/connectors/injected/index.ts | 35 ++++++++++----------- src/connectors/webwallet/index.ts | 29 ++++++++++++++++-- 4 files changed, 94 insertions(+), 45 deletions(-) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index 88e09ac..68e4141 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -11,7 +11,11 @@ import { UserNotConnectedError, } from "../../errors" import { resetWalletConnect } from "../../helpers/resetWalletConnect" -import { Connector, type ConnectorIcons } from "../connector" +import { + Connector, + type ConnectorData, + type ConnectorIcons, +} from "../connector" import type { StarknetAdapter } from "./modal/starknet/adapter" import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" @@ -66,14 +70,21 @@ export class ArgentMobileConnector extends Connector { return this._wallet } - async connect(): Promise { + async connect(): Promise { await this.ensureWallet() if (!this._wallet) { throw new ConnectorNotFoundError() } - return this._wallet.account as unknown as AccountInterface + const account = this._wallet.account as unknown as AccountInterface + + const chainId = await this.chainId() + + return { + account: account.address, + chainId: BigInt(chainId), + } } async disconnect(): Promise { @@ -108,6 +119,18 @@ export class ArgentMobileConnector extends Connector { return this._wallet.account as AccountInterface } + async chainId(): Promise { + this.ensureWallet() + + if (!this._wallet) { + throw new ConnectorNotConnectedError() + } + + const chainIdHex = await this._wallet.provider.getChainId() + const chainId = BigInt(chainIdHex) + return chainId + } + // needed, methods required by starknet-react. Otherwise an exception is throwd async initEventListener(accountChangeCb: AccountChangeEventHandler) { if (!this._wallet) { diff --git a/src/connectors/connector.ts b/src/connectors/connector.ts index de4c333..337ec3c 100644 --- a/src/connectors/connector.ts +++ b/src/connectors/connector.ts @@ -1,15 +1,6 @@ import EventEmitter from "eventemitter3" - import type { StarknetWindowObject } from "get-starknet-core" -import type { AccountInterface } from "starknet" - -/** Connector data. */ -export type ConnectorData = { - /** Connector account. */ - account?: string - /** Connector network. */ - chainId?: bigint -} +import { AccountInterface } from "starknet" /** Connector icons, as base64 encoded svg. */ export type ConnectorIcons = { @@ -19,6 +10,14 @@ export type ConnectorIcons = { light?: string } +/** Connector data. */ +export type ConnectorData = { + /** Connector account. */ + account?: string + /** Connector network. */ + chainId?: bigint +} + /** Connector events. */ export interface ConnectorEvents { /** Emitted when account or network changes. */ @@ -28,22 +27,27 @@ export interface ConnectorEvents { /** Emitted when connection is lost. */ disconnect(): void } -// eslint-disable-next-line @typescript-eslint/no-explicit-any + export abstract class Connector extends EventEmitter { + /** Unique connector id. */ + abstract get id(): string + /** Connector name. */ + abstract get name(): string + /** Connector icons. */ + abstract get icon(): ConnectorIcons + /** Whether connector is available for use */ abstract available(): boolean - /** Whether connector is already authorized */ - abstract ready?(): Promise - abstract connect(): Promise + abstract ready(): Promise + /** Connect wallet. */ + abstract connect(): Promise + /** Disconnect wallet. */ abstract disconnect(): Promise - abstract account(): Promise - /** Unique connector id */ - abstract get id(): string - /** Connector name */ - abstract get name(): string - /** Connector icon */ - abstract get icon(): ConnectorIcons + /** Get current account. */ + abstract account(): Promise + /** Get current chain id. */ + abstract chainId(): Promise /** Connector StarknetWindowObject */ abstract get wallet(): StarknetWindowObject } diff --git a/src/connectors/injected/index.ts b/src/connectors/injected/index.ts index 51afc5a..183e997 100644 --- a/src/connectors/injected/index.ts +++ b/src/connectors/injected/index.ts @@ -7,7 +7,11 @@ import { UserNotConnectedError, UserRejectedRequestError, } from "../../errors" -import { Connector, type ConnectorIcons } from "../connector" +import { + Connector, + type ConnectorData, + type ConnectorIcons, +} from "../connector" import { WALLET_NOT_FOUND_ICON_DARK, WALLET_NOT_FOUND_ICON_LIGHT, @@ -103,14 +107,14 @@ export class InjectedConnector extends Connector { } } - async connect(): Promise { - await this.ensureWallet() + async connect(): Promise { + this.ensureWallet() if (!this._wallet) { throw new ConnectorNotFoundError() } - let accounts: string[] + let accounts try { accounts = await this._wallet.enable({ starknetVersion: "v5" }) } catch { @@ -118,19 +122,6 @@ export class InjectedConnector extends Connector { throw new UserRejectedRequestError() } - if (!this._wallet.isConnected) { - // NOTE: Argent v3.0.0 swallows the `.enable` call on reject, so this won't get hit. - throw new UserRejectedRequestError() - } - - // This is to ensure that v5 account interface is used. - // TODO: add back once Braavos updates their interface. - /* - if (!(this._wallet.account instanceof AccountInterface)) { - throw new UnsupportedAccountInterfaceError() - } - */ - if (!this._wallet.isConnected || !accounts) { // NOTE: Argent v3.0.0 swallows the `.enable` call on reject, so this won't get hit. throw new UserRejectedRequestError() @@ -146,7 +137,15 @@ export class InjectedConnector extends Connector { await this.onAccountsChanged(accounts) - return this._wallet.account + const account = this._wallet.account.address + const chainId = await this.chainId() + + this.emit("connect", { account, chainId }) + + return { + account, + chainId, + } } async disconnect(): Promise { diff --git a/src/connectors/webwallet/index.ts b/src/connectors/webwallet/index.ts index c579928..c683b10 100644 --- a/src/connectors/webwallet/index.ts +++ b/src/connectors/webwallet/index.ts @@ -3,7 +3,11 @@ import type { StarknetWindowObject, } from "get-starknet-core" import type { AccountInterface } from "starknet" -import { Connector, type ConnectorIcons } from "../connector" +import { + Connector, + type ConnectorData, + type ConnectorIcons, +} from "../connector" import { setPopupOptions, trpcProxyClient } from "./helpers/trpc" import { @@ -76,7 +80,7 @@ export class WebWalletConnector extends Connector { return "Powered by Argent" } - async connect(): Promise { + async connect(): Promise { await this.ensureWallet() if (!this._wallet) { @@ -94,7 +98,14 @@ export class WebWalletConnector extends Connector { throw new UserRejectedRequestError() } - return this._wallet.account as unknown as AccountInterface + const account = this._wallet.account as unknown as AccountInterface + + const chainId = await this.chainId() + + return { + account: account.address, + chainId: BigInt(chainId), + } } async disconnect(): Promise { @@ -127,6 +138,18 @@ export class WebWalletConnector extends Connector { return this._wallet.account as unknown as AccountInterface } + async chainId(): Promise { + this.ensureWallet() + + if (!this._wallet) { + throw new ConnectorNotConnectedError() + } + + const chainIdHex = await this._wallet.provider.getChainId() + const chainId = BigInt(chainIdHex) + return chainId + } + async initEventListener(accountChangeCb: AccountChangeEventHandler) { this._wallet = _wallet if (!this._wallet) { From 324e7ac5709165c100cb4c55e1de15f4aa655670 Mon Sep 17 00:00:00 2001 From: Dhruv Kelawala Date: Sat, 11 Nov 2023 11:19:28 +0300 Subject: [PATCH 2/2] fix: double bigint --- src/connectors/argentMobile/index.ts | 2 +- src/connectors/webwallet/index.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index 68e4141..efb672d 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -83,7 +83,7 @@ export class ArgentMobileConnector extends Connector { return { account: account.address, - chainId: BigInt(chainId), + chainId, } } diff --git a/src/connectors/webwallet/index.ts b/src/connectors/webwallet/index.ts index c683b10..bd30153 100644 --- a/src/connectors/webwallet/index.ts +++ b/src/connectors/webwallet/index.ts @@ -104,7 +104,7 @@ export class WebWalletConnector extends Connector { return { account: account.address, - chainId: BigInt(chainId), + chainId, } }