Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
1b39d69
fix(identity): wire the Identity Hub unload button and stop zombie re…
lklimek Aug 25, 2026
f5f8234
Merge branch 'v1.0-dev' into fix/cannot-delete-identity
lklimek Aug 27, 2026
48dc572
fix(identity): survive partial purge failure and reset stale UI state…
lklimek Aug 27, 2026
469e6ea
fix(identity): resume orphaned vault cleanups on boot
lklimek Aug 27, 2026
0b9e092
fix(identity): never resume a cleanup for an identity still on the ro…
lklimek Aug 27, 2026
4821e0a
docs(identity): correct the boot-sweep call-site comment
lklimek Aug 27, 2026
daf7c90
fix(identity): close boot-sweep purge and re-import races
lklimek Aug 27, 2026
ec23526
fix(identity): stop reporting a completed removal as failed
lklimek Aug 27, 2026
f061d70
fix(identity): correct misleading unload messaging, close voter parit…
lklimek Aug 27, 2026
8b92a68
fix(identity): name both outcomes when a voter failure and cleanup-pe…
lklimek Aug 27, 2026
5b4df13
fix(identity): serialize every write to the identity roster
lklimek Aug 27, 2026
9770656
fix(identity): stop reporting stale bookkeeping as undeleted keys
lklimek Aug 27, 2026
4d03643
fix(identity): make the unload prompt name its target and hold the sc…
lklimek Aug 27, 2026
ffa02c0
test(identity): cover the removal-outcome classifier directly
lklimek Aug 27, 2026
fedfa70
docs(identity): match the unload story and key notes to the new behavior
lklimek Aug 27, 2026
5a16766
Merge remote-tracking branch 'origin/v1.0-dev' into fix/cannot-delete…
lklimek Aug 27, 2026
a5557b1
test(identity): make the roster race certain instead of likely
lklimek Aug 27, 2026
112a5a2
fix(identity): reset identity caches when a fallback identity is unlo…
lklimek Aug 27, 2026
8d0d294
fix(identity): stop the cleanup warnings claiming more than the flag …
lklimek Aug 27, 2026
f8528c7
fix(identity): give the worse cleanup outcome the same precaution
lklimek Aug 27, 2026
a9f1aa4
Merge branch 'pr962-round5' into fix/cannot-delete-identity
lklimek Aug 27, 2026
72bf16b
test(identity): release the roster rendezvous on evidence, not a clock
lklimek Aug 27, 2026
df0a73b
test(identity): prove the sweep blocks on the lock, not on a stopwatch
lklimek Aug 27, 2026
a3a9401
fix(identity): pin the unload target across the frame that reconciles it
lklimek Aug 27, 2026
643934c
Merge round-6 test-soundness fixes (S1, S2, B2) into fix/cannot-delet…
lklimek Aug 27, 2026
9d228a5
fix(identity): keep automatic discovery from restoring an unloaded id…
lklimek Aug 28, 2026
fe9da84
test(identity): scope lock instrumentation to the test that installs it
lklimek Aug 28, 2026
c2533e6
fix(identity): track every in-flight unload, not just the most recent
lklimek Aug 28, 2026
27c21be
fix(identity): decline every write that would re-create an unloaded i…
lklimek Aug 28, 2026
5ae9f3e
Merge round-6 unload-marker work (B1, C1, C2, chokepoint gate) into f…
lklimek Aug 28, 2026
19f84c7
test(identity): pin that a failed unload's record cannot misfire
lklimek Aug 28, 2026
b332f6b
test(identity): pin that legacy recovery refuses an unloaded identity
lklimek Aug 28, 2026
a2c0fa7
Merge round-6 characterization pins into fix/cannot-delete-identity
lklimek Aug 28, 2026
424cbd6
fix(identity): key the unload guard on the roster, not the blob
lklimek Aug 28, 2026
f99226c
fix(identity): hold the record guard across the add-key seal and write
lklimek Aug 28, 2026
727ec12
fix(identity): stop promising a cleanup attempt on a specific launch
lklimek Aug 28, 2026
1b55e4c
Merge round-7 fixes (roster-keyed guard, add-key seal window, banner …
lklimek Aug 28, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions docs/kv-keys.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ The identity blob and top-up history are **identity-scoped** (`DetScope::Identit
| Key | Scope | Store | Value type | Notes |
|-----|-------|-------|------------|-------|
| `det:identity:v1` | `DetScope::Identity(&id)` | `det-<net>.sqlite` | `StoredQualifiedIdentity` | Fields: `qi_bytes` (inner bincode, redacted in `Debug`), `status: u8`, `identity_type: String`, `wallet_hash: Option<[u8;32]>`, `wallet_index: Option<u32>` |
| `det:identity_index:v1` | `None` | `det-<net>.sqlite` | `Vec<[u8;32]>` | Complete enumeration index of stored identity ids |
| `det:identity_index:v1` | `None` | `det-<net>.sqlite` | `Vec<[u8;32]>` | Complete enumeration index of stored identity ids. Rewritten wholesale on every add/remove, so all read-modify-write access is serialized by one process-wide lock — absence from this roster authorizes the vault-cleanup sweep to delete an identity's private keys, and a lost update would forge that evidence |
| `det:identity_order:v1` | `None` | `det-<net>.sqlite` | `Vec<[u8;32]>` | User-chosen display ordering of identity ID raw bytes |
| `det:top_ups:v1` | `DetScope::Identity(&id)` | `det-<net>.sqlite` | `BTreeMap<u32, u64>` | Top-up history: account index → credits |
| `det:vault_cleanup_pending:v1:<id_base58>` | `None` | `det-<net>.sqlite` | `Vec<(StoredPrivateKeyTarget, KeyID)>` | Durable manifest of the vault-key placements a `delete_local_qualified_identity` call must still clear; persisted before that call's first mutation, cleared once every listed key is deleted (a clear that fails leaves a stale manifest, not a failed removal — the sweep re-runs the idempotent deletes and clears it). Global-scoped (not `DetScope::Identity`) so it survives the index removal that unlists `id` — the exact step it exists to protect against. Resumed by the boot-time `AppContext::resume_pending_vault_cleanups` sweep. |
| `det:identity_unloaded:v1:<id_base58>` | `None` | `det-<net>.sqlite` | `u64` | Presence-only marker that the user unloaded this identity from this device; the stored value is the unload's unix timestamp, kept for diagnostics only and never read for a decision. Written by `delete_local_qualified_identity` **before** `index_remove_identity` delists the identity, and by the devnet wipe for every identity it clears, so no window exists in which the identity is gone and the marker is not yet on file. Consulted by `AppContext::store_discovered_identity` — the single guarded store used by the discovery passes and by the `finish_unwire` migration import — which refuses to store an identity carrying one. That refusal is what makes an unload survive the automatic passes (boot sweep, post-unlock, wallet import, resumed migration), all of which re-derive or re-read the same identity from material that outlives the removal. The absent-record branch of `write_local_qualified_identity_locked` declines the same way, as a backstop covering every update path that could otherwise re-create a record for an identity a removal had already taken away. Global-scoped for the same reason as the manifest above: it must outlive the identity's own scope. Retired only by `insert_local_qualified_identity`, i.e. by the user deliberately loading the identity again. Never expired on a timer — an expiring tombstone is a resurrection with a delay — and never reaped: it grows by one entry per identity the user has actually unloaded on this network, a bound set by user action rather than by anything automatic, which any change letting an automatic path write these would break. |

**Exception to the cascade above**: an identity stored without a wallet association (`wallet_hash: None`) is *also* mirrored into the upstream `identities` table under the **unowned scope** — the all-zero `WalletId`, which upstream stores as a NULL `wallet_id`. Masternode/evonode nodes are the expected case, but any wallet-less identity DET stores takes this path (e.g. a `User` identity looked up by id with no owning wallet). The scope is load-bearing, not incidental: a NULL `wallet_id` activates no foreign key, so no wallet's `ON DELETE CASCADE` reaches the row, and the `cascade_meta_on_identity_delete` trigger — which would delete the identity's `meta_identity` rows, i.e. the `det:identity:v1` record above — never fires for it. That is the whole point: filing the same identity under a real wallet's scope would make removing that unrelated wallet destroy the node's DET record. These rows are also kept out of every wallet's `IdentityManager`, because the `identities` upsert promotes an unowned row to the first wallet that flushes it.

Expand Down Expand Up @@ -216,8 +218,8 @@ Source: `src/wallet_backend/single_key.rs` (`SINGLE_KEY_PRIV_LABEL_PREFIX`, `SIN
| Store | Key count |
|-------|-----------|
| `det-app.sqlite` | 4 (settings, wallet-meta sidecar, single-key-meta sidecar, migration sentinel) |
| `det-<net>.sqlite` | 21 (across 8 domains) |
| `det-<net>.sqlite` | 23 (across 8 domains) |
| `SecretStore` | 2 label patterns (seed envelopes, imported-key private bytes) |
| **Total** | **27** |
| **Total** | **29** |

Prefixed/templated keys (e.g. `det:identity:<id>`) are counted once per prefix, not per instance. `SecretStore` entries are counted as label-pattern families, not per-wallet instances.
19 changes: 19 additions & 0 deletions docs/user-stories.md
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,25 @@ As a user whose identity was already in the app before the upgrade — a mastern
- The previous version's data is never modified, so restoring can be repeated safely; repeating it reports that there was nothing left to restore and changes nothing.
- Restoring keeps the app usable while it waits for the identity password: other identities can still be removed, and anything else saved for this identity while the password prompt is open survives the restore.

### IDN-021: Unload an identity from this device [Implemented]
**Persona:** Alex, Priya

As a user, I want to remove an identity from this device from the identity's own Settings tab, so that I can clear out an identity I no longer use without hunting for a separate screen.

- The identity's Settings tab has an "Unload this identity from this device" action in its Danger zone, and it is usable rather than announced as a coming feature.
- The confirmation names the identity it is about to unload and states plainly that this permanently deletes that identity's private keys stored on this device, and that using it here again needs the user's own backup — the identity record itself stays on Dash Platform, but "stays on Platform" is not "keys survive".
- While the confirmation is open the controls behind it, including the identity switcher, cannot be used, so the answer is always given for the identity the confirmation names.
- Confirming removes the identity, its top-up history, and its scheduled DPNS votes from this device.
- The removal applies to the identity that was on screen when the action was chosen, even if the selected identity changes while the confirmation is open.
- Choosing "Keep", or dismissing the confirmation, changes nothing.
- While a storage update is running the removal is refused with an explanation and can be retried once it finishes.
- Once the identity is gone, where the app lands depends on what is left: with no identities remaining it shows first-run onboarding, with exactly one it opens that identity, and with two or more it shows the identity picker so the user chooses which one to continue with rather than being dropped onto an arbitrary one.
- The outcome is reported: a plain success, a success noting a voter identity tied to it stayed behind, or — when the identity is delisted but a later step of the removal did not finish — a success noting its private keys may still be on this device, that the app will keep trying to clear them automatically, and that until then the device should be treated as still holding them. The report claims no more than that: not that keys are definitely present (the unfinished step may have had no keys to clear), and not that any particular launch clears them (the automatic attempt can return without doing anything, or can fail again). It names no launch at all, because this message does not survive a restart and the launch it named might make no attempt. Keys that were deleted are reported as deleted, whatever internal bookkeeping failed afterwards.
- A removal that cannot be completed at all leaves the identity's private keys intact, so a retry still has everything it needs; no identity is ever left listed without its keys, and no identity is ever reported as still removable once it is already gone from every list.
- The unload sticks. The wallet that derives the identity stays loaded, and the app re-derives and looks up that wallet's identities on its own — at start-up, after a wallet is unlocked, and when a wallet is imported — but none of those automatic passes put back an identity the user unloaded, whether the pass starts before, during, or after the removal. The same holds for the developer-only devnet "clear all identities" sweep.
- Adding a key to an identity that is unloaded while the key is being registered on the network does not leave that key's private half on this device. The key exists on Dash Platform and the app says so plainly, naming what to do — load the identity again, then add the key again — rather than reporting a save that did not happen.
- Loading the identity again is always available and always works: choosing it explicitly, or searching for it by wallet index, loads it back and ends the exclusion, so a later automatic pass keeps it up to date again like any other identity.

---

## DPNS (DPN)
Expand Down
14 changes: 14 additions & 0 deletions src/backend_task/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,20 @@ pub enum TaskError {
source: Box<TaskError>,
},

/// The identity was removed from this device between the add-key broadcast
/// and the local write. The key is on Platform, but nothing about it is
/// saved here — deliberately: the user asked for this identity's keys to be
/// destroyed, and the new key is one of them. Sealing it anyway would leave
/// private key material on a device that reports it holds none, referenced
/// by no record and reachable by no cleanup.
///
/// No loss beyond the on-chain slot: the private key was supplied by the
/// user on the add-key screen, so they still hold it.
#[error(
"The new key was added to your identity on the network, but this identity was removed from this device before the key could be saved here. Load the identity again, then add the key again."
)]
IdentityKeyAddedButIdentityUnloaded,

