All files / lib/messages DlcCloseMetadata.ts

7.69% Statements 3/39
100% Branches 0/0
0% Functions 0/4
7.69% Lines 3/39

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 1191x   1x           1x                                                                                                                                                                                                                            
import { Tx } from '@node-dlc/bitcoin';
 
import { DlcAccept, DlcOffer, DlcTransactions } from '..';
 
/**
 * DlcClose Metadata object contains information required for verifying DlcClose
 * message.
 */
export class DlcCloseMetadata {
  /**
   * Convert JSON to DlcCloseMetadata
   * @param json
   */
  public static fromJSON(json: IDlcCloseMetadataJSON): DlcCloseMetadata {
    const instance = new DlcCloseMetadata();
 
    instance.offerFundingPubKey = Buffer.from(json.offerFundingPubKey, 'hex');
    instance.acceptFundingPubKey = Buffer.from(json.acceptFundingPubKey, 'hex');
    instance.offerPayoutSPK = Buffer.from(json.offerPayoutSPK, 'hex');
    instance.acceptPayoutSPK = Buffer.from(json.acceptPayoutSPK, 'hex');
    instance.offerPayoutSerialId = BigInt(json.offerPayoutSerialId);
    instance.acceptPayoutSerialId = BigInt(json.acceptPayoutSerialId);
    instance.feeRatePerVb = BigInt(json.feeRatePerVb);
    instance.fundTx = Tx.fromHex(json.fundTx);
    instance.fundTxVout = json.fundTxVout;
 
    return instance;
  }
 
  public static fromDlcMessages(
    dlcOffer: DlcOffer,
    dlcAccept: DlcAccept,
    dlcTxs: DlcTransactions,
  ): DlcCloseMetadata {
    const instance = new DlcCloseMetadata();
 
    instance.offerFundingPubKey = dlcOffer.fundingPubkey;
    instance.acceptFundingPubKey = dlcAccept.fundingPubkey;
    instance.offerPayoutSPK = dlcOffer.payoutSpk;
    instance.acceptPayoutSPK = dlcAccept.payoutSpk;
    instance.offerPayoutSerialId = dlcOffer.payoutSerialId;
    instance.acceptPayoutSerialId = dlcAccept.payoutSerialId;
    instance.feeRatePerVb = dlcOffer.feeRatePerVb;
    instance.fundTx = dlcTxs.fundTx;
    instance.fundTxVout = dlcTxs.fundTxVout;
 
    return instance;
  }
 
  public offerFundingPubKey: Buffer;
 
  public acceptFundingPubKey: Buffer;
 
  public offerPayoutSPK: Buffer;
 
  public acceptPayoutSPK: Buffer;
 
  public offerPayoutSerialId: bigint;
 
  public acceptPayoutSerialId: bigint;
 
  public feeRatePerVb: bigint;
 
  public fundTx: Tx;
 
  public fundTxVout: number;
 
  /**
   * Converts dlc_close_metadata to JSON
   */
  public toJSON(): IDlcCloseMetadataJSON {
    return {
      offerFundingPubKey: this.offerFundingPubKey.toString('hex'),
      acceptFundingPubKey: this.acceptFundingPubKey.toString('hex'),
      offerPayoutSPK: this.offerPayoutSPK.toString('hex'),
      acceptPayoutSPK: this.acceptPayoutSPK.toString('hex'),
      offerPayoutSerialId: Number(this.offerPayoutSerialId),
      acceptPayoutSerialId: Number(this.acceptPayoutSerialId),
      feeRatePerVb: Number(this.feeRatePerVb),
      fundTx: this.fundTx.serialize().toString('hex'),
      fundTxVout: this.fundTxVout,
    };
  }
 
  public toDlcMessages(): {
    dlcOffer: DlcOffer;
    dlcAccept: DlcAccept;
    dlcTxs: DlcTransactions;
  } {
    const dlcOffer = new DlcOffer();
    const dlcAccept = new DlcAccept();
    const dlcTxs = new DlcTransactions();
 
    dlcOffer.fundingPubkey = this.offerFundingPubKey;
    dlcAccept.fundingPubkey = this.acceptFundingPubKey;
    dlcOffer.payoutSpk = this.offerPayoutSPK;
    dlcAccept.payoutSpk = this.acceptPayoutSPK;
    dlcOffer.payoutSerialId = this.offerPayoutSerialId;
    dlcAccept.payoutSerialId = this.acceptPayoutSerialId;
    dlcOffer.feeRatePerVb = this.feeRatePerVb;
    dlcTxs.fundTx = this.fundTx;
    dlcTxs.fundTxVout = this.fundTxVout;
 
    return { dlcOffer, dlcAccept, dlcTxs };
  }
}
 
export interface IDlcCloseMetadataJSON {
  offerFundingPubKey: string;
  acceptFundingPubKey: string;
  offerPayoutSPK: string;
  acceptPayoutSPK: string;
  offerPayoutSerialId: number;
  acceptPayoutSerialId: number;
  feeRatePerVb: number;
  fundTx: string;
  fundTxVout: number;
}