import { BufferReader , BufferWriter } from "@node-lightning/bufio" ; import { ShortChannelId } from "@node-lightning/core" ; import { shortChannelIdFromBuffer } from "@node-lightning/core" ; import { MessageType } from "../MessageType" ; import { Encoder } from "../serialize/Encoder" ; import { EncodingType } from "../serialize/EncodingType" ; import { readTlvs } from "../serialize/readTlvs" ; import { IWireMessage } from "./IWireMessage" ; export class ReplyChannelRangeMessage implements IWireMessage { public static deserialize ( payload ) { const instance = new ReplyChannelRangeMessage ( ) ; const reader = new BufferReader ( payload ) ; reader . readUInt16BE ( ) ; instance . chainHash = reader . readBytes ( 32 ) ; instance . firstBlocknum = reader . readUInt32BE ( ) ; instance . numberOfBlocks = reader . readUInt32BE ( ) ; instance . fullInformation = reader . readUInt8 ( ) === 1 ; const encodedLen = reader . readUInt16BE ( ) ; const encodedScidBytes = reader . readBytes ( encodedLen ) ; const scidsBytes = new Encoder ( ) . decode ( encodedScidBytes ) ; const scidsReader = new BufferReader ( scidsBytes ) ; while ( ! scidsReader . eof ) { const scidBytes = scidsReader . readBytes ( 8 ) ; instance . shortChannelIds . push ( shortChannelIdFromBuffer ( scidBytes ) ) ; } readTlvs ( reader , ( type , valueReader ) => { switch ( type ) { case BigInt ( 1 ) : { const bytes = valueReader . readBytes ( ) ; const decodedBytes = new Encoder ( ) . decode ( bytes ) ; const decodedReader = new BufferReader ( decodedBytes ) ; while ( ! decodedReader . eof ) { instance . timestamps . push ( [ decodedReader . readUInt32BE ( ) , decodedReader . readUInt32BE ( ) , ] ) ; } return true ; } case BigInt ( 3 ) : { while ( ! valueReader . eof ) { instance . checksums . push ( [ valueReader . readUInt32BE ( ) , valueReader . readUInt32BE ( ) , ] ) ; } return true ; } default : return false ; } } ) ; return instance ; } public type : MessageType = MessageType . ReplyChannelRange ; public chainHash : Buffer ; public firstBlocknum : number ; public numberOfBlocks : number ; public fullInformation : boolean ; public shortChannelIds : ShortChannelId [ ] = [ ] ; public timestamps : Array < [ number , number ] > = [ ] ; public checksums : Array < [ number , number ] > = [ ] ; public serialize ( encoding = EncodingType . ZlibDeflate ) { const rawSids = Buffer . concat ( this . shortChannelIds . map ( p => p . toBuffer ( ) ) ) ; const esids = new Encoder ( ) . encode ( encoding , rawSids ) ; const writer = new BufferWriter ( ) ; writer . writeUInt16BE ( this . type ) ; writer . writeBytes ( this . chainHash ) ; writer . writeUInt32BE ( this . firstBlocknum ) ; writer . writeUInt32BE ( this . numberOfBlocks ) ; writer . writeUInt8 ( this . fullInformation ? 1 : 0 ) ; writer . writeUInt16BE ( esids . length ) ; writer . writeBytes ( esids ) ; if ( this . timestamps . length ) { const valueWriter = new BufferWriter ( ) ; for ( const [ v1 , v2 ] of this . timestamps ) { valueWriter . writeUInt32BE ( v1 ) ; valueWriter . writeUInt32BE ( v2 ) ; } const value = new Encoder ( ) . encode ( encoding , valueWriter . toBuffer ( ) ) ; writer . writeBigSize ( 1 ) ; writer . writeBigSize ( value . length ) ; writer . writeBytes ( value ) ; } if ( this . checksums . length ) { const valueWriter = new BufferWriter ( ) ; for ( const [ v1 , v2 ] of this . checksums ) { valueWriter . writeUInt32BE ( v1 ) ; valueWriter . writeUInt32BE ( v2 ) ; } const value = valueWriter . toBuffer ( ) ; writer . writeBigSize ( 3 ) ; writer . writeBigSize ( value . length ) ; writer . writeBytes ( value ) ; } return writer . toBuffer ( ) ; } }