Skip to content

fix(key-wallet): a pending spend needs its owner's signature - #1523

Draft
PastaPastaPasta wants to merge 19 commits into
devfrom
fix/pending-spend-authorization
Draft

PastaPastaPasta wants to merge 19 commits into
devfrom
fix/pending-spend-authorization

Conversation

@PastaPastaPasta

@PastaPastaPasta PastaPastaPasta commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

A pending transaction marks the coins it spends as spent as soon as a peer relays it. Any peer can therefore hide a wallet coin from the balance by relaying an unsigned claim on it. This works on a coin the wallet already holds, and on a coin whose funding the wallet hasn't seen yet. The claim never confirms, but the coin stays hidden.

This builds on #1416 at f134961, which adds verify_input_authorization. The first commits shown here are #1416's. The four commits after them are this change.

Verifier. verify_input_authorization now returns the new SpendAuthorizationError::RuntimeAuthorization instead of Ok(false) in two cases: an input script that isn't push-only and has no plain push of the owner's key, or one that has no pushed signature that verifies. Such a script may build the key or signature at run time, for example with OP_CAT. Push-only verdicts are unchanged.

Wallet. For a pending transaction (not mined, not InstantSend-locked), an input provably lacks its owner's authorization in two cases. The first is a structural invalidity: its script doesn't parse or exceeds MAX_SCRIPT_SIZE, or the transaction spends its outpoint twice. Each of these makes the transaction invalid whatever its scripts compute. The second is a push-only script for which the verifier returns Ok(false). A script that isn't push-only is never counted, even when the verifier returns Ok(false): it may build the owner's signature or key at run time, and a miner can include a transaction that relay policy rejects. Such an input keeps its claim, the same as an input the verifier can't judge. On that basis:

  • A new pending transaction that spends a coin the wallet holds or keeps aside, with an input that provably lacks authorization, is ignored. It isn't recorded, and the coin stays counted.
  • When a new transaction makes known an output that a pending spend already claimed, the claim is dropped, with everything built on it, if its input provably lacks authorization. The refusal and the drop use the same test. The coin is credited. The drop indexes the records once, checks for settled descendants with one parent-to-children map, and abandons every claim in one walk, so a peer can't make it quadratic. It is reported as a TransactionsSwept sweep, with superseded_by set to the funding transaction. Outpoints that the dropped claims spent are reported as released, unless another record or claim still holds them, a block spent them, or the funding transaction itself spends them.
  • A claim is never dropped while it, or anything spending its outputs, is mined, InstantSend-locked or ChainLocked. A spend of its outputs seen in a block also counts. Mined and InstantSend-locked transactions are never checked.

This relies on the transaction context that the wallet's caller passes. dash-spv marks a transaction InstantSend-locked only for a lock that InstantSendManager::verify checked against its quorum. On dev that has been true since #1336; on #1416's older base, a lock still waiting for its quorum is passed on as well. A block reaches the wallet only on dash-spv's validated header chain. Other callers of WalletInterface must pass the same verified evidence.

Not covered:

  • A claim whose input script isn't push-only is kept and holds its coin, even an unsigned one padded with OP_NOP. It holds the coin until it is abandoned or a rival spend settles.
  • Inputs the verifier can't judge, such as other script types or signature hash types other than ALL, are taken as before. This includes ALL|ANYONECANPAY, as used by CoinJoin, so a mixing participant who holds such a signature can still hold a coin back until the mix settles.
  • Claims recorded before this change are only checked when their funding next arrives as a new transaction.
  • A claim restored from spent_claims without a record is never checked.
  • If a block holds the funding and then a claim that provably lacks authorization, the claim is dropped and then recorded again as mined. Its pending descendants are not. Only an invalid block can hold such a claim, and a claim whose script isn't push-only is never dropped.
  • A funding transaction the wallet already holds, re-checked after one of its outputs' addresses is derived, doesn't run the drop again.
  • dash-spv's mempool manager doesn't keep a transaction the wallet ignored, so a lock that arrives for it later waits until the transaction is mined. This matters only if a genuine spend were ignored.
  • Consumers that treat every TransactionsSwept as a spend beaten by a final rival should handle the dropped-claim case, where superseded_by is the funding and the claimant's inputs are given back.

