UNPKG

@aeternity/aepp-sdk

Version:

SDK for the æternity blockchain

96 lines (95 loc) 4.23 kB
import AeSdk from './AeSdk.js'; import RpcClient from './aepp-wallet-communication/rpc/RpcClient.js'; import { METHODS, RPC_STATUS, WALLET_TYPE } from './aepp-wallet-communication/schema.js'; import AccountBase from './account/Base.js'; import BrowserConnection from './aepp-wallet-communication/connection/Browser.js'; import { Accounts, AeppApi, Network, NetworkToSelect, WalletApi, WalletInfo } from './aepp-wallet-communication/rpc/types.js'; import { Encoded } from './utils/encoder.js'; type RpcClientWallet = RpcClient<AeppApi, WalletApi>; type OnConnection = (clientId: string, params: Omit<Parameters<WalletApi[METHODS.connect]>[0], 'version'>, origin: string) => void; type OnSubscription = (clientId: string, params: Parameters<WalletApi[METHODS.subscribeAddress]>[0], origin: string) => void; type OnDisconnect = (clientId: string, params: Parameters<WalletApi[METHODS.closeConnection]>[0]) => void; type OnAskAccounts = (clientId: string, params: undefined, origin: string) => void; type OnAskToSelectNetwork = (clientId: string, params: NetworkToSelect, origin: string) => void; interface RpcClientsInfo { id: string; status: RPC_STATUS; connectNode: boolean; addressSubscription: Set<'connected' | 'current'>; rpc: RpcClientWallet; } /** * Contain functionality for aepp interaction and managing multiple aepps * @category aepp wallet communication */ export default class AeSdkWallet extends AeSdk { id: string; _type: WALLET_TYPE; name: string; _clients: Map<string, RpcClientsInfo>; onConnection: OnConnection; onSubscription: OnSubscription; onDisconnect: OnDisconnect; onAskAccounts: OnAskAccounts; onAskToSelectNetwork: OnAskToSelectNetwork; /** * @param options - Options * @param options.name - Wallet name * @param options.id - Wallet id * @param options.type - Wallet type * @param options.onConnection - Call-back function for incoming AEPP connection * @param options.onSubscription - Call-back function for incoming AEPP account subscription * @param options.onAskAccounts - Call-back function for incoming AEPP get address request * @param options.onAskToSelectNetwork - Call-back function for incoming AEPP select network * request. If the request is fine then this function should change the current network. * @param options.onDisconnect - Call-back function for disconnect event */ constructor({ name, id, type, onConnection, onSubscription, onDisconnect, onAskAccounts, onAskToSelectNetwork, ...options }: { id: string; type: WALLET_TYPE; name: string; onConnection: OnConnection; onSubscription: OnSubscription; onDisconnect: OnDisconnect; onAskAccounts: OnAskAccounts; onAskToSelectNetwork: OnAskToSelectNetwork; } & ConstructorParameters<typeof AeSdk>[0]); _getAccountsForClient({ addressSubscription }: RpcClientsInfo): Accounts; _pushAccountsToApps(): void; selectAccount(address: Encoded.AccountAddress): void; addAccount(account: AccountBase, options?: Parameters<AeSdk['addAccount']>[1]): void; _getNode(): { node: Network['node']; }; selectNode(name: string): Promise<void>; _getClient(clientId: string): RpcClientsInfo; _isRpcClientConnected(clientId: string): boolean; _disconnectRpcClient(clientId: string): void; /** * Remove specific RpcClient by ID * @param id - Client ID */ removeRpcClient(id: string): void; /** * Add new client by AEPP connection * @param clientConnection - AEPP connection object * @returns Client ID */ addRpcClient(clientConnection: BrowserConnection): string; /** * Send shareWalletInfo message to notify AEPP about wallet * @param clientId - ID of RPC client send message to */ shareWalletInfo(clientId: string): Promise<void>; /** * Get Wallet info object * @returns Object with wallet information */ getWalletInfo(): Promise<WalletInfo>; /** * Get Wallet accounts * @returns Object with accounts information (\{ connected: Object, current: Object \}) */ getAccounts(): Accounts; } export {};