All files / lib/messages FundingSignedMessage.ts

100% Statements 17/17
100% Branches 0/0
100% Functions 3/3
100% Lines 17/17

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 651x 1x 1x                           1x 1x               1x 1x   1x 1x 1x   1x           2x                                     1x 1x 1x 1x 1x      
import { BufferReader, BufferWriter } from "@node-lightning/bufio";
import { ChannelId } from "@node-lightning/core";
import { MessageType } from "../MessageType";
import { IWireMessage } from "./IWireMessage";
 
/**
 * The `funding_signed` message is sent by the channel acceptor after
 * they have recieved a `funding_created` message from the initiator.
 * This message includes the signature for the initiator's first
 * commitment transaction. After the initiator receives this message
 * the channel can be broadcast to the Bitcoin network as both
 * participants can spend the outputs of the funding transaction. This
 * message also is the first instance of the `channel_id` and both sides
 * can transition from using the `temporary_channel_id` to the actual
 * `channel_id`.
 */
export class FundingSignedMessage implements IWireMessage {
    public static type: MessageType = MessageType.FundingSigned;
 
    /**
     * Deserializes the funding_signed message
     * @param buf
     * @returns
     */
    public static deserialize(buf: Buffer): FundingSignedMessage {
        const instance = new FundingSignedMessage();
        const reader = new BufferReader(buf);
 
        reader.readUInt16BE(); // read type
        instance.channelId = new ChannelId(reader.readBytes(32));
        instance.signature = reader.readBytes(64);
 
        return instance;
    }
 
    /**
     * The type for message. funding_signed = 35
     */
    public readonly type: MessageType = FundingSignedMessage.type;
 
    /**
     * ChannelId generated from the funding transactions outpoint.
     */
    public channelId: ChannelId;
 
    /**
     * Signature for the counterpary's first commitment transaction.
     * This signature allows the counterparty to spend the commitment
     * using their own signature. The signature must be 64-bytes
     * representing the 32-byte (r,s) values for an ECDSA signature.
     */
    public signature: Buffer;
 
    /**
     * Serializes the message into a Buffer
     */
    public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeUInt16BE(this.type);
        writer.writeBytes(this.channelId.toBuffer());
        writer.writeBytes(this.signature);
        return writer.toBuffer();
    }
}