@node-lightning/wire
Version:
Lightning Network Wire Protocol
134 lines • 5.6 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ChannelAnnouncementMessage = void 0;
const bufio_1 = require("@node-lightning/bufio");
const core_1 = require("@node-lightning/core");
const core_2 = require("@node-lightning/core");
const crypto = __importStar(require("@node-lightning/crypto"));
const MessageType_1 = require("../MessageType");
/**
* Message contains ownership information regarding a channel.
* It ties each on-chain Bitcoin key to the associated Lightning
* node key, and vice-versa. Proviing the existance of a channel
* between node_1 and node_2 requires:
* 1. proving that the funding pays to bitcoin_key_1 and bitcoin_key_2
* 2. proving that node_1 owns bitcoin_key_1
* 3. proving that node_2 owns bitcoin_key_2
*
* This also varifies that both nodes want to announce the channel.
* The required data to perform all of these proofs is available
* in this message.
*/
class ChannelAnnouncementMessage {
constructor() {
/**
* The message type - 256
*/
this.type = MessageType_1.MessageType.ChannelAnnouncement;
}
/**
* Deserializes the Buffer into a ChannelAnnouncementMessage.
*/
static deserialize(payload) {
const instance = new ChannelAnnouncementMessage();
const reader = new bufio_1.BufferReader(payload);
reader.readUInt16BE(); // read off type
instance.nodeSignature1 = reader.readBytes(64);
instance.nodeSignature2 = reader.readBytes(64);
instance.bitcoinSignature1 = reader.readBytes(64);
instance.bitcoinSignature2 = reader.readBytes(64);
const len = reader.readUInt16BE();
instance.features = core_1.BitField.fromBuffer(reader.readBytes(len));
instance.chainHash = reader.readBytes(32);
instance.shortChannelId = core_2.shortChannelIdFromBuffer(reader.readBytes(8));
instance.nodeId1 = reader.readBytes(33);
instance.nodeId2 = reader.readBytes(33);
instance.bitcoinKey1 = reader.readBytes(33);
instance.bitcoinKey2 = reader.readBytes(33);
return instance;
}
/**
* Message hashing is after the first 258 bytes of the message
* and excludes the type and signatures. It performs a double
* sha-256 hash of the remaining bytes.
*/
static hash(msg) {
const bytes = msg.serialize().slice(258);
return crypto.hash256(bytes);
}
/**
* Performs validation the message was signed by each node and the
* the corresponding bitcoin key is owned by the owner of the node.
*
* This is accomplished by:
* 1. verifying the bitcoinSignatures1/2 are validate signatures
* from bitcoinKey1/2
* 2. verifying the nodeSignature1/2 are validate signatures
* from nodeId1/2
*/
static verifySignatures(msg) {
const hash = ChannelAnnouncementMessage.hash(msg);
return (crypto.verifySig(hash, msg.bitcoinSignature1, msg.bitcoinKey1) &&
crypto.verifySig(hash, msg.bitcoinSignature2, msg.bitcoinKey2) &&
crypto.verifySig(hash, msg.nodeSignature1, msg.nodeId1) &&
crypto.verifySig(hash, msg.nodeSignature2, msg.nodeId2));
}
/**
* Serializes the intancee into a Buffer suitable
* for wire transport
*/
serialize() {
const featuresBuffer = this.features.toBuffer();
const featuresLen = featuresBuffer.length;
const len = 2 + // type
64 + // node_signature_1
64 + // node_signature_2
64 + // bitcoin_signature_1
64 + // bitcoin_signature_2
2 + // len
featuresLen +
32 + // chain_hash
8 + // short_channel_id
33 + // node_id_1
33 + // node_id_2
33 + // bitcoin_key_1
33; // bitcoin_key_2
const writer = new bufio_1.BufferWriter(Buffer.alloc(len));
writer.writeUInt16BE(this.type);
writer.writeBytes(this.nodeSignature1);
writer.writeBytes(this.nodeSignature2);
writer.writeBytes(this.bitcoinSignature1);
writer.writeBytes(this.bitcoinSignature2);
writer.writeUInt16BE(featuresLen);
if (featuresLen > 0)
writer.writeBytes(featuresBuffer);
writer.writeBytes(this.chainHash);
writer.writeBytes(this.shortChannelId.toBuffer());
writer.writeBytes(this.nodeId1);
writer.writeBytes(this.nodeId2);
writer.writeBytes(this.bitcoinKey1);
writer.writeBytes(this.bitcoinKey2);
return writer.toBuffer();
}
}
exports.ChannelAnnouncementMessage = ChannelAnnouncementMessage;
//# sourceMappingURL=ChannelAnnouncementMessage.js.map