2

I'm sorry if this has been answered before, but I haven't found anyone really bring this up.

I am trying to construct a PSBT, which assumes a fixed feerate and a fixed amount to send to some address. Any extra sats is change back to the sender. Something like:

const psbt = new Psbt({ network: bitcoin.networks.bitcoin });
psbt.addInput({hash, index, witnessUtxo: {...}});
psbt.addOutput({address: recipient, value: amountToSend});
psbt.addOutput({address: sender, value: changeAmount}); // total inputs - amountToSend - fees

However, creating that last change output seems like a circular exercise:

  • To determine the last output to add to the PSBT, we need to know the exact fee in satoshis we're willing to spend.
  • To know the exact fee in satoshis, we need to know the virtual bytes that the tx will consume, which requires us having a fully constructed and signed PSBT i.e. signedPsbt.extractTransaction().virtualSize();.

Am I missing something obvious here? Is there something to help estimate vsize before signing a PSBT (by estimating the size of signatures)? Or something to help estimate fees given number of inputs and outputs?

Any help is appreciated. Thank you in advance!

1 Answer 1

2

Standard script types generally have known maximum sizes, so you can use that to estimate the size of the transaction to determine the fee. Murch has created a table with the sizes of these standard script types to make it easier to find this information.

However, as you point out, the easiest way to determine the size is to just sign once, calculate the size and the necessary fee, then discard that signed transaction, modify the original, and sign again.

3
  • Would using an "effective value" approach not help too? Oct 10, 2023 at 19:30
  • 2
    Using effective values still requires estimating the size of the input.
    – Ava Chow
    Oct 10, 2023 at 19:42
  • For single-sig types, there is also an overview here: bitcoin.stackexchange.com/a/84006/5406
    – Murch
    Oct 11, 2023 at 15:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.