Bitcoin

bitcoinjs – How to get vsize before psbt completes

I’m using bitcoinjs-lib to configure transactions via PSBT, but I’m having trouble getting the virtual size (vsize). My process is as follows:

  1. Obtain the user’s address through the browser wallet (OKX Wallet).

  2. Retrieve the list of UTXOs for that address via REST API.

  3. Construct PSBT by adding inputs and outputs (assuming fee=0 when calculating change).

  4. Send psbt.toHex() to the browser wallet to request the user’s signature (unsigned PSBT cannot complete).

  5. Psbt.fromHex(signResult).extractTransaction().virtualSize() * Calculate the fee using feeRate.

  6. Based on the fee, we recalculate the changes, send the modified PSBT back to the wallet, request the user’s signature, then reconstruct the PSBT from the signature results to fetch the transaction and broadcast it.

This process requires requesting the user’s signature twice, which reduces the user experience.

Is there a way to only sign once while calculating changes based on the actual vsize?

Related Articles

Back to top button