All files / lib/messages FundingCreatedMessage.ts

20% Statements 4/20
100% Branches 0/0
0% Functions 0/3
20% Lines 4/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 78 79 80 81 82 83 841x   1x                       1x 1x                                                                                                                                        
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IWireMessage } from './IWireMessage';
 
/**
 * The `funding_created` message is sent by the channel initiator after
 * the channel has been accepted with `accept_channel`. The channel
 * initiator includes the outpoint for the funding transaction as well
 * as their signature for the counter-parties first commitment
 * transaction. This message provides the channel acceptor the
 * information they need to sign the initiator's first commitment
 * transaction in the next message: `funding_signed`.
 */
export class FundingCreatedMessage implements IWireMessage {
  public static type = MessageType.FundingCreated;
 
  /**
   * Deserializes an funding_created message
   * @param buf
   */
  public static deserialize(buf: Buffer): FundingCreatedMessage {
    const instance = new FundingCreatedMessage();
    const reader = new BufferReader(buf);
 
    reader.readUInt16BE(); // read type
    instance.temporaryChannelId = reader.readBytes(32);
    instance.fundingTxId = reader.readBytes(32);
    instance.fundingOutputIndex = reader.readUInt16BE();
    instance.signature = reader.readBytes(64);
 
    return instance;
  }
 
  /**
   * The type for message. funding_created = 34
   */
  public type = FundingCreatedMessage.type;
 
  /**
   * A unique and temporary identifier used during the channel creation
   * process. This value is a placeholder for the channel_id that is
   * created from the funding transaction that can be used after the
   * funding_created message creates this transaction and the
   * counterparty uses the channel_id in the funding_signed message.
   */
  public temporaryChannelId: Buffer;
 
  /**
   * Transaction identifier for the funding transaction as represented
   * as a sha256 hash of the serialized funding transaction. This
   * value uses internal or natural byte order. Transaction identifiers
   * often are in RPC or reversed byte order.
   */
  public fundingTxId: Buffer;
 
  /**
   * Output index for the transaction which should be between 0 and
   * 65535 u16 value.
   */
  public fundingOutputIndex: number;
 
  /**
   * 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.temporaryChannelId);
    writer.writeBytes(this.fundingTxId);
    writer.writeUInt16BE(this.fundingOutputIndex);
    writer.writeBytes(this.signature);
    return writer.toBuffer();
  }
}