@covenance/dlc
Version:
Crypto and Bitcoin functions for Covenance DLC implementation
90 lines (89 loc) • 5.22 kB
TypeScript
import { Script, Address, Transaction, Networks } from '../btc';
import { DlcInitTx, UTXO, OracleEvent, OracleCET, LoanConfig } from './types';
import { PubKey, Sighash, Signature } from '../crypto/types';
/**
* Creates a DLC initialization transaction that locks funds in a 2-of-2 multisig output
* @param collateralUtxos Array of collateral UTXOs controlled by the borrower
* @param collateralAmount Amount of collateral to lock in the DLC
* @param borrowerPubKey Borrower's public key for the DLC
* @param lenderPubKey Lender's public key for the DLC
* @param changeAddress Address to receive any change from the transaction
* @param feeRate Fee rate in satoshis per vB
* @param network Network to use for addresses
* @returns DLC init transaction object containing the transaction, multisig script and address
*/
export declare function createDlcInitTx(collateralUtxos: UTXO[], collateralAmount: number, borrowerDlcPubKey: PubKey, lenderDlcPubKey: PubKey, changeAddress: Address, feeRate?: number, network?: Networks.Network): DlcInitTx;
/**
* Creates a Contract Execution Transaction (CET) that distributes funds between borrower and lender
* @param dlcUtxo The UTXO from the DLC initialization transaction
* @param borrowerReceiveAmount Amount of satoshis the borrower should receive
* @param lenderReceiveAmount Amount of satoshis the lender should receive
* @param borrowerAddress Borrower's address to receive funds
* @param lenderAddress Lender's address to receive funds
* @param network Network to use for addresses
* @returns The CET transaction
*/
export declare function createCet(dlcUtxo: UTXO, borrowerReceiveAmount: number, lenderReceiveAmount: number, borrowerAddress: Address, lenderAddress: Address): Transaction;
/**
* Funds the fees for a Contract Execution Transaction (CET) by creating a separate funding transaction
* that can be unlocked by the CET. Since the CET parties sign it using SIGHASH_ANYONECANPAY,
* we can add an additional input without invalidating the signatures.
* @param cet The Contract Execution Transaction to fund fees for
* @param feeRate Fee rate in satoshis per vB
* @param fundsUtxos Array of UTXOs to use for funding the fees
* @param fundingAddress Address of the final funding output, which will be the CET input
* @param changeAddress Address to receive any change from the funding transaction
* @returns Object containing the updated CET and the funding transaction
*/
export declare function fundCetFees(cet: Transaction, fundsUtxos: UTXO[], fundingAddress: Address, changeAddress: Address, feeRate?: number): {
cet: Transaction;
fundingTx: Transaction;
};
/**
* Returns the sighash for a transaction input.
* @param tx The transaction object
* @param sighash The sighash flag
* @param inputIndex The index of the input to hash
* @param prevScriptPubKey The previous scriptPubKey of the input
* @param satoshis The amount of satoshis in the input
* @returns The sighash
*/
export declare function getTxSigHash(tx: Transaction, sighash: number, inputIndex: number, prevScriptPubKey: Script, satoshis: number): Sighash;
/**
* Creates Contract Execution Transactions (CETs) for liquidation events
* @param events Array of liquidation events
* @param config Configuration object containing loan parameters
* @param dlcUtxo The UTXO from the DLC initialization transaction
* @param borrowerAddress Borrower's address to receive funds
* @param lenderAddress Lender's address to receive funds
* @returns Array of OracleCET objects
*/
export declare function createLiquidationCets(events: OracleEvent[], config: LoanConfig, dlcUtxo: UTXO, borrower: Address, lender: Address): OracleCET[];
/**
* Creates Contract Execution Transactions (CETs) for the loan maturity event
* @param event Liquidation event
* @param grid Price grid
* @param config Configuration object containing loan parameters
* @param dlcUtxo The UTXO from the DLC initialization transaction
* @param borrowerAddress Borrower's address to receive funds
* @param lenderAddress Lender's address to receive funds
* @returns Array of OracleCET objects
*/
export declare function createMaturityCets(event: OracleEvent, startTime: number, config: LoanConfig, dlcUtxo: UTXO, borrower: Address, lender: Address): OracleCET[];
/**
* Creates a Contract Execution Transaction (CET) for repayment that returns the full collateral to the borrower
* @param dlcUtxo The UTXO from the DLC initialization transaction
* @param collateralAmount Amount of satoshis to return to the borrower
* @param borrowerAddress Borrower's address to receive funds
* @returns The CET transaction
*/
export declare function createRepaymentCet(dlcUtxo: UTXO, collateralAmount: number, borrowerAddress: Address): Transaction;
/**
* Applies signatures to a CET
* @param cet The CET to apply signatures to
* @param witnessScript The witness script of the CET
* @param sigBorrower The borrower's signature
* @param sigLender The lender's signature
* @param cBlock The cBlock of the CET
*/
export declare function applySignaturesCet(cet: Transaction, witnessScript: Script, sigBorrower: Signature, sigLender: Signature, cBlock: string, sighash?: number): Transaction;