import BigNumber from "bignumber.js" ; type Bit = 0 | 1 ; export function parsePagingToken ( token ) { const binaryToken = new BigNumber ( token ) . toString ( 2 ) . padStart ( 64 , "0" ) ; return { ledgerSeq : parseInt ( binaryToken . slice ( 0 , 32 ) , 2 ) , txIndex : parseInt ( binaryToken . slice ( 32 , 52 ) , 2 ) , opIndex : parseInt ( binaryToken . slice ( 52 ) , 2 ) } ; } export function buildOperationId ( ledgerSeq , txIndex , opIndex ) { const result = Buffer . alloc ( 8 ) ; const ledgerPart = toBitsArray ( ledgerSeq , 32 ) ; const txPart = toBitsArray ( txIndex , 20 ) ; const opPart = toBitsArray ( opIndex , 12 ) ; opPart . reverse ( ) . forEach ( ( bitValue , i ) => { setBit ( result , i , bitValue ) ; } ) ; txPart . reverse ( ) . forEach ( ( bitValue , i ) => { setBit ( result , 12 + i , bitValue ) ; } ) ; ledgerPart . reverse ( ) . forEach ( ( bitValue , i ) => { setBit ( result , 32 + i , bitValue ) ; } ) ; return new BigNumber ( result . toString ( "hex" ) , 16 ) . toString ( ) ; } function setBit ( buffer , bitPosition , value ) { const byteNum = buffer . length - Math . floor ( bitPosition / 8 ) - 1 ; const bitNum = bitPosition % 8 ; if ( value === 0 ) { buffer [ byteNum ] &= ~ ( 1 << bitNum ) ; } else { buffer [ byteNum ] |= 1 << bitNum ; } } function toBitsArray ( num , padding = 32 ) { return num . toString ( 2 ) . padStart ( padding , "0" ) . split ( "" ) . map ( e => parseInt ( e , 10 ) ) as Bit [ ] ; }