Skip to content

Commit

Permalink
feat: remove closingPSBT related functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jun 19, 2024
1 parent 9ac9b58 commit 0b4d913
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 201 deletions.
61 changes: 0 additions & 61 deletions src/dlc-handlers/ledger-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
import {
addNativeSegwitSignaturesToPSBT,
addTaprootInputSignaturesToPSBT,
createClosingTransaction,
createFundingTransaction,
createWithdrawalTransaction,
getNativeSegwitInputsToSign,
Expand Down Expand Up @@ -337,66 +336,6 @@ export class LedgerDLCHandler {
}
}

async createClosingPSBT(
vault: RawVault,
fundingTransactionID: string,
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Psbt> {
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,
Expand Down
35 changes: 0 additions & 35 deletions src/dlc-handlers/private-key-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
getUnspendableKeyCommittedToUUID,
} from '../functions/bitcoin/bitcoin-functions.js';
import {
createClosingTransaction,
createFundingTransaction,
createWithdrawalTransaction,
} from '../functions/bitcoin/psbt-functions.js';
Expand Down Expand Up @@ -208,40 +207,6 @@ export class PrivateKeyDLCHandler {
return fundingTransaction;
}

async createClosingPSBT(
vault: RawVault,
fundingTransactionID: string,
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Transaction> {
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,
Expand Down
37 changes: 0 additions & 37 deletions src/dlc-handlers/software-wallet-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
getUnspendableKeyCommittedToUUID,
} from '../functions/bitcoin/bitcoin-functions.js';
import {
createClosingTransaction,
createFundingTransaction,
createWithdrawalTransaction,
} from '../functions/bitcoin/psbt-functions.js';
Expand Down Expand Up @@ -192,42 +191,6 @@ export class SoftwareWalletDLCHandler {
}
}

async createClosingPSBT(
vault: RawVault,
fundingTransactionID: string,
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Transaction> {
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,
Expand Down
7 changes: 5 additions & 2 deletions src/functions/bitcoin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
66 changes: 0 additions & 66 deletions src/functions/bitcoin/psbt-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0b4d913

Please sign in to comment.