Skip to content

fix(identity): stop identity refreshes and key adds from dropping saved keys - #1010

Merged
lklimek merged 15 commits into
feat/derived-identity-keyfrom
fix/identity-key-data-loss
Sep 25, 2026
Merged

lklimek merged 15 commits into
feat/derived-identity-keyfrom
fix/identity-key-data-loss

Conversation

@Claudius-Maginificent

@Claudius-Maginificent Claudius-Maginificent commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

TL;DR: Identity refreshes and key additions no longer delete identity keys saved on this device. If a key reaches the network but cannot be saved here, the screen keeps it available to copy.

User story

As an identity owner, I want keys I added by hand to stay on this device, so that a wallet unlock, an identity refresh or a second key addition does not delete them.

Scenario

Base flow

A wallet-owned identity has a key added by entering its private key, which may be password-protected. The wallet is unlocked, the app restarts, or the identity is loaded again from the wallet.

Actual behavior

  • Automatic discovery and "load from wallet" delete manual keys. When the wallet is unlocked or the app starts, automatic identity discovery rewrites the stored identity with only the keys the wallet can derive. "Load from wallet" does the same. Keys added by hand, including password-protected ones, disappear from the identity and their stored secrets become orphaned.
  • Adding a key can erase other keys. After broadcasting, Add Key saves the copy of the identity it read before broadcasting. Any key another operation saved in the meantime is erased (discovery, load, password protection, refresh, alias edit).
  • Only one save failure is reported properly. If the key reached the network but saving it here failed, only the sealing error was reported as "added but not saved". Other failures showed raw errors. The advice to "add a key again" would create a second key on the network, and a randomly generated private key could be lost.

Expected behavior

  • Discovery and "load from wallet" keep every stored key they do not recreate. Protection is unchanged, with no downgrade.
  • Adding a key updates the current stored identity and adds only the new key.
  • Every failure after a successful broadcast is reported as "The key is on the network but was not saved here". The Add Key screen keeps the private key available, masked, with Copy private key, until the user leaves.

