import { BigNumber } from 'ethers' ; export const toHexString32 = ( value ) : string => { if ( typeof value === 'string' && value . startsWith ( '0x' ) ) { if ( value . length === 42 ) { return '0x' + remove0x ( value ) . padStart ( 64 , '0' ) . toLowerCase ( ) ; } else { return '0x' + remove0x ( value ) . padEnd ( 64 , '0' ) . toLowerCase ( ) ; } } else if ( typeof value === 'boolean' ) { return '0x' + `${ value ? 1 : 0 }` . padStart ( 64 , '0' ) ; } else { return '0x' + remove0x ( BigNumber . from ( value ) . toHexString ( ) ) . padStart ( 64 , '0' ) . toLowerCase ( ) ; } } ; param inp return export const fromHexString = ( inp ) : Buffer => { if ( typeof inp === 'string' && inp . startsWith ( '0x' ) ) { return Buffer . from ( inp . slice ( 2 ) , 'hex' ) ; } return Buffer . from ( inp ) ; } ; param inp return export const toHexString = ( inp ) : string => { if ( typeof inp === 'number' ) { return BigNumber . from ( inp ) . toHexString ( ) ; } else { return '0x' + fromHexString ( inp ) . toString ( 'hex' ) ; } } ; param str returns export const remove0x = ( str ) : string => { if ( str === undefined ) { return str ; } return str . startsWith ( '0x' ) ? str . slice ( 2 ) : str ; } ; param bn returns export function bigNumberToHex ( bn ) { let bi = BigInt ( bn . toBigInt ( ) ) ; var pos = true ; if ( bi < 0 ) { pos = false ; bi = bitnot ( bi ) ; } var hex = bi . toString ( 16 ) ; if ( hex . length % 2 ) { hex = '0' + hex ; } if ( pos && 0x80 & parseInt ( hex . slice ( 0 , 2 ) , 16 ) ) { hex = '00' + hex ; } return hex ; } function bitnot ( bi ) { var bin = ( - bi ) . toString ( 2 ) ; var prefix = '' ; while ( bin . length % 8 ) { bin = '0' + bin ; } if ( '1' === bin [ 0 ] && - 1 !== bin . slice ( 1 ) . indexOf ( '1' ) ) { prefix = '11111111' ; } bin = bin . split ( '' ) . map ( function ( i ) { return '0' === i ? '1' : '0' ; } ) . join ( '' ) ; return BigInt ( '0b' + prefix + bin ) + BigInt ( 1 ) ; }