diff --git a/package.json b/package.json index 5040fdf..114b6fc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "btc-staking-ts", - "version": "0.2.2", + "version": "0.2.3", "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", @@ -45,4 +45,4 @@ "@bitcoin-js/tiny-secp256k1-asmjs": "^2.2.3", "bitcoinjs-lib": "^6.1.5" } -} +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 6085ead..345aa7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -363,9 +363,13 @@ function withdrawalTransaction( } // withdraw tx always has 1 output only const estimatedFee = getEstimatedFee(feeRate, psbt.txInputs.length, 1); + const value = tx.outs[outputIndex].value - estimatedFee; + if (!value) { + throw new Error("Not enough funds to cover the fee for withdrawal transaction"); + } psbt.addOutput({ address: withdrawalAddress, - value: tx.outs[outputIndex].value - estimatedFee, + value, }); return { @@ -699,7 +703,10 @@ export function unbondingTransaction( scriptTree: outputScriptTree, network, }); - + const value = stakingTx.outs[outputIndex].value - transactionFee; + if (!value) { + throw new Error("Not enough funds to cover the fee for unbonding transaction"); + } // Add the unbonding output psbt.addOutput({ address: unbondingOutput.address!,