diff --git a/src/dlc-handlers/ledger-dlc-handler.ts b/src/dlc-handlers/ledger-dlc-handler.ts index 6a63caf..4592c48 100644 --- a/src/dlc-handlers/ledger-dlc-handler.ts +++ b/src/dlc-handlers/ledger-dlc-handler.ts @@ -16,7 +16,6 @@ import { import { addNativeSegwitSignaturesToPSBT, addTaprootInputSignaturesToPSBT, - createClosingTransaction, createFundingTransaction, createWithdrawalTransaction, getNativeSegwitInputsToSign, @@ -337,66 +336,6 @@ export class LedgerDLCHandler { } } - async createClosingPSBT( - vault: RawVault, - fundingTransactionID: string, - feeRateMultiplier?: number, - customFeeRate?: bigint - ): Promise { - try { - const { nativeSegwitPayment, taprootMultisigPayment, taprootDerivedPublicKey } = - this.getPayment(); - - if (nativeSegwitPayment.address === undefined) { - throw new Error('Could not get Addresses from Payments'); - } - - const feeRate = - customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - - const closingTransaction = createClosingTransaction( - vault.valueLocked.toBigInt(), - this.bitcoinNetwork, - fundingTransactionID, - taprootMultisigPayment, - nativeSegwitPayment.address, - feeRate, - vault.btcFeeRecipient, - vault.btcRedeemFeeBasisPoints.toBigInt() - ); - - const closingTransactionSigningConfiguration = createBitcoinInputSigningConfiguration( - closingTransaction, - this.walletAccountIndex, - this.bitcoinNetwork - ); - - const formattedClosingPSBT = Psbt.fromBuffer(Buffer.from(closingTransaction.toPSBT()), { - network: this.bitcoinNetwork, - }); - - const closingInputByPaymentTypeArray = getInputByPaymentTypeArray( - closingTransactionSigningConfiguration, - formattedClosingPSBT.toBuffer(), - this.bitcoinNetwork - ); - - const taprootInputsToSign = getTaprootInputsToSign(closingInputByPaymentTypeArray); - - await updateTaprootInputs( - taprootInputsToSign, - taprootDerivedPublicKey, - this.masterFingerprint, - formattedClosingPSBT - ); - - return formattedClosingPSBT; - } catch (error: any) { - throw new Error(`Error creating Closing PSBT: ${error}`); - } - } - async createWithdrawalPSBT( vault: RawVault, withdrawAmount: bigint, diff --git a/src/dlc-handlers/private-key-dlc-handler.ts b/src/dlc-handlers/private-key-dlc-handler.ts index 4e4d9d8..d5b971a 100644 --- a/src/dlc-handlers/private-key-dlc-handler.ts +++ b/src/dlc-handlers/private-key-dlc-handler.ts @@ -14,7 +14,6 @@ import { getUnspendableKeyCommittedToUUID, } from '../functions/bitcoin/bitcoin-functions.js'; import { - createClosingTransaction, createFundingTransaction, createWithdrawalTransaction, } from '../functions/bitcoin/psbt-functions.js'; @@ -208,40 +207,6 @@ export class PrivateKeyDLCHandler { return fundingTransaction; } - async createClosingPSBT( - vault: RawVault, - fundingTransactionID: string, - feeRateMultiplier?: number, - customFeeRate?: bigint - ): Promise { - if (this.payment === undefined) { - throw new Error('Payment objects have not been set'); - } - - const { nativeSegwitPayment, taprootMultisigPayment } = this.payment; - - if (nativeSegwitPayment.address === undefined) { - throw new Error('Could not get Addresses from Payments'); - } - - const feeRate = - customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - - const closingTransaction = createClosingTransaction( - vault.valueLocked.toBigInt(), - this.bitcoinNetwork, - fundingTransactionID, - taprootMultisigPayment, - nativeSegwitPayment.address, - feeRate, - vault.btcFeeRecipient, - vault.btcRedeemFeeBasisPoints.toBigInt() - ); - - return closingTransaction; - } - async createWithdrawalPSBT( vault: RawVault, withdrawAmount: bigint, diff --git a/src/dlc-handlers/software-wallet-dlc-handler.ts b/src/dlc-handlers/software-wallet-dlc-handler.ts index 0ff258b..215a75e 100644 --- a/src/dlc-handlers/software-wallet-dlc-handler.ts +++ b/src/dlc-handlers/software-wallet-dlc-handler.ts @@ -11,7 +11,6 @@ import { getUnspendableKeyCommittedToUUID, } from '../functions/bitcoin/bitcoin-functions.js'; import { - createClosingTransaction, createFundingTransaction, createWithdrawalTransaction, } from '../functions/bitcoin/psbt-functions.js'; @@ -192,42 +191,6 @@ export class SoftwareWalletDLCHandler { } } - async createClosingPSBT( - vault: RawVault, - fundingTransactionID: string, - feeRateMultiplier?: number, - customFeeRate?: bigint - ): Promise { - try { - const { nativeSegwitPayment, taprootMultisigPayment } = this.getPayment(); - - if ( - taprootMultisigPayment.address === undefined || - nativeSegwitPayment.address === undefined - ) { - throw new Error('Payment Address is undefined'); - } - - const feeRate = - customFeeRate ?? - BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier)); - - const closingTransaction = createClosingTransaction( - vault.valueLocked.toBigInt(), - this.bitcoinNetwork, - fundingTransactionID, - taprootMultisigPayment, - nativeSegwitPayment.address!, - feeRate, - vault.btcFeeRecipient, - vault.btcRedeemFeeBasisPoints.toBigInt() - ); - return closingTransaction; - } catch (error: any) { - throw new Error(`Error creating Closing PSBT: ${error}`); - } - } - async createWithdrawalPSBT( vault: RawVault, withdrawAmount: bigint, diff --git a/src/functions/bitcoin/index.ts b/src/functions/bitcoin/index.ts index 14c5b9f..bc9e6fd 100644 --- a/src/functions/bitcoin/index.ts +++ b/src/functions/bitcoin/index.ts @@ -4,11 +4,14 @@ import { fetchBitcoinTransaction, getBalance, } from '../bitcoin/bitcoin-request-functions.js'; -import { createClosingTransaction, createFundingTransaction } from '../bitcoin/psbt-functions.js'; +import { + createFundingTransaction, + createWithdrawalTransaction, +} from '../bitcoin/psbt-functions.js'; export { - createClosingTransaction, createFundingTransaction, + createWithdrawalTransaction, broadcastTransaction, fetchBitcoinBlockchainBlockHeight, fetchBitcoinTransaction, diff --git a/src/functions/bitcoin/psbt-functions.ts b/src/functions/bitcoin/psbt-functions.ts index 44e57ef..50a1f83 100644 --- a/src/functions/bitcoin/psbt-functions.ts +++ b/src/functions/bitcoin/psbt-functions.ts @@ -68,72 +68,6 @@ export async function createFundingTransaction( return fundingTX; } -/** - * Creates the Closing Transaction. - * Uses the Funding Transaction's ID to create the Closing Transaction. - * The Closing Transaction is sent to the User's Native Segwit Address. - * - * @param bitcoinAmount - The Amount of Bitcoin to fund the Transaction with. - * @param bitcoinNetwork - The Bitcoin Network to use. - * @param fundingTransactionID - The ID of the Funding Transaction. - * @param multisigTransaction - The Multisig Transaction. - * @param userNativeSegwitAddress - The User's Native Segwit Address. - * @param feeRate - The Fee Rate to use for the Transaction. - * @param feePublicKey - The Fee Recipient's Public Key. - * @param feeBasisPoints - The Fee Basis Points. - * @returns The Closing Transaction. - */ -export function createClosingTransaction( - bitcoinAmount: bigint, - bitcoinNetwork: Network, - fundingTransactionID: string, - multisigTransaction: P2TROut, - userNativeSegwitAddress: string, - feeRate: bigint, - feePublicKey: string, - feeBasisPoints: bigint -): Transaction { - const feeAddress = getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork); - const feeAmount = getFeeAmount(Number(bitcoinAmount), Number(feeBasisPoints)); - - const inputs = [ - { - txid: hexToBytes(fundingTransactionID), - index: 0, - witnessUtxo: { - amount: bitcoinAmount, - script: multisigTransaction.script, - }, - ...multisigTransaction, - }, - ]; - - const outputs = [ - { - address: feeAddress, - amount: BigInt(feeAmount), - }, - ]; - - const selected = selectUTXO(inputs, outputs, 'default', { - changeAddress: userNativeSegwitAddress, - feePerByte: feeRate, - bip69: false, - createTx: true, - network: bitcoinNetwork, - }); - - const closingTX = selected?.tx; - - if (!closingTX) throw new Error('Could not create Closing Transaction'); - - closingTX.updateInput(0, { - sequence: 0xfffffff0, - }); - - return closingTX; -} - /** * Creates a Withdrawal Transaction. * Uses the Funding Transaction's ID to create the Withdrawal Transaction.