import { CoinIdentifier } from '../models' ; import { ErrorFactory } from './errors' ; const EMPTY_HEX = '\\x' ; param toFormat export const hexStringFormatter = ( toFormat ? ) : string => ( ! toFormat || toFormat === '' ? EMPTY_HEX : toFormat ) ; export const isEmptyHexString = ( toCheck ) : boolean => toCheck === EMPTY_HEX ; param buffer export const hexFormatter = ( buffer ) : string => buffer . toString ( 'hex' ) ; export const hexStringToBuffer = ( input ) : Buffer => Buffer . from ( isEmptyHexString ( input ) ? '' : input , 'hex' ) ; export const bytesToHex = ( bytes ) : string => hexFormatter ( Buffer . from ( bytes ) ) ; export const add0xPrefix = ( hex ) : string => ( hex . startsWith ( '0x' ) ? hex : `0x${ hex }` ) ; export const remove0xPrefix = ( hex ) : string => ( hex . startsWith ( '0x' ) ? hex . slice ( '0x' . length ) : hex ) ; export const coinIdentifierFormatter = ( coinIdentifier ? ) : CoinIdentifier | undefined => { if ( ! coinIdentifier ) return ; const coin = coinIdentifier . split ( ':' ) ; if ( coin . length === 2 ) { const [ hash , index ] = coin ; if ( Number . isNaN ( parseInt ( index ) ) ) throw ErrorFactory . badFormedCoinError ( `Given coin identifier is ${ coinIdentifier }` ) ; return { hash , index } ; } throw ErrorFactory . badFormedCoinError ( `Given coin identifier is ${ coinIdentifier }` ) ; } ;