Skip to content

Commit

Permalink
Merge pull request #28 from argentlabs/fix/connector-connect
Browse files Browse the repository at this point in the history
fix: connector connect
  • Loading branch information
dhruvkelawala authored Nov 11, 2023
2 parents 725444b + 324e7ac commit 4963d0f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 45 deletions.
29 changes: 26 additions & 3 deletions src/connectors/argentMobile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -66,14 +70,21 @@ export class ArgentMobileConnector extends Connector {
return this._wallet
}

async connect(): Promise<AccountInterface> {
async connect(): Promise<ConnectorData> {
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,
}
}

async disconnect(): Promise<void> {
Expand Down Expand Up @@ -108,6 +119,18 @@ export class ArgentMobileConnector extends Connector {
return this._wallet.account as AccountInterface
}

async chainId(): Promise<bigint> {
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) {
Expand Down
46 changes: 25 additions & 21 deletions src/connectors/connector.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -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. */
Expand All @@ -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<ConnectorEvents> {
/** 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<boolean>
abstract connect(): Promise<AccountInterface>
abstract ready(): Promise<boolean>
/** Connect wallet. */
abstract connect(): Promise<ConnectorData>
/** Disconnect wallet. */
abstract disconnect(): Promise<void>
abstract account(): Promise<AccountInterface | null>
/** 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<AccountInterface>
/** Get current chain id. */
abstract chainId(): Promise<bigint>
/** Connector StarknetWindowObject */
abstract get wallet(): StarknetWindowObject
}
35 changes: 17 additions & 18 deletions src/connectors/injected/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -103,34 +107,21 @@ export class InjectedConnector extends Connector {
}
}

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

if (!this._wallet) {
throw new ConnectorNotFoundError()
}

let accounts: string[]
let accounts
try {
accounts = await this._wallet.enable({ starknetVersion: "v5" })
} catch {
// NOTE: Argent v3.0.0 swallows the `.enable` call on reject, so this won't get hit.
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()
Expand All @@ -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<void> {
Expand Down
29 changes: 26 additions & 3 deletions src/connectors/webwallet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -76,7 +80,7 @@ export class WebWalletConnector extends Connector {
return "Powered by Argent"
}

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

if (!this._wallet) {
Expand All @@ -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,
}
}

async disconnect(): Promise<void> {
Expand Down Expand Up @@ -127,6 +138,18 @@ export class WebWalletConnector extends Connector {
return this._wallet.account as unknown as AccountInterface
}

async chainId(): Promise<bigint> {
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) {
Expand Down

0 comments on commit 4963d0f

Please sign in to comment.