# All files / lib/messages CloseTLV.ts

17.86% Statements 5/28
100% Branches 0/0
0% Functions 0/4
17.86% Lines 5/28

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
    84
    85
    86
    87
    88
    891x
    1x

    1x

    1x
    1x

     import { OutPoint } from '@node-lightning/bitcoin';
    import { BufferWriter, StreamReader } from '@node-lightning/bufio';

    import { MessageType } from '../MessageType';
    import { IDlcMessage } from './DlcMessage';

    export class CloseTLV implements IDlcMessage {
      public static type = MessageType.CloseTLV;
      /**
       * Deserializes a close_tlv message
       * @param buf
       */
      public static deserialize(buf: Buffer): CloseTLV {
        const instance = new CloseTLV();
        const reader = StreamReader.fromBuffer(buf);

        reader.readBigSize(); // read type
        instance.length = reader.readBigSize();
        instance.contractId = reader.readBytes(32);
        instance.offerPayoutSatoshis = reader.readBigUInt64BE();
        instance.offerFundingPubKey = reader.readBytes(33);
        instance.acceptFundingPubkey = reader.readBytes(33);
        instance.outpoint = OutPoint.parse(reader);

        return instance;
      }

      public type = CloseTLV.type;

      public length: bigint;

      public contractId: Buffer;

      public offerPayoutSatoshis: bigint;

      public offerFundingPubKey: Buffer;

      public acceptFundingPubkey: Buffer;

      public outpoint: OutPoint;

      /**
       * Converts close_tlv to JSON
       */
      public toJSON(): ICloseTLVJSON {
        return {
          type: this.type,
          contractId: this.contractId.toString('hex'),
          offerPayoutSatoshis: Number(this.offerPayoutSatoshis),
          offerFundingPubKey: this.offerFundingPubKey.toString('hex'),
          acceptFundingPubkey: this.acceptFundingPubkey.toString('hex'),
          outpoint: this.outpoint.toJSON(), // Use OutPoint's toJSON method
        };
      }

      /**
       * Serializes the close_tlv message into a Buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeBigSize(CloseTLV.type);

        const dataWriter = new BufferWriter();

        dataWriter.writeBytes(this.contractId);
        dataWriter.writeUInt64BE(this.offerPayoutSatoshis);
        dataWriter.writeBytes(this.offerFundingPubKey);
        dataWriter.writeBytes(this.acceptFundingPubkey);
        dataWriter.writeBytes(this.outpoint.serialize()); // Use OutPoint's serialize method

        writer.writeBigSize(dataWriter.size);
        writer.writeBytes(dataWriter.toBuffer());

        return writer.toBuffer();
      }
    }

    export interface ICloseTLVJSON {
      type: number;
      contractId: string;
      offerPayoutSatoshis: number;
      offerFundingPubKey: string;
      acceptFundingPubkey: string;
      outpoint: {
        txid: string;
        index: number;
      };
    }