Skip to content

fix(key-wallet): stop counting a contact's watch-only coins as the wallet's - #926

Merged
QuantumExplorer merged 1 commit into
devfrom
fix/dashpay-external-not-funding
Aug 7, 2026
Merged

fix(key-wallet): stop counting a contact's watch-only coins as the wallet's#926
QuantumExplorer merged 1 commit into
devfrom
fix/dashpay-external-not-funding

Conversation

@romchornyi

@romchornyi romchornyi commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Problem

ManagedAccountCollection::all_funding_accounts includes dashpay_external_accounts.

A DashPay external account is watch-only by construction — the crate's own doc comment on the variant says so (managed_account_type.rs: "DashPay external (watch-only) account"). Its addresses derive from a contact's xpub, so this wallet can observe those outputs but can never sign for them. They are the contact's coins; this wallet only ever pays into them.

Treating them as funding has two visible consequences.

The balance does not move after paying a contact. The amount leaves the standard account and reappears under the external one, because the payment output landed there. Observed on an iOS device after sending 0.01 DASH to a contact:

account_balances=[Standard{idx:0,BIP44}=>0.99587131 DASH, DashpayExternal{idx:0}=>0.01 DASH]

The external account climbs across successive payments (0.111 → 0.461 → 0.511 in one session). To a user this reads as "my balance is wrong", or worse, "the send did not happen".

Unspendable outputs reach coin selection. utxos() feeds get_spendable_utxos(), so selection can pick an output the wallet has no key for and fail at signing time with funds apparently available.

What changed

dashpay_external_accounts is dropped from all_funding_accounts and all_funding_accounts_mut. That covers every consumer at once — account_balances, balance/update_balance, utxos, get_spendable_utxos, immature_transactions, matured_coinbase_records.

dashpay_receival_accounts stays: those addresses derive from our xpub and a contact pays into them, so the funds are genuinely ours.

The doc comment now states the exclusion and the reason, so the line does not get "fixed" back.

What deliberately did not change

The wallet still sees these transactions.

  • Detection runs through transaction_checking, whose AccountTypeToCheck list still includes DashpayExternalAccount (transaction_router/mod.rs, account_checker.rs), so a send to a contact is still routed and recorded.
  • The monitored-address / compact-filter set is built from all_accounts, not all_funding_accounts, and still includes the external account.
  • all_accounts / all_accounts_mut are untouched, so chain-lock promotion, transaction history and instant-lock marking keep working per-account.

Only balance and UTXO aggregation change.

Alternatives considered

  • Filtering in update_balance / get_spendable_utxos only — strictly worse: account_balances() and raw utxos() would keep reporting the contact's coins as the wallet's, i.e. the same bug on a different surface.
  • Adding a watch-only flag to the managed layer — unnecessary machinery. is_watch_only exists on the immutable Account but not on ManagedCoreFundsAccount; the fact is already encoded in the account type, which is what the aggregation matches on anyway.

Testing

cargo test -p key-wallet — 604 passed, 0 failed. No existing test encoded the old behaviour.

New regression test external_dashpay_accounts_are_not_funding_accounts builds a collection with a standard, a receival and an external account and asserts the external one is absent from the funding view while the receival one is present, that the mutable view visits the same set, and that the account itself still exists in the collection. Verified to fail without the fix ("the contact's watch-only account must not count as this wallet's funds").

cargo clippy -p key-wallet --all-features --all-targets -- -D warnings and cargo fmt --check are clean.

Summary by CodeRabbit

  • Bug Fixes
    • Funding account views now exclude DashPay external accounts while continuing to include DashPay receival accounts.
    • DashPay external accounts remain securely stored and monitored, but no longer appear in funding account lists.
    • Improved consistency between read-only and editable funding account views.

…llet's

`all_funding_accounts` included `dashpay_external_accounts`. A DashPay
external account is watch-only by construction — its addresses derive
from a *contact's* xpub, so this wallet can observe those outputs but can
never sign for them. They are the contact's coins, and this wallet only
ever pays into them.

Counting them as funding had two visible consequences.

