UNPKG

@web3-wallet/core

Version:
215 lines 7.92 kB
import type { DetectProviderOptions } from '@web3-wallet/detect-provider'; import type { WalletName, WalletStore, WalletStoreActions } from './types'; import { ProviderNoFoundError } from './types'; import type { AddEthereumChainParameter, Provider, ProviderConnectInfo, ProviderRpcError, WatchAssetParameters } from './types/provider'; export declare type ProviderFilter = (provider: Provider) => boolean; /** * ProviderOptions is specific to each wallet provider, and it will be used to * to create/initialize the wallet provider instance. */ export declare type ProviderOptions = object | undefined; export declare type BaseConnectorOptions = { detectProviderOptions?: DetectProviderOptions; providerFilter?: ProviderFilter; }; /** * The wallet options object */ export declare type ConnectorOptions<TProviderOptions extends ProviderOptions = undefined> = TProviderOptions extends undefined ? BaseConnectorOptions : BaseConnectorOptions & { providerOptions: TProviderOptions; }; export declare abstract class Connector<TConnectorOptions extends ConnectorOptions = ConnectorOptions> { /** * {@link WalletName} */ abstract walletName: WalletName; /** * {@link Provider} **/ provider?: Provider; /** * The wallet options object, specific to each wallet */ options?: TConnectorOptions; /** * {@link WalletStore} */ store: WalletStore; /** * {@link WalletStoreActions} */ protected actions: WalletStoreActions; /** * * ProviderNoFoundError should be thrown in the following cases * 1. detectProvider failed to retrieve the provider from a host environment. * 2. calling functions that requires a provider, but we have been unable to retrieve the provider */ protected get providerNotFoundError(): ProviderNoFoundError; /** * * @param walletName - {@link Connector#WalletName} * @param actions - {@link WalletStoreActions} */ constructor(options?: TConnectorOptions); /** * Detect provider in the host environment. * * @param providerFilter - providerFilter is provided the detected provider as it's input * and providerFilter returns a boolean to indicated wether the detected provider can be used. * 1. detectProvider should throw the ProviderNoFoundError if providerFilter returns false * 2. detectProvider should throw the ProviderNoFoundError if providerFilter returns false * * detectProvider is internally called by {@link Connector#lazyInitialize} * * @return Promise<p> - * 1. resolve with the provider if the detection succeeded. * 2. reject with an ProviderNotFoundError if it failed to retrieve the provider from the host environment. */ detectProvider(providerFilter?: ProviderFilter, options?: DetectProviderOptions): Promise<Provider>; /** * `lazyInitialize` does the following two things: * 1. triggers the provider detection process {@link Connector#detectProvider} * 2. Register event listeners to the provider {@link Connector#addEventListeners} * * `lazyInitialize` is internally called by the following public methods * - {@link Connector#connect} * - {@link Connector#autoConnect} * * @returns Promise<void> */ protected lazyInitialize(): Promise<void>; /** * Try to connect to wallet. * * autoConnect never reject, it will always resolve. * * autoConnect only try to connect to wallet, if it don't need any further * user interaction with the wallet in the connecting process. * * @return Promise<boolean> - * 1. resolve with `true` if the connection succeeded. * 2. resolve with `false` if the connection failed. * */ autoConnect(): Promise<boolean>; /** * Initiates a connection. * * @param chain - If defined, indicates the desired chain to connect to. If the user is * already connected to this chain, no additional steps will be taken. Otherwise, the user will be prompted to switch * to the chain, if one of two conditions is met: either they already have it added in their extension, or the * argument is of type AddEthereumChainParameter, in which case the user will be prompted to add the chain with the * specified parameters first, before being prompted to switch. * * @returns Promise<void> */ connect(chain?: number | AddEthereumChainParameter): Promise<void>; /** * Disconnect wallet * * Wallet connector implementors should override this method if the wallet supports * force disconnect. * * What is force disconnect? * - force disconnect will actually disconnect the wallet. * - non-disconnect only reset the wallet store to it's initial state. * * For some wallets, MetaMask for example, there're not ways to force disconnect MetaMask. * For some wallets, Walletconnect for example, we are able to force disconnect Walletconnect. * @param _force - wether to force disconnect to wallet, default is false. * */ disconnect(_force?: boolean): Promise<void>; /** * Add an asset to the wallet assets list * * @param asset - {@link WatchAssetParameters} * @return Promise<void> */ watchAsset(asset: WatchAssetParameters): Promise<void>; /** * Update the wallet store with new the chainId * * @param chainId - the chainId to update * @return void */ protected updateChainId(chainId: string | number): void; /** * Update the wallet store with new the new accounts * * @param accounts * @return void */ protected updateAccounts(accounts: string[]): void; /** * Wallet connect listener * * @param info - the connect info * @return void */ protected onConnect(info: ProviderConnectInfo): void; /** * Wallet disconnect listener * * @param error - the disconnect ProviderRpcError * @return void */ protected onDisconnect(_: ProviderRpcError): void; /** * Wallet chainId change listener * * @param chainId - the new chainId * @return void */ protected onChainChanged(chainId: number | string): void; /** * wallet account change listener */ protected onAccountsChanged(accounts: string[]): void; /** * Remove event listeners * * Returned from addEventListeners {@link Connector#addEventListeners} * * don't need to remove event listeners if the provider never recreated in the whole lifecycle * * @returns void */ protected removeEventListeners?: () => void; /** * Register event listeners to the provider * * @return removeEventListeners - a function to remove the registered event listeners {@link Connector#removeEventListeners} */ protected addEventListeners(): Connector['removeEventListeners']; /** * Switch network * * - {@link SwitchEthereumChainParameter} * * @param chainId - the if the the chain to switch to * @returns Promise<void> */ protected switchChain(chainId: number): Promise<void>; /** * Add a new network/chain to wallet * * @param - {@link AddEthereumChainParameter} * @returns Promise<void> */ protected addChain(addChainParameter: AddEthereumChainParameter): Promise<void>; /** * Fetch wallet accounts via the wallet provider * * @return Promise<string[]> - the fetched accounts */ protected requestAccounts(): Promise<string[]>; /** * Fetch wallet chainId via the wallet provider * * @return Promise<string> - the fetched chainId */ protected requestChainId(): Promise<string>; } //# sourceMappingURL=Connector.d.ts.map