Tests:

  • key-wallet pending_spend_authorization_tests covers:
    • a spend before its funding: signed, unsigned, push-only with a bad signature, signed for another coin, six scripts that aren't push-only and are kept (a signature or key built with OP_CAT, and a valid or invalid signature and key followed by OP_NOP, among them), and three structurally invalid inputs that are dropped (a script that doesn't parse, one over MAX_SCRIPT_SIZE, an outpoint spent twice);
    • settled spends;
    • mined, InstantSend-locked and block-observed descendants;
    • a pending descendant;
    • a second claim keeping the coin;
    • the release filter;
    • for held coins: authorized from another device, signed ALL|ANYONECANPAY as in CoinJoin, unsigned, signed for another coin, the same six scripts that aren't push-only (taken), the same three structural invalidities (ignored), mined with OP_CAT, one of two inputs unsigned, a coin a pending spend or a signed claim holds, taken once mined or locked, and a lock that arrived first.
  • The verifier table adds the OP_CAT cases.
  • A unit test of the wallet's check: a push that matches the owner's key hash but isn't a key makes the verifier return Ok(false), which counts for a push-only script and not for one with other opcodes.
  • key-wallet-manager adds an event test for a funding that exposes an unsigned claim.
  • Existing tests now sign the mempool spends they make of held coins.
  • On a branch that also carries fix(dash-spv): defer unverified InstantLocks #1336, dash-spv's wallet_view_tests (a_quorum_pending_lock_does_not_keep_an_unsigned_claim, a_lock_once_verified_keeps_an_unsigned_claim) check that a lock waiting for its quorum keeps no unsigned claim, and that a verified one does. The first fails with fix(dash-spv): defer unverified InstantLocks #1336's deferral removed.

Merge order: #1516, #1518, #1519 and #1520 add tests that feed unsigned mempool spends of held coins, which this PR refuses. Whichever of this PR or those lands second must sign those spends, as this PR does for the existing tests.

Each part of the fix was removed in turn, and the tests failed each time:

  • the verifier split;
  • each of the three structural checks;
  • the push-only condition on the verifier's Ok(false);
  • the descendant guard;
  • the drop;
  • the refusal;
  • the kept-aside lookup;
  • each release filter.

Counting a script that isn't push-only as unauthorized again, or giving only one of the refusal and the drop a different test, also fails the tests.

cargo fmt --all --check, clippy with -D warnings for key-wallet, key-wallet-manager and dash-spv (default and all features), cargo doc with -D warnings for the wallet crates, and the key-wallet and key-wallet-manager tests (default and all features) pass on this branch; dash-spv's tests compile.

🤖 Generated with Claude Code

lklimek and others added 18 commits November 16, 2026 07:07
`verify_input_authorization` checks that a transaction input carries a valid P2PKH / `SIGHASH_ALL` owner signature for the funding output it claims. A wallet can use it to decide whether a pending spend may reserve one of its coins before the spend is final.

Unsupported funding scripts and signature hash types return a typed `SpendAuthorizationError`, so a caller does not release an existing hold by mistake. Malformed or invalid signatures return `false`. The check covers authorization only: it validates neither amounts nor consensus rules, and it is no evidence of finality.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The repeated-outpoint case appended the duplicate input after signing, so the signature check alone rejected it and no test failed without the uniqueness guard. The owner now signs the transaction with the outpoint already listed twice, so only the guard rejects it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`EcdsaPublicKey::verify` already accepts high-S signatures, which `dashcore-crypto`'s `verify_accepts_high_s` covers, so the `normalize_s` call before it changed nothing and `should_accept_high_s_owner_authorization` passed without it. Both are removed, along with that test's copy of the group-order subtraction.

The doc comment of `verify_input_authorization` is cut to three lines. The scope caveats it dropped are in the PR description.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…zation

`SpendAuthorizationError` implements `Display` and `std::error::Error` by hand like the crate's other error types, so `key-wallet` no longer depends on `thiserror`. The module is declared with its siblings in `transaction_checking`, which re-exports `verify_input_authorization` and `SpendAuthorizationError`, and loses its doc comments.

