Repository navigation
Description
Checked on payjoin-1.2.0 (8e07297).
Offline and DIY hardware signers that require previous transactions to authenticate input scripts and amounts cannot complete those checks when a receiver-contributed input carries only witness_utxo. That field contains the claimed output but does not itself authenticate it against the referenced txid. For SegWit v0 inputs, BIP143 commits to the amount of the input being signed, not the other inputs' amounts. Matching the previous transaction's txid and selecting its output authenticates the script and amount against that outpoint; it does not prove confirmation or that the output is unspent, and that is all these signers ask of it. This does not assume all signing devices use the same verification design.
What the code does now
The library already handles the field when given it:
previous_txoutprefersnon_witness_utxo, andvalidate_utxochecks its txid against the outpoint and its consistency with anywitness_utxo:rust-payjoin/payjoin/src/core/psbt/mod.rs
Lines 137 to 204 in 8e07297
pub fn previous_txout(&self) -> Result<&TxOut, PrevTxOutError> { match (&self.psbtin.non_witness_utxo, &self.psbtin.witness_utxo) { (None, None) => Err(PrevTxOutError::MissingUtxoInformation), // Prefer the `non_witness_utxo`: its txid is committed to by // `previous_output`, so it authenticates the spent `TxOut`, unlike a bare // `witness_utxo`. (Some(tx), _) => { let vout: usize = self.txin.previous_output.vout.try_into().map_err(|_| { PrevTxOutError::IndexOutOfBounds { output_count: tx.output.len(), index: self.txin.previous_output.vout, } })?; tx.output.get(vout).ok_or(PrevTxOutError::IndexOutOfBounds { output_count: tx.output.len(), index: self.txin.previous_output.vout, }) } (None, Some(txout)) => Ok(txout), } } /// Validates that [`TxIn`] and the applicable UTXO field(s) of the [`psbt::Input`] refer to the same UTXO. pub fn validate_utxo(&self) -> Result<(), InternalPsbtInputError> { match (&self.psbtin.non_witness_utxo, &self.psbtin.witness_utxo) { (None, None) => Err(InternalPsbtInputError::PrevTxOut(PrevTxOutError::MissingUtxoInformation)), (Some(tx), None) if tx.compute_txid() == self.txin.previous_output.txid => tx .output .get::<usize>(self.txin.previous_output.vout.try_into().map_err(|_| { PrevTxOutError::IndexOutOfBounds { output_count: tx.output.len(), index: self.txin.previous_output.vout, } })?) .ok_or_else(|| { PrevTxOutError::IndexOutOfBounds { output_count: tx.output.len(), index: self.txin.previous_output.vout, } .into() }) .map(drop), (Some(_), None) => Err(InternalPsbtInputError::UnequalTxid), (None, Some(_)) => Ok(()), (Some(tx), Some(witness_txout)) if tx.compute_txid() == self.txin.previous_output.txid => { let non_witness_txout = tx .output .get::<usize>(self.txin.previous_output.vout.try_into().map_err(|_| { PrevTxOutError::IndexOutOfBounds { output_count: tx.output.len(), index: self.txin.previous_output.vout, } })?) .ok_or(PrevTxOutError::IndexOutOfBounds { output_count: tx.output.len(), index: self.txin.previous_output.vout, })?; if witness_txout == non_witness_txout { Ok(()) } else { Err(InternalPsbtInputError::SegWitTxOutMismatch) } } (Some(_), Some(_)) => Err(InternalPsbtInputError::UnequalTxid), } - the legacy
InputPairconstructors set it, and the genericInputPair::newaccepts a caller-built input carrying it:rust-payjoin/payjoin/src/core/receive/mod.rs
Lines 66 to 105 in 8e07297
pub fn new( txin: TxIn, psbtin: psbt::Input, expected_weight: Option<Weight>, ) -> Result<Self, PsbtInputError> { let raw = InternalInputPair { txin: &txin, psbtin: &psbtin }; raw.validate_utxo()?; let expected_weight = match (raw.expected_input_weight(), expected_weight) { (Ok(_), Some(_)) => { return Err(InternalPsbtInputError::ProvidedUnnecessaryWeight.into()); } (Ok(weight), None) => weight, (Err(InputWeightError::NotSupported), Some(expected_weight)) => expected_weight, (Err(e), _) => return Err(InternalPsbtInputError::from(e).into()), }; let input_pair = Self { expected_weight, txin, psbtin }; Ok(input_pair) } /// Helper function for creating legacy input pairs fn new_legacy_input_pair( non_witness_utxo: Transaction, outpoint: OutPoint, redeem_script: Option<ScriptBuf>, ) -> Result<Self, PsbtInputError> { let txin = TxIn { previous_output: OutPoint { txid: outpoint.txid, vout: outpoint.vout }, ..Default::default() }; let psbtin = psbt::Input { non_witness_utxo: Some(non_witness_utxo), redeem_script, ..psbt::Input::default() }; Self::new(txin, psbtin, None) } prepare_psbtcopiesnon_witness_utxointo the outgoing proposal when present in the processed PSBT:rust-payjoin/payjoin/src/core/receive/mod.rs
Lines 412 to 418 in 8e07297
filtered_psbt.inputs.push(bitcoin::psbt::Input { witness_utxo: input.witness_utxo.clone(), non_witness_utxo: input.non_witness_utxo.clone(), final_script_sig: input.final_script_sig.clone(), final_script_witness: input.final_script_witness.clone(), ..Default::default() });
The payjoin-cli construction leaves non_witness_utxo unset, with a comment calling it unnecessary:
rust-payjoin/payjoin-cli/src/app/wallet.rs
Lines 218 to 234 in 8e07297
| pub fn input_pair_from_corepc(utxo: ListUnspentItem) -> InputPair { | |
| let psbtin = Input { | |
| // NOTE: non_witness_utxo is not necessary because bitcoin-cli always supplies | |
| // witness_utxo, even for non-witness inputs | |
| witness_utxo: Some(TxOut { | |
| value: Amount::from_btc(utxo.amount.to_btc()).expect("Valid amount"), | |
| script_pubkey: utxo.script_pubkey, | |
| }), | |
| redeem_script: None, | |
| witness_script: None, // Not available in this version | |
| ..Default::default() | |
| }; | |
| let txin = TxIn { | |
| previous_output: OutPoint { txid: utxo.txid, vout: utxo.vout }, | |
| ..Default::default() | |
| }; | |
| InputPair::new(txin, psbtin, None).expect("Input pair should be valid") |
This is from reading the source. I have not yet verified the complete wallet-processing, transport and signer-export flow.
Tradeoffs
The field is optional for existing protocol operation, so nothing breaks for receivers that skip it. It only helps these signers if it survives processing and export, though. It also grows the proposal by roughly one previous transaction per contributed input, which matters for QR transports. Payload size, and what happens when fetching the transaction fails, would need explicit handling.
Question for maintainers
Would you prefer receiver-side inclusion (payjoin-cli fetching it from its node and setting it on the InputPair), sender-side enrichment before export to the signer, or support for both? And is the generic InputPair::new the intended way for integrators to supply it, or would you want it on the segwit constructors?
Related: #678, #1836, #276. None of them proposes this receiver-side addition, though I may have missed other discussion.
Happy to open a PR once you have a preference, with generic PSBT fixtures covering matching previous transactions, mismatched txids or outputs, and preservation through processing and export, plus an end-to-end test against a signer that requires the proof.
Activity