Detailed discussion

  • SEC-102 — discovery and "load from wallet":
    • KeyStorage::retain_local_keys_from (pure, in model/) merges the stored record into the wallet-only rebuild. Stored keys the rebuild lacks are kept.
    • On a slot conflict, the stored entry wins unless it is only a wallet derivation path, which the rebuild refreshes.
    • store_discovered_identity applies the merge under the existing per-identity record lock, only while the identity is listed. A record left by a half-finished removal is still replaced.
    • Unload markers are checked first, unchanged.
    • load_identity_from_wallet now goes through store_discovered_identity(UserRequested) instead of its own unlocked read-then-write.
  • SEC-101 — add key: persist_added_identity_key re-reads the record under identity_record_lock and applies only the post-broadcast on-chain identity and the one new key. The Tier-2 seal happens on that fresh record. The pre-broadcast snapshot is never written back.
  • SEC-104 — failures after the broadcast:
    • Every failure after the broadcast maps to IdentityKeyAddedButNotSaved { source }; an unloaded identity keeps IdentityKeyAddedButIdentityUnloaded.
    • Both messages were reworded so that neither suggests adding the key again.
    • AddKeyScreen gets a KeyOnNetworkNotSaved state with the masked key, Copy private key and Back to Identities.
    • A wallet-derived key (feat(identity): add wallet-derived keys to existing identities #980) has no private key to copy. It gets DerivedIdentityKeyAddedButNotSaved / DerivedIdentityKeyAddedButIdentityUnloaded and a DerivedKeyOnNetworkNotSaved screen state that tells the user to load the identity from the wallet again.
    • After the broadcast, a derived key is filed on the fresh record through KeyStorage::insert_wallet_derived, which never replaces a stored private key or another key.
  • Behavior change: an identity with mixed protection (some keys password-protected, some not) now fails closed during a discovery refresh with IdentityKeyProtectionDowngrade. Its stored record stays untouched. This matches what an alias edit already does; before, discovery silently dropped those keys.

Testing

New tests; the five marked ✱ failed before the fix:

  • a_wallet_rebuild_keeps_every_stored_key_it_did_not_recreate ✱
  • a_stored_derivation_path_does_not_displace_a_rebuilt_private_key
  • automatic_discovery_keeps_manual_and_protected_keys ✱
  • automatic_discovery_still_refuses_an_unloaded_identity
  • persisting_an_added_key_keeps_what_others_saved_during_the_broadcast ✱
  • a_protect_during_the_broadcast_reports_the_key_as_not_saved ✱
  • an_occupied_slot_after_the_broadcast_reports_the_key_as_not_saved ✱
  • only_post_broadcast_save_failures_keep_the_private_key_on_screen
  • a_stale_stored_secret_is_kept_when_the_on_chain_key_differs
  • Derived keys: derived_post_broadcast_failure_never_asks_to_copy_a_private_key, persisting_a_derived_key_keeps_what_others_saved_during_the_broadcast, an_occupied_slot_reports_the_derived_key_as_not_saved, an_unload_during_the_broadcast_reports_the_derived_key_as_unloaded, a_wallet_derived_key_never_displaces_a_held_or_foreign_entry, derived_key_not_saved_offers_nothing_to_copy

Test runs:

  • After stacking on feat(identity): add wallet-derived keys to existing identities #980: 117 focused tests pass (add key backend/screen, key storage, identity DB, derived-key modules); full Clippy --all-features --all-targets -D warnings passes.
  • Before stacking: 290 tests across the identity backend, identity DB, key storage, Add Key screen and migration modules pass, plus 115 wallet-lifecycle tests.
  • Focused Clippy with -D warnings passes, and so does cargo fmt --check.
  • No manual GUI run.

Known limitations (pre-existing, not changed here)

  • If writing the record fails after a successful Tier-2 seal, the sealed secret stays in the vault without a reference. The private key is still on screen.
  • If the user leaves Add Key during the broadcast, only the global banner explains the situation; the key panel is not shown.
  • For a protected identity, re-entering the private key from Key Info is blocked by the protection-downgrade guard.

Prior work

Found during a data-loss audit of #980. The lost-keys issue in automatic discovery was first noted during #925. Stacked on #980 (base feat/derived-identity-key): merge #980 first, then retarget this PR to v1.0-dev. Post-broadcast persistence covers both manual and wallet-derived keys. It overlaps lightly with #976 in identity_db.rs (store_discovered_identity only) and error.rs (message text only).

🤖 Co-authored by Claudius the Magnificent AI Agent

Summary by CodeRabbit

  • New Features

    • Add identity keys directly from a wallet using a guided key-slot selector.
    • Automatically choose available wallet slots and support compatible key types.
    • Verify wallet-derived keys before adding them, including recovery-phrase validation.
    • Show whether wallet-derived keys are protected by the wallet or identity password.
  • Bug Fixes

    • Preserve manually added identity keys during refreshes and wallet rediscovery.
    • Prevent locally unsaved keys from being lost; provide recovery guidance and private-key copying where applicable.
    • Refresh key-slot options after network changes or rejected selections.

@coderabbitai

coderabbitai Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

🗂️ Base branches to auto review (2)
  • master
  • v1.0-dev

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository: dashpay/dash-evo-tool/.coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: 356923e7-77a8-47e1-9627-0e13daa603b7

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
📝 Generate docstrings
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR

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

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

@github-actions

github-actions Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

PR Hygiene: the checklist is in the description.

@github-actions github-actions Bot added the waiting-bots Waiting for the review bots to report on this head label Sep 21, 2026
@thepastaclaw

thepastaclaw commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

⛔ Blockers found — Phase 2 deferred (commit d6c2486) · triage: critical
Canonical validated blockers: 1

@github-actions

Copy link
Copy Markdown
Contributor

@coderabbitai review

No review for ddd83d45 yet, so PR Hygiene is asking once. If nothing arrives, the requirement is dropped for this commit and the pull request is labelled bot-review-skipped.

@github-actions

Copy link
Copy Markdown
Contributor

@thepastaclaw review

No review for ddd83d45 yet, so PR Hygiene is asking once. If nothing arrives, the requirement is dropped for this commit and the pull request is labelled bot-review-skipped.

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ DEGRADED — Final validation — Phase 1 + Phase 2

⚠️ DEGRADED review. The primary review models were unavailable (gpt-6-astra unavailable: All credentials for model gpt-6-astra are cooling down (last error: usage_limit_reached: The usage limit has been reache), so this review ran on stand-in models: gpt-5.6-luna → muse-spark-1.3-contributor, gpt-5.6-sol → muse-spark-1.3-contributor, gpt-5.6-terra → muse-spark-1.3-contributor, gpt-6-astra → muse-spark-1.3-contributor. Both review phases and the independent verifiers still ran, but on weaker models, with Phase 1 capped at high effort. Treat the verdict as provisional; a full-strength re-review will run on the next push once the primary models are back.

This PR fixes real identity-key data loss (discovery/load merges, guarded add-key persist, typed post-broadcast errors) with solid tests. Remaining in-scope concerns are suggestions only: the advertised mixed-protection discovery refusal has no regression test, the discovery merge pins stale disabled_at metadata, the rescue key stays editable, and the wrapper recovery message dead-ends for two wrapped causes.

🟡 4 suggestion(s)

1 finding(s) not shown inline (the lines are not part of this PR's diff)

🟡 Suggestion: Added-but-not-saved advice dead-ends for two wrapped causes
src/backend_task/error.rs:658-674

This PR widens IdentityKeyAddedButNotSaved to wrap every post-broadcast failure and rewrites its Display to advise: refresh the identity, open the new key and enter its private key to save it here. That exact path is refused for two of the wrapped causes: a protection-downgrade refusal (Key Info re-entry is blocked by the downgrade guard, as the PR's own known-limitations note admits) and an occupied slot (insert_non_encrypted returns IdentityKeySlotOccupied until the other saved half is removed). The accurate remedies live only in the nested source message shown in details. Hedge the wrapper text so it does not promise a step that fails — e.g. refresh the identity and try to save the key from its key screen — or split variants so the protected/occupied cases carry their accurate instructions.

source: glm-5.3-flash (phase1-reviewer: general, rust-quality)

Review provenance

Source: reviewer 1: glm-5.3-flash (agent: phase1-reviewer, role: general); reviewer 2: glm-5.3-flash (agent: phase1-reviewer, role: rust-quality); reviewer 3: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: phase2-reviewer, role: general); reviewer 4: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: phase2-reviewer, role: rust-quality); final verifier: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: astra-verifier, role: final-verifier)

  • Degraded mode: gpt-6-astra unavailable: All credentials for model gpt-6-astra are cooling down (last error: usage_limit_reached: The usage limit has been reache (detected by probe, since 2026-09-20T20:45:34Z); stand-ins gpt-5.6-luna → muse-spark-1.3-contributor, gpt-5.6-sol → muse-spark-1.3-contributor, gpt-5.6-terra → muse-spark-1.3-contributor, gpt-6-astra → muse-spark-1.3-contributor; Phase 1 effort capped at high
  • Triage: critical by muse-spark-1.3-contributor (standing in for gpt-6-astra) (effort low) — Large (+722/-93) intricate persistence/concurrency fix that directly changes private-key handling via KeyStorage::retain_local_keys_from and persist_added_identity_key.
  • Phase 1 reviewers: glm-5.3-flash — general (completed, effort high); agent phase1-reviewer, glm-5.3-flash — rust-quality (completed, effort high); agent phase1-reviewer
  • Phase 1 model: glm-5.3-flash — zai quota: 5h 92% left, weekly 89% left; passed over gemini-3.8-flash-high (lane failed)
  • Fresh verifier: muse-spark-1.3-contributor (standing in for gpt-6-astra) — final-verifier; agent astra-verifier
  • Phase 2 reviewers: muse-spark-1.3-contributor (standing in for gpt-6-astra) — general (completed, effort xhigh); agent phase2-reviewer, muse-spark-1.3-contributor (standing in for gpt-6-astra) — rust-quality (completed, effort xhigh); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `src/context/identity_db.rs`:
- [SUGGESTION] src/context/identity_db.rs:2185-2193: Mixed-protection discovery refusal has no regression test
  The PR description advertises a behavior change: a discovery merge producing a record with both Tier-2 keys and resident plaintext now fails closed with IdentityKeyProtectionDowngrade via encode_identity_blob_vault_first, leaving the stored record untouched. No test stages that mixed record and calls store_discovered_identity with DiscoveryIntent::Automatic asserting the Downgrade error plus a byte-identical stored record. The existing discovery test seals every key (write succeeds), the encode-guard unit test never reaches the discovery merge, and the discovery path swallows the error into a tracing::warn with no banner — so the exact refusal, the untouched-record invariant, and the scan-continues behavior are unpinned against future refactors of the encode guard or merge order.

In `src/model/qualified_identity/encrypted_key_storage.rs`:
- [SUGGESTION] src/model/qualified_identity/encrypted_key_storage.rs:667-685: Discovery merge pins stale disabled_at on held keys
  retain_local_keys_from lets a stored entry replace the rebuild's entry wholesale whenever the stored half holds private material. Before this PR a discovery rebuild refreshed the whole key map from the fetched on-chain identity; after it, a held stored half pins its saved public-key snapshot indefinitely. disabled_at is the one field Platform lets move after a key is added (same_key deliberately ignores it), so a key disabled on-chain elsewhere keeps disabled_at: None in the stored wrapper and direct available_withdrawal_keys consumers keep offering a key whose transitions fail on-chain. The main withdrawal resolver cross-checks the fresh identity object so signing stays safe, but the staleness is deterministic on every discovery, not a race. For same-key entries keep the stored private half but refresh disabled_at from the rebuild's entry; keep the warn-and-keep path for genuinely differing keys.

In `src/ui/identity/keys/add_key_screen.rs`:
- [SUGGESTION] src/ui/identity/keys/add_key_screen.rs:341-378: Rescue private key lives in a still-editable input
  KeyOnNetworkNotSaved is a unit variant with no payload, so the only copy of the on-chain key remains self.private_key_input, which show_key_not_saved renders with PasswordInput::show — an editable TextEdit with reveal. The Copy button copies whatever text is currently in the widget, so a stray keystroke after the failure silently changes the rescued secret and the user copies the wrong key. Snapshot the key text into the status state (or a dedicated field) at failure time, or render the rescue view read-only, so the copy source is independent of later widget mutation.

In `src/backend_task/error.rs`:
- [SUGGESTION] src/backend_task/error.rs:658-674: Added-but-not-saved advice dead-ends for two wrapped causes
  This PR widens IdentityKeyAddedButNotSaved to wrap every post-broadcast failure and rewrites its Display to advise: refresh the identity, open the new key and enter its private key to save it here. That exact path is refused for two of the wrapped causes: a protection-downgrade refusal (Key Info re-entry is blocked by the downgrade guard, as the PR's own known-limitations note admits) and an occupied slot (insert_non_encrypted returns IdentityKeySlotOccupied until the other saved half is removed). The accurate remedies live only in the nested source message shown in details. Hedge the wrapper text so it does not promise a step that fails — e.g. refresh the identity and try to save the key from its key screen — or split variants so the protected/occupied cases carry their accurate instructions.
Out-of-scope follow-up suggestions (1)

These are valid observations, but they are outside this PR's scope and should be handled in separate issues or author/maintainer-requested PRs rather than blocking this review.

  • refresh_identity retains the lost-update window fixed elsewhere here — refresh_identity loads the record, awaits a Platform fetch, then writes its stale snapshot back via update_local_qualified_identity, so a key, alias, or protection change saved mid-fetch is lost — the same SEC-101 class this PR closes for discovery and add-key. Pre-existing and untouched here, but the same defect class.
    • Follow-up: Route refresh_identity (and refresh_loaded_identities_dpns_names) through a guarded read-modify-write under identity_record_lock in a separate issue/PR.

Comment thread src/context/identity_db.rs Resolved
Comment thread src/ui/identity/keys/add_key_screen.rs Resolved
@github-actions github-actions Bot added the bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. label Sep 22, 2026
@lklimek lklimek added the claudius-review Triggers automated code review using claudius plugin, runs as a CI job label Sep 22, 2026
lklimek and others added 3 commits September 22, 2026 09:23
…ed keys

Wallet discovery (boot/unlock) and "load from wallet" rebuilt an identity
from wallet-derivable keys only and replaced the stored record, so manual
keys - including password-protected Tier-2 ones - vanished and their vault
secrets were orphaned. store_discovered_identity now folds the stored
record's keys into the rebuild under the record lock
(KeyStorage::retain_local_keys_from): a held private half is never replaced
by a derivation path, so protection is never downgraded. Delisted records
(half-finished removals) are still replaced, and the unload marker check is
unchanged. load_identity_from_wallet routes through the same guarded store.

Add-key no longer writes its pre-broadcast snapshot back: under the record
lock it re-reads the stored record and applies only the refreshed on-chain
identity and the new key entry, so keys, aliases or protection saved
during the broadcast survive.

Every post-broadcast failure now maps to IdentityKeyAddedButNotSaved (or
IdentityKeyAddedButIdentityUnloaded), and the Add Key screen switches to a
"save your new private key" state with a copy button instead of letting a
generated key be lost.

Fixes audit findings SEC-101, SEC-102, SEC-104.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…on-chain key

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…t asking to copy it

Stacking the post-broadcast persist fixes onto wallet-derived identity keys
left a derived key's "added but not saved" outcome asking the user to copy
a private key that does not exist.

- Add DerivedIdentityKeyAddedButNotSaved and
  DerivedIdentityKeyAddedButIdentityUnloaded; the persist step picks them
  when the new key has no private half.
- The Add Key screen gets a DerivedKeyOnNetworkNotSaved state with no key
  field and no copy button; it says the key can be saved again by loading
  the identity from the wallet.
- Tests: derived persist onto the fresh record keeps concurrent writes,
  occupied slot and unload map to the derived variants,
  KeyStorage::insert_wallet_derived keeps a held private half, and the
  screen offers nothing to copy for a derived key.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@lklimek
lklimek force-pushed the fix/identity-key-data-loss branch from ddd83d4 to ad7b170 Compare September 22, 2026 09:27
@Claudius-Maginificent
Claudius-Maginificent changed the base branch from v1.0-dev to feat/derived-identity-key September 22, 2026 09:28
@github-actions github-actions Bot removed waiting-bots Waiting for the review bots to report on this head bot-review-skipped A required review bot did not report; it was skipped by the window or by a person. labels Sep 22, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ DEGRADED — Re-review — Final validation — Phase 2 only (queue backlog)

⚠️ DEGRADED review. The primary review models were unavailable (gpt-6-astra unavailable: All credentials for model gpt-6-astra are cooling down (last error: usage_limit_reached: The usage limit has been reache), so this review ran on stand-in models: gpt-5.6-luna → muse-spark-1.3-contributor, gpt-5.6-sol → muse-spark-1.3-contributor, gpt-5.6-terra → muse-spark-1.3-contributor, gpt-6-astra → muse-spark-1.3-contributor. Both review phases and the independent verifiers still ran, but on weaker models, with Phase 1 capped at high effort. Treat the verdict as provisional; a full-strength re-review will run on the next push once the primary models are back.

Identity refreshes and key adds now preserve locally saved keys, with typed post-broadcast errors and a rescue screen. Four prior suggestions still apply at this head: static recovery advice that dead-ends for two causes, an editable rescue field, stale disabled_at pinning, and missing mixed-protection discovery test, plus one new nitpick on the bool-flag error mapper.

🟡 4 suggestion(s) | 💬 1 nitpick(s)

1 finding(s) not shown inline (the lines are not part of this PR's diff)

🟡 Suggestion: Added-but-not-saved advice dead-ends for two wrapped causes
src/backend_task/error.rs:709-725

IdentityKeyAddedButNotSaved wraps every post-broadcast failure but its Display gives one static recovery path: refresh the identity, open the new key and enter its private key. That path cannot succeed for two wrapped causes it explicitly names: an occupied slot re-fails KeyStorage::insert_non_encrypted with IdentityKeySlotOccupied on the same slot, and a protection-downgrade refusal is blocked on re-entry by the IdentityKeyProtectionDowngrade guard (acknowledged in the PR known limitations for protected identities). The cause distinction survives only in #[source] for logs while the user-facing text promises re-entry works. Split the recovery wording per cause or soften the static text so it does not assume re-entry works.

source: muse-spark-1.3-contributor (phase2-reviewer: general, rust-quality)

3 carried-forward finding(s) already raised on this PR; not re-posting as new inline comments.

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: phase2-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: phase2-reviewer, role: rust-quality); reviewer 3: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: phase2-reviewer, role: general); reviewer 4: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: phase2-reviewer, role: rust-quality); final verifier: muse-spark-1.3-contributor (standing in for gpt-6-astra) (agent: astra-verifier, role: final-verifier)

  • Degraded mode: gpt-6-astra unavailable: All credentials for model gpt-6-astra are cooling down (last error: usage_limit_reached: The usage limit has been reache (detected by probe, since 2026-09-20T20:45:34Z); stand-ins gpt-5.6-luna → muse-spark-1.3-contributor, gpt-5.6-sol → muse-spark-1.3-contributor, gpt-5.6-terra → muse-spark-1.3-contributor, gpt-6-astra → muse-spark-1.3-contributor; Phase 1 effort capped at high
  • Triage: critical by muse-spark-1.3-contributor (standing in for gpt-6-astra) (effort low) — Large 8-file (+1117) change rewrites identity private-key persistence/merging via persist_added_identity_key and KeyStorage::retain_local_keys_from.
  • Phase 1 reviewers: not run (skipped for throughput: 27 PRs queued, above the 10 limit)
  • Fresh final gate: an independent Phase-2 review ran after iterative findings were reconciled
  • Fresh verifier: muse-spark-1.3-contributor (standing in for gpt-6-astra) — final-verifier; agent astra-verifier
  • Phase 2 reviewers: muse-spark-1.3-contributor (standing in for gpt-6-astra) — general (completed, effort xhigh); agent phase2-reviewer, muse-spark-1.3-contributor (standing in for gpt-6-astra) — rust-quality (completed, effort xhigh); agent phase2-reviewer, muse-spark-1.3-contributor (standing in for gpt-6-astra) — general (completed, effort xhigh); agent phase2-reviewer, muse-spark-1.3-contributor (standing in for gpt-6-astra) — rust-quality (completed, effort xhigh); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `src/backend_task/error.rs`:
- [SUGGESTION] src/backend_task/error.rs:709-725: Added-but-not-saved advice dead-ends for two wrapped causes
  IdentityKeyAddedButNotSaved wraps every post-broadcast failure but its Display gives one static recovery path: refresh the identity, open the new key and enter its private key. That path cannot succeed for two wrapped causes it explicitly names: an occupied slot re-fails KeyStorage::insert_non_encrypted with IdentityKeySlotOccupied on the same slot, and a protection-downgrade refusal is blocked on re-entry by the IdentityKeyProtectionDowngrade guard (acknowledged in the PR known limitations for protected identities). The cause distinction survives only in #[source] for logs while the user-facing text promises re-entry works. Split the recovery wording per cause or soften the static text so it does not assume re-entry works.

In `src/backend_task/identity/add_key_to_identity.rs`:
- [NITPICK] src/backend_task/identity/add_key_to_identity.rs:578-585: Bool flag selects between two safety-critical error variants
  key_added_but_not_saved(source, derived: bool) uses a bare bool to choose between IdentityKeyAddedButNotSaved (copy the private key) and DerivedIdentityKeyAddedButNotSaved (nothing to copy). The flag is derived one step removed at the call site and test call sites read as true/false literals, so a wrong literal silently gives the wrong recovery advice. Two tiny constructors selected by matching on the key material would make each call site self-describing.

In `src/ui/identity/keys/add_key_screen.rs`:
- [SUGGESTION] src/ui/identity/keys/add_key_screen.rs:592-629: Rescue private key lives in a still-editable input
  (existing thread: https://github.com/dashpay/dash-evo-tool/pull/1010#discussion_r4068435142)
  show_key_not_saved renders the rescue secret with self.private_key_input.show(ui), the normal editable PasswordInput, and the Copy button copies whatever is currently in that field. In this state the field can be the only copy of a generated key, yet it still accepts typing, pasting, or clearing, so an accidental keystroke silently corrupts the sole copy before Copy is pressed with no validation it still matches the on-chain key. Make the rescue panel read-only display plus copy instead of reusing the entry widget.

In `src/model/qualified_identity/encrypted_key_storage.rs`:
- [SUGGESTION] src/model/qualified_identity/encrypted_key_storage.rs:667-685: Discovery merge pins stale disabled_at on held keys
  (existing thread: https://github.com/dashpay/dash-evo-tool/pull/1010#discussion_r4068435137)
  retain_local_keys_from inserts the stored (QualifiedIdentityPublicKey, PrivateKeyData) verbatim whenever a same_key match holds, and same_key explicitly ignores disabled_at. When a key was disabled on-chain after the local snapshot, the rebuild's fresh public half with disabled_at set is discarded and the stored copy with the stale value wins inside private_keys, while the record identity keeps the fresh value. Readers via identity_public_keys() then see a stale enabled flag. Keep the rebuilt public half with the stored private half, or refresh disabled_at from the on-chain copy, instead of the wholesale insert.

In `src/context/identity_db.rs`:
- [SUGGESTION] src/context/identity_db.rs:2185-2193: Mixed-protection discovery refusal has no regression test
  (existing thread: https://github.com/dashpay/dash-evo-tool/pull/1010#discussion_r4068435131)
  store_discovered_identity merges retained keys then calls write_local_qualified_identity_locked, which fails closed with IdentityKeyProtectionDowngrade for a mixed (some Tier-2, some plaintext) record, leaving the stored record untouched. That fail-closed discovery behavior is a stated change, but no test drives store_discovered_identity with a mixed record asserting the Err variant and the untouched blob. Existing coverage is fully-protected discovery-keep and the encode guard in isolation. Add a mixed-record discovery test mirroring automatic_discovery_keeps_manual_and_protected_keys.

lklimek and others added 5 commits September 23, 2026 06:09
…fix/identity-key-data-loss

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
retain_local_keys_from kept the stored entry wholesale for a same-key
match, pinning the saved public-key snapshot. disabled_at is the one
field Platform lets move after a key is added, so a key disabled
on-chain stayed offered as usable. Keep the held private half but take
the rebuild's public key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Stage a password-protected identity and run a discovery merge that
carries resident plaintext. Assert IdentityKeyProtectionDowngrade, a
byte-identical stored record, and no keyless vault write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
key_added_but_not_saved took a bare derived flag to choose between two
recovery errors. AddedKeyOrigin classifies by key material and owns
both the not-saved and identity-unloaded mappings, so call sites read
as intent rather than true/false.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When an added key reached the network but was not saved, the rescue
view rendered and copied the still-editable private-key input, so a
stray edit changed the key the user was told to keep. Snapshot the key
at submit into a read-only PasswordInput (new with_read_only builder)
and show/copy that instead; clear it on success or ordinary failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Claudius-Maginificent Claudius-Maginificent added claudius-review Triggers automated code review using claudius plugin, runs as a CI job and removed claudius-review Triggers automated code review using claudius plugin, runs as a CI job labels Sep 23, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review — Final validation — Phase 2 only (queue backlog)

Verified the findings against head 22045c4. Four prior findings are fixed, but an unrelated task error can clear the submitted key before the rescue panel needs it, violating the PR's key-preservation guarantee. The recovery guidance also remains incomplete for protection-downgrade and occupied-slot failures; verification was by source inspection, without rerunning tests.

🔴 1 blocking | 🟡 1 suggestion(s)

1 finding(s) not shown inline (the lines are not part of this PR's diff)

🟡 Suggestion: Added-but-not-saved advice dead-ends for two wrapped causes
src/backend_task/error.rs:719-724

The message unconditionally says that refreshing the identity and entering the new private key will save it, but two wrapped causes survive that sequence. For IdentityKeyProtectionDowngrade, KeyInfoScreen::validate_and_store_private_key performs insert_non_encrypted followed by persistence through the same protection guard, so the paste is refused again. For IdentityKeySlotOccupied, refresh_identity updates the public identity without removing the conflicting saved private-key entry, so insert_non_encrypted rejects the paste again. The documented protected-import limitation can remain out of scope; the new recovery message should not promise that refresh-and-paste alone resolves it. Keep the immediate copy-and-back-up instruction, then use the typed cause to explain the required protection or conflicting-slot prerequisite, or avoid promising that the paste alone completes recovery.

source: gpt-6-astra (phase2-reviewer: general)

Review provenance

Source: reviewer 1: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 2: gpt-6-astra (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6-astra (agent: astra-verifier, role: final-verifier)

  • Triage: critical by gpt-6-astra (effort low) — The large, intricate diff directly changes private-key retention, conflict resolution, and protection in KeyStorage::retain_local_keys_from in src/model/qualified_identity/encrypted_key_storage.rs, plus locked post-broadcast key persistence and Tier-2 sealing in persist_added_identity_key in src/backend_task/identity/add_key_to_identity.rs.
  • Phase 1 reviewers: not run (skipped for throughput: 21 PRs queued, above the 10 limit)
  • Fresh verifier: gpt-6-astra — final-verifier; agent astra-verifier
  • Phase 2 reviewers: gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — rust-quality (completed, effort xhigh); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `src/ui/identity/keys/add_key_screen.rs`:
- [BLOCKING] src/ui/identity/keys/add_key_screen.rs:729-731: Keep the submitted private key across unrelated task errors
  An unrelated task failure can reach this callback while the key addition is still running. For example, AppState forwards scheduled-vote sweep failures to the visible screen at src/app.rs:3098–3118. Because the add is still WaitingForResult, this branch clears submitted_private_key and changes the status to Error. If the add subsequently reports IdentityKeyAddedButNotSaved or IdentityKeyAddedButIdentityUnloaded, display_task_error only changes the status: the rescue panel then displays an empty field, and Copy private key copies an empty string while reporting success. If the editable form was changed during the broadcast, this discards the screen's last retained copy of the accepted key. Preserve the snapshot and pending state across unrelated errors, and clear them only on a terminal result correlated with their own add operation. Extend the submitted-key regression with an unrelated error between submission and the post-broadcast failure.

In `src/backend_task/error.rs`:
- [SUGGESTION] src/backend_task/error.rs:719-724: Added-but-not-saved advice dead-ends for two wrapped causes
  The message unconditionally says that refreshing the identity and entering the new private key will save it, but two wrapped causes survive that sequence. For IdentityKeyProtectionDowngrade, KeyInfoScreen::validate_and_store_private_key performs insert_non_encrypted followed by persistence through the same protection guard, so the paste is refused again. For IdentityKeySlotOccupied, refresh_identity updates the public identity without removing the conflicting saved private-key entry, so insert_non_encrypted rejects the paste again. The documented protected-import limitation can remain out of scope; the new recovery message should not promise that refresh-and-paste alone resolves it. Keep the immediate copy-and-back-up instruction, then use the typed cause to explain the required protection or conflicting-slot prerequisite, or avoid promising that the paste alone completes recovery.

Comment thread src/ui/identity/keys/add_key_screen.rs Resolved
lklimek and others added 2 commits September 24, 2026 11:21
…data-loss

# Conflicts:
#	CHANGELOG.md
#	src/ui/components/README.md
The Add Key screen treated every task error routed to it as the end of
its own add: an unrelated failure mid-broadcast (e.g. a scheduled-vote
sweep) cleared the submitted private key and flipped the status to
Error, so a later "added but not saved" result showed an empty rescue
field and copied an empty string.

Tag both add-key tasks with a BackendTaskContext::IdentityKeyAdd for
their identity, and let the screen react only to errors from its own
add. Extend the submitted-key regression with an unrelated error and
another identity's add failure between submit and the post-broadcast
failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@lklimek lklimek removed the claudius-review Triggers automated code review using claudius plugin, runs as a CI job label Sep 24, 2026
@lklimek lklimek added the claudius-review Triggers automated code review using claudius plugin, runs as a CI job label Sep 24, 2026
…fix/identity-key-data-loss

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claudius's review: 9 findings (3 MEDIUM, 5 LOW, 1 INFO) — 6 blocking.

The headline data-loss fixes in this branch (dropped saved keys, stale-slot detection, unflagged wallet-derived keys) hold up under adversarial re-reading — QA traced the whole chain by hand and confirmed the new tests genuinely stage the adversarial scenarios rather than asserting current behaviour. Nice work there.

But the security pass found this PR introduces new defects in the same problem class it set out to fix, several serious enough to trip mandatory blocker gates. Posted as inline comments (MEDIUM, in the diff):

  • SEC-001 (add_key_screen.rs:634-645, G-SECRET): the rescued identity private key is copied to the OS clipboard with no zeroization and no auto-clear — the first place in this repo that puts secret material on the clipboard.
  • SEC-002 (encrypted_key_storage.rs:705-728, G-DATA): the key-merge on discovery/wallet-load silently overwrites a live, on-chain-resolvable key with a stale stored one, removing the identity's ability to sign with a key it demonstrably controls.
  • SEC-003 (load_identity_from_wallet.rs:252-260, G-UI-TEXT): loading a partially-protected ("Mixed") identity now hard-fails with an IdentityKeyProtectionDowngrade message written for a settings-change flow ("...make your change..."), leaving the user no actionable path — and the same failure is silently swallowed on the automatic discovery path.

Also blocking, but LOW severity so summarized here rather than inlined:

  • SEC-004 (add_key_screen.rs:765-777, G-DATA): navigating away from the Add Key screen while the broadcast is in flight destroys the only copy of a rescued private key, yet the banner still tells the user to copy it.
  • SEC-005 (add_key_screen.rs:801-808, G-DATA): the success handler for AddedKeyToIdentity isn't gated by identity (unlike the error handler this PR added gating for) — another identity's concurrent add-key success wipes this screen's rescued key and shows a false success page.
  • SEC-007 (add_key_to_identity.rs:407-417, G-DATA): the post-broadcast persist writes the pre-broadcast snapshot's identity body back wholesale, rolling back a concurrent writer's balance/revision — a partial regression of the lost-update protection this same function introduces.

Non-blocking, filed for the record: SEC-006 (un-zeroized second copy of added-key plaintext on error paths), SEC-008 (ephemeral review-finding IDs SEC-101/102/104 committed into source/test comments — please reword before merge, they'll be meaningless to future readers), CALL-001 (informational — call-tree walk found no stale caller).

Note: claudius:project-reviewer-adams was not run this pass — the CI early-stop rule fired once security reported a blocking MEDIUM, so its structural/convention pass (AGENTS.md secret-seam and error-variant conventions) is deferred to the next round.

Full report attached via the workflow's report artifact/link.
📊 View full HTML review report

Comment thread src/ui/identity/keys/add_key_screen.rs Resolved
@github-actions github-actions Bot removed the claudius-review Triggers automated code review using claudius plugin, runs as a CI job label Sep 24, 2026

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review — Final validation — Phase 2 only (queue backlog)

The PR addresses several lost-key paths, but the merge can still hide a usable on-chain key behind stale local data, and a foreign add success can erase the recovery key shown by the active Add Key screen. Loading partially protected identities also exposes an incorrect recovery path and silently leaves automatic discovery stale. These correctness issues require changes before merge.

🔴 3 blocking | 🟡 4 suggestion(s)

Review provenance

Source: reviewer 1: gpt-6-astra (agent: phase2-reviewer, role: general); reviewer 2: gpt-6-astra (agent: phase2-reviewer, role: rust-quality); final verifier: gpt-6-astra (agent: astra-verifier, role: final-verifier)

  • Triage: critical by gpt-6-astra (effort low) — The diff intricately changes private-key retention, conflict resolution, protection preservation, and post-broadcast persistence across discovery and key addition, notably in KeyStorage::retain_local_keys_from and persist_added_identity_key, directly affecting key handling and potential irreversible key loss.
  • Phase 1 reviewers: not run (skipped for throughput: 16 PRs queued, above the 10 limit)
  • Fresh verifier: gpt-6-astra — final-verifier; agent astra-verifier
  • Phase 2 reviewers: gpt-6-astra — general (completed, effort xhigh); agent phase2-reviewer, gpt-6-astra — rust-quality (completed, effort xhigh); agent phase2-reviewer
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `src/ui/identity/keys/add_key_screen.rs`:
- [BLOCKING] src/ui/identity/keys/add_key_screen.rs:798-807: Foreign key-add success can erase this screen's recovery key
  The screen correlates errors with the identity being added, but successful `AddedKeyToIdentity` results are still forwarded unconditionally. If this screen is visible while another identity's add completes, or the user leaves and later returns while an add is in flight, the foreign success clears `submitted_private_key` and switches the screen to `Complete`. If this screen's own add then fails after broadcast, the rescue panel no longer contains the submitted key and its copy action returns an empty value. Only process `AddedKeyToIdentity` when its `BackendTaskContext` identifies this screen's identity.
- [SUGGESTION] src/ui/identity/keys/add_key_screen.rs:613-645: Copying the rescue key leaves a raw secret in the system clipboard
  The recovery action copies the complete identity private key into the OS clipboard as an ordinary `String`, where other applications, clipboard history, and synchronization services may retain it indefinitely. The read-only in-app field does not mitigate exposure after copying, and clearing the screen's snapshot does not clear the clipboard. Because copying is an intentional recovery feature, retain it but warn clearly that the clipboard is exposed and provide a best-effort expiry or clearing mechanism where the platform permits.
- [SUGGESTION] src/ui/identity/keys/add_key_screen.rs:654-670: Wallet-derived rescue advice can repeat the same unsolved local conflict
  `DerivedIdentityKeyAddedButNotSaved` always tells the user to load the identity from the wallet again. For an `IdentityKeySlotOccupied` failure, that load rebuilds the new wallet key at the same placement while `retain_local_keys_from` preserves the conflicting held entry, so the key remains unavailable locally. For a mixed-protection failure, the load path refuses with `IdentityKeyProtectionDowngrade` instead. The derived-key recovery state needs source-specific guidance or a way to resolve the conflicting or protected local record.

In `src/model/qualified_identity/encrypted_key_storage.rs`:
- [BLOCKING] src/model/qualified_identity/encrypted_key_storage.rs:712-726: A stale stored key displaces the usable on-chain key
  When the rebuilt record contains a different public key at a placement occupied by a stored entry with private material, the mismatch is logged but the stored entry is still inserted at that placement. `KeyStorage` has only one value for the placement, so the wallet-derived/on-chain entry is discarded. Later `candidates()` rejects the stale stored public key with `same_key`, leaving the identity unable to sign with the key that is present on-chain. The test `a_stale_stored_secret_is_kept_when_the_on_chain_key_differs` currently preserves the stale secret by locking in this displacement rather than verifying that the rebuilt key remains usable. Keep the rebuilt entry active and preserve the stale secret somewhere that cannot shadow it, or refuse the merge without losing reachability of the rebuilt key.

In `src/backend_task/identity/load_identity_from_wallet.rs`:
- [BLOCKING] src/backend_task/identity/load_identity_from_wallet.rs:252-260: Mixed-protection refresh refusal gives the load flow the wrong recovery path
  A partially protected record can contain Tier-2 keys together with resident plaintext after the `ProtectedSkipped` migration state. The new merge passes that state to `store_discovered_identity`, which correctly refuses the keyless rewrite with `IdentityKeyProtectionDowngrade`, but the propagated message is written for a user mutation: it tells the user to remove protection, make a change, and add protection again even though they only requested a wallet load. Automatic discovery has the related problem in `discover_identities.rs`: it logs the refusal and continues with a successful scan summary, leaving the local identity stale without user-visible recovery guidance. Use a load/refresh-specific typed outcome and surface an actionable result for automatic discovery.

In `src/backend_task/identity/add_key_to_identity.rs`:
- [SUGGESTION] src/backend_task/identity/add_key_to_identity.rs:412-421: The post-broadcast merge can revert newer identity metadata
  The fresh local record is used to preserve public-key IDs absent from the pre-broadcast snapshot, but the entire `Identity` value is then replaced with `snapshot.identity`. A concurrent refresh, transfer, top-up, or other key update can therefore have newer balance, revision, disabled-key metadata, or other same-ID state that is silently reverted by this write. This defeats the intended lost-update protection for fields other than aliases and additional public keys. Apply an explicit field-level merge policy or obtain a fresh post-broadcast identity before writing, and cover concurrent balance or revision changes.

In `src/backend_task/error.rs`:
- [SUGGESTION] src/backend_task/error.rs:736-737: Added-but-not-saved advice dead-ends for two wrapped causes
  The generic post-broadcast message still tells the user to refresh, open the new key, and enter its private key even when the wrapped cause is `IdentityKeyProtectionDowngrade` or `IdentityKeySlotOccupied`. Re-entry is rejected by the protected-identity write guard in the first case, and by the occupied local placement in the second case. Make the recovery guidance cause-aware or explain the additional protection/conflict-resolution step required before re-entry can succeed.

Comment thread src/ui/identity/keys/add_key_screen.rs Resolved
Comment thread src/ui/identity/keys/add_key_screen.rs Resolved
Comment thread src/backend_task/error.rs Resolved
Comment thread src/ui/identity/keys/add_key_screen.rs Resolved
lklimek and others added 4 commits September 25, 2026 08:35
- A wallet rebuild no longer lets a stale saved key displace the on-chain
  key at the same id: the rebuilt entry stays usable for signing, and the
  stale resident bytes are wiped (SEC-002).
- A refresh blocked by partial password protection gets its own typed
  error, IdentityRefreshBlockedByPartialProtection, naming the identity and
  the recovery steps; background discovery raises it as a sticky warning
  banner instead of only logging it (SEC-003).
- The Add Key screen ignores another identity's AddedKeyToIdentity
  success, so it cannot clear the key kept for rescue.
- Copying the rescue key warns that other apps can read the clipboard and
  clears it after 60 seconds if it still holds the key (SEC-001).
- Added-but-not-saved advice for an entered key follows its cause: a
  protection refusal or an occupied slot names the extra step first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A stale stored key whose secret lives only in the identity blob (Clear,
AlwaysClear, legacy Encrypted) now keeps its slot instead of being wiped:
the Keys screen lists it as an unpublished saved key, and the save moves
Clear/AlwaysClear bytes into the vault, so the next refresh hands the slot
to the on-chain key while the vault keeps the stale secret. An InVault stale
entry still yields immediately. End-to-end test covers both refreshes.

Drop the clipboard auto-clear: arboard is built without Wayland support
here, so it could not be relied on to read or clear the clipboard egui
wrote. The rescue view keeps the warning and now asks the user to replace
the clipboard contents after pasting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…fix/identity-key-data-loss

# Conflicts:
#	src/ui/identity/keys/add_key_screen.rs
…fix/identity-key-data-loss

# Conflicts:
#	src/backend_task/error.rs

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-review — Preliminary review — Phase 1 blocker gate

The current head fixes the task-correlation, rescue-key snapshot, refresh messaging, and same-key metadata issues. One blocking issue remains: a conflicting legacy Encrypted entry survives every refresh without yielding its slot to the usable on-chain key. Verification was performed against d6c2486 by source inspection; tests were not executed.

Validated blockers were found by the Phase-1 review and confirmed by a fresh verifier. Phase 2 is deferred until a fresh same-head revalidation clears the blocker gate.

🔴 1 blocking

1 carried-forward finding(s) already raised on this PR; not re-posting as new inline comments.

Review provenance

Source: reviewer 1: muse-spark-1.3-contributor (agent: phase1-reviewer, role: general); reviewer 2: muse-spark-1.3-contributor (agent: phase1-reviewer, role: rust-quality); final verifier: gpt-6-astra (agent: astra-gate-verifier, role: verifier)

  • Triage: critical by gpt-6-astra (effort low) — This is a large, intricate diff that changes identity private-key handling, encrypted key storage, persistence, and failure recovery across backend, model, context, and UI code.
  • Phase 1 reviewers: muse-spark-1.3-contributor — general (completed, effort xhigh); agent phase1-reviewer, muse-spark-1.3-contributor — rust-quality (completed, effort xhigh); agent phase1-reviewer
  • Phase 1 model: muse-spark-1.3-contributor — not quota-gated; passed over gemini-3.8-flash-high (antigravity below 15% reserve: weekly 34% left, 5h 0% left), glm-5.3-flash (not used above high effort; tier asks max)
  • Fresh verifier: gpt-6-astra — verifier; agent astra-gate-verifier
  • Phase 2 reviewers: not run (deferred by blocker gate)
🤖 Prompt for all review comments with AI agents
These findings are from an automated code review. Verify each finding against the current code and only fix it if needed.

In `src/model/qualified_identity/encrypted_key_storage.rs`:
- [BLOCKING] src/model/qualified_identity/encrypted_key_storage.rs:723-736: A stale stored key displaces the usable on-chain key
  (existing thread: https://github.com/dashpay/dash-evo-tool/pull/1010#discussion_r4097515428)
  The vaulted-key fix closes the normal case, but the displacement remains for a legacy Encrypted entry whose public key differs from the wallet-rebuilt key at the same placement. This branch overwrites the rebuilt entry with the stale encrypted entry. Unlike Clear and AlwaysClear, Encrypted is never moved by take_plaintext_for_vault, so the author's described save-then-refresh handoff never occurs: every subsequent discovery or wallet load repeats the overwrite, and candidates() rejects the stale entry for the on-chain key. The test a_stale_secret_held_only_in_the_blob_is_kept explicitly includes Encrypted but does not test eventual availability of the rebuilt key. Preserve the legacy ciphertext without shadowing the active placement, or explicitly refuse and explain the conflict rather than reporting a successful refresh that leaves the wallet-derived key unavailable. Do not fix this by discarding the ciphertext, which may be the only saved copy.

@lklimek
lklimek merged commit 456349b into feat/derived-identity-key Sep 25, 2026
11 checks passed
@lklimek
lklimek deleted the fix/identity-key-data-loss branch September 25, 2026 10:44
lklimek added a commit that referenced this pull request Sep 30, 2026
* feat(identity): add wallet-derived keys to existing identities

Default Add Key to wallet derivation, select the first unused index and
keep used indices disabled. Preserve derivation paths for seed recovery
and signing; retain manual private-key entry behind the checkbox.

Co-Authored-By: Codex GPT-6 <noreply@openai.com>

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

* fix(identity): harden wallet-derived key addition per review

- Default the wallet key slot to the new key's id (key_index == key_id,
  as platform-wallet discovery and the HASH160 signing fallback assume),
  falling back to the lowest free slot only when that one is taken.
- Verify the cached public key against the seed through the secret
  chokepoint before broadcast; a mismatch repairs the cache entry and
  fails with the new DerivedKeySeedMismatch, nothing is broadcast.
- Build every seed-recovery scan bound and the selectable limit on one
  shared recovery_scan_bound, with a test that fails if any scan stops
  covering the highest selectable slot.
- Add is_derivable_key_type in model/ for UI and backend.
- Move chooser state into ui/state/derived_key_chooser.rs: computed on
  load/refresh/task results (no per-frame cache reads), slot load state
  separate from submission status, Retry only after a real failure, no
  duplicate or looping warm tasks, warm only while the chooser is shown.
- Default "Create from wallet" on only when the identity has a wallet
  path; otherwise open on manual entry and explain why.
- After a rejected slot, refresh the identity from the network and
  select another free slot; error texts give on-screen actions.
- Disabled tooltips, spinners, plain wording, and a notice that a key
  created for a password-protected identity is protected by the wallet.
- Document the derived-key TaskError variants and the persist path;
  offline tests for every backend guard and the screen flows.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(identity): report a locked wallet when seed verification cannot open the seed

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* style: apply rustfmt to seed verification helper

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(identity): refuse key-id drift, skip needless password prompt, serialise auth-pubkey cache writes

- SEC-103: AddDerivedKeyToIdentity now carries the key id the chooser
  worked from (local max key id + 1). If the freshly fetched network
  record assigns a different id, the add fails with the new
  DerivedKeyIdChanged before signing or broadcasting; the chooser drops
  its selection (without blacklisting the slot) and reloads the
  identity. A slot deliberately picked off the key id is still allowed.
- SEC-105: a wallet-derived key has no private bytes to seal, so adding
  one to a password-protected identity no longer prompts for the
  identity password. The manual-key path is unchanged.
- SEC-106: every auth-pubkey cache writer (warm task, cold-fill resolve,
  bootstrap warm, add-derived repair) goes through
  AuthPubkeyCacheView::update, a read-modify-write serialised on a
  per-backend mutex held only for the synchronous KV call, so a stale
  snapshot can no longer undo a repair.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(identity): correlate derived key loading with its wallet

Handle successful warm tasks through their wallet-aware context, clarify
ambiguous wallet errors, avoid per-frame slot allocation, and tighten
the off-id derivation regression assertion.

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

Co-Authored-By: OpenAI Codex <noreply@openai.com>

* fix(identity): harden derived-key chooser against network, wallet and removal races

- Drop stacked detail screens on network switch so an Add Key form
  prepared on the previous network cannot submit through the new context.
- Consume every chooser slot-load error on the Add Key screen, so another
  wallet's warm failure no longer marks the form failed.
- Restart the chooser's slot load when the identity's wallet changes, so
  it cannot stay in Loading waiting on a result it now rejects.
- Delete the auth-pubkey cache on wallet removal under its write lock and
  skip writes for wallets that are no longer loaded, so a late warm cannot
  leave an orphaned entry.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): stop identity refreshes and key adds from dropping saved keys (#1010)

* fix(identity): stop identity refreshes and key adds from dropping saved keys

Wallet discovery (boot/unlock) and "load from wallet" rebuilt an identity
from wallet-derivable keys only and replaced the stored record, so manual
keys - including password-protected Tier-2 ones - vanished and their vault
secrets were orphaned. store_discovered_identity now folds the stored
record's keys into the rebuild under the record lock
(KeyStorage::retain_local_keys_from): a held private half is never replaced
by a derivation path, so protection is never downgraded. Delisted records
(half-finished removals) are still replaced, and the unload marker check is
unchanged. load_identity_from_wallet routes through the same guarded store.

Add-key no longer writes its pre-broadcast snapshot back: under the record
lock it re-reads the stored record and applies only the refreshed on-chain
identity and the new key entry, so keys, aliases or protection saved
during the broadcast survive.

Every post-broadcast failure now maps to IdentityKeyAddedButNotSaved (or
IdentityKeyAddedButIdentityUnloaded), and the Add Key screen switches to a
"save your new private key" state with a copy button instead of letting a
generated key be lost.

Fixes audit findings SEC-101, SEC-102, SEC-104.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(identity): keep and flag a stale saved key that differs from the on-chain key

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(identity): report a wallet-derived key added but not saved without asking to copy it

Stacking the post-broadcast persist fixes onto wallet-derived identity keys
left a derived key's "added but not saved" outcome asking the user to copy
a private key that does not exist.

- Add DerivedIdentityKeyAddedButNotSaved and
  DerivedIdentityKeyAddedButIdentityUnloaded; the persist step picks them
  when the new key has no private half.
- The Add Key screen gets a DerivedKeyOnNetworkNotSaved state with no key
  field and no copy button; it says the key can be saved again by loading
  the identity from the wallet.
- Tests: derived persist onto the fresh record keeps concurrent writes,
  occupied slot and unload map to the derived variants,
  KeyStorage::insert_wallet_derived keeps a held private half, and the
  screen offers nothing to copy for a derived key.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>

* fix(identity): refresh disabled_at when a rebuild keeps a held key

retain_local_keys_from kept the stored entry wholesale for a same-key
match, pinning the saved public-key snapshot. disabled_at is the one
field Platform lets move after a key is added, so a key disabled
on-chain stayed offered as usable. Keep the held private half but take
the rebuild's public key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(identity): pin the mixed-protection discovery refusal

Stage a password-protected identity and run a discovery merge that
carries resident plaintext. Assert IdentityKeyProtectionDowngrade, a
byte-identical stored record, and no keyless vault write.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(identity): name the added key's origin instead of a bool

key_added_but_not_saved took a bare derived flag to choose between two
recovery errors. AddedKeyOrigin classifies by key material and owns
both the not-saved and identity-unloaded mappings, so call sites read
as intent rather than true/false.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): rescue the submitted key, not the live form field

When an added key reached the network but was not saved, the rescue
view rendered and copied the still-editable private-key input, so a
stray edit changed the key the user was told to keep. Snapshot the key
at submit into a read-only PasswordInput (new with_read_only builder)
and show/copy that instead; clear it on success or ordinary failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): keep the submitted key across unrelated task errors

The Add Key screen treated every task error routed to it as the end of
its own add: an unrelated failure mid-broadcast (e.g. a scheduled-vote
sweep) cleared the submitted private key and flipped the status to
Error, so a later "added but not saved" result showed an empty rescue
field and copied an empty string.

Tag both add-key tasks with a BackendTaskContext::IdentityKeyAdd for
their identity, and let the screen react only to errors from its own
add. Extend the submitted-key regression with an unrelated error and
another identity's add failure between submit and the post-broadcast
failure.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): harden key rescue and wallet refresh per review

- A wallet rebuild no longer lets a stale saved key displace the on-chain
  key at the same id: the rebuilt entry stays usable for signing, and the
  stale resident bytes are wiped (SEC-002).
- A refresh blocked by partial password protection gets its own typed
  error, IdentityRefreshBlockedByPartialProtection, naming the identity and
  the recovery steps; background discovery raises it as a sticky warning
  banner instead of only logging it (SEC-003).
- The Add Key screen ignores another identity's AddedKeyToIdentity
  success, so it cannot clear the key kept for rescue.
- Copying the rescue key warns that other apps can read the clipboard and
  clears it after 60 seconds if it still holds the key (SEC-001).
- Added-but-not-saved advice for an entered key follows its cause: a
  protection refusal or an occupied slot names the extra step first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): never drop a stale key's only copy on wallet refresh

A stale stored key whose secret lives only in the identity blob (Clear,
AlwaysClear, legacy Encrypted) now keeps its slot instead of being wiped:
the Keys screen lists it as an unpublished saved key, and the save moves
Clear/AlwaysClear bytes into the vault, so the next refresh hands the slot
to the on-chain key while the vault keeps the stale secret. An InVault stale
entry still yields immediately. End-to-end test covers both refreshes.

Drop the clipboard auto-clear: arboard is built without Wayland support
here, so it could not be relied on to read or clear the clipboard egui
wrote. The rescue view keeps the warning and now asks the user to replace
the clipboard contents after pasting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Lukasz Klimek <842586+lklimek@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>

* fix(ui): attribute derived-key slot rejection to the submitted slot

The slot chooser stays editable while an add is pending, so a
DerivedKeyIndexUnavailable error marked whatever slot was selected when
the error arrived. Snapshot the submitted wallet and slot at dispatch
and apply the rejection to that snapshot; a slot picked meanwhile keeps
its selection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(identity): deliver the slot rejection with the add's own task context

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): name the out-of-range cause in the unavailable-slot message

DerivedKeyIndexUnavailable is also returned for a slot outside the seed-recovery window, where the old wording blamed an existing key.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): keep the wallet path a rebuild finds for a held key

A key imported before its wallet was loaded has no in_wallet_at_derivation_path. The refresh merge kept the stored entry whole, dropping the rebuild's path, so derivation_wallet found no wallet and Create from wallet stayed unavailable. The held private half is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): merge keys of an unlisted identity that was never unloaded

Replacing the stored key map is reserved for a partial removal, which always writes the unload marker before delisting. An identity off the roster without the marker (an insert that stopped between its blob write and roster add) keeps its live vault keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(wallet): delete auth-pubkey caches again after clearing all wallets

The bulk clear deletes each cache while its wallet is still loaded, so a warm in flight could pass the membership gate and write the entry back. clear_network_database now deletes the caches once more after WalletContext is cleared.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): settle the key-slot load only on its own dispatch

A warm left running by a closed Add Key screen shares wallet coordinates with the new screen's warm, so its failure marked the new chooser failed. The chooser now dispatches with BackendTaskContext::for_dispatch and matches outcomes against that exact context.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(identity): settle an add only on the dispatch that owns its key

An add left running by a closed Add Key screen targets the same identity,
so matching outcomes by identity ID let its success or failure clear the
newer submission's rescue key or replace its not-saved view. Each add is
now dispatched with BackendTaskContext::for_dispatch, and only an outcome
carrying that exact context settles it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
Co-authored-by: OpenAI Codex <noreply@openai.com>
Co-authored-by: Claudius the Magnificent AI, on behalf of lklimek <8431764+Claudius-Maginificent@users.noreply.github.com>
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.

3 participants