import BigNum from 'browserify-bignum' ; export const bigNumberToBuffer = ( bignumber , size ) => new BigNum ( bignumber ) . toBuffer ( { size , endian : 'big' } ) ; export const bufferToBigNumberString = ( bigNumberBuffer ) : string => BigNum . fromBuffer ( bigNumberBuffer ) . toString ( ) ; export const bufferToHex = ( buffer ) : string => Buffer . from ( buffer ) . toString ( 'hex' ) ; const hexRegex = /^[0-9a-f]+/i ; export const hexToBuffer = ( hex , argumentName = 'Argument' ) : Buffer => { if ( typeof hex !== 'string' ) { throw new TypeError ( `${ argumentName } must be a string.` ) ; } const matchedHex = ( hex . match ( hexRegex ) || [ ] ) [ 0 ] ; if ( ! matchedHex || matchedHex . length !== hex . length ) { throw new TypeError ( `${ argumentName } must be a valid hex string.` ) ; } if ( matchedHex . length % 2 !== 0 ) { throw new TypeError ( `${ argumentName } must have a valid length of hex string.` ) ; } return Buffer . from ( matchedHex , 'hex' ) ; } ;