UNPKG

@ponziland/account

Version:

Account management library for Starknet with wallet modal and connection state

74 lines (73 loc) 2.58 kB
import { accountConfig } from '../consts.js'; import getStarknet from "@starknet-io/get-starknet-core"; import { WALLET_API } from '@starknet-io/types-js'; import { WalletAccount, wallet, validateAndParseAddress, constants as SNconstants, Account, Provider, CallData, selector, AccountInterface, } from 'starknet'; export class CommonStarknetWallet { _wallet; _session; _walletObject; constructor(walletObject) { this._walletObject = walletObject; } async connect() { this._wallet = await WalletAccount.connect(new Provider({ nodeUrl: accountConfig.rpcUrl, // We won't be using argent / braavos on slot deployments any time soon chainId: accountConfig.chainId == 'mainnet' ? SNconstants.StarknetChainId.SN_MAIN : SNconstants.StarknetChainId.SN_SEPOLIA, }), this._walletObject); // This is where we need to catch errors if the user cancelled const result = await this._wallet.requestAccounts(true); if (typeof result == 'string') { // This is extracted from the example https://github.com/PhilippeR26/Starknet-WalletAccount/blob/main/src/app/components/client/WalletHandle/SelectWallet.tsx // not sure why this means that the wallet is not compatible, but welp throw 'This wallet is incompatible'; } const isConnectedWallet = await this._wallet .getPermissions() .then((res) => res.length > 0); if (!isConnectedWallet) { this._wallet = undefined; throw 'The wallet was not connected correctly'; } } async disconnect() { getStarknet({ windowObject: this._walletObject, }).disconnect(); this._wallet = undefined; this._session = undefined; } async loadSession(storage) { this._session = new Account(this._wallet, storage.address, storage.privateKey); } getWalletAccount() { if (this._wallet != undefined) { return this._wallet; } else { return undefined; } } get icon() { const icon = this._walletObject.icon; if (typeof icon === 'string') { return icon; } else { return icon.dark; } } } export class NoSessionStarknetWallet extends CommonStarknetWallet { supportsSession() { return false; } getAccount() { return this._wallet; } async setupSession() { // no-op } }