Skip to content

Commit

Permalink
hotfix: Ensure that withdraw and unbond output values are below dust …
Browse files Browse the repository at this point in the history
…and positive
  • Loading branch information
vitsalis authored Jun 28, 2024
2 parents b867081 + cde2f25 commit ae7292a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "btc-staking-ts",
"version": "0.2.6",
"version": "0.2.7",
"description": "Library exposing methods for the creation and consumption of Bitcoin transactions pertaining to Babylon's Bitcoin Staking protocol. Experimental version, should not be used for production purposes or with real funds.",
"module": "dist/index.js",
"main": "dist/index.cjs",
Expand Down
22 changes: 12 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,20 +331,19 @@ function withdrawalTransaction(
sequence: timelock,
});

const outputValue = tx.outs[outputIndex].value;
if (outputValue < BTC_DUST_SAT) {
throw new Error("Output value is less than dust limit");
}
const estimatedFee = getWithdrawTxFee(feeRate);
const value = tx.outs[outputIndex].value - estimatedFee;
if (!value) {
const outputValue = tx.outs[outputIndex].value - estimatedFee;
if (outputValue < 0) {
throw new Error(
"Not enough funds to cover the fee for withdrawal transaction",
);
}
if (outputValue < BTC_DUST_SAT) {
throw new Error("Output value is less than dust limit");
}
psbt.addOutput({
address: withdrawalAddress,
value,
value: outputValue,
});

return {
Expand Down Expand Up @@ -680,16 +679,19 @@ export function unbondingTransaction(
scriptTree: outputScriptTree,
network,
});
const value = stakingTx.outs[outputIndex].value - transactionFee;
if (!value) {
const outputValue = stakingTx.outs[outputIndex].value - transactionFee;
if (outputValue < 0) {
throw new Error(
"Not enough funds to cover the fee for unbonding transaction",
);
}
if (outputValue < BTC_DUST_SAT) {
throw new Error("Output value is less than dust limit");
}
// Add the unbonding output
psbt.addOutput({
address: unbondingOutput.address!,
value: stakingTx.outs[outputIndex].value - transactionFee,
value: outputValue,
});

return {
Expand Down

0 comments on commit ae7292a

Please sign in to comment.