From ad34b57c163fedd4c3f530b63f52a4a0c5feb80b Mon Sep 17 00:00:00 2001 From: Polybius93 Date: Wed, 6 Nov 2024 11:31:15 +0100 Subject: [PATCH] feat: add withdraw and deposit functions to dfnshandler --- src/dlc-handlers/dfns-dlc-handler.ts | 88 +++++++++++++++++++++++----- 1 file changed, 72 insertions(+), 16 deletions(-) diff --git a/src/dlc-handlers/dfns-dlc-handler.ts b/src/dlc-handlers/dfns-dlc-handler.ts index b556c9c..6eca5a0 100644 --- a/src/dlc-handlers/dfns-dlc-handler.ts +++ b/src/dlc-handlers/dfns-dlc-handler.ts @@ -12,6 +12,7 @@ import { ecdsaPublicKeyToSchnorr, getBalance, getFeeRate, + getInputIndicesByScript, getUnspendableKeyCommittedToUUID, } from '../functions/bitcoin/bitcoin-functions.js'; import { @@ -21,6 +22,7 @@ import { } from '../functions/bitcoin/psbt-functions.js'; import { PaymentInformation } from '../models/bitcoin-models.js'; import { RawVault } from '../models/ethereum-models.js'; +import { createRangeFromLength } from '../utilities/index.js'; export class DFNSDLCHandler { private dfnsDelegatedAPIClient: DfnsDelegatedApiClient; @@ -89,6 +91,13 @@ export class DFNSDLCHandler { this.taprootDerivedPublicKey = taprootDerivedPublicKey; } + getTaprootDerivedPublicKey(): string { + if (!this.taprootDerivedPublicKey) { + throw new Error('Taproot Derived Public Key not set'); + } + return this.taprootDerivedPublicKey; + } + getVaultRelatedAddress(paymentType: 'funding' | 'multisig'): string { const payment = this.getPayment(); @@ -172,9 +181,9 @@ export class DFNSDLCHandler { attestorGroupPublicKey ); - const feeRate = - customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); + const feeRate = 200n; + // customFeeRate ?? + // BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); const addressBalance = await getBalance(fundingPayment, this.bitcoinBlockchainAPI); @@ -193,6 +202,14 @@ export class DFNSDLCHandler { vault.btcMintFeeBasisPoints.toBigInt() ); + createRangeFromLength(fundingTransaction.inputsLength).forEach(index => { + console.log('index', index); + fundingTransaction.updateInput(index, { + sighashType: 0x01, + }); + console.log('input', fundingTransaction.getInput(index)); + }); + return fundingTransaction; } catch (error: any) { throw new Error(`Error creating Funding PSBT: ${error}`); @@ -213,9 +230,9 @@ export class DFNSDLCHandler { attestorGroupPublicKey ); - const feeRate = - customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); + const feeRate = 200n; + // customFeeRate ?? + // BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); const withdrawTransaction = await createWithdrawTransaction( this.bitcoinBlockchainAPI, @@ -228,6 +245,14 @@ export class DFNSDLCHandler { vault.btcFeeRecipient, vault.btcRedeemFeeBasisPoints.toBigInt() ); + + createRangeFromLength(withdrawTransaction.inputsLength).forEach(index => { + console.log('index', index); + withdrawTransaction.updateInput(index, { + sighashType: 0x01, + }); + console.log('input', withdrawTransaction.getInput(index)); + }); return withdrawTransaction; } catch (error: any) { throw new Error(`Error creating Withdraw PSBT: ${error}`); @@ -247,9 +272,9 @@ export class DFNSDLCHandler { attestorGroupPublicKey ); - const feeRate = - customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); + const feeRate = 200n; + // customFeeRate ?? + // BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); const depositTransaction = await createDepositTransaction( this.bitcoinBlockchainAPI, @@ -263,10 +288,21 @@ export class DFNSDLCHandler { vault.btcMintFeeBasisPoints.toBigInt() ); + createRangeFromLength(depositTransaction.inputsLength).forEach(index => { + console.log('index', index); + depositTransaction.updateInput(index, { + sighashType: 0x01, + }); + console.log('input', depositTransaction.getInput(index)); + }); + return depositTransaction; } - async signPSBT(transaction: Transaction): Promise { + async signPSBT( + transaction: Transaction, + transactionType: 'funding' | 'deposit' | 'withdraw' + ): Promise { try { const walletId = this.walletID; if (!walletId) { @@ -300,13 +336,33 @@ export class DFNSDLCHandler { console.log('generateSignatureCompleteResponse', generateSignatureCompleteResponse); - // ==> Finalize Funding Transaction - const fundingTransaction = Transaction.fromPSBT( - hexToBytes(generateSignatureCompleteResponse.signedData!) - ); - fundingTransaction.finalize(); + const signedPSBT = generateSignatureCompleteResponse.signedData; - return fundingTransaction; + if (!signedPSBT) { + throw new Error('No signed data returned'); + } + + // ==> Finalize Funding Transaction + const signedTransaction = Transaction.fromPSBT(hexToBytes(signedPSBT.slice(2)!)); + + switch (transactionType) { + case 'funding': + signedTransaction.finalize(); + break; + case 'deposit': + getInputIndicesByScript( + this.getPayment().fundingPayment.script, + signedTransaction + ).forEach(index => { + signedTransaction.finalizeIdx(index); + }); + break; + case 'withdraw': + break; + default: + throw new Error('Invalid Transaction Type'); + } + return signedTransaction; } catch (error: any) { throw new Error(`Error signing PSBT: ${error}`); }