# All files / lib/messages FundingSignatures.ts

100% Statements 36/36
100% Branches 0/0
100% Functions 6/6
100% Lines 34/34

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
    991x

    1x

    1x

    1x
    1x

    52x
    52x

    52x

    52x
    49x
    49x
    49x

    97x
    97x

    97x
    97x
    97x
    97x

    49x

    52x

    110x

    110x

    21x

    22x
    43x

    114x

    114x

    114x

    114x
    106x
    106x
    211x

    114x

    114x

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

    import { MessageType } from '../MessageType';
    import { IDlcMessage } from './DlcMessage';
    import { IScriptWitnessV0JSON, ScriptWitnessV0 } from './ScriptWitnessV0';

    /**
     * FundingSignatures contains signatures of the funding transaction
     * and any necessary information linking the signatures to their inputs.
     */
    export class FundingSignatures implements IDlcMessage {
      public static type = MessageType.FundingSignatures;

      /**
       * Deserializes a funding_signatures message
       * @param buf
       */
      public static deserialize(buf: Buffer): FundingSignatures {
        const instance = new FundingSignatures();
        const reader = new BufferReader(buf);

        // reader.readBigSize(); // read type
        // instance.length = reader.readBigSize();
        const numWitnesses = Number(reader.readBigSize());

        for (let i = 0; i < numWitnesses; i++) {
          const numWitnessElements = Number(reader.readBigSize());
          const witnessElements: ScriptWitnessV0[] = [];
          for (let j = 0; j < numWitnessElements; j++) {
            // Read witness element directly: [bigsize:len][len*byte:witness]
            const witnessLength = Number(reader.readBigSize());
            const witnessBytes = reader.readBytes(witnessLength);

            const witness = new ScriptWitnessV0();
            witness.length = witnessLength;
            witness.witness = witnessBytes;
            witnessElements.push(witness);
          }
          instance.witnessElements.push(witnessElements);
        }

        return instance;
      }

      /**
       * The type for funding_signatures message. funding_signatures = 42776
       */
      public type = FundingSignatures.type;

      public length: bigint;

      public witnessElements: ScriptWitnessV0[][] = [];

      /**
       * Converts funding_signatures to JSON (canonical rust-dlc format)
       */
      public toJSON(): IFundingSignaturesJSON {
        return {
          fundingSignatures: this.witnessElements.map((witnessElement) => {
            return {
              witnessElements: witnessElement.map((witness) => witness.toJSON()),
            };
          }),
        };
      }

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

        const dataWriter = new BufferWriter();

        dataWriter.writeBigSize(this.witnessElements.length);

        for (const witnessElements of this.witnessElements) {
          dataWriter.writeBigSize(witnessElements.length);
          for (const witnessElement of witnessElements) {
            dataWriter.writeBytes(witnessElement.serialize());
          }
        }

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

        return writer.toBuffer();
      }
    }

    export interface IFundingSignaturesJSON {
      fundingSignatures: IFundingSignatureJSON[];
    }

    export interface IFundingSignatureJSON {
      witnessElements: IScriptWitnessV0JSON[];
    }