packageDocumentation module import { isAddress , isBech32Address , isBech32TestNetAddress } from '@harmony-js/utils' ; import { toChecksumAddress } from './keyTool' ; import { fromBech32 , toBech32 , HRP , tHRP } from './bech32' ; /**
 * ### How to use it?
 *
 * ```
 * // Step 1: import the class
 * const { HarmonyAddress } = require('@harmony-js/crypto');
 *
 * // Step 2: call functions
 * const addr = 'one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7'
 * const res = HarmonyAddress.isValidBech32(addr);
 * console.log(res);
 * ```
 */ export class HarmonyAddress { example static isValidBasic ( str ) { const toTest = new HarmonyAddress ( str ) ; return toTest . raw === toTest . basic ; } example static isValidChecksum ( str ) { const toTest = new HarmonyAddress ( str ) ; return toTest . raw === toTest . checksum ; } example static isValidBech32 ( str ) { const toTest = new HarmonyAddress ( str ) ; return toTest . raw === toTest . bech32 ; } example static isValidBech32TestNet ( str ) { const toTest = new HarmonyAddress ( str ) ; return toTest . raw === toTest . bech32TestNet ; } raw : string ; basic : string ; example get basicHex ( ) { return `0x${ this . basic }` ; } example get checksum ( ) { return toChecksumAddress ( `0x${ this . basic }` ) ; } example get bech32 ( ) { return toBech32 ( this . basic , HRP ) ; } example get bech32TestNet ( ) { return toBech32 ( this . basic , tHRP ) ; } constructor ( raw ) { this . raw = raw ; this . basic = this . getBasic ( this . raw ) ; } param addr example private getBasic ( addr ) { const basicBool = isAddress ( addr ) ; const bech32Bool = isBech32Address ( addr ) ; const bech32TestNetBool = isBech32TestNetAddress ( addr ) ; if ( basicBool ) { return addr . replace ( '0x' , '' ) . toLowerCase ( ) ; } if ( bech32Bool ) { const fromB32 = fromBech32 ( addr , HRP ) ; return fromB32 . replace ( '0x' , '' ) . toLowerCase ( ) ; } if ( bech32TestNetBool ) { const fromB32TestNet = fromBech32 ( addr , tHRP ) ; return fromB32TestNet . replace ( '0x' , '' ) . toLowerCase ( ) ; } throw new Error ( `"${ addr }" is an invalid address format` ) ; } } param address example export function getAddress ( address ) { try { return new HarmonyAddress ( address ) ; } catch ( error ) { throw error ; } }