import { BufferReader , BufferWriter } from "@node-lightning/bufio" ; import { BitField } from "@node-lightning/core" ; import { ShortChannelId } from "@node-lightning/core" ; import { shortChannelIdFromBuffer } from "@node-lightning/core" ; import { QueryScidFlags } from "../flags/QueryScidFlags" ; 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 QueryShortChannelIdsMessage implements IWireMessage { public static deserialize ( payload ) { const instance = new QueryShortChannelIdsMessage ( ) ; const reader = new BufferReader ( payload ) ; reader . readUInt16BE ( ) ; instance . chainHash = reader . readBytes ( 32 ) ; const encodedScidLen = reader . readUInt16BE ( ) ; const encodedScidBuffer = reader . readBytes ( encodedScidLen ) ; const scidBuffer = new Encoder ( ) . decode ( encodedScidBuffer ) ; const scidReader = new BufferReader ( scidBuffer ) ; while ( ! scidReader . eof ) { const scid = shortChannelIdFromBuffer ( scidReader . readBytes ( 8 ) ) ; instance . shortChannelIds . push ( scid ) ; } readTlvs ( reader , ( type , valueReader ) => { switch ( type ) { case BigInt ( 1 ) : { const bytes = valueReader . readBytes ( ) ; const flagBytes = new Encoder ( ) . decode ( bytes ) ; const flagReader = new BufferReader ( flagBytes ) ; while ( ! flagReader . eof ) { const rawFlags = flagReader . readBigSize ( ) ; const flags = new BitField < QueryScidFlags > ( rawFlags ) ; instance . flags . push ( flags ) ; } return true ; } default : return false ; } } ) ; return instance ; } /**
     * Type 261
     */ public type : MessageType = MessageType . QueryShortChannelIds ; /**
     * 32-byte chain hash
     */ public chainHash : Buffer ; /**
     * List of channels to query
     */ public shortChannelIds : ShortChannelId [ ] = [ ] ; /**
     * Optional flags that can be set when gossip_queries_ex is enabled.
     */ public flags : Array < BitField < QueryScidFlags > > = [ ] ; public serialize ( encoding = EncodingType . ZlibDeflate ) { const rawIdsBuffer = Buffer . concat ( this . shortChannelIds . map ( p => p . toBuffer ( ) ) ) ; const esids = new Encoder ( ) . encode ( encoding , rawIdsBuffer ) ; const writer = new BufferWriter ( ) ; writer . writeUInt16BE ( this . type ) ; writer . writeBytes ( this . chainHash ) ; writer . writeUInt16BE ( esids . length ) ; writer . writeBytes ( esids ) ; if ( this . flags . length ) { const flagBufs = [ ] ; for ( const flag of this . flags ) { flagBufs . push ( flag . toBuffer ( ) ) ; } const flagBytes = Buffer . concat ( flagBufs ) ; const encodedFlagBytes = new Encoder ( ) . encode ( encoding , flagBytes ) ; writer . writeBigSize ( 1 ) ; writer . writeBigSize ( encodedFlagBytes . length ) ; writer . writeBytes ( encodedFlagBytes ) ; } return writer . toBuffer ( ) ; } }