@node-lightning/wire
Version:
Lightning Network Wire Protocol
91 lines • 3.97 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryShortChannelIdsMessage = void 0;
const bufio_1 = require("@node-lightning/bufio");
const core_1 = require("@node-lightning/core");
const core_2 = require("@node-lightning/core");
const MessageType_1 = require("../MessageType");
const Encoder_1 = require("../serialize/Encoder");
const EncodingType_1 = require("../serialize/EncodingType");
const readTlvs_1 = require("../serialize/readTlvs");
class QueryShortChannelIdsMessage {
constructor() {
/**
* Type 261
*/
this.type = MessageType_1.MessageType.QueryShortChannelIds;
/**
* List of channels to query
*/
this.shortChannelIds = [];
/**
* Optional flags that can be set when gossip_queries_ex is enabled.
*/
this.flags = [];
}
static deserialize(payload) {
const instance = new QueryShortChannelIdsMessage();
const reader = new bufio_1.BufferReader(payload);
reader.readUInt16BE(); // read off type
instance.chainHash = reader.readBytes(32); // chain_hash
// process the encoded short channel ids by reading the length of
// encoded data, then using the decoder to get the raw buffer after
// using the appropriate decoding scheme
const encodedScidLen = reader.readUInt16BE();
const encodedScidBuffer = reader.readBytes(encodedScidLen);
const scidBuffer = new Encoder_1.Encoder().decode(encodedScidBuffer);
// After we have a raw buffer of scid values (each 8 bytes) we can read
// the data from the buffer
const scidReader = new bufio_1.BufferReader(scidBuffer);
while (!scidReader.eof) {
const scid = core_2.shortChannelIdFromBuffer(scidReader.readBytes(8));
instance.shortChannelIds.push(scid);
}
// Process all TLVs available on the reader
readTlvs_1.readTlvs(reader, (type, valueReader) => {
switch (type) {
case BigInt(1): {
const bytes = valueReader.readBytes();
const flagBytes = new Encoder_1.Encoder().decode(bytes);
const flagReader = new bufio_1.BufferReader(flagBytes);
while (!flagReader.eof) {
const rawFlags = flagReader.readBigSize();
const flags = new core_1.BitField(rawFlags);
instance.flags.push(flags);
}
return true;
}
default:
return false;
}
});
return instance;
}
serialize(encoding = EncodingType_1.EncodingType.ZlibDeflate) {
const rawIdsBuffer = Buffer.concat(this.shortChannelIds.map(p => p.toBuffer()));
const esids = new Encoder_1.Encoder().encode(encoding, rawIdsBuffer);
const writer = new bufio_1.BufferWriter();
writer.writeUInt16BE(this.type);
writer.writeBytes(this.chainHash);
writer.writeUInt16BE(esids.length);
writer.writeBytes(esids);
// encode TLV 1
if (this.flags.length) {
// combine all BitFields into buffers
const flagBufs = [];
for (const flag of this.flags) {
flagBufs.push(flag.toBuffer());
}
const flagBytes = Buffer.concat(flagBufs);
// encode the flag bytes according to the encoding strategy
const encodedFlagBytes = new Encoder_1.Encoder().encode(encoding, flagBytes);
// write TLV data
writer.writeBigSize(1);
writer.writeBigSize(encodedFlagBytes.length);
writer.writeBytes(encodedFlagBytes);
}
return writer.toBuffer();
}
}
exports.QueryShortChannelIdsMessage = QueryShortChannelIdsMessage;
//# sourceMappingURL=QueryShortChannelIdsMessage.js.map