diff --git a/package.json b/package.json index 7ff923d..a01af0f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "starknetkit", - "version": "2.3.0", + "version": "2.3.2", "repository": "github:argentlabs/starknetkit", "private": false, "browser": { diff --git a/src/connectors/argentMobile/index.ts b/src/connectors/argentMobile/index.ts index 27904b8..54447c4 100644 --- a/src/connectors/argentMobile/index.ts +++ b/src/connectors/argentMobile/index.ts @@ -1,11 +1,4 @@ import { type AccountChangeEventHandler } from "@starknet-io/get-starknet-core" -import { - AccountInterface, - ProviderInterface, - ProviderOptions, - WalletAccount, - constants, -} from "starknet" import { Permission, RequestFnCall, @@ -13,6 +6,13 @@ import { RpcTypeToMessageMap, type StarknetWindowObject, } from "@starknet-io/types-js" +import { + Account, + AccountInterface, + ProviderInterface, + ProviderOptions, + constants, +} from "starknet" import { ConnectorNotConnectedError, ConnectorNotFoundError, @@ -27,10 +27,10 @@ import { type ConnectorData, type ConnectorIcons, } from "../connector" +import { InjectedConnector, InjectedConnectorOptions } from "../injected" import { DEFAULT_ARGENT_MOBILE_ICON, DEFAULT_PROJECT_ID } from "./constants" -import type { StarknetAdapter } from "./modal/starknet/adapter" import { isInArgentMobileAppBrowser } from "./helpers" -import { InjectedConnector, InjectedConnectorOptions } from "../injected" +import type { StarknetAdapter } from "./modal/starknet/adapter" export interface ArgentMobileConnectorOptions { dappName: string @@ -131,8 +131,12 @@ export class ArgentMobileBaseConnector extends Connector { if (!this._wallet) { throw new ConnectorNotConnectedError() } + const accounts = await this._wallet.request({ + type: "wallet_requestAccounts", + params: { silent_mode: true }, + }) - return new WalletAccount(provider, this._wallet) + return new Account(provider, accounts[0], "") } async chainId(): Promise { diff --git a/src/connectors/connector.ts b/src/connectors/connector.ts index 36198d8..a4e2593 100644 --- a/src/connectors/connector.ts +++ b/src/connectors/connector.ts @@ -28,6 +28,10 @@ export interface ConnectorEvents { disconnect(): void } +export type ConnectOptions = { + silent_mode: boolean +} + export abstract class Connector extends EventEmitter { /** Unique connector id. */ abstract get id(): string @@ -41,7 +45,7 @@ export abstract class Connector extends EventEmitter { /** Whether connector is already authorized */ abstract ready(): Promise /** Connect wallet. */ - abstract connect(): Promise + abstract connect(params?: ConnectOptions): Promise /** Disconnect wallet. */ abstract disconnect(): Promise /** Get current account silently. Return null if the account is not authorized */ diff --git a/src/connectors/injected/index.ts b/src/connectors/injected/index.ts index 26d7143..6ab2653 100644 --- a/src/connectors/injected/index.ts +++ b/src/connectors/injected/index.ts @@ -1,9 +1,3 @@ -import { - AccountInterface, - ProviderInterface, - ProviderOptions, - WalletAccount, -} from "starknet" import { Permission, RequestFnCall, @@ -11,6 +5,12 @@ import { RpcTypeToMessageMap, type StarknetWindowObject, } from "@starknet-io/types-js" +import { + Account, + AccountInterface, + ProviderInterface, + ProviderOptions, +} from "starknet" import { ConnectorNotConnectedError, ConnectorNotFoundError, @@ -18,6 +18,7 @@ import { } from "../../errors" import { removeStarknetLastConnectedWallet } from "../../helpers/lastConnected" import { + ConnectOptions, Connector, type ConnectorData, type ConnectorIcons, @@ -130,10 +131,15 @@ export class InjectedConnector extends Connector { throw new ConnectorNotConnectedError() } - return new WalletAccount(provider, this._wallet) + const accounts = await this.request({ + type: "wallet_requestAccounts", + params: { silent_mode: true }, + }) + + return new Account(provider, accounts[0], "") } - async connect(): Promise { + async connect(params: ConnectOptions): Promise { this.ensureWallet() if (!this._wallet) { @@ -142,9 +148,16 @@ export class InjectedConnector extends Connector { let accounts: string[] try { - accounts = await this.request({ - type: "wallet_requestAccounts", - }) + accounts = await this.request( + params + ? { + type: "wallet_requestAccounts", + params, + } + : { + type: "wallet_requestAccounts", + }, + ) } catch { throw new UserRejectedRequestError() } diff --git a/src/connectors/webwallet/index.ts b/src/connectors/webwallet/index.ts index 8678456..fe7964f 100644 --- a/src/connectors/webwallet/index.ts +++ b/src/connectors/webwallet/index.ts @@ -7,10 +7,10 @@ import { type StarknetWindowObject, } from "@starknet-io/types-js" import { + Account, AccountInterface, ProviderInterface, ProviderOptions, - WalletAccount, } from "starknet" import { ConnectorNotConnectedError, @@ -30,6 +30,7 @@ import { setPopupOptions } from "./helpers/trpc" import type { WebWalletStarknetWindowObject } from "./starknetWindowObject/argentStarknetWindowObject" let _wallet: StarknetWindowObject | null = null +let _address: string | null = null interface WebWalletConnectorOptions { url?: string @@ -51,6 +52,7 @@ export class WebWalletConnector extends Connector { async ready(): Promise { if (!_wallet) { this._wallet = null + _address = null return false } @@ -116,6 +118,8 @@ export class WebWalletConnector extends Connector { const hexChainId = getStarknetChainId(chainId) + _address = account[0] + return { account: account[0], chainId: BigInt(hexChainId), @@ -145,6 +149,7 @@ export class WebWalletConnector extends Connector { } _wallet = null + _address = null this._wallet = _wallet removeStarknetLastConnectedWallet() } @@ -158,7 +163,11 @@ export class WebWalletConnector extends Connector { throw new ConnectorNotConnectedError() } - return new WalletAccount(provider, this._wallet) + if (!_address) { + throw new ConnectorNotConnectedError() + } + + return new Account(provider, _address, "") } async chainId(): Promise { @@ -192,6 +201,7 @@ export class WebWalletConnector extends Connector { this._wallet.off("accountsChanged", accountChangeCb) _wallet = null + _address = null this._wallet = null } diff --git a/src/hooks/useStarknetkitConnectModal.ts b/src/hooks/useStarknetkitConnectModal.ts index 0727ac3..68c1354 100644 --- a/src/hooks/useStarknetkitConnectModal.ts +++ b/src/hooks/useStarknetkitConnectModal.ts @@ -6,12 +6,12 @@ type UseStarknetkitConnectors = { } const useStarknetkitConnectModal = ( - options: ConnectOptionsWithConnectors, + options?: ConnectOptionsWithConnectors, ): UseStarknetkitConnectors => { const starknetkitConnectModal = async (): Promise => { return await connect({ ...options, - resultType: options.resultType ?? "connector", + resultType: options?.resultType ?? "connector", }) } diff --git a/src/main.ts b/src/main.ts index c3ff489..a99cbe3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -52,7 +52,9 @@ export const connect = async ({ let connectorData: ConnectorData | null = null if (connector && resultType === "wallet") { - connectorData = await connector.connect() + connectorData = await connector.connect({ + silent_mode: true, + }) } return { @@ -139,9 +141,8 @@ export const connect = async ({ callback: async (connector: StarknetkitConnector | null) => { try { selectedConnector = connector - const connectorData = (await connector?.connect()) ?? null - if (resultType === "wallet") { + const connectorData = (await connector?.connect()) ?? null if (connector !== null) { setStarknetLastConnectedWallet(connector.id) } @@ -155,7 +156,7 @@ export const connect = async ({ resolve({ connector, wallet: null, - connectorData, + connectorData: null, }) } } catch (error) {