All files / lib/messages ClosingSignedMessage.ts

30% Statements 6/20
100% Branches 0/0
0% Functions 0/3
30% Lines 6/20

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 781x 1x 1x   1x                             1x 1x                                                                                                                  
import { Value } from '@node-dlc/bitcoin';
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
import { ChannelId } from '@node-dlc/common';
 
import { MessageType } from '../MessageType';
import { IWireMessage } from './IWireMessage';
 
/**
 * The `closing_signed` message is sent by the channel funder after
 * the `shutdown_message`. The funder chooses a fee it thinks
 * is fair, and signs the closing transaction with the scriptpubkey fields from
 * the shutdown messages (along with its chosen fee) and sends the signature.
 * The other node then replies similarly, using a fee it thinks is fair.
 * One of the benefits of mutual close is that the fee rate can be negotiated
 * down from the existing fee rate(which is generally expensive) to a more reasonable one.
 * In order for fee rate to converge, it has to be strictly between the last proposed
 * and the counterparty's last proposed value. This exchange continues using the
 * channelID until both agree on the same fee or when one side fails the channel.
 */
export class ClosingSignedMessage implements IWireMessage {
  public static type: MessageType = MessageType.ClosingSigned;
 
  /**
   * Deserializes the closing_signed message
   * @param buf
   * @returns
   */
  public static deserialize(buf: Buffer): ClosingSignedMessage {
    const instance = new ClosingSignedMessage();
    const reader = new BufferReader(buf);
 
    reader.readUInt16BE(); // read type
    instance.channelId = new ChannelId(reader.readBytes(32));
    instance.feeSatoshis = Value.fromSats(reader.readUInt64BE());
    instance.signature = reader.readBytes(64);
 
    return instance;
  }
 
  /**
   * The type for message. Closing_Signed = 39
   */
  public readonly type: MessageType = ClosingSignedMessage.type;
 
  /**
   * ChannelId generated from the funding transactions outpoint.
   */
  public channelId: ChannelId;
 
  /**
   * Expected value for fee_satoshis could vary how fee rate negotiations happens between the
   * two nodes.Assuming initially both nodes have different fee estimates, in every iteration
   * they should ensure fee rate is strictly between their last proposed and the counterparty's
   * last proposed value. Hence changes are made accordingly on both ends and communicated
   * between the channel until both of them comes to an agreement or when one side fails the
   * channel.
   */
  public feeSatoshis: Value;
 
  /**
   * Sender signs the closing transaction with the scriptpubkey fields from the
   * shutdown messages (along with its chosen fee) and sends the 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.writeUInt64BE(this.feeSatoshis.sats);
    writer.writeBytes(this.signature);
    return writer.toBuffer();
  }
}