All files / lib/messages AnnouncementSignaturesMessage.ts

16.67% Statements 4/24
100% Branches 0/0
0% Functions 0/3
16.67% Lines 4/24

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 831x   1x 1x                             1x                                                                                                                                
import { BufferReader, BufferWriter } from "@node-lightning/bufio";
import { ShortChannelId } from "@node-lightning/core";
import { shortChannelIdFromBuffer } from "@node-lightning/core";
import { MessageType } from "../MessageType";
import { IWireMessage } from "./IWireMessage";
 
/**
 * This is a direct messagee between two endpoints of a channel
 * and serves as an opt-in mechanism to allow the
 * announcement of the channel to the rest of the network. It
 * contains the necessary signatuures, by the sender, to construct
 * the channel_announcement message.
 *
 * The message constructed by constructing a channel_announcement
 * message, corresponding to the newly created channel, and signing
 * it with the secrets matching an endpoint's node_id and
 * bitcoin_key.
 */
export class AnnouncementSignaturesMessage implements IWireMessage {
    /**
     * Deserializes a Buffer into an AnnouncementSignaturesMessage.
     */
    public static deserialize(payload: Buffer): AnnouncementSignaturesMessage {
        const reader = new BufferReader(payload);
        reader.readUInt16BE(); // read off type
 
        const instance = new AnnouncementSignaturesMessage();
        instance.channelId = reader.readBytes(32);
        instance.shortChannelId = shortChannelIdFromBuffer(reader.readBytes(8));
        instance.nodeSignature = reader.readBytes(64);
        instance.bitcoinSignature = reader.readBytes(64);
        return instance;
    }
 
    /**
     * Message type - 259
     */
    public type: MessageType = MessageType.AnnouncementSignatures;
 
    /**
     * Buffer of the channel_id for the message.
     */
    public channelId: Buffer = Buffer.alloc(0);
 
    /**
     * ShortChannelId is a unique reference to the funding output of
     * the channel.
     */
    public shortChannelId: ShortChannelId;
 
    /**
     * Buffer containing the signature of the channel_announcement message
     * signed by the endpoint's node_id.
     */
    public nodeSignature: Buffer = Buffer.alloc(0);
 
    /**
     * Buffer containing the signaturee of the channel_announcment message
     * signed by the endpoint's bitcoin_key.
     */
    public bitcoinSignature: Buffer = Buffer.alloc(0);
 
    /**
     * Serializes the instance into a Buffer suitable for
     * transmission on the wire.
     */
    public serialize(): Buffer {
        const len =
            2 + // type
            32 + // channel_id
            8 + // short_channel_id
            64 + // node_signature
            64; // bitcoin_signaturee
        const writer = new BufferWriter(Buffer.alloc(len));
        writer.writeUInt16BE(this.type);
        writer.writeBytes(this.channelId);
        writer.writeBytes(this.shortChannelId.toBuffer());
        writer.writeBytes(this.nodeSignature);
        writer.writeBytes(this.bitcoinSignature);
        return writer.toBuffer();
    }
}