Skip to content

Nest TransactionParseError in FFI InputPairError - #2035

Merged
benalleng merged 2 commits into
payjoin:masterfrom
Christopherdominic:ffi-rename-transaction-decode
Nov 10, 2026
Merged

benalleng merged 2 commits into
payjoin:masterfrom
Christopherdominic:ffi-rename-transaction-decode

Conversation

@Christopherdominic

@Christopherdominic Christopherdominic commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor

Closes #1974.

InputPairError::InvalidTransaction(String) reported the same failure as TransactionParseError::InvalidTransaction (bytes that don't consensus decode) under a second name and message. This PR makes them one error.

Commits

  1. Reject trailing bytes when decoding FFI transactions. The shared decode_transaction helper used consensus_decode, which ignores bytes after the transaction, while InputPair::new_p2pkh used consensus::deserialize, which rejects them. The helper now uses deserialize, so classify_tx / classify_receiver_tx reject trailing bytes too (both are still unreleased). Adds decode_transaction_rejects_trailing_bytes, which fails against the old helper.
  2. Nest TransactionParseError in InputPairError. Replaces the variant with InputPairError::TransactionDecode(#[from] TransactionParseError) (#[error(transparent)]), and new_p2pkh now decodes through decode_transaction(..)?. Same pattern as the existing nested FfiValidation(FfiValidationError).

The variant shipped in 0.25.0, so this is a breaking change for bindings that match on it, noted in payjoin-ffi/changelog.d/input-pair-transaction-decode.md. TransactionParseError itself is unchanged, so the C#, JS and Kotlin tests that match TransactionParseError.InvalidTransaction from classify_tx don't change.

Checked locally on each commit after rebasing onto master: cargo fmt --all -- --check, cargo clippy -p payjoin-ffi --all-targets --all-features -- -D warnings, cargo test -p payjoin-ffi --all-features, contrib/release/changelog-fragments.sh payjoin-ffi/changelog.d. Also generated the Python bindings to confirm UniFFI emits InputPairError.TransactionDecode wrapping TransactionParseError. I didn't run the full binding test suites locally.

Reviewer: @benalleng

AI disclosure: the change, commit messages and this description were drafted with AI assistance (Claude Code). A local AI review of the branch was run against the AGENTS.md criteria with no findings.

Pull Request Checklist

Please confirm the following before requesting review:

@coveralls

coveralls commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Coverage Report for CI Build 42824111686

Warning

Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes.
Quick fix: rebase this PR. Learn more →

Coverage remained the same at 87.825%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: No coverable lines changed in this PR.
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 18620
Covered Lines: 16353
Line Coverage: 87.82%
Coverage Strength: 314.08 hits per line

💛 - Coveralls

@benalleng benalleng left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 1a9fb64

@Xoulomon

Xoulomon commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

tACK 1a9fb64

  • nit: TransactionParseError::InvalidTransaction (payjoin-ffi/src/receive/error.rs:279) reports the same failure (bytes that don't consensus decode) and keeps the old name and the "Invalid transaction:" message. After this PR, one module has two names for one error. Worth a follow-up so they match; not blocking here.

Tested: fmt, clippy (-p payjoin-ffi --all-targets --all-features), and cargo test -p payjoin-ffi --all-features pass. Mapping the decode error in InputPair::new_p2pkh to another variant makes p2pkh_constructor_rejects_invalid_funding_transaction fail. No binding tests reference the old variant.

AI-assisted review; I ran the tests and mutation myself.

@benalleng
benalleng self-requested a review November 6, 2026 17:17

@benalleng benalleng left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh I missed that, perhaps we should change that enum on line 279 and simply use it to replace the original field as a nested enum forInputPairError

@caarloshenriq caarloshenriq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concept ACK 1a9fb64

The rename makes sense, and I agree with @benalleng’s suggestion to nest TransactionParseError instead of keeping two names for the same decode failure

Comment thread payjoin-ffi/src/receive/mod.rs Outdated
let non_witness_utxo: payjoin::bitcoin::Transaction =
payjoin::bitcoin::consensus::deserialize(&non_witness_utxo)
.map_err(|e| InputPairError::InvalidTransaction(e.to_string()))?;
.map_err(|e| InputPairError::TransactionDecode(e.to_string()))?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 on nesting TransactionParseError here. There's already a decode_transaction helper at L195 that could be reused:

decode_transaction(&non_witness_utxo).map_err(InputPairError::TransactionDecode)?

One thing though: decode_transaction uses consensus_decode, which accepts trailing bytes, while deserialize here rejects them (the trailing_bytes case in the test passes through the helper). If you reuse it, switching the helper to deserialize keeps this behavior and fixes the other callers too.

@Christopherdominic
Christopherdominic force-pushed the ffi-rename-transaction-decode branch from 1a9fb64 to 0e030b9 Compare November 7, 2026 09:46
@Christopherdominic Christopherdominic changed the title Rename FFI InputPairError::InvalidTransaction Nest TransactionParseError in FFI InputPairError Nov 7, 2026
@Christopherdominic

Copy link
Copy Markdown
Contributor Author

Thanks @Xoulomon, @benalleng, @caarloshenriq. Reworked as suggested, now two commits:

  • 8f22aa8: decode_transaction uses consensus::deserialize, so it rejects trailing bytes like new_p2pkh did. This also tightens classify_tx / classify_receiver_tx (both unreleased). Added a test that fails against the old helper.
  • 0e030b9: InputPairError::TransactionDecode(TransactionParseError), with new_p2pkh reusing decode_transaction as @caarloshenriq suggested.

I left TransactionParseError::InvalidTransaction itself as is, so the existing binding tests for classify_tx don't change. Happy to rename the inner variant too if you'd prefer.

AI disclosure: drafted with AI assistance (Claude Code).

@Christopherdominic

Copy link
Copy Markdown
Contributor Author

CI is green on 0e030b9. @benalleng ready for another look when you have time.

@codaMW codaMW left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK 0e030b9

Nice unification routing new_p2pkh and both classify_tx paths through decode_transaction on consensus::deserialize makes trailing-byte rejection consistent, and nesting TransactionParseError reads cleaner than the bare string.

Verified the new test is load-bearing: reverting decode_transaction to Transaction::consensus_decode flips decode_transaction_rejects_trailing_bytes red, since consensus_decode leaves trailing bytes unconsumed. Full payjoin-ffi --lib suite green.

Non-blocking: the switch also tightens classify_tx / classify_receiver_tx to reject trailing bytes the CHANGELOG's classify_tx note covers it, maybe worth a word in the PR body too.

AI-assisted review; ran the build, test, and mutation myself.

@benalleng benalleng left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very close! just a minor error transparency change to make it even cleaner.

Comment thread payjoin-ffi/src/receive/error.rs Outdated
#[error("Invalid transaction: {0}")]
InvalidTransaction(String),
#[error(transparent)]
TransactionDecode(TransactionParseError),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
TransactionDecode(TransactionParseError),
TransactionDecode(#[from] TransactionParseError),

This will allow you to use decode_transaction(..)? instead of a .map_err()

Comment thread payjoin-ffi/src/receive/mod.rs Outdated
payjoin::bitcoin::consensus::deserialize(&non_witness_utxo)
.map_err(|e| InputPairError::InvalidTransaction(e.to_string()))?;
let non_witness_utxo =
decode_transaction(&non_witness_utxo).map_err(InputPairError::TransactionDecode)?;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
decode_transaction(&non_witness_utxo).map_err(InputPairError::TransactionDecode)?;
decode_transaction(&non_witness_utxo)?;

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.
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.
@Christopherdominic
Christopherdominic force-pushed the ffi-rename-transaction-decode branch from 0e030b9 to 015a204 Compare November 10, 2026 13:23
@Christopherdominic

Christopherdominic commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor Author

Applied both suggestions: TransactionDecode(#[from] TransactionParseError), and new_p2pkh now uses decode_transaction(&non_witness_utxo)?. Thanks @benalleng.

Also rebased onto master to resolve the CHANGELOG conflict. The entry now lives in payjoin-ffi/changelog.d/input-pair-transaction-decode.md, per the new fragment process.

Thanks for testing, @codaMW. The PR body already calls out that classify_tx / classify_receiver_tx now reject trailing bytes.

@benalleng benalleng left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UTACK 015a204

@caarloshenriq caarloshenriq left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 015a204

@benalleng
benalleng merged commit 793a4e0 into payjoin:master Nov 10, 2026
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Consider renaming FFi InputPairError::InvalidTransaction

6 participants