The wallet balance did not move after paying a contact: the amount left
the standard account and reappeared under the external one, because the
payment output landed there. Observed on device — after sending 0.01
DASH, `account_balances=[Standard{idx:0,BIP44}=>0.99587131 DASH,
DashpayExternal{idx:0}=>0.01 DASH]`, with the external account climbing
across successive payments (0.111 → 0.461 → 0.511). To a user this reads
as "my balance is wrong", or worse, "the send did not happen".

And `utxos()` fed `get_spendable_utxos()`, so coin selection could pick
an output the wallet cannot sign for, failing at signing time with funds
apparently available.

Receival accounts stay: those addresses derive from our own xpub and a
contact pays into them, so the funds are genuinely ours.

This does not stop the wallet seeing those transactions. Detection runs
through `transaction_checking`, whose `AccountTypeToCheck` list still
includes `DashpayExternalAccount`, and the monitored-address /
compact-filter set is built from `all_accounts`, which also still
includes it. Only balance and UTXO aggregation change.
@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: edf487d8-2ede-4567-969b-5f63d3817a1a

📥 Commits

Reviewing files that changed from the base of the PR and between 712bab1 and d521873.

📒 Files selected for processing (1)
  • key-wallet/src/managed_account/managed_account_collection.rs

📝 Walkthrough

Walkthrough

The funding-account iterators now exclude DashPay external accounts. They continue to include standard and DashPay receival accounts. Tests verify both iterator variants and confirm that external accounts remain stored.

Changes

Funding account scope

Layer / File(s) Summary
Funding iterators and coverage
key-wallet/src/managed_account/managed_account_collection.rs
The immutable and mutable funding iterators exclude dashpay_external_accounts. Documentation identifies these accounts as watch-only. Tests verify included accounts, excluded accounts, and collection retention.

Estimated code review effort: 2 (Simple) | ~10 minutes

Suggested reviewers: xdustinface, zocolini

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: excluding contact-owned watch-only coins from wallet funding calculations.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/dashpay-external-not-funding

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Aug 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 75.04%. Comparing base (712bab1) to head (d521873).

Additional details and impacted files
@@            Coverage Diff             @@
##              dev     #926      +/-   ##
==========================================
+ Coverage   74.95%   75.04%   +0.09%     
==========================================
  Files         328      328              
  Lines       76884    76933      +49     
==========================================
+ Hits        57626    57738     +112     
+ Misses      19258    19195      -63     
Flag Coverage Δ
core 77.29% <ø> (ø)
ffi 50.13% <ø> (-0.01%) ⬇️
rpc 20.00% <ø> (ø)
spv 91.28% <ø> (+0.03%) ⬆️
wallet 76.07% <100.00%> (+0.32%) ⬆️
Files with missing lines Coverage Δ
.../src/managed_account/managed_account_collection.rs 62.75% <100.00%> (+6.35%) ⬆️

... and 6 files with indirect coverage changes

@github-actions github-actions Bot added the ready-for-review CodeRabbit has approved this PR label Aug 6, 2026
@QuantumExplorer
QuantumExplorer merged commit 2b1f744 into dev Aug 7, 2026
37 of 38 checks passed
@QuantumExplorer
QuantumExplorer deleted the fix/dashpay-external-not-funding branch August 7, 2026 07:10
bfoss765 added a commit to bfoss765/platform that referenced this pull request Aug 9, 2026
… the persisted transaction row

dashpay/rust-dashcore#926 established that a `DashpayExternalAccount` is
watch-only by construction — its addresses derive from a contact's xpub,
so they are the contact's coins and this wallet only ever pays into them
— and removed those accounts from `all_funding_accounts` so they stop
counting toward balance and UTXO aggregation.

The persistence seam is the same rule's second home, and it was missed.

