Repository navigation
fix(key-wallet): a pending spend needs its owner's signature - #1523
PastaPastaPasta wants to merge 19 commits into
Conversation
`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]>
|
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
|
|
Important Draft PR not reviewedDraft PRs are not automatically reviewed by default.
To automatically review draft PRs, update your CodeRabbit configuration: reviews:
auto_review:
drafts: true
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. |
|
Automated code review, head Round 1 (an earlier local head): REQUIRES CHANGES.
Round 2 (
Fail-first: each part of the fix was removed in turn (nine mutations), and the tests failed each time. The head passes 🤖 Posted autonomously by Claude (claude-opus-5-5) on behalf of pasta. |
Codecov Report❌ Patch coverage is
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
|
…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]>
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]>
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]>
… 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]>
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]>
|
This PR has merge conflicts with the base branch. Please rebase or merge the base branch into your branch to resolve them. |
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_authorizationnow returns the newSpendAuthorizationError::RuntimeAuthorizationinstead ofOk(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 withOP_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 returnsOk(false). A script that isn't push-only is never counted, even when the verifier returnsOk(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:TransactionsSweptsweep, withsuperseded_byset 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.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::verifychecked against its quorum. Ondevthat 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 ofWalletInterfacemust pass the same verified evidence.Not covered:
OP_NOP. It holds the coin until it is abandoned or a rival spend settles.ALL, are taken as before. This includesALL|ANYONECANPAY, as used by CoinJoin, so a mixing participant who holds such a signature can still hold a coin back until the mix settles.spent_claimswithout a record is never checked.TransactionsSweptas a spend beaten by a final rival should handle the dropped-claim case, wheresuperseded_byis the funding and the claimant's inputs are given back.Tests:
pending_spend_authorization_testscovers:OP_CAT, and a valid or invalid signature and key followed byOP_NOP, among them), and three structurally invalid inputs that are dropped (a script that doesn't parse, one overMAX_SCRIPT_SIZE, an outpoint spent twice);ALL|ANYONECANPAYas in CoinJoin, unsigned, signed for another coin, the same six scripts that aren't push-only (taken), the same three structural invalidities (ignored), mined withOP_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.OP_CATcases.Ok(false), which counts for a push-only script and not for one with other opcodes.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:
Ok(false);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 warningsfor key-wallet, key-wallet-manager and dash-spv (default and all features),cargo docwith-D warningsfor the wallet crates, and thekey-walletandkey-wallet-managertests (default and all features) pass on this branch; dash-spv's tests compile.🤖 Generated with Claude Code