What can a read-only PSBT inspector trust when it prints the fee from witnessUtxo?
I am adding a read-only sign command to an open-source, signet/testnet-only self-custody toolkit (btcguardianshq). This step only decodes a base64 PSBT and prints inputs, outputs and the fee. It does not load keys and it does not sign. Signing is planned as a later step.
The inspector currently:
- refuses
--network mainnet/bitcoin - requires the file to start with
cHNidP8 - requires every input to have
witnessUtxoand takes the amount from there - computes
fee = sum(inputs) - sum(outputs), rejects a negative fee, and warns if the fee is over 50,000 sats
The amount check looks like this (from vault/psbt-sign.js on main):
if (!inp.witnessUtxo) throw new Error(`Input ${i} has no witnessUtxo; cannot check amount.`);
const v = BigInt(inp.witnessUtxo.value);
inTotal += v;
// ...
const fee = inTotal - outTotal;
if (fee < 0n) throw new Error('Outputs exceed inputs: invalid PSBT.');
if (fee > 50000n) console.log('WARNING: fee is over 50,000 sats. Check carefully.');
Two things I am unsure about before I grow this into an actual signer.
1. The displayed fee can be a lie, and nothing is being signed yet.
witnessUtxo.value is just data the creator put in the PSBT. For a hardware signer, BIP143 commits to the amount, so a faked value makes that signature invalid (with the known multi-input caveat where a host can collect valid signatures across two signing attempts). A read-only inspector never signs, so that defence does not apply. If someone later signs what they just saw, they may have approved a fee that only existed in the PSBT fields.
Should a read-only inspector require nonWitnessUtxo (the full previous transaction) even for segwit inputs, hash it, and check that the txid matches the input outpoint before it prints a fee? If only witnessUtxo is present, should it refuse to print a fee and warn instead? I also currently reject a perfectly valid legacy input that only has nonWitnessUtxo and no witnessUtxo, which feels wrong.
2. A PSBT has no network field.
The tool is meant to stay on signet/testnet and uses bitcoin.networks.testnet for both, because signet and testnet share tb1 addresses. Refusing the --network mainnet flag is easy, but that does not stop someone passing a mainnet-crafted PSBT while the flag is still signet. The scripts themselves do not carry a network, so a mainnet P2WPKH input would be shown as a tb1 address.
What, if anything, can an inspector reliably use to refuse a mainnet PSBT? I realise signet and testnet cannot be told apart from addresses alone (Why doesn't signet use a unique bech32 prefix?). Global xpubs (xpub vs tpub) are only present sometimes. Is "trust the user-supplied network, show addresses for that network, and say so clearly" the honest limit here?
I have read the threads on PSBT signing safety checks, the fee-overpayment issue for hardware signers (here and here), and Testnet vs Mainnet PSBTs. Those are mostly about what a signer should verify, or whether test vectors transfer across networks. I am asking what a viewer that does not sign yet should refuse to display, or should display only with a warning.
A later check I also want to add is verifying a change output by re-deriving it from bip32Derivation (and a known xpub) rather than trusting the address string. I will treat that as a follow-up unless it belongs in the same "minimum inspector checks" list.
from Recent Questions - Bitcoin Stack Exchange https://ift.tt/QtnW542
via IFTTT