Upstream `check_core_transaction` emits one `TransactionRecord` per
matched account, so a payment to a contact produces two records sharing
one txid: the funding account's (`Outgoing`, `net = change - spent`) and
the external account's (`Incoming`, `net = +paid`). `build_core_changeset`
projected both into `CoreChangeSet.records`, and `derive_new_utxos` turned
the contact's output into a wallet UTXO. Because the persisted
`transactions` row is keyed by txid alone — the
`transaction_account_involvements` table is only written for provider-key
accounts, so there is no per-account dimension to disambiguate — the
watch-only record defined the stored row. Field capture on testnet: a
0.69998912 DASH payment away was persisted as `direction=incoming`,
`netAmount=+69998912` instead of `-70000000`, and the paid output sat in
`txos` with `isSpent=0` indefinitely, inflating any SQL-sum balance.

Records owned by an external account are now excluded from the
persist-time projection: no transaction row, no new TXO. The funding
account's record — already correct — becomes the row that lands.

Everything genuinely ours from the same event is preserved: address-used
flips and highest-used watermarks (so contact address rotation keeps
working), derived-address rows, and `derive_spent_utxos`, which stays
unfiltered so a contact spending an output persisted by a pre-fix build
still clears the stale row.

Eight regression tests cover the record pair a real contact payment
produces, the standalone first-sighting path, the confirmation re-emit,
a genuine receive, a DashPay *receival* account receive (the boundary
dashpay#926 drew, which must stay incoming/positive), and an internal transfer.
The four fix-dependent ones were verified to fail without the change.
QuantumExplorer added a commit that referenced this pull request Aug 11, 2026
…es (#952)

A DashpayExternalAccount derives its addresses from the contact's xpub,
so its coins are the contact's, never this wallet's. That policy now has
two enforcement sites — balance/UTXO aggregation dropped the accounts
from all_funding_accounts (#926), and dashpay/platform#4363 filters the
same records out of its persistence projection — but each site hardcodes
its own account-type list, which can silently drift when a new
contact-owned account type is added.

Give the policy one canonical home: AccountType::is_contact_owned(),
with an exhaustive match so a new account type cannot compile without
deciding whether its coins are the wallet's or a contact's, plus a
delegating ManagedAccountType::is_contact_owned(). The #926 funding-
scope test now asserts all_funding_accounts agrees with the predicate.

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
QuantumExplorer pushed a commit to dashpay/platform that referenced this pull request Aug 11, 2026
… the persisted transaction row

dashpay/rust-dashcore#926 established that a `DashpayExternalAccount` is
watch-only by construction — its addresses derive from a contact's xpub,
so they are the contact's coins and this wallet only ever pays into them
— and removed those accounts from `all_funding_accounts` so they stop
counting toward balance and UTXO aggregation.

The persistence seam is the same rule's second home, and it was missed.

Upstream `check_core_transaction` emits one `TransactionRecord` per
matched account, so a payment to a contact produces two records sharing
one txid: the funding account's (`Outgoing`, `net = change - spent`) and
the external account's (`Incoming`, `net = +paid`). `build_core_changeset`
projected both into `CoreChangeSet.records`, and `derive_new_utxos` turned
the contact's output into a wallet UTXO. Because the persisted
`transactions` row is keyed by txid alone — the
`transaction_account_involvements` table is only written for provider-key
accounts, so there is no per-account dimension to disambiguate — the
watch-only record defined the stored row. Field capture on testnet: a
0.69998912 DASH payment away was persisted as `direction=incoming`,
`netAmount=+69998912` instead of `-70000000`, and the paid output sat in
`txos` with `isSpent=0` indefinitely, inflating any SQL-sum balance.

Records owned by an external account are now excluded from the
persist-time projection: no transaction row, no new TXO. The funding
account's record — already correct — becomes the row that lands.

Everything genuinely ours from the same event is preserved: address-used
flips and highest-used watermarks (so contact address rotation keeps
working), derived-address rows, and `derive_spent_utxos`, which stays
unfiltered so a contact spending an output persisted by a pre-fix build
still clears the stale row.

Eight regression tests cover the record pair a real contact payment
produces, the standalone first-sighting path, the confirmation re-emit,
a genuine receive, a DashPay *receival* account receive (the boundary
#926 drew, which must stay incoming/positive), and an internal transfer.
The four fix-dependent ones were verified to fail without the change.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-for-review CodeRabbit has approved this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants