UNPKG

@node-lightning/wire

Version:
96 lines 3.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.InitMessage = void 0; const bufio_1 = require("@node-lightning/bufio"); const core_1 = require("@node-lightning/core"); const MessageType_1 = require("../MessageType"); const readTlvs_1 = require("../serialize/readTlvs"); /** * InitMessage is defined in BOLT #1. Once authentication is complete, the first * message reveals the features supported or required by the node sending the * message. This message is sent even on a reconnection. * * This message contains two fields; global features and local features, that * are used to signal how the message should operate. The values of are defined * in the BOLT #9. */ class InitMessage { constructor() { /** * Message type 16 */ this.type = MessageType_1.MessageType.Init; /** * BitField containing the features provided in by the local or remote node */ this.features = new core_1.BitField(); /** * Supported chain_hashes for the remote peer */ this.chainHashes = []; } /** * Processes a buffer containing the message information. This method * will capture the arbitrary length global and local * features into two internal properties of the newly constructed * init message object. */ static deserialize(buffer) { const instance = new InitMessage(); const reader = new bufio_1.BufferReader(buffer); // read the type bytes reader.readUInt16BE(); // read the global features and per the specification, the global // features should not exceed features greater than 13. const gflen = reader.readUInt16BE(); const gf = core_1.BitField.fromBuffer(reader.readBytes(gflen)); // Read the local length and parse into a BN value. const lflen = reader.readUInt16BE(); const lf = core_1.BitField.fromBuffer(reader.readBytes(lflen)); // construct a single features object by bitwise or of the global and // local features. instance.features = new core_1.BitField().or(gf).or(lf); // process TLVs readTlvs_1.readTlvs(reader, (type, valueReader) => { switch (type) { // Process networks TLVs which is a series of chain_hash 32 // byte values. This method will simply read from the stream // until every thing has been read case BigInt(1): { while (!valueReader.eof) { const chainHash = valueReader.readBytes(32); instance.chainHashes.push(chainHash); } return true; } } }); return instance; } /** * Serialize will construct a properly formatted message based on the * properties of the configured message. */ serialize() { const writer = new bufio_1.BufferWriter(); // write the type writer.writeUInt16BE(this.type); // write gflen const gflen = 0; writer.writeUInt16BE(gflen); // write features const features = this.features.toBuffer(); const featuresLen = features.length; writer.writeUInt16BE(featuresLen); writer.writeBytes(features); // write chainhash tlv if (this.chainHashes.length) { writer.writeBigSize(1); // type writer.writeBigSize(this.chainHashes.length * 32); // length writer.writeBytes(Buffer.concat(this.chainHashes)); // value } return writer.toBuffer(); } } exports.InitMessage = InitMessage; //# sourceMappingURL=InitMessage.js.map