# All files / lib/messages DlcTransactions.ts

96% Statements 72/75
72.73% Branches 8/11
100% Functions 7/7
95.95% Lines 71/74

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
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    2141x
    1x

    1x

    1x
    1x

    6x
    6x

    6x

    6x

    6x
    6x

    6x

    6x
    6x
    6x

    6x

    6x
    6x

    6x
    6x
    8x
    8x
    7x

    1x

    6x
    6x
    6x

    6x

    6x

    6x

    6x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    8x

    1x

    2x

    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    1x
    1x
    2x
    2x

    1x
    1x
    1x
    1x
    1x

    1x

    1x
    1x

    1x

    1x
    1x
    1x
    1x
    1x

    1x

     import { Tx } from '@node-dlc/bitcoin';
    import { BufferReader, BufferWriter, StreamReader } from '@node-dlc/bufio';

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

    /**
     * DlcTransactions message contains information about state of DLC
     * contract such as fundtx and closetx
     */
    export class DlcTransactions implements IDlcMessage {
      public static type = MessageType.DlcTransactions;

      /**
       * Deserializes a dlc_transactions message
       * @param buf
       */
      public static deserialize(buf: Buffer, parseCets = true): DlcTransactions {
        const instance = new DlcTransactions();
        const reader = new BufferReader(buf);

        reader.readUInt16BE(); // read type

        instance.contractId = reader.readBytes(32);

        const fundTxLen = reader.readUInt16BE();
        instance.fundTx = Tx.decode(
          StreamReader.fromBuffer(reader.readBytes(fundTxLen)),
        );

        instance.fundTxVout = reader.readUInt32BE();

        const fundHash = reader.readBytes(32);
        const fundHeight = reader.readUInt32BE();
        instance.fundEpoch = {
          hash: fundHash,
          height: fundHeight,
        };

        instance.fundBroadcastHeight = reader.readUInt32BE();

        const refundTxLen = reader.readUInt16BE();
        instance.refundTx = Tx.decode(
          StreamReader.fromBuffer(reader.readBytes(refundTxLen)),
        );

        const numCets = reader.readBigSize(); // num_cets
        for (let i = 0; i < numCets; i++) {
          const cetLen = reader.readUInt16BE();
          if (parseCets || i === 0) {
            instance.cets.push(
              Tx.decode(StreamReader.fromBuffer(reader.readBytes(cetLen))),
            );
          } else {
            reader.readBytes(cetLen);
          }
        }

        const closeHash = reader.readBytes(32);
        const closeHeight = reader.readUInt32BE();
        instance.closeEpoch = {
          hash: closeHash,
          height: closeHeight,
        };

        instance.closeTxHash = reader.readBytes(32);

        instance.closeType = reader.readUInt8();

        instance.closeBroadcastHeight = reader.readUInt32BE();

        return instance;
      }

      /**
       * The type for dlc_transactions message. dlc_transactions = 61230
       */
      public type = DlcTransactions.type;

      public contractId: Buffer;

      public fundTx: Tx;

      public fundTxVout: number;

      public fundEpoch: BlockEpoch = {
        hash: Buffer.alloc(32),
        height: 0,
      };

      public fundBroadcastHeight = 0;

      public refundTx: Tx;

      public cets: Tx[] = [];

      public closeEpoch: BlockEpoch = {
        hash: Buffer.alloc(32),
        height: 0,
      };

      public closeTxHash: Buffer = Buffer.alloc(32);

      public closeType: CloseType = 0;

      public closeBroadcastHeight = 0;

      /**
       * Converts dlc_transactions to JSON
       */
      public toJSON(): IDlcTransactionsJSON {
        return {
          type: this.type,
          contractId: this.contractId.toString('hex'),
          fundTx: this.fundTx.serialize().toString('hex'),
          fundTxVout: this.fundTxVout,
          fundEpoch: {
            hash: this.fundEpoch.hash.toString('hex'),
            height: this.fundEpoch.height,
          },
          fundBroadcastHeight: this.fundBroadcastHeight,
          refundTx: this.refundTx.serialize().toString('hex'),
          cets: this.cets.map((cet) => cet.serialize().toString('hex')),
          closeEpoch: {
            hash: this.closeEpoch.hash.toString('hex'),
            height: this.closeEpoch.height,
          },
          closeTxHash: this.closeTxHash.toString('hex'),
          closeType: closeTypeToStr(this.closeType),
          closeBroadcastHeight: this.closeBroadcastHeight,
        };
      }

      /**
       * Serializes the dlc_transactions message into a Buffer
       */
      public serialize(): Buffer {
        const writer = new BufferWriter();
        writer.writeUInt16BE(this.type);
        writer.writeBytes(this.contractId);
        writer.writeUInt16BE(this.fundTx.serialize().length);
        writer.writeBytes(this.fundTx.serialize());
        writer.writeUInt32BE(this.fundTxVout);
        writer.writeBytes(this.fundEpoch.hash);
        writer.writeUInt32BE(this.fundEpoch.height);
        writer.writeUInt32BE(this.fundBroadcastHeight);
        writer.writeUInt16BE(this.refundTx.serialize().length);
        writer.writeBytes(this.refundTx.serialize());

        writer.writeBigSize(this.cets.length);
        for (const cet of this.cets) {
          writer.writeUInt16BE(cet.serialize().length);
          writer.writeBytes(cet.serialize());
        }

        writer.writeBytes(this.closeEpoch.hash);
        writer.writeUInt32BE(this.closeEpoch.height);
        writer.writeBytes(this.closeTxHash);
        writer.writeUInt8(this.closeType);
        writer.writeUInt32BE(this.closeBroadcastHeight);

        return writer.toBuffer();
      }
    }

    const closeTypeToStr = (closeType: CloseType): string => {
      switch (closeType) {
        case CloseType.ExecuteClose:
          return 'ExecuteClose';
        case CloseType.RefundClose:
          return 'RefundClose';
        case CloseType.CooperativeClose:
          return 'CooperativeClose';
        default:
          return 'NotClosed';
      }
    };

    export interface IDlcTransactionsJSON {
      type: number;
      contractId: string;
      fundTx: string;
      fundTxVout: number;
      fundEpoch: IBlockEpochJSON;
      fundBroadcastHeight: number;
      refundTx: string;
      cets: string[];
      closeEpoch: IBlockEpochJSON;
      closeTxHash: string;
      closeType: string;
      closeBroadcastHeight: number;
    }

    export interface IBlockEpochJSON {
      hash: string;
      height: number;
    }

    export interface BlockEpoch {
      hash: Buffer;
      height: number;
    }

    export enum CloseType {
      NotClosed = 0,
      ExecuteClose = 1,
      RefundClose = 2,
      CooperativeClose = 3,
    }

    // Backward compatibility aliases
    export const DlcTransactionsV0 = DlcTransactions;
    export type IDlcTransactionsV0JSON = IDlcTransactionsJSON;