Skip to content

spend: deterministic, canonical input construction - #2192

Open
bc1cindy wants to merge 2 commits into
wizardsardine:masterfrom
bc1cindy:determinism
Open

spend: deterministic, canonical input construction#2192
bc1cindy wants to merge 2 commits into
wizardsardine:masterfrom
bc1cindy:determinism

Conversation

@bc1cindy

@bc1cindy bc1cindy commented Jul 9, 2026

Copy link
Copy Markdown

build spend/recovery inputs one canonical, deterministic way, so callers reusing this path can't be told apart from a normal spend.

  • deterministic coin selection: candidates come from a HashMap, so tie-breaking (and input order) was random per run, sort by outpoint.
  • coin_to_psbt_input helper: 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

bc1cindy added 2 commits July 9, 2026 09:55
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
bc1cindy marked this pull request as ready for review July 9, 2026 13:54
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.

Non-deterministic coin selection

1 participant