From cde2f25f581dd8e591f39e6f9c7224950c578fc2 Mon Sep 17 00:00:00 2001 From: Vitalis Salis Date: Thu, 27 Jun 2024 22:05:54 +0300 Subject: [PATCH] hotfix: Ensure that withdraw and unbond output values are below dust and positive --- package.json | 2 +- src/index.ts | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/package.json b/package.json index 1638c61..2198d2d 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/index.ts b/src/index.ts index f455982..de4ebba 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 { @@ -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 {