An out-of-range `input_index` returns `SpendAuthorizationError::InputIndexOutOfRange` instead of `Ok(false)`. It is a caller bug, and reading it as "not authorized" could release a hold.

The tests share one transaction builder and call `sign_input` directly. The SIGHASH_SINGLE constant-digest case is folded into `should_preserve_unsupported_authorization_as_an_error`, and the witness test is removed: Dash has no segwit and never serializes the witness.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The legacy signature hash does not commit to the input script, so anyone can prepend `OP_0` or `OP_NOP` to an owner's `<sig> <pubkey>` without invalidating the spend under consensus rules. `verify_input_authorization` accepted only a two-push script and returned `Ok(false)` for such a spend, which tells the caller to release the hold.

The signature and public key are now taken from the last two pushes. When they verify behind a prefix, the function returns the new `SpendAuthorizationError::UnsupportedScriptSig`; a padded script without a valid owner signature still returns `Ok(false)`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Dash consensus does not require a push-only input script, so `<sig> <pk> OP_NOP`, `<sig> <pk> <x> OP_DROP` or `<pk> <sig> OP_SWAP` still spend a P2PKH output. These returned `Ok(false)` and let the caller release the hold. A push-only input script is still checked through its last two pushes. Any other input script returns `UnsupportedScriptSig` when one of its pushes is a valid owner signature.

The test for unauthenticated hash flags is folded into the test that re-signs under the same modes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
An input script with opcodes makes every push a signature candidate, and each candidate recomputed the legacy sighash over the whole transaction. An attacker who knows the owner's public key could pad a spend with thousands of fake signatures and cost seconds of CPU on every wallet load. Candidates now share one digest per hash type, and an input script over Dash Core's `MAX_SCRIPT_SIZE` returns `false` before parsing, since consensus rejects it anyway.

The function doc states that a signature or key built at run time is not recognized. The test for input scripts that cannot spend the funding output also covers a malformed signature next to the owner's key.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Exercise the wallet verification outcomes with a shared owner-signed spend. Check asset lock payload binding alongside the dashcore legacy sighash tests.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>

Co-Authored-By: Codex GPT-6 <[email protected]>
Sign against the funding script to isolate the encoded public key mismatch. Exercise both NONE and SINGLE with a matching output alongside the outputless SINGLE case.