/// Fail-closed guard at the opt-in protect boundary: the task found
/// keys still resident as plaintext on disk after the eager load-path vault
/// migration, so the identity cannot be reported as fully protected. The
Expand Down
155 changes: 151 additions & 4 deletions src/backend_task/identity/add_key_to_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::wallet_backend::secret_prompt::SecretScope;
use crate::wallet_backend::{SecretAccess, VerifiedIdentityPassword};
use dash_sdk::Error as SdkError;
use dash_sdk::Sdk;
use dash_sdk::dpp::identity::KeyID;
use dash_sdk::dpp::identity::accessors::{IdentityGettersV0, IdentitySettersV0};
use dash_sdk::dpp::identity::identity_public_key::accessors::v0::{
IdentityPublicKeyGettersV0, IdentityPublicKeySettersV0,
Expand Down Expand Up @@ -161,14 +162,78 @@ impl AppContext {
PrivateKeyOnMainIdentity,
public_key_to_add.identity_public_key.id(),
);
self.persist_added_identity_key(
&mut qualified_identity,
new_key,
&private_key,
verified_password,
)?;
Ok(BackendTaskSuccessResult::AddedKeyToIdentity(fee_result))
}

/// Seal the new key and store the updated record, both under this
/// identity's record guard.
///
/// The guard spans the seal because the seal writes private key material
/// and the write is what makes anything point at it. Without it, an unload
/// completing during the broadcast leaves the removal deleting only the
/// placements the stored blob named — not this one, which is not in it yet
/// — and clearing its cleanup manifest; the seal then lands afterwards and
/// the record write is declined. The key would sit in the vault for an
/// identity with no record, no roster entry and no manifest: unreachable
/// through the identity model, so no sweep can ever collect it, on a device
/// that told the user it had destroyed that identity's keys.
///
/// So the roster is rechecked under the guard first, and a delisted
/// identity ends the task before anything is sealed. Nothing is lost that
/// the user does not already have: the private key came from the add-key
/// screen, typed in by them.
fn persist_added_identity_key(
&self,
qualified_identity: &mut QualifiedIdentity,
new_key: (crate::model::qualified_identity::PrivateKeyTarget, KeyID),
private_key: &[u8; 32],
verified_password: Option<VerifiedIdentityPassword>,
) -> Result<(), TaskError> {
let identity_id = qualified_identity.identity.id();
let lock = self.identity_record_lock(identity_id);
let _record_guard = lock
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);

