UNPKG

@tedcryptoorg/cosmos-signer

Version:

Cosmos Signer - A library for signing transactions for Cosmos SDK chains

147 lines (146 loc) 4.78 kB
export default class BaseWallet { provider; logger; key = null; signer = null; suggestChainSupport = true; options = {}; constructor(provider, logger) { this.provider = provider; this.logger = logger; } available() { return !!this.provider; } connected() { return this.available(); } isLedger() { return this.key?.isNanoLedger ?? this.key?.isHardware ?? false; } signDirectSupport() { return !!this.signer?.signDirect; } signAminoSupport() { return !!this.signer?.signAmino; } getOptions() { return this.options; } setOptions(options) { this.options = options; } async connect(network) { this.key = null; this.signer = null; try { await this.enable(network); await this.getKey(network); await this.getSigner(network); return this.key; } catch (e) { this.logger?.log(e); if (!this.suggestChainSupport) { this.logger?.log('Suggest chain not supported', network, e); throw new Error('Failed to connect wallet and suggest chain is not supported.'); } try { await this.suggestChain(network); await this.getKey(network); await this.getSigner(network); return this.key; } catch (s) { this.logger?.log('Failed to suggest the chain and connect', e, s); throw new Error('Failed to connect wallet and suggest chain'); } } } disconnect() { this.key = null; this.signer = null; this.logger?.log('Disconnecting wallet'); } async enable(network) { const { chainId } = network; await this.provider.enable(chainId); } async getKey(network) { if (this.key === null) { const { chainId } = network; this.key = await this.provider.getKey(chainId); } return this.key; } async getSigner(network) { if (!this.signer) { const { chainId } = network; this.signer = await this.provider.getOfflineSignerAuto(chainId); } return this.signer; } async getAddress() { if (this.signer?.getAddress) { return await this.signer.getAddress(); } else { const accounts = await this.getAccounts(); if (accounts === undefined || accounts.length === 0) { throw new Error('No accounts found in wallet'); } return accounts[0].address; } } async getAccounts() { return await this.signer?.getAccounts(); } async signDirect(...args) { return await this.signer?.signDirect(...args); } async signAmino(...args) { return await this.signer?.signAmino(...args); } async suggestChain(network) { if (this.suggestChainSupport) { await this.provider.experimentalSuggestChain(this.suggestChainData(network)); } else { throw new Error(`${network.prettyName} (${network.chainId}) is not supported`); } } suggestChainData(network) { const currency = { coinDenom: network.symbol, coinMinimalDenom: network.denom, coinDecimals: network.decimals }; if (network.coinGeckoId) { currency.coinGeckoId = network.coinGeckoId; } return { rpc: network.rpcUrl, rest: network.restUrl, chainId: network.chainId, chainName: network.prettyName, stakeCurrency: currency, bip44: { coinType: network.slip44 }, walletUrlForStaking: "https://restake.app/" + network.name, bech32Config: { bech32PrefixAccAddr: network.prefix, bech32PrefixAccPub: network.prefix + "pub", bech32PrefixValAddr: network.prefix + "valoper", bech32PrefixValPub: network.prefix + "valoperpub", bech32PrefixConsAddr: network.prefix + "valcons", bech32PrefixConsPub: network.prefix + "valconspub" }, currencies: [currency], feeCurrencies: [{ ...currency, gasPriceStep: network.gasPriceStep }], ...(network.data.keplrFeatures !== undefined ? { features: network.data.keplrFeatures } : (network.slip44 === 60 ? { features: ["ibc-transfer", "ibc-go", "eth-address-gen", "eth-key-sign"] } : {})) }; } }