<sub>🤖 Co-authored by [Claudius the Magnificent](https://github.com/lklimek/claudius) AI Agent</sub>

Co-Authored-By: Codex GPT-6 <[email protected]>
Re-sign and flag-swap each mode the previous suite exercised (0, 2, 3, 4, 0x81, 0x82, 0x83, 0xff), and keep SINGLE without a matching output for both 3 and 0x83.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…authorization

# Conflicts:
#	key-wallet/src/transaction_checking/mod.rs
Dash enables OP_CAT (DIP-0020), so an input script with other opcodes can build the owner's key or signature at run time. Such an input isn't relayed, but it can be mined, so the verifier can't say it fails. verify_input_authorization returned Ok(false) for it, which a caller reads as "can't succeed".

When an input script that isn't push-only lacks a plain push of the owner's key, or a pushed signature that verifies, the verifier now returns the new RuntimeAuthorization error. Push-only input scripts keep their verdicts.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rives

A spend seen in the mempool before the wallet knows the coin it claims marks that coin spent, at a peer's word. When the funding transaction arrives, each such claimant is now checked with verify_input_authorization. One that provably lacks the owner's signature for the funding output is dropped with everything built on it. The drop is reported as a sweep with the coin released, and the coin is credited.

A claimant is never dropped while the network has accepted it or anything built on it, that is while it or a descendant is mined, InstantSend-locked or ChainLocked. A descendant seen in a block counts even after its record is gone. An input the verifier can't evaluate keeps the claim.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…nature

A mempool transaction that spends a coin the wallet holds used to mark the coin spent and drop it from the balance on a peer's word alone. A peer could hide any coin by relaying an unsigned claim on it.

A new pending transaction that spends a coin the wallet holds or keeps aside is now ignored when an input lacks the owner's authorization for that coin. The same rule decides which spends seen before their funding are dropped once the funding arrives. An input lacks authorization when its script isn't push-only, or when verify_input_authorization returns Ok(false). Relay policy rejects any input script that isn't push-only, so no relayed or locked transaction carries one. Mined and InstantSend-locked transactions are taken as before, as are inputs of other script or signature hash types.

The drop indexes the wallet's records once and abandons every claim in one walk, so its cost stays linear in the wallet's history. Coins a dropped claim gave up are not reported as released when a block already spends them or the funding transaction itself does.

Tests sign their mempool spends of held coins with a new test_utils::sign_owned_inputs helper.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@manki-review

manki-review Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

This pull request is a draft, so Manki does not review it automatically. Marking it ready for review starts automatic reviews. Until then, someone with write access to this repository can tick a box below: Review this draft reviews the current commit once, Deeper review does so with more effort, and Review every push reviews it now and again after each push.


Not reviewed yet · head 8d7f9b2 · draft, not reviewed automatically

  • Deeper review
  • Review this draft
  • Review every push

@coderabbitai

coderabbitai Bot commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@PastaPastaPasta

Copy link
Copy Markdown
Member Author

Automated code review, head 920f1037945b7b315ad5da167769d814a30c40de: APPROVED WITH SUGGESTIONS. Two rounds of independent automated code review ran on this change; every finding below was fixed before push.

Round 1 (an earlier local head): REQUIRES CHANGES.

  • A pending claim whose input script wasn't push-only (just OP_NOP, for example) returned RuntimeAuthorization and was kept, so any peer could hide a coin with no signature. Fixed: on the pending paths, an input script that isn't push-only counts as unauthorized. Relay policy rejects such scripts, so no relayed or locked transaction carries one.
  • The refusal only covered coins in utxos. Fixed: it now covers coins the wallet keeps aside too (held_or_kept_aside).
  • The drop could report as released an outpoint that a block had already spent, or that the funding transaction itself spends. Fixed: both are now filtered out.
  • Fully qualified paths and long doc comments. Fixed.

Round 2 (2c7c11358a): APPROVED WITH SUGGESTIONS.

  • A peer could make the drop quadratic in the wallet's history while the wallet is locked. Fixed: the records are indexed once, settled descendants are found through one parent-to-children map, and all claims are abandoned in one walk.
  • A malleated copy of a real signature, such as <sig> <key> OP_NOP, counted as authorized. Fixed by the push-only rule above.
  • The drop refreshed the balance before the caller took its snapshot. Fixed: the drop no longer refreshes it, and the caller does at the end.
  • Fixed: imports, the TransactionsSwept doc, and new tests for a CoinJoin ALL|ANYONECANPAY spend and a coin a signed claim holds.
  • Left as notes in the description: a re-checked funding gaining an output, a refused transaction's later lock, and reusing TransactionsSwept for the drop.

Fail-first: each part of the fix was removed in turn (nine mutations), and the tests failed each time. The head passes cargo fmt --all --check, cargo doc -D warnings, contrib/run_clippy.py, and the CI wallet and spv groups.


🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta.

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.51553% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.71%. Comparing base (09e4087) to head (8d7f9b2).
⚠️ Report is 21 commits behind head on dev.

Files with missing lines Patch % Lines
...et/src/transaction_checking/spend_authorization.rs 95.98% 10 Missing ⚠️
...y-wallet/src/wallet/managed_wallet_info/helpers.rs 98.70% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #1523      +/-   ##
==========================================
+ Coverage   86.78%   87.71%   +0.93%     
==========================================
  Files         270      271       +1     
  Lines       71309    72168     +859     
==========================================
+ Hits        61883    63300    +1417     
+ Misses       9426     8868     -558     
Flag Coverage Δ
core 83.60% <97.49%> (+1.38%) ⬆️
rpc 83.60% <97.49%> (+1.38%) ⬆️
spv 95.06% <100.00%> (+0.07%) ⬆️
wallet 83.60% <97.49%> (+1.38%) ⬆️
Files with missing lines Coverage Δ
dash-spv/src/sync/mempool/sync_manager.rs 99.53% <100.00%> (+<0.01%) ⬆️
dash/src/blockdata/constants.rs 100.00% <ø> (ø)
dash/src/crypto/sighash.rs 88.75% <100.00%> (+0.92%) ⬆️
key-wallet-manager/src/events.rs 68.59% <ø> (ø)
key-wallet-manager/src/process_block.rs 95.76% <ø> (ø)
key-wallet-manager/src/test_helpers.rs 100.00% <100.00%> (ø)
.../src/managed_account/managed_core_funds_account.rs 89.83% <100.00%> (+0.06%) ⬆️
...wallet/src/transaction_checking/account_checker.rs 67.36% <ø> (ø)
...-wallet/src/transaction_checking/wallet_checker.rs 98.68% <100.00%> (+0.01%) ⬆️
...y-wallet/src/wallet/managed_wallet_info/helpers.rs 81.25% <98.70%> (+6.43%) ⬆️
... and 1 more

... and 19 files with indirect coverage changes

PastaPastaPasta added a commit that referenced this pull request Nov 25, 2026
…ture

Merges fix/pending-spend-authorization at 920f103 (built on #1416 at f134961).

Conflict resolutions:
- key-wallet-manager events.rs and process_block.rs: R9's wording for the drop trigger combined with #1507's `winner_block` field; R9's event test reads `winner_block: None`.
- key-wallet helpers.rs: #1518/#1519's dismissal and give-back helpers and R9's authorization helpers are kept side by side; imports are merged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to dashpay/platform that referenced this pull request Nov 25, 2026
The pin adds rust-dashcore's DismissalsChanged wallet event (dashpay/rust-dashcore#1522) and the pending-spend authorization check (dashpay/rust-dashcore#1523). Platform's matches over wallet events treat DismissalsChanged as an event that carries no records, spends, balance or payment evidence, and the changeset adapter stores nothing for it yet.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to dashpay/platform that referenced this pull request Nov 25, 2026
rust-dashcore now ignores a pending spend of a coin the wallet holds unless the coin's owner signed it (dashpay/rust-dashcore#1523). The tests that feed the wallet such a spend from the mempool now sign it with key-wallet's test signer, so they still exercise what they assert.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A script with opcodes besides pushes can build the owner's signature or key at run time, for example with `OP_CAT`, and a miner can include such a transaction even though relay policy rejects it. The verifier's `Ok(false)` is no proof of a missing signature for such a script, so refusing it, or dropping it once its coin's funding arrives, could give back a coin a valid spend still uses.

`lacks_authorization` now counts only proof: an input whose script doesn't parse or exceeds `MAX_SCRIPT_SIZE`, a transaction that spends the same outpoint twice, or a push-only script without the owner's signature. Each of the first three makes the transaction invalid whatever its scripts compute. Any other input keeps the claim and the coin it spends, as an input the verifier can't judge already does. The refusal of a new spend of a held coin and the drop of a recorded spend share this predicate.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit that referenced this pull request Nov 25, 2026
… push-only

Takes #1523's 8d7f9b2. A pending claim on a held coin is dropped only when it provably lacks the owner's authorization: its script doesn't parse or exceeds MAX_SCRIPT_SIZE, it spends the outpoint twice, or it is push-only and fails verification. Any other script keeps the coin spent.

helpers.rs keeps e058d4c's sweep dismissal of a dropped claim beside the new predicate and its docs.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
PastaPastaPasta added a commit to dashpay/platform that referenced this pull request Nov 25, 2026
Takes rust-dashcore's pending-spend authorization (dashpay/rust-dashcore#1523 at 8d7f9b22ca): a pending spend of a held coin is ignored unless it may carry the owner's authorization. Test fixtures sign their pending spends again with key_wallet::test_utils::sign_owned_inputs. The pin also carries the per-set dismissal reports of dashpay/rust-dashcore#1522.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added the merge-conflict The PR conflicts with the target branch. label Nov 27, 2026
@github-actions

Copy link
Copy Markdown
Contributor

This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-conflict The PR conflicts with the target branch.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants