Skip to content

Commit

Permalink
feat: add comments, modify withdrawal transaction recipients
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Jun 13, 2024
1 parent cf9980a commit 1895913
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 6 deletions.
67 changes: 67 additions & 0 deletions src/dlc-handlers/ledger-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
addTaprootInputSignaturesToPSBT,
createClosingTransaction,
createFundingTransaction,
createWithdrawalTransaction,
getNativeSegwitInputsToSign,
getTaprootInputsToSign,
updateNativeSegwitInputs,
Expand Down Expand Up @@ -396,6 +397,72 @@ export class LedgerDLCHandler {
}
}

async createWithdrawalPSBT(
vault: RawVault,
withdrawAmount: bigint,
attestorGroupPublicKey: string,
fundingTransactionID: string,
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Psbt> {
try {
const { nativeSegwitPayment, taprootDerivedPublicKey, taprootMultisigPayment } =
await this.createPayment(vault.uuid, attestorGroupPublicKey);

if (
taprootMultisigPayment.address === undefined ||
nativeSegwitPayment.address === undefined
) {
throw new Error('Payment Address is undefined');
}

const feeRate =
customFeeRate ??
BigInt(await getFeeRate(this.bitcoinBlockchainFeeRecommendationAPI, feeRateMultiplier));

const withdrawalPSBT = await createWithdrawalTransaction(
this.bitcoinBlockchainAPI,
withdrawAmount,
this.bitcoinNetwork,
fundingTransactionID,
taprootMultisigPayment,
nativeSegwitPayment.address,
feeRate,
vault.btcFeeRecipient,
vault.btcRedeemFeeBasisPoints.toBigInt()
);

const withdrawalTransactionSigningConfiguration = createBitcoinInputSigningConfiguration(
withdrawalPSBT,
this.walletAccountIndex,
this.bitcoinNetwork
);

const formattedWithdrawalPSBT = Psbt.fromBuffer(Buffer.from(withdrawalPSBT), {
network: this.bitcoinNetwork,
});

const withdrawalInputByPaymentTypeArray = getInputByPaymentTypeArray(
withdrawalTransactionSigningConfiguration,
formattedWithdrawalPSBT.toBuffer(),
this.bitcoinNetwork
);

const taprootInputsToSign = getTaprootInputsToSign(withdrawalInputByPaymentTypeArray);

await updateTaprootInputs(
taprootInputsToSign,
taprootDerivedPublicKey,
this.masterFingerprint,
formattedWithdrawalPSBT
);

return formattedWithdrawalPSBT;
} catch (error: any) {
throw new Error(`Error creating Withdrawal PSBT: ${error}`);
}
}

async signPSBT(psbt: Psbt, transactionType: 'funding' | 'closing'): Promise<Transaction> {
try {
const {
Expand Down
9 changes: 7 additions & 2 deletions src/dlc-handlers/software-wallet-dlc-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,17 @@ export class SoftwareWalletDLCHandler {

async createWithdrawalPSBT(
vault: RawVault,
withdrawAmount: bigint,
attestorGroupPublicKey: string,
fundingTransactionID: string,
feeRateMultiplier?: number,
customFeeRate?: bigint
): Promise<Transaction> {
try {
const { nativeSegwitPayment, taprootMultisigPayment } = this.getPayment();
const { nativeSegwitPayment, taprootMultisigPayment } = await this.createPayments(
vault.uuid,
attestorGroupPublicKey
);

if (
taprootMultisigPayment.address === undefined ||
Expand All @@ -250,7 +255,7 @@ export class SoftwareWalletDLCHandler {

const withdrawalTransaction = await createWithdrawalTransaction(
this.bitcoinBlockchainAPI,
vault.valueLocked.toBigInt(),
withdrawAmount,
this.bitcoinNetwork,
fundingTransactionID,
taprootMultisigPayment,
Expand Down
22 changes: 18 additions & 4 deletions src/functions/bitcoin/psbt-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,48 +173,62 @@ export async function createWithdrawalTransaction(
fundingTransactionID,
bitcoinBlockchainURL
);

console.log('fundingTransaction', fundingTransaction);

const fundingTransactionOutputIndex = fundingTransaction.vout.findIndex(
output => output.scriptpubkey_address === multisigTransactionAddress
);

console.log('fundingTransactionOutputIndex', fundingTransactionOutputIndex);

if (fundingTransactionOutputIndex === -1) {
throw new Error('Could not find Funding Transaction Output Index');
}

const feeAddress = getFeeRecipientAddressFromPublicKey(feePublicKey, bitcoinNetwork);
const feeAmount = getFeeAmount(Number(bitcoinAmount), Number(feeBasisPoints));

console.log('bitcoinAmount', bitcoinAmount);
const inputs = [
{
txid: hexToBytes(fundingTransactionID),
index: fundingTransactionOutputIndex,
witnessUtxo: {
amount: bitcoinAmount,
amount: BigInt(fundingTransaction.vout[fundingTransactionOutputIndex].value),
script: multisigTransaction.script,
},
...multisigTransaction,
},
];

console.log('inputs', inputs);

const outputs = [
{
address: feeAddress,
amount: BigInt(feeAmount),
},
{
address: userNativeSegwitAddress,
amount: BigInt(bitcoinAmount),
address: multisigTransactionAddress,
amount:
BigInt(fundingTransaction.vout[fundingTransactionOutputIndex].value) -
BigInt(bitcoinAmount),
},
];

console.log('outputs', outputs);

const selected = selectUTXO(inputs, outputs, 'default', {
changeAddress: multisigTransactionAddress,
changeAddress: userNativeSegwitAddress,
feePerByte: feeRate,
bip69: false,
createTx: true,
network: bitcoinNetwork,
});

console.log('selected', selected);

const closingTX = selected?.tx;

if (!closingTX) throw new Error('Could not create Closing Transaction');
Expand Down

0 comments on commit 1895913

Please sign in to comment.