From d48fa7c15a717aa3a4c7db2e829ca2c6f63ddbcc Mon Sep 17 00:00:00 2001 From: christopherdominic Date: Sat, 7 Nov 2026 10:44:15 +0100 Subject: [PATCH 1/2] Reject trailing bytes when decoding FFI transactions decode_transaction used consensus_decode, which stops after one transaction and ignores any bytes that follow it. InputPair::new_p2pkh decodes with consensus::deserialize instead, which rejects trailing bytes, so the same input could decode in one place and fail in another. Switch the helper to consensus::deserialize so classify_tx and classify_receiver_tx reject trailing bytes too, and so new_p2pkh can share the helper. --- payjoin-ffi/src/receive/mod.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/payjoin-ffi/src/receive/mod.rs b/payjoin-ffi/src/receive/mod.rs index 36ce33433..4d2002efb 100644 --- a/payjoin-ffi/src/receive/mod.rs +++ b/payjoin-ffi/src/receive/mod.rs @@ -6,7 +6,6 @@ pub use error::{ InputPairError, JsonReply, OutputSubstitutionError, ProtocolError, PsbtInputError, ReceiverCreateRequestError, ReceiverError, SessionError, TransactionParseError, }; -use payjoin::bitcoin::consensus::Decodable; use payjoin::bitcoin::psbt::Psbt; use payjoin::bitcoin::FeeRate; use payjoin::persist::{MaybeFatalTransition, NextStateTransition}; @@ -192,8 +191,9 @@ macro_rules! impl_pending_fallback_cancel_for_receiver { }; } +/// Decode a consensus-encoded transaction, rejecting trailing bytes. fn decode_transaction(tx: &[u8]) -> Result { - payjoin::bitcoin::Transaction::consensus_decode(&mut &tx[..]) + payjoin::bitcoin::consensus::deserialize(tx) .map_err(|e| TransactionParseError::InvalidTransaction(e.to_string())) } @@ -2408,6 +2408,19 @@ mod tests { } } + #[test] + fn decode_transaction_rejects_trailing_bytes() { + let tx = funding_tx(p2pkh_script_pubkey()); + let mut tx_bytes = payjoin::bitcoin::consensus::serialize(&tx); + assert_eq!(decode_transaction(&tx_bytes).expect("valid transaction"), tx); + + tx_bytes.push(0x00); + assert!(matches!( + decode_transaction(&tx_bytes), + Err(TransactionParseError::InvalidTransaction(_)) + )); + } + #[test] fn p2pkh_constructor_accepts_funding_transaction() { let tx = funding_tx(p2pkh_script_pubkey()); From 015a204d8fce01ef6dced3a645b416b036a45248 Mon Sep 17 00:00:00 2001 From: christopherdominic Date: Tue, 10 Nov 2026 14:22:37 +0100 Subject: [PATCH 2/2] Nest TransactionParseError in FFI InputPairError InputPairError::InvalidTransaction reported the same failure as TransactionParseError::InvalidTransaction (bytes that do not consensus decode) under a second name, a second message and a String payload. Replace it with InputPairError::TransactionDecode, which wraps TransactionParseError, and have InputPair::new_p2pkh decode through the shared decode_transaction helper. A decode failure now has one type and one message whether it comes from new_p2pkh or classify_tx. This changes the public FFI API, so add a payjoin-ffi changelog fragment. --- payjoin-ffi/changelog.d/input-pair-transaction-decode.md | 5 +++++ payjoin-ffi/src/receive/error.rs | 4 ++-- payjoin-ffi/src/receive/mod.rs | 8 +++----- 3 files changed, 10 insertions(+), 7 deletions(-) create mode 100644 payjoin-ffi/changelog.d/input-pair-transaction-decode.md diff --git a/payjoin-ffi/changelog.d/input-pair-transaction-decode.md b/payjoin-ffi/changelog.d/input-pair-transaction-decode.md new file mode 100644 index 000000000..077c6fbc9 --- /dev/null +++ b/payjoin-ffi/changelog.d/input-pair-transaction-decode.md @@ -0,0 +1,5 @@ +- Replace `InputPairError::InvalidTransaction(String)` with + `InputPairError::TransactionDecode(TransactionParseError)`, the same error + `classify_tx` returns for bytes that do not decode. Bindings that match on + the old variant need to match the new one and read the nested + `TransactionParseError`. diff --git a/payjoin-ffi/src/receive/error.rs b/payjoin-ffi/src/receive/error.rs index a8a038d7d..65f1121d4 100644 --- a/payjoin-ffi/src/receive/error.rs +++ b/payjoin-ffi/src/receive/error.rs @@ -258,8 +258,8 @@ pub enum InputPairError { #[error("Invalid input: {0}")] FfiValidation(FfiValidationError), /// Provided transaction could not be consensus decoded. - #[error("Invalid transaction: {0}")] - InvalidTransaction(String), + #[error(transparent)] + TransactionDecode(#[from] TransactionParseError), } impl InputPairError { diff --git a/payjoin-ffi/src/receive/mod.rs b/payjoin-ffi/src/receive/mod.rs index 4d2002efb..968d63a57 100644 --- a/payjoin-ffi/src/receive/mod.rs +++ b/payjoin-ffi/src/receive/mod.rs @@ -1547,9 +1547,7 @@ impl InputPair { non_witness_utxo: Vec, outpoint: OutPoint, ) -> Result { - let non_witness_utxo: payjoin::bitcoin::Transaction = - payjoin::bitcoin::consensus::deserialize(&non_witness_utxo) - .map_err(|e| InputPairError::InvalidTransaction(e.to_string()))?; + let non_witness_utxo = decode_transaction(&non_witness_utxo)?; for txout in &non_witness_utxo.output { validate_amount_sat(txout.value.to_sat())?; } @@ -2459,7 +2457,7 @@ mod tests { assert!(matches!( InputPair::new_p2pkh(vec![0x00], outpoint()), - Err(InputPairError::InvalidTransaction(_)) + Err(InputPairError::TransactionDecode(TransactionParseError::InvalidTransaction(_))) )); let mut too_much = funding_tx(p2pkh_script_pubkey()); @@ -2476,7 +2474,7 @@ mod tests { trailing_bytes.push(0x00); assert!(matches!( InputPair::new_p2pkh(trailing_bytes, outpoint()), - Err(InputPairError::InvalidTransaction(_)) + Err(InputPairError::TransactionDecode(TransactionParseError::InvalidTransaction(_))) )); } }