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 36ce33433..968d63a57 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())) } @@ -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())?; } @@ -2408,6 +2406,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()); @@ -2446,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()); @@ -2463,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(_))) )); } }