param value export const numberToBinUintLE = ( value ) => { const baseUint8Array = 256 ; const result = [ ] ; let remaining = value ; while ( remaining >= baseUint8Array ) { result . push ( remaining % baseUint8Array ) ; remaining = Math . floor ( remaining / baseUint8Array ) ; } if ( remaining > 0 ) result . push ( remaining ) ; return Uint8Array . from ( result ) ; } ; param bin param bytes export const binToFixedLength = ( bin , bytes ) => { const fixedBytes = new Uint8Array ( bytes ) ; const maxValue = 255 ; bin . length > bytes ? fixedBytes . fill ( maxValue ) : fixedBytes . set ( bin ) ; return fixedBytes ; } ; param value export const numberToBinUint16LEClamped = ( value ) => { const uint16 = 2 ; return binToFixedLength ( numberToBinUintLE ( value ) , uint16 ) ; } ; param value export const numberToBinUint32LEClamped = ( value ) => { const uint32 = 4 ; return binToFixedLength ( numberToBinUintLE ( value ) , uint32 ) ; } ; param value export const numberToBinUint16LE = ( value ) => { const uint16Length = 2 ; const bin = new Uint8Array ( uint16Length ) ; const writeAsLittleEndian = true ; const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; view . setUint16 ( 0 , value , writeAsLittleEndian ) ; return bin ; } ; param value export const numberToBinInt16LE = ( value ) => { const int16Length = 2 ; const bin = new Uint8Array ( int16Length ) ; const writeAsLittleEndian = true ; const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; view . setInt16 ( 0 , value , writeAsLittleEndian ) ; return bin ; } ; param value export const numberToBinInt32LE = ( value ) => { const int32Length = 4 ; const bin = new Uint8Array ( int32Length ) ; const writeAsLittleEndian = true ; const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; view . setInt32 ( 0 , value , writeAsLittleEndian ) ; return bin ; } ; param bin export const binToNumberInt16LE = ( bin ) => { const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; const readAsLittleEndian = true ; return view . getInt16 ( 0 , readAsLittleEndian ) ; } ; param bin export const binToNumberInt32LE = ( bin ) => { const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; const readAsLittleEndian = true ; return view . getInt32 ( 0 , readAsLittleEndian ) ; } ; param value export const numberToBinUint16BE = ( value ) => { const uint16Length = 2 ; const bin = new Uint8Array ( uint16Length ) ; const writeAsLittleEndian = false ; const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; view . setUint16 ( 0 , value , writeAsLittleEndian ) ; return bin ; } ; param value export const numberToBinUint32LE = ( value ) => { const uint32Length = 4 ; const bin = new Uint8Array ( uint32Length ) ; const writeAsLittleEndian = true ; const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; view . setUint32 ( 0 , value , writeAsLittleEndian ) ; return bin ; } ; param value export const numberToBinUint32BE = ( value ) => { const uint32Length = 4 ; const bin = new Uint8Array ( uint32Length ) ; const writeAsLittleEndian = false ; const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; view . setUint32 ( 0 , value , writeAsLittleEndian ) ; return bin ; } ; param value export const bigIntToBinUintLE = ( value ) => { const baseUint8Array = 256 ; const base = BigInt ( baseUint8Array ) ; const result = [ ] ; let remaining = value ; while ( remaining >= base ) { result . push ( Number ( remaining % base ) ) ; remaining /= base ; } if ( remaining > BigInt ( 0 ) ) result . push ( Number ( remaining ) ) ; return Uint8Array . from ( result . length > 0 ? result : [ 0 ] ) ; } ; param value export const bigIntToBinUint64LEClamped = ( value ) => { const uint64 = 8 ; return binToFixedLength ( bigIntToBinUintLE ( value ) , uint64 ) ; } ; param value export const bigIntToBinUint64LE = ( value ) => { const uint64LengthInBits = 64 ; const valueAsUint64 = BigInt . asUintN ( uint64LengthInBits , value ) ; const fixedLengthBin = bigIntToBinUint64LEClamped ( valueAsUint64 ) ; return fixedLengthBin ; } ; remarks param value export const numberToBinInt32TwosCompliment = ( value ) => { const bytes = 4 ; const bitsInAByte = 8 ; const bin = new Uint8Array ( bytes ) ; for ( let offset = 0 ; offset < bytes ; offset ++ ) { bin [ offset ] = value ; value >>>= bitsInAByte ; } return bin ; } ; privateRemarks param bin param bytes export const binToNumberUintLE = ( bin , bytes = bin . length ) => { const base = 2 ; const bitsInAByte = 8 ; if ( bin . length !== bytes ) { throw new TypeError ( `Bin length must be ${ bytes }.` ) ; } return new Uint8Array ( bin . buffer , bin . byteOffset , bin . length ) . reduce ( ( accumulated , byte , i ) => accumulated + byte * base ** ( bitsInAByte * i ) , 0 ) ; } ; param bin export const binToNumberUint16LE = ( bin ) => { const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; const readAsLittleEndian = true ; return view . getUint16 ( 0 , readAsLittleEndian ) ; } ; param bin export const binToNumberUint32LE = ( bin ) => { const view = new DataView ( bin . buffer , bin . byteOffset , bin . byteLength ) ; const readAsLittleEndian = true ; return view . getUint32 ( 0 , readAsLittleEndian ) ; } ; param bin param bytes export const binToBigIntUintBE = ( bin , bytes = bin . length ) => { const bitsInAByte = 8 ; const shift = BigInt ( bitsInAByte ) ; if ( bin . length !== bytes ) { throw new TypeError ( `Bin length must be ${ bytes }.` ) ; } return new Uint8Array ( bin . buffer , bin . byteOffset , bin . length ) . reduce ( ( accumulated , byte ) => ( accumulated << shift ) | BigInt ( byte ) , BigInt ( 0 ) ) ; } ; param bin export const binToBigIntUint256BE = ( bin ) => { const uint256Bytes = 32 ; return binToBigIntUintBE ( bin , uint256Bytes ) ; } ; param value export const bigIntToBinUint256BEClamped = ( value ) => { const uint256Bytes = 32 ; return binToFixedLength ( bigIntToBinUintLE ( value ) , uint256Bytes ) . reverse ( ) ; } ; param bin param bytes export const binToBigIntUintLE = ( bin , bytes = bin . length ) => { const bitsInAByte = 8 ; if ( bin . length !== bytes ) { throw new TypeError ( `Bin length must be ${ bytes }.` ) ; } return new Uint8Array ( bin . buffer , bin . byteOffset , bin . length ) . reduceRight ( ( accumulated , byte ) => ( accumulated << BigInt ( bitsInAByte ) ) | BigInt ( byte ) , BigInt ( 0 ) ) ; } ; param bin export const binToBigIntUint64LE = ( bin ) => { const uint64LengthInBytes = 8 ; const truncatedBin = bin . length > uint64LengthInBytes ? bin . slice ( 0 , uint64LengthInBytes ) : bin ; return binToBigIntUintLE ( truncatedBin , uint64LengthInBytes ) ; } ; const enum VarInt { uint8MaxValue = 0xfc , uint16Prefix = 0xfd , uint16MaxValue = 0xffff , uint32Prefix = 0xfe , uint32MaxValue = 0xffffffff , uint64Prefix = 0xff } param firstByte export const varIntPrefixToSize = ( firstByte ) => { const uint8 = 1 ; const uint16 = 2 ; const uint32 = 4 ; const uint64 = 8 ; switch ( firstByte ) { case VarInt . uint16Prefix : return uint16 + 1 ; case VarInt . uint32Prefix : return uint32 + 1 ; case VarInt . uint64Prefix : return uint64 + 1 ; default : return uint8 ; } } ; param bin param offset export const readBitcoinVarInt = ( bin , offset = 0 ) => { const bytes = varIntPrefixToSize ( bin [ offset ] ) ; const hasPrefix = bytes !== 1 ; return { nextOffset : offset + bytes , value : hasPrefix ? binToBigIntUintLE ( bin . subarray ( offset + 1 , offset + bytes ) , bytes - 1 ) : binToBigIntUintLE ( bin . subarray ( offset , offset + bytes ) , 1 ) , } ; } ; param value export const bigIntToBitcoinVarInt = ( value ) => value <= BigInt ( VarInt . uint8MaxValue ) ? Uint8Array . of ( Number ( value ) ) : value <= BigInt ( VarInt . uint16MaxValue ) ? Uint8Array . from ( [ VarInt . uint16Prefix , ... numberToBinUint16LE ( Number ( value ) ) , ] ) : value <= BigInt ( VarInt . uint32MaxValue ) ? Uint8Array . from ( [ VarInt . uint32Prefix , ... numberToBinUint32LE ( Number ( value ) ) , ] ) : Uint8Array . from ( [ VarInt . uint64Prefix , ... bigIntToBinUint64LE ( value ) ] ) ;