All files / lib/messages CetAdaptorSignatures.ts

100% Statements 26/26
100% Branches 0/0
100% Functions 5/5
100% Lines 25/25

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 901x   1x             1x 1x             100x 100x   100x     100x 15293x 15293x 15293x     100x           195x       195x           36x     5173x 5173x                     214x     214x 214x   214x 30669x 30669x       214x 214x                                
import { BufferReader, BufferWriter } from '@node-dlc/bufio';
 
import { MessageType } from '../MessageType';
import { IDlcMessage } from './DlcMessage';
 
/**
 * CetAdaptorSignatures contains CET signatures and any necessary
 * information linking the signatures to their corresponding outcome
 */
export class CetAdaptorSignatures implements IDlcMessage {
  public static type = MessageType.CetAdaptorSignatures;
 
  /**
   * Deserializes a cet_adaptor_signature message
   * @param buf
   */
  public static deserialize(buf: Buffer): CetAdaptorSignatures {
    const instance = new CetAdaptorSignatures();
    const reader = new BufferReader(buf);
 
    const nbSignatures = Number(reader.readBigSize()); // nb_signatures
 
    // Read exactly nbSignatures * (65 + 97) bytes to match serialize format
    for (let i = 0; i < nbSignatures; i++) {
      const encryptedSig = reader.readBytes(65);
      const dleqProof = reader.readBytes(97);
      instance.sigs.push({ encryptedSig, dleqProof });
    }
 
    return instance;
  }
 
  /**
   * The type for cet_adaptor_signature message. cet_adaptor_signature = 42774
   */
  public type = CetAdaptorSignatures.type;
 
  public length: bigint;
 
  public sigs: ISig[] = [];
 
  /**
   * Converts cet_adaptor_signature to JSON (canonical rust-dlc format)
   */
  public toJSON(): ICetAdaptorSignaturesJSON {
    return {
      ecdsaAdaptorSignatures: this.sigs.map((sig) => {
        // Combine encryptedSig and dleqProof into single signature field as expected by rust-dlc
        const signature = Buffer.concat([sig.encryptedSig, sig.dleqProof]);
        return {
          signature: signature.toString('hex'),
        };
      }),
    };
  }
 
  /**
   * Serializes the cet_adaptor_signature message into a Buffer
   */
  public serialize(): Buffer {
    const writer = new BufferWriter();
    // writer.writeBigSize(this.type);
 
    const dataWriter = new BufferWriter();
    dataWriter.writeBigSize(this.sigs.length);
 
    for (const sig of this.sigs) {
      dataWriter.writeBytes(sig.encryptedSig);
      dataWriter.writeBytes(sig.dleqProof);
    }
 
    // writer.writeBigSize(dataWriter.size);
    writer.writeBytes(dataWriter.toBuffer());
    return writer.toBuffer();
  }
}
 
interface ISig {
  encryptedSig: Buffer;
  dleqProof: Buffer;
}
 
export interface ICetAdaptorSignaturesJSON {
  ecdsaAdaptorSignatures: ISigJSON[];
}
 
export interface ISigJSON {
  signature: string;
}