import cryptoHash from 'crypto-hashing' ; import base58 from 'bs58' ; import bech32 from 'bech32' ; function isHex ( hex ) { if ( ! hex . match ( /([0-9]|[a-f])/gim ) ) return false ; const buf = Buffer . from ( hex , 'hex' ) . toString ( 'hex' ) ; return buf === hex . toLowerCase ( ) ; } param { string } message return { string } function ensureBuffer ( message ) { if ( Buffer . isBuffer ( message ) ) return message ; switch ( typeof message ) { case 'string' : message = isHex ( message ) ? Buffer . from ( message , 'hex' ) : Buffer . from ( message ) ; break ; case 'object' : message = Buffer . from ( JSON . stringify ( message ) ) ; break ; } return Buffer . isBuffer ( message ) ? message : false ; } param { ! string } algorithm param { ! string | Buffer } message return { string } function hashToHex ( algorithm , message ) { return cryptoHash ( algorithm , ensureBuffer ( message ) ) . toString ( 'hex' ) ; } param { ! string | Buffer } message return { string } function hash160 ( message ) { return hashToHex ( 'hash160' , message ) ; } param { ! string | Buffer } message return { string } function sha256 ( message ) { return hashToHex ( 'sha256' , message ) ; } param { ! string | Buffer } message return { string } function ripemd160 ( message ) { return hashToHex ( 'ripemd160' , message ) ; } param { string } hex param { number } lengthBytes return { string } function padHexStart ( hex , lengthBytes ? ) { let lengthString = lengthBytes * 2 || hex . length ; lengthString += lengthString % 2 ; return hex . padStart ( lengthString , '0' ) ; } export { base58 , bech32 , sha256 , ripemd160 , hash160 , ensureBuffer , padHexStart , isHex } ;