All files / lib/messages DlcInput.ts

94.44% Statements 51/54
85% Branches 17/20
100% Functions 8/8
94.44% Lines 51/54

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 1681x   1x               1x 1x               19x     19x 19x 19x           19x 19x 19x           19x 19x 19x         19x               1x 1x   1x 1x     1x     1x     1x   1x               5x 5x     5x     5x     5x   5x           25x                             6x 1x   5x 1x   4x 1x               4x                     2x 2x   2x 2x 2x 2x   2x 2x   2x             8x 8x 8x 8x   8x                  
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
/**
 * DlcInput contains information about a DLC input to be used in a funding transaction.
 * Contains the local and remote funding public keys and the contract ID of the DLC input.
 * Matches rust-dlc DlcInput struct.
 */
export class DlcInput implements IDlcMessage {
  public static type = MessageType.DlcInput;
 
  /**
   * Creates a DlcInput from JSON data
   * @param json JSON object representing DLC input
   */
  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types, @typescript-eslint/no-explicit-any
  public static fromJSON(json: any): DlcInput {
    const instance = new DlcInput();
 
    // Parse local funding public key
    const localFundPubkey = json.localFundPubkey || json.local_fund_pubkey;
    Eif (typeof localFundPubkey === 'string') {
      instance.localFundPubkey = Buffer.from(localFundPubkey, 'hex');
    } else {
      throw new Error('localFundPubkey is required');
    }
 
    // Parse remote funding public key
    const remoteFundPubkey = json.remoteFundPubkey || json.remote_fund_pubkey;
    Eif (typeof remoteFundPubkey === 'string') {
      instance.remoteFundPubkey = Buffer.from(remoteFundPubkey, 'hex');
    } else {
      throw new Error('remoteFundPubkey is required');
    }
 
    // Parse contract ID (32 bytes)
    const contractId = json.contractId || json.contract_id;
    Eif (typeof contractId === 'string') {
      instance.contractId = Buffer.from(contractId, 'hex');
    } else {
      throw new Error('contractId is required');
    }
 
    return instance;
  }
 
  /**
   * Deserializes a dlc_input message
   * @param buf
   */
  public static deserialize(buf: Buffer): DlcInput {
    const instance = new DlcInput();
    const reader = new BufferReader(buf);
 
    reader.readBigSize(); // read type
    instance.length = reader.readBigSize();
 
    // Read local funding public key (33 bytes)
    instance.localFundPubkey = reader.readBytes(33);
 
    // Read remote funding public key (33 bytes)
    instance.remoteFundPubkey = reader.readBytes(33);
 
    // Read contract ID (32 bytes)
    instance.contractId = reader.readBytes(32);
 
    return instance;
  }
 
  /**
   * Deserializes a dlc_input without TLV wrapper (for use in FundingInput)
   * @param buf
   */
  public static deserializeBody(buf: Buffer): DlcInput {
    const instance = new DlcInput();
    const reader = new BufferReader(buf);
 
    // Read local funding public key (33 bytes)
    instance.localFundPubkey = reader.readBytes(33);
 
    // Read remote funding public key (33 bytes)
    instance.remoteFundPubkey = reader.readBytes(33);
 
    // Read contract ID (32 bytes)
    instance.contractId = reader.readBytes(32);
 
    return instance;
  }
 
  /**
   * The type for dlc_input message. dlc_input = 42773
   */
  public type = DlcInput.type;
 
  public length: bigint;
 
  public localFundPubkey: Buffer;
 
  public remoteFundPubkey: Buffer;
 
  public contractId: Buffer;
 
  /**
   * Validates correctness of all fields
   * @throws Will throw an error if validation fails
   */
  public validate(): void {
    if (!this.localFundPubkey) {
      throw new Error('localFundPubkey is required');
    }
    if (!this.remoteFundPubkey) {
      throw new Error('remoteFundPubkey is required');
    }
    if (!this.contractId || this.contractId.length !== 32) {
      throw new Error('contractId must be a 32-byte buffer');
    }
  }
 
  /**
   * Converts dlc_input to JSON
   */
  public toJSON(): IDlcInputJSON {
    return {
      localFundPubkey: this.localFundPubkey.toString('hex'),
      remoteFundPubkey: this.remoteFundPubkey.toString('hex'),
      contractId: this.contractId.toString('hex'),
    };
  }
 
  /**
   * Serializes the dlc_input message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeBytes(this.localFundPubkey);
    dataWriter.writeBytes(this.remoteFundPubkey);
    dataWriter.writeBytes(this.contractId);
 
    writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
 
    return writer.toBuffer();
  }
 
  /**
   * Serializes dlc_input body without TLV wrapper (for embedding in FundingInput)
   */
  public serializeBody(): Buffer {
    const writer = new BufferWriter();
    writer.writeBytes(this.localFundPubkey);
    writer.writeBytes(this.remoteFundPubkey);
    writer.writeBytes(this.contractId);
 
    return writer.toBuffer();
  }
}
 
export interface IDlcInputJSON {
  localFundPubkey: string;
  remoteFundPubkey: string;
  contractId: string;
}