I'm using bitcoinjs-lib to construct a transaction via PSBT, but I'm having trouble getting the virtual size (vsize). My process is as follows:
-
Get the user's address through the browser wallet (OKX Wallet).
-
Retrieve the UTXO list for this address via a REST API.
-
Construct the PSBT, adding inputs and outputs (assuming charges = 0 when calculating change).
-
Send psbt.toHex() to the browser wallet to request the user's signature (unsigned PSBT cannot be finalized).
-
Calculate fees using Psbt.fromHex(signResult).extractTransaction().virtualSize() * feeRate.
-
Recalculate the modification based on the fee, send the modified PSBT back to the wallet to request the user's signature, then reconstruct the PSBT from the signature result to obtain the transaction and broadcast it.
During this process I have to ask for the user's signature twice, resulting in a poor user experience.
Is there a way to sign only once and still calculate the change based on the actual size?