/**
 * Utility class for writing arbitrary data into a Buffer. This class will
 * automatically expand the underlying Buffer and return a trimmed view
 * when complete.
 */ export class BufferWriter { private _position : number ; private _fixed : boolean ; private _buffer : Buffer ; param buffer constructor ( buffer ? ) { this . _position = 0 ; this . _fixed = ! ! buffer ; this . _buffer = buffer || Buffer . alloc ( 0 ) ; } /**
     * Gets the current size of the output Buffer
     */ public get size ( ) : number { return this . _position ; } /**
     * Returns the Buffer which will be either the full Buffer if this was a
     * fixed Buffer or will be the expandable Buffer sliced to the current
     * position
     */ public toBuffer ( ) { if ( this . _fixed ) return this . _buffer ; else return this . _buffer . slice ( 0 , this . _position ) ; } param val public writeUInt8 ( val ) { this . _writeStandard ( this . writeUInt8 . name , val , 1 ) ; } param val public writeUInt16LE ( val ) { this . _writeStandard ( this . writeUInt16LE . name , val , 2 ) ; } param val public writeUInt16BE ( val ) { this . _writeStandard ( this . writeUInt16BE . name , val , 2 ) ; } param val public writeUInt32LE ( val ) { this . _writeStandard ( this . writeUInt32LE . name , val , 4 ) ; } param val public writeUInt32BE ( val ) { this . _writeStandard ( this . writeUInt32BE . name , val , 4 ) ; } param value public writeUInt64LE ( value ) { const val = BigInt ( value ) ; if ( val < 0 || val >= BigInt ( 2 ) ** BigInt ( 64 ) ) { throw new RangeError ( `The value of "value" is out of range. It must be >= 0 and <= 18446744073709551615. Received ${ value . toString ( ) }` ) ; } const buf = Buffer . from ( val . toString ( 16 ) . padStart ( 16 , "0" ) , "hex" ) ; this . writeBytes ( buf . reverse ( ) ) ; } param value public writeUInt64BE ( value ) { const val = BigInt ( value ) ; if ( val < 0 || val >= BigInt ( 2 ) ** BigInt ( 64 ) ) { throw new RangeError ( `The value of "value" is out of range. It must be >= 0 and <= 18446744073709551615. Received ${ value . toString ( ) }` ) ; } const buf = Buffer . from ( val . toString ( 16 ) . padStart ( 16 , "0" ) , "hex" ) ; this . writeBytes ( buf ) ; } param buffer public writeBytes ( buffer ) { if ( ! buffer || ! buffer . length ) return ; this . _expand ( buffer . length ) ; buffer . copy ( this . _buffer , this . _position ) ; this . _position += buffer . length ; } /**
     * Reads a variable length unsigned integer in little-endian as specified in
     * the Bitcoin protocol documentation.
     *
     * < 0xfd = 1 byte number
     *   0xfd = 2 byte number (3 bytes total)
     *   0xfe = 4 byte number (5 bytes total)
     *   0xff = 8 byte number (9 bytes total)
     */ public writeVarInt ( val ) { const num = BigInt ( val ) ; if ( num < BigInt ( 0xfd ) ) { this . writeUInt8 ( Number ( num ) ) ; } else if ( num < BigInt ( 0x10000 ) ) { this . writeUInt8 ( 0xfd ) ; this . writeUInt16LE ( Number ( num ) ) ; } else if ( num < BigInt ( 0x100000000 ) ) { this . writeUInt8 ( 0xfe ) ; this . writeUInt32LE ( Number ( num ) ) ; } else { this . writeUInt8 ( 0xff ) ; this . writeUInt64LE ( num ) ; } } remarks public writeBigSize ( val ) { const num = BigInt ( val ) ; if ( num < BigInt ( 0xfd ) ) { this . writeUInt8 ( Number ( num ) ) ; } else if ( num < BigInt ( 0x10000 ) ) { this . writeUInt8 ( 0xfd ) ; this . writeUInt16BE ( Number ( num ) ) ; } else if ( num < BigInt ( 0x100000000 ) ) { this . writeUInt8 ( 0xfe ) ; this . writeUInt32BE ( Number ( num ) ) ; } else { this . writeUInt8 ( 0xff ) ; this . writeUInt64BE ( num ) ; } } param val public writeTUInt16 ( val ) { if ( val === 0 ) return ; const size = val > 0xff ? 2 : 1 ; this . _expand ( size ) ; this . _buffer . writeUIntBE ( val , this . _position , size ) ; this . _position += size ; } /**
     * TLV 0 to 4 byte unsigned integer encoded in big-endian.
     */ public writeTUInt32 ( val ) { if ( val === 0 ) return ; const size = val > 0xffffff ? 4 : val > 0xffff ? 3 : val > 0xff ? 2 : 1 ; this . _expand ( size ) ; this . _buffer . writeUIntBE ( val , this . _position , size ) ; this . _position += size ; } /**
     * TLV 0 to 8 byte unsigned integer encoded in big-endian.
     */ public writeTUInt64 ( val ) { if ( val === BigInt ( 0 ) ) return ; let valString = val . toString ( 16 ) ; if ( valString . length % 2 === 1 ) valString = "0" + valString ; const buf = Buffer . from ( valString , "hex" ) ; this . writeBytes ( buf ) ; } param needed private _expand ( needed ) { const required = this . _position + needed ; if ( this . _fixed && required > this . _buffer . length ) { throw new RangeError ( "Out of range" ) ; } if ( this . _buffer . length < required ) { const newLen = 1 << Math . ceil ( Math . log2 ( required ) ) ; const newBuf = Buffer . alloc ( newLen ) ; this . _buffer . copy ( newBuf ) ; this . _buffer = newBuf ; } } param fn param val param len private _writeStandard ( fn , val , len ) { this . _expand ( len ) ; this . _buffer [ fn ] ( val , this . _position ) ; this . _position += len ; } }