if !self.is_identity_listed(&identity_id)? {
tracing::warn!(
target = "backend_task::identity",
identity_id = %identity_id,
"Identity was removed from this device while its new key was being added; the key is on the network and was not sealed here",
);
return Err(TaskError::IdentityKeyAddedButIdentityUnloaded);
}

// A password-protected identity must never acquire a keyless
// key. The object password was already verified up front (before the
// broadcast above), so here we just seal the newly-added key Tier-2
// under that SAME password and mark it `InVault` BEFORE saving, so the
// at-rest encode writes no plaintext for it. The encode-path guard
// (`encode_identity_blob_vault_first` → `IdentityKeyProtectionDowngrade`)
// still fails closed if this seal is ever skipped.
//
// This seal is the one fallible disk write between the broadcast above
// and the persist below, and on-chain + local cannot be made atomic. If
// it fails (I/O error, corrupt keystore), the key is already on-chain but
// not saved here: fail with the typed, actionable
// `IdentityKeyAddedButNotSaved` (the key is on the network; retry after
// freeing disk space) instead of a silent loss or a misleading storage
// error — and NEVER fall back to a keyless write (that would strip the
// protection this branch exists to preserve).
if let Some(password) = verified_password {
self.wallet_backend()?
.secret_access()
.seal_new_identity_key_with_password(
qualified_identity.identity.id().to_buffer(),
identity_id.to_buffer(),
&new_key.0,
new_key.1,
&private_key,
private_key,
&password,
)
.map_err(key_added_but_not_saved)?;
Expand All @@ -186,8 +251,7 @@ impl AppContext {
}
}

self.update_local_qualified_identity(&qualified_identity)?;
Ok(BackendTaskSuccessResult::AddedKeyToIdentity(fee_result))
self.write_local_qualified_identity_locked(qualified_identity)
}
}

Expand Down Expand Up @@ -284,6 +348,89 @@ mod tests {
}
}

/// R1: an unload that completes during the broadcast must not leave the new
/// key's private half in the vault.
///
/// The removal deletes the placements the *stored blob* names. The key
/// being added is not in that blob yet, so it is not in the removal's
/// delete set and not in the cleanup manifest it clears. A seal that lands
/// afterwards writes private key material for an identity with no record,
/// no roster entry and no manifest — unreachable through the identity
/// model, so no sweep can ever collect it, on a device that has just told
/// the user it destroyed that identity's keys.
///
/// The record guard now spans the recheck, the seal and the write, so a
/// delisted identity ends the task before anything is sealed.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn an_unload_during_the_broadcast_leaves_no_key_in_the_vault() {
use crate::context::test_staging::stage_identity_with_vaulted_keys_using_prompt;
use crate::model::secret::Secret;

const PW: &str = "identity-object-passwordpw";
const NEW_KEY_ID: u32 = 9;
let prompt = Arc::new(TestPrompt::new([ScriptedAnswer::once(PW)]));
let staged = stage_identity_with_vaulted_keys_using_prompt(
Network::Testnet,
Some(prompt),
[0xAA; 32],
[0xBB; 32],
)
.await;
let ctx = &staged.ctx;

// A password-protected identity, which is the shape that seals.
ctx.protect_identity_keys(staged.id, Secret::new(PW), None)
.expect("seal the identity Tier-2");
let mut qualified_identity = ctx
.get_local_qualified_identity(&staged.id)
.expect("read the protected identity")
.expect("identity present");

// The precondition the real flow satisfies before broadcasting.
let verify_scope = ctx
.protected_identity_verify_scope(&qualified_identity)
.expect("read the verify scope")
.expect("a protected identity has one");
let password = ctx
.wallet_backend()
.expect("backend wired")
.secret_access()
.verify_identity_object_password(&verify_scope)
.await
.expect("the scripted password verifies");

// The user unloads the identity while the broadcast is in flight.
ctx.delete_local_qualified_identity(&staged.id)
.expect("unload the identity");

let new_key = (PrivateKeyTarget::PrivateKeyOnMainIdentity, NEW_KEY_ID);
let error = ctx
.persist_added_identity_key(
&mut qualified_identity,
(new_key.0.clone(), new_key.1),
&[0xCD; 32],
Some(password),
)
.expect_err("an identity that is gone cannot take a new key");
assert!(
matches!(error, TaskError::IdentityKeyAddedButIdentityUnloaded),
"expected IdentityKeyAddedButIdentityUnloaded, got {error:?}",
);

assert!(
crate::wallet_backend::IdentityKeyView::new(&staged.store, staged.id.to_buffer())
.get(&new_key.0, new_key.1)
.expect("read the vault")
.is_none(),
"the new key must not be sealed for an identity whose keys the removal \
just destroyed — nothing would ever point at it or collect it",
);
assert!(
!ctx.is_identity_listed(&staged.id).expect("read the roster"),
"and the identity must not be put back on the roster",
);
}

/// O-2 fail-closed: a HEADLESS add-key precondition for a PROTECTED identity
/// returns `SecretPromptUnavailable`. `add_key_to_identity` propagates this
/// with `?` BEFORE it builds or broadcasts the AddKeys state transition, so
Expand Down
Loading
Loading