@logosnetwork/logos-webwallet-sdk
Version:
Create Logos wallets with or without a full Logos node
647 lines (646 loc) • 20.1 kB
TypeScript
import Logos from '@logosnetwork/logos-rpc-client';
import TokenAccount, { TokenAccountJSON } from './TokenAccount';
import LogosAccount, { LogosAccountJSON, LogosAccountOptions } from './LogosAccount';
import { Request, Issuance } from './Requests';
export interface RPCOptions {
proxy?: string;
nodeURL: string;
nodePort: string;
wsPort?: string;
}
interface AccountJSONMap {
[address: string]: LogosAccountJSON;
}
interface TokenAccountJSONMap {
[address: string]: TokenAccountJSON;
}
interface AccountMap {
[address: string]: LogosAccount;
}
interface TokenAccountMap {
[address: string]: TokenAccount;
}
interface WalletJSON {
password: string;
seed: string;
deterministicKeyIndex: number;
currentAccountAddress: string;
accounts?: AccountJSONMap;
tokenAccounts?: TokenAccountJSONMap;
walletID: string;
batchSends: boolean;
fullSync: boolean;
lazyErrors: boolean;
tokenSync: boolean;
validateSync: boolean;
ws: boolean;
p2pPropagation: boolean;
mqtt: string;
rpc: RPCOptions | false;
}
interface WalletOptions {
password?: string;
seed?: string;
deterministicKeyIndex?: number;
currentAccountAddress?: string;
accounts?: AccountMap;
tokenAccounts?: TokenAccountMap;
walletID?: string;
batchSends?: boolean;
fullSync?: boolean;
lazyErrors?: boolean;
tokenSync?: boolean;
validateSync?: boolean;
ws?: boolean;
p2pPropagation?: boolean;
mqtt?: string;
rpc?: RPCOptions | false;
version?: number;
}
/**
* ## Wallet
* The wallet is the primary way you will interact with the SDK.
*/
export default class Wallet {
private _password;
private _deterministicKeyIndex;
private _currentAccountAddress;
private _walletID;
private _batchSends;
private _fullSync;
private _tokenSync;
private _validateSync;
private _lazyErrors;
private _rpc;
private _iterations;
private _seed;
private _accounts;
private _tokenAccounts;
private _wsConnected;
private _mqtt;
private _ws;
private _wsClient;
private _mqttClient;
private _delegates;
private _p2pPropagation;
/**
* ### Instantiating
* ```typescript
* import Wallet from '@logosnetwork/logos-webwallet-sdk'
* const wallet = new Wallet({
* password: null,
* seed: null,
* deterministicKeyIndex: 0,
* currentAccountAddress: null,
* accounts: {},
* tokenAccounts: {},
* walletID: null,
* batchSends: true,
* fullSync: true,
* lazyErrors: false,
* tokenSync: false,
* validateSync: true,
* ws: false,
* mqtt: defaultMQTT,
* rpc: defaultRPC
* })
* ```
*
* All wallet options are optional defaults are shown in the example above
*
* |Wallet Option| Description |
* |--|--|
* | [[password]] | Password is used to encrypt and decrypt the wallet data |
* | [[seed]] | Seed is the deterministic entropy that we will use to generate key pairs from |
* | [[deterministicKeyIndex]] | index of where you wish to start generating key paris from |
* | [[currentAccountAddress]] | the current selected account address |
* | [[accounts]] | [[AccountMap]] of all the [[LogosAccount|logos accounts]] in the Wallet |
* | [[tokenAccounts]] | [[TokenAccountMap]] of all the [[TokenAccount|token accounts]] in the Wallet |
* | [[walletID]] | identifier of this wallet instance |
* | [[batchSends]] | when batchsends is true the SDK automatically combines send transactions to reduce overall amount of transactions |
* | [[fullSync]] | when fullSync is true the SDK will load the full history of the TokenAccounts and Accounts in the system. This is recommend to be true when working with tokens. |
* | [[lazyErrors]] | when lazyErrors is true the SDK will not throw errors for transactions that have insufficient funds and will queue the transactions until the account has the funds to complete the action. |
* | [[tokenSync]] | when tokenSync is true the SDK will load and sync the TokenAccounts that have interacted with the LogosAccounts. |
* | [[validateSync]] | when validateSync is true the SDK will check all signatures of all the requests in the account chains. This is recommended to be true but when syncing an account with a long history this can be computationally heavy. |
* | [[ws]] | when ws is true connect to local logos node websocket. You should only use mqtt or ws not both! mqtt will take priority over WS the MQTT setup uses less resources at the current time. |
* | [[mqtt]] | address of your mqtt server `'wss://pla.bs:8443'` is the default server. Check out the logos backend repo to run your own backend mqtt. |
* | [[rpc]] | Node information where you are sending requests to. See [[RPCOptions]]. Do not use the ip address of a delegate node, it won't work and also delegates shouldn't have RPC enabled... |
*/
constructor(options?: WalletOptions);
private loadOptions;
/**
* The id of the wallet
* #### Example
* ```typescript
* const walletID = wallet.walletID
* ```
*/
/**
* The id of the wallet
* #### Example
* ```typescript
* wallet.walletID = '2c0a4be6b9ccda9158ed96f0dd596f72dad66015e8444c64e2ea0b9c7e553ec6'
* ```
*/
walletID: string;
/**
* Is the wallet batching requests
* #### Example
* ```typescript
* const isBatchingSends = wallet.batchSends
* ```
*/
/**
* Is the wallet batching requests
* #### Example
* ```typescript
* wallet.batchSends = true
* ```
*/
batchSends: boolean;
/**
* Full Sync - syncs the entire send and recieve chains
* This is recommend to be true when using an untrusted RPC node
* In the future this will be safe when we have BLS sig validation of Request Blocks
* #### Example
* ```typescript
* const isFullSyncing = wallet.fullSync
* ```
*/
/**
* Full Sync - syncs the entire send and recieve chains
* This is recommend to be true when using an untrusted RPC node
* In the future this will be safe when we have BLS sig validation of Request Blocks
* #### Example
* ```typescript
* wallet.fullSync = true
* ```
*/
fullSync: boolean;
/**
* Sync Tokens - Syncs all associated token's of the accounts on the account sync instead of on use
* #### Example
* ```typescript
* const areTokensSyncing = wallet.tokenSync
* ```
*/
/**
* Sync Tokens - Syncs all associated token's of the accounts on the account sync instead of on use
* #### Example
* ```typescript
* wallet.tokenSync = false
* ```
*/
tokenSync: boolean;
/**
* Validate Sync
* if this option is true the SDK will generate hashes of each requests based on the content data and verify signatures
* This should always be true when using a untrusted RPC node
* #### Example
* ```typescript
* const isValidatingSignatures = wallet.validateSync
* ```
*/
/**
* Validate Sync
* if this option is true the SDK will generate hashes of each requests based on the content data and verify signatures
* This should always be true when using a untrusted RPC node
* #### Example
* ```typescript
* wallet.validateSync = false
* ```
*/
validateSync: boolean;
/**
* Lazy Errors allows you to add request that are not valid for the current pending balances to the pending chain
* #### Example
* ```typescript
* const delayingErros = wallet.lazyErrors
* ```
*/
/**
* Lazy Errors allows you to add request that are not valid for the current pending balances to the pending chain
* #### Example
* ```typescript
* wallet.lazyErrors = false
* ```
*/
lazyErrors: boolean;
/**
* When ws is true connect to local logos node websocket
* If mqtt is set then logos node websocket will not be used
* #### Example
* ```typescript
* const usingLogosWebsocket = wallet.ws
* ```
*/
/**
* When ws is true connect to local logos node websocket
* If mqtt is set then logos node websocket will not be used
* #### Example
* ```typescript
* wallet.ws = true
* ```
*/
ws: boolean;
/**
* When p2pPropagation is true public to local logos node instead of delegates
* #### Example
* ```typescript
* const usingP2pPropagation = wallet.p2pPropagation
* ```
*/
/**
* When p2pPropagation is true public to local logos node instead of delegates
* #### Example
* ```typescript
* wallet.p2pPropagation = true
* ```
*/
p2pPropagation: boolean;
/**
* [[AccountMap]] of all the [[LogosAccount|LogosAccounts]] in the wallet
* #### Example
* ```typescript
* const accounts = wallet.accounts
* ```
*/
/**
* [[AccountMap]] of all the [[LogosAccount|LogosAccounts]] in the wallet
* #### Example
* ```typescript
* wallet.accounts = {
* 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo': new LogosAccount({
* privateKey: 34F0A37AAD20F4A260F0A5B3CB3D7FB50673212263E58A380BC10474BB039CE4
* })
* }
* ```
*/
accounts: AccountMap;
/**
* [[TokenAccountMap]] of all the [[TokenAccount|TokenAccounts]] in the wallet
* #### Example
* ```typescript
* const tokenAccounts = wallet.tokenAccounts
* ```
*/
readonly tokenAccounts: TokenAccountMap;
/**
* Returns the current [[LogosAccount]] of the wallet
* #### Example
* ```typescript
* const account = wallet.account
* ```
*/
readonly account: LogosAccount;
/**
* The current account address
* #### Example
* ```typescript
* const currentAccountAddress = wallet.currentAccountAddress
* ```
*/
/**
* The current account address
* #### Example
* ```typescript
* wallet.currentAccountAddress = 'lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo'
* ```
*/
currentAccountAddress: string;
/**
* The current balance of all the [[LogosAccount|LogosAccounts]] in reason
* #### Example
* ```typescript
* const walletBalanceInReason = wallet.balance
* ```
*/
readonly balance: string;
/**
* The mqtt host for listening to confirmations from Logos consensus
* #### Example
* ```typescript
* const mqttWsAddress = wallet.mqtt
* ```
*/
/**
* The mqtt host for listening to confirmations from Logos consensus
* #### Example
* ```typescript
* wallet.mqtt = 'wss://pla.bs:8443'
* ```
*/
mqtt: string;
/**
* The [[RPCOptions]] for connecting to the RPC or set this to false to disable communication
* #### Example
* ```typescript
* const rpcInfo = wallet.rpc
* ```
*/
/**
* The [[RPCOptions]] for connecting to the RPC or set this to false to disable communication
* #### Example
* ```typescript
* wallet.rpc = {
* proxy: 'https://pla.bs',
* nodeURL: '3.215.28.211',
* nodePort: '55000',
* wsPort: '18000'
* }
* ```
*/
rpc: RPCOptions | false;
/**
* Returns a Logos RPC Client Instance
*
* @returns {Logos}
* #### Example
* ```typescript
* const rpcClient = wallet.rpcClient
* ```
*/
readonly rpcClient: Logos;
/**
* The password of the wallet
* in the future we will remove the ability to store the password and request it in realtime so it is in memory for less time
* #### Example
* ```typescript
* const password = wallet.password
* ```
*/
/**
* The password of the wallet
* in the future we will remove the ability to store the password and request it in realtime so it is in memory for less time
* #### Example
* ```typescript
* wallet.password = 'password'
* ```
*/
password: string;
/**
* The current delegates of the network
* #### Example
* ```typescript
* const delegates = wallet.delegates
* ```
*/
/**
* The current delegates of the network
* #### Example
* ```typescript
* wallet.delegates = ['3.215.28.211'] // Should be 32 length but I cba
* ```
*/
delegates: string[];
/**
* Return the seed of the wallet
* in the future we will remove the ability to access the seed unless you pass a password
* #### Example
* ```typescript
* wallet.seed = '6A4C54C619A784891D5DBCA1FCC5FA08D6B910B49A51BEA13C3DC913BB45AF13'
* ```
*/
/**
* Return the seed of the wallet
* in the future we will remove the ability to access the seed unless you pass a password
* #### Example
* ```typescript
* const seed = wallet.seed
* ```
*/
seed: string;
/**
* Return boolean if all the accounts in the wallet are synced
* #### Example
* ```typescript
* const isWalletSynced = wallet.synced
* ```
*/
readonly synced: boolean;
/**
* Return all the requests that are pending in every [[LogosAccount]] in this wallet
* #### Example
* ```typescript
* const pendingRequests = wallet.pendingRequests
* ```
*/
readonly pendingRequests: Request[];
/**
* Generates and sets a random seed for the wallet
*
* #### Example
* ```typescript
* wallet.createSeed()
* ```
*/
createSeed(overwrite?: boolean): string;
/**
* Adds a account to the wallet
*
* #### Example
* ```typescript
* wallet.addAccount(new LogosAccount(
* {
* privateKey: 34F0A37AAD20F4A260F0A5B3CB3D7FB50673212263E58A380BC10474BB039CE4
* }
* ))
* ```
*/
addAccount(account: LogosAccount): LogosAccount;
/**
* Removes an account to the wallet
*
* #### Example
* ```typescript
* wallet.removeAccount('lgs_3e3j5tkog48pnny9dmfzj1r16pg8t1e76dz5tmac6iq689wyjfpiij4txtdo')
* ```
*/
removeAccount(address: string): boolean;
/**
* Adds a tokenAccount to the wallet
*
* #### Example
* ```typescript
* const tokenAccount = await wallet.createTokenAccount('lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd')
* wallet.addTokenAccount(tokenAccount)
* ```
*/
addTokenAccount(tokenAccount: TokenAccount): TokenAccount;
/**
* Create a TokenAccount
*
* You are allowed to add a tokenAccount using the address
*
* #### Example
* ```typescript
* const tokenAccount = await wallet.createTokenAccount('lgs_3q69z3kf6cq9n9smago3p1ptuyqy9pa3mdykyi9o8f7gnof47qdyxj9gejxd')
* ```
*/
createTokenAccount(address: string, issuance?: Issuance): Promise<TokenAccount>;
/**
* Create an account
*
* You are allowed to create an account using your seed, precalculated account options, or a privateKey
*
* @param {LogosAccountOptions} options - the options to populate the account. If you send just private key it will generate the account from that privateKey. If you just send index it will genereate the account from that determinstic seed index.
* @param {boolean} setCurrent - sets the current account to newly created accounts this is default true
* @returns {Promise<LogosAccount>}
*
* #### Example
* ```typescript
* const account = await wallet.createAccount()
* ```
*/
createAccount(options?: LogosAccountOptions, setCurrent?: boolean): Promise<LogosAccount>;
/**
* Updates the balance of all the accounts
* @returns {void}
* #### Example
* ```typescript
* wallet.recalculateWalletBalancesFromChain()
* ```
*/
recalculateWalletBalancesFromChain(): void;
/**
* Finds the request object of the specified hash of one of our accounts
*
* @param {string} hash - The hash of the request we are looking for the object of
* @returns {Request | false } false if no request object of the specified hash was found
* #### Example
* ```typescript
* wallet.getRequest('E8CA715349FFD12DE7CB76045CAAA52448655F3B34624A1E31514763C81C4795')
* ```
*/
getRequest(hash: string): Request | false;
/**
* Encrypts and packs the wallet data in a hex string
*
* @returns {string}
* #### Example
* ```typescript
* wallet.encrypt()
* ```
*/
encrypt(): string;
/**
* Scans the accounts to make sure they are synced and if they are not synced it syncs them
*
* @param {boolean} - encrypted wallet
* @returns {Promise<boolean>}
* #### Example
* ```typescript
* const isWalletSynced = await wallet.sync()
* ```
*/
sync(force?: boolean): Promise<boolean>;
/**
* Constructs the wallet from an encrypted base64 encoded wallet and the password
*
* @param {string} - encrypted wallet
* @returns {Wallet} wallet data
* #### Example
* ```typescript
* const wallet = wallet.load(encryptedWalletData)
* ```
*/
load(encryptedWallet: string): Wallet;
/**
* Fetches the delegates from the server and sets our delegate list
*
* @returns {Promise<string[]>} returns the list of active delegates ips
* #### Example
* ```typescript
* const delegates = await wallet.fetchDelegates()
* ```
*/
fetchDelegates(): Promise<string[]>;
/**
* Decrypts the wallet data
*
* @param {Buffer | string} - encrypted wallet
* @returns {Buffer | false} The request data or returns false if it is unable to decrypt the data
* @private
*/
private decrypt;
/**
* Generates an account based on the determinstic index of the key
*
* @param {number} - The determinstic seed index
* @returns {AccountOptions} The minimal account options to create the account
* @private
*/
private generateAccountOptionsFromSeed;
/**
* Generates an account based on the given private key
*
* @param {string} - The private key
* @returns {AccountOptions} The minimal account options to create the account
* @private
*/
private generateAccountOptionsFromPrivateKey;
/**
* Subscribe to the mqtt topic
*
* @param {string} topic - topic to subscribe to
* @returns {void}
* @private
*/
private subscribe;
/**
* Unsubscribe to the mqtt topic
*
* @param {string} topic - topic to unsubscribe to
* @returns {void}
* @private
*/
private unsubscribe;
/**
* Disconnect from the mqtt
*
* @returns {void}
* #### Example
* ```typescript
* wallet.wsDisconnect()
* ```
*/
wsDisconnect(): void;
/**
* Connect to the mqtt
*
* @returns {void}
* #### Example
* ```typescript
* wallet.wsConnect()
* ```
*/
wsConnect(): void;
/**
* Process Request finds the account if it is in our wallet and update its local ledger
*
* @param {RpcRequest} requestInfo - JSON of the request block
* @param {string} address - address we are looking for
* @param {boolean} logosAccount - update logos accounts
* @param {boolean} tokenAccount - update token accounts
* @returns {void}
* @private
*/
private processRequest;
/**
* Resets all LogosAccounts
*
* @returns {void}
* @private
*/
private reset;
/**
* Returns the base Wallet JSON
* @returns {WalletJSON} JSON request
* #### Example
* ```typescript
* const walletJSON = wallet.toJSON()
* ```
*/
toJSON(): WalletJSON;
}
export {};