packageDocumentation module import { generatePrivateKey , getAddressFromPrivateKey , getPubkeyFromPrivateKey , encrypt , decrypt , EncryptOptions , Keystore , Signature , getAddress , } from '@harmony-js/crypto' ; import { isPrivateKey , add0xToString , hexToNumber , AddressSuffix , ChainType , } from '@harmony-js/utils' ; import { Transaction , RLPSign } from '@harmony-js/transaction' ; import { StakingTransaction } from '@harmony-js/staking' ; import { Messenger , RPCMethod } from '@harmony-js/network' ; import { Shards } from './types' ; import { defaultMessenger } from './utils' ; export interface Balance { balance ? : string ; nonce ? : number ; shardID ? : number ; } class Account { example static new ( ) { const newAcc = new Account ( ) . _new ( ) ; return newAcc ; } param { string } key example static add ( key ) { const newAcc = new Account ( ) . _import ( key ) ; return newAcc ; } hidden privateKey ? : string ; hidden publicKey ? : string ; hidden address ? : string ; hidden balance ? : string = '0' ; hidden nonce ? : number = 0 ; hidden shardID : number ; hidden shards : Shards ; hidden messenger : Messenger ; hidden encrypted : boolean = false ; example get checksumAddress ( ) : string { return this . address ? getAddress ( this . address ) . checksum : '' ; } example get bech32Address ( ) : string { return this . address ? getAddress ( this . address ) . bech32 : '' ; } example get bech32TestNetAddress ( ) : string { return this . address ? getAddress ( this . address ) . bech32TestNet : '' ; } example get getShardsCount ( ) : number { return this . shards . size ; } param key param messenger example constructor ( key ? , messenger = defaultMessenger ) { this . messenger = messenger ; if ( ! key ) { this . _new ( ) ; } else { this . _import ( key ) ; } this . shardID = this . messenger . currentShard || 0 ; this . shards = new Map ( ) ; this . shards . set ( this . shardID , { address : `${ this . bech32Address }${ AddressSuffix }0` , balance : this . balance || '0' , nonce : this . nonce || 0 , } ) ; } async toFile ( password , options ? ) { if ( this . privateKey && isPrivateKey ( this . privateKey ) ) { const file = await encrypt ( this . privateKey , password , options ) ; this . privateKey = file ; this . encrypted = true ; return file ; } else { throw new Error ( 'Encryption failed because PrivateKey is not correct' ) ; } } async fromFile ( keyStore , password ) { try { if ( typeof password !== 'string' ) { throw new Error ( 'you must provide password' ) ; } const file = JSON . parse ( keyStore . toLowerCase ( ) ) ; const decyptedPrivateKey = await decrypt ( file , password ) ; if ( isPrivateKey ( decyptedPrivateKey ) ) { return this . _import ( decyptedPrivateKey ) ; } else { throw new Error ( 'decrypted failed' ) ; } } catch ( error ) { throw error ; } } param blockNumber example async getBalance ( blockNumber = 'latest' ) { try { if ( this . messenger ) { const balance = await this . messenger . send ( RPCMethod . GetBalance , [ this . address , blockNumber ] , this . messenger . chainPrefix , this . messenger . currentShard || 0 ) ; const nonce = await this . messenger . send ( RPCMethod . GetTransactionCount , [ this . address , blockNumber ] , this . messenger . chainPrefix , this . messenger . currentShard || 0 ) ; if ( balance . isError ( ) ) { throw balance . error . message ; } if ( nonce . isError ( ) ) { throw nonce . error . message ; } this . balance = hexToNumber ( balance . result ) ; this . nonce = Number . parseInt ( hexToNumber ( nonce . result ) , 10 ) ; this . shardID = this . messenger . currentShard || 0 ; } else { throw new Error ( 'No Messenger found' ) ; } return { balance : this . balance , nonce : this . nonce , shardID : this . shardID , } ; } catch ( error ) { throw error ; } } function async updateBalances ( blockNumber = 'latest' ) { const shardProviders = this . messenger . shardProviders ; if ( shardProviders . size > 1 ) { for ( const [ name , val ] of shardProviders ) { const balanceObject = await this . getShardBalance ( val . shardID , blockNumber ) ; await this . shards . set ( name === val . shardID ? name : val . shardID , balanceObject ) ; } } else { const currentShard = await this . getShardBalance ( this . messenger . currentShard || 0 , blockNumber ) ; this . shards . set ( this . messenger . currentShard || 0 , currentShard ) ; } } function async signTransaction ( transaction , updateNonce = true , encodeMode = 'rlp' , blockNumber = 'latest' ) { if ( ! this . privateKey || ! isPrivateKey ( this . privateKey ) ) { throw new Error ( `${ this . privateKey } is not found or not correct` ) ; } if ( updateNonce ) { const txShardID = transaction . txParams . shardID ; const shardNonce = await this . getShardNonce ( typeof txShardID === 'string' ? Number . parseInt ( txShardID , 10 ) : txShardID , blockNumber ) ; transaction . setParams ( { ... transaction . txParams , from : this . messenger . chainPrefix === ChainType . Harmony ? this . bech32Address : this . checksumAddress || '0x' , nonce : shardNonce , } ) ; } if ( encodeMode === 'rlp' ) { const [ signature , rawTransaction ] = RLPSign ( transaction , this . privateKey ) ; return transaction . map ( ( obj ) => { return { ... obj , signature , rawTransaction , from : this . messenger . chainPrefix === ChainType . Harmony ? this . bech32Address : this . checksumAddress || '0x' , } ; } ) ; } else { return transaction ; } } param staking param updateNonce param encodeMode param blockNumber param shardID async signStaking ( staking , updateNonce = true , encodeMode = 'rlp' , blockNumber = 'latest' , shardID = this . messenger . currentShard ) { if ( ! this . privateKey || ! isPrivateKey ( this . privateKey ) ) { throw new Error ( `${ this . privateKey } is not found or not correct` ) ; } if ( updateNonce ) { const txShardID = shardID ; const shardNonce = await this . getShardNonce ( typeof txShardID === 'string' ? Number . parseInt ( txShardID , 10 ) : txShardID , blockNumber ) ; staking . setFromAddress ( this . messenger . chainPrefix === ChainType . Harmony ? this . bech32Address : this . checksumAddress || '0x' ) ; staking . setNonce ( shardNonce ) ; } if ( encodeMode === 'rlp' ) { const [ signature , rawTransaction ] = staking . rlpSign ( this . privateKey ) ; staking . setRawTransaction ( rawTransaction ) ; staking . setSignature ( signature ) ; staking . setFromAddress ( this . messenger . chainPrefix === ChainType . Harmony ? this . bech32Address : this . checksumAddress || '0x' ) ; return staking ; } else { return staking ; } } param messenger example setMessenger ( messenger ) { this . messenger = messenger ; } param shardID example getAddressFromShardID ( shardID ) { const shardObject = this . shards . get ( shardID ) ; if ( shardObject ) { return shardObject . address ; } else { return undefined ; } } example getAddresses ( ) { const addressArray = [ ] ; for ( const [ name , val ] of this . shards ) { const index = typeof name === 'string' ? Number . parseInt ( name , 10 ) : name ; addressArray [ index ] = val . address ; } return addressArray ; } param shardID param blockNumber example async getShardBalance ( shardID , blockNumber = 'latest' ) { const balance = await this . messenger . send ( RPCMethod . GetBalance , [ this . address , blockNumber ] , this . messenger . chainPrefix , shardID ) ; const nonce = await this . messenger . send ( RPCMethod . GetTransactionCount , [ this . address , blockNumber ] , this . messenger . chainPrefix , shardID ) ; if ( balance . isError ( ) ) { throw balance . error . message ; } if ( nonce . isError ( ) ) { throw nonce . error . message ; } return { address : `${ this . bech32Address }${ AddressSuffix }${ shardID }` , balance : hexToNumber ( balance . result ) , nonce : Number . parseInt ( hexToNumber ( nonce . result ) , 10 ) , } ; } param shardID param blockNumber example async getShardNonce ( shardID , blockNumber = 'latest' ) { const nonce = await this . messenger . send ( RPCMethod . GetAccountNonce , [ this . address , blockNumber ] , this . messenger . chainPrefix , shardID ) ; if ( nonce . isError ( ) ) { throw nonce . error . message ; } return nonce . result ; } function return { Account } ignore private _new ( ) { const prv = generatePrivateKey ( ) ; if ( ! isPrivateKey ( prv ) ) { throw new Error ( 'key gen failed' ) ; } return this . _import ( prv ) ; } function param { string } key return { Account } ignore private _import ( key ) { if ( ! isPrivateKey ( key ) ) { throw new Error ( `${ key } is not PrivateKey` ) ; } this . privateKey = add0xToString ( key ) ; this . publicKey = getPubkeyFromPrivateKey ( this . privateKey ) ; this . address = getAddressFromPrivateKey ( this . privateKey ) ; this . shardID = this . messenger . currentShard || 0 ; this . shards = new Map ( ) ; this . shards . set ( this . shardID , { address : `${ this . bech32Address }${ AddressSuffix }0` , balance : this . balance || '0' , nonce : this . nonce || 0 , } ) ; this . encrypted = false ; return this ; } } /**
 * This comment _supports_ [Markdown](https://marked.js.org/)
 */ export { Account } ;