@node-lightning/wire
Version:
Lightning Network Wire Protocol
119 lines • 5.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ReplyChannelRangeMessage = void 0;
const bufio_1 = require("@node-lightning/bufio");
const core_1 = 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 ReplyChannelRangeMessage {
constructor() {
this.type = MessageType_1.MessageType.ReplyChannelRange;
this.shortChannelIds = [];
this.timestamps = [];
this.checksums = [];
}
static deserialize(payload) {
const instance = new ReplyChannelRangeMessage();
const reader = new bufio_1.BufferReader(payload);
// read type bytes
reader.readUInt16BE();
instance.chainHash = reader.readBytes(32);
instance.firstBlocknum = reader.readUInt32BE();
instance.numberOfBlocks = reader.readUInt32BE();
instance.fullInformation = reader.readUInt8() === 1;
// read encoded_short_ids
const encodedLen = reader.readUInt16BE(); // encoded_short_channel_id bytes
const encodedScidBytes = reader.readBytes(encodedLen);
const scidsBytes = new Encoder_1.Encoder().decode(encodedScidBytes);
const scidsReader = new bufio_1.BufferReader(scidsBytes);
while (!scidsReader.eof) {
const scidBytes = scidsReader.readBytes(8);
instance.shortChannelIds.push(core_1.shortChannelIdFromBuffer(scidBytes));
}
// read tlvs in the reply_channel_range realm
readTlvs_1.readTlvs(reader, (type, valueReader) => {
switch (type) {
// timestamps TLVs include the timestamps for the node1/2
// node_update messages. A tuple [number, number] will be
// returned for each short_channel_id that is returned.
// Timestamps are an encoded field where the first byte
// indicates the encoding type (RAW or ZLIB DEFLATE).
case BigInt(1): {
const bytes = valueReader.readBytes();
const decodedBytes = new Encoder_1.Encoder().decode(bytes);
const decodedReader = new bufio_1.BufferReader(decodedBytes);
while (!decodedReader.eof) {
instance.timestamps.push([
decodedReader.readUInt32BE(),
decodedReader.readUInt32BE(),
]);
}
return true;
}
// checksum TLVs include the checksums for the node1/2
// node_update message. A tuple [number, number] will be
// returned for each short_channel_id that is returned
case BigInt(3): {
while (!valueReader.eof) {
instance.checksums.push([
valueReader.readUInt32BE(),
valueReader.readUInt32BE(),
]);
}
return true;
}
// return that the TLV type was not handled
default:
return false;
}
});
return instance;
}
serialize(encoding = EncodingType_1.EncodingType.ZlibDeflate) {
// encode short channel ids
const rawSids = Buffer.concat(this.shortChannelIds.map(p => p.toBuffer()));
const esids = new Encoder_1.Encoder().encode(encoding, rawSids);
const writer = new bufio_1.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);
// write timestamp TLV if it is required. The timestaps are encoded as
// uint32BE tuples corresponding to the timestamps for node1/2
// channel_update messages. This buffer uses the encoding format
// supplied and will be either RAW or ZLIB DEFLARE
if (this.timestamps.length) {
const valueWriter = new bufio_1.BufferWriter();
for (const [v1, v2] of this.timestamps) {
valueWriter.writeUInt32BE(v1);
valueWriter.writeUInt32BE(v2);
}
const value = new Encoder_1.Encoder().encode(encoding, valueWriter.toBuffer());
writer.writeBigSize(1); // type
writer.writeBigSize(value.length); // length
writer.writeBytes(value); // value
}
// write checksums TLV if it is required. The checksums are encoded as
// uint32BE tuples corresponding to the checksums for node1/2
// channel_update messages
if (this.checksums.length) {
const valueWriter = new bufio_1.BufferWriter();
for (const [v1, v2] of this.checksums) {
valueWriter.writeUInt32BE(v1);
valueWriter.writeUInt32BE(v2);
}
const value = valueWriter.toBuffer();
writer.writeBigSize(3); // type
writer.writeBigSize(value.length); // length
writer.writeBytes(value); // value
}
return writer.toBuffer();
}
}
exports.ReplyChannelRangeMessage = ReplyChannelRangeMessage;
//# sourceMappingURL=ReplyChannelRangeMessage.js.map