spend: deterministic, canonical input construction - #2192
Open
bc1cindy wants to merge 2 commits into
Open
Conversation
Coin selection (both the BnB algorithm and the descending-value-per-weight fallback) depends on the order in which candidate coins are supplied. Callers build that list from DatabaseConnection::coins, which returns a HashMap, so the order is randomized per run. When several coins are equivalent for selection (equal value per weight unit), which coins get selected and the resulting input order are non-deterministic. Sort candidates by outpoint before selecting, so ties break deterministically and a given set of coins always yields the same spend. This keeps spend and recovery reproducible and testable.
Building a coin's transaction input and PSBT input (its sequence, witness_utxo, and whether a non_witness_utxo is included for non-Taproot) is inlined in create_spend. A payjoin receiver being added separately reimplements the same construction and diverges from it: it hardcodes the sequence and always sets a non_witness_utxo, even for Taproot, making a payjoin input distinguishable from an ordinary spend input. Extract the construction into a shared coin_to_psbt_input helper so spend, recovery and payjoin build inputs identically. This is a pure refactor of the spend path here; the payjoin receiver will reuse the helper once rebased on top.
bc1cindy
marked this pull request as ready for review
July 9, 2026 13:54
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
build spend/recovery inputs one canonical, deterministic way, so callers reusing this path can't be told apart from a normal spend.
coin_to_psbt_inputhelper: a single place builds a coin's input (sequence, witness / non-witness utxo), instead of each path diverging.divergent input construction is itself a wallet fingerprint.
ref: payjoin/rust-payjoin#1597 (comment)
closes #2191