From ef95e7a2ea31c167aa7ab80ff7f75bcdbe92a0b6 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 11:26:42 +0700 Subject: [PATCH 1/7] feat(platform-wallet): a dedicated shielded account for each identity, proved with the seed The part of #5288 that maps a wallet identity to its own shielded account, taken out so it can land on its own. The code is #5288's, unchanged where it is kept. - An identity at index i uses shielded account 0x40000000 + i (`shielded_identity_account_index`, `is_shielded_identity_account`). - `prepare_shielded_identity_address` proves with the seed that the identity carries the MASTER key the wallet derives at its recorded index, binds the account that index selects, and records it as prepared for this session. A restored index is not proof: hosts restore a placeholder 0. - A bind replaces the registration, so `shielded_config_lock` now serializes every bind entry point and `shielded_add_account`, and the preparation reads the bound set and binds the augmented copy as one step. - A bind and a seedless rebind also take the accounts of the wallet's identities and every identity account with a stored viewing key, so those accounts keep scanning after a restart. - A coordinator Clear waits for a preparation in flight (`operation_guard`). Not taken from #5288: tip sending, profile address publication, the lock that orders an identity moving between slots, storage, FFI and the host SDKs. `prepared_shielded_identity_account` is new: it reports the account prepared for an identity while the preparation stands. Co-authored-by: pasta Co-Authored-By: Claude Fable 5.2 --- packages/rs-platform-wallet/src/lib.rs | 5 + .../src/wallet/platform_wallet.rs | 91 ++- .../src/wallet/shielded/coordinator.rs | 13 + .../src/wallet/shielded/mod.rs | 4 + .../src/wallet/shielded/tips.rs | 322 +++++++++ .../wallet/shielded/viewing_key_bind_tests.rs | 636 ++++++++++++++++++ 6 files changed, 1061 insertions(+), 10 deletions(-) create mode 100644 packages/rs-platform-wallet/src/wallet/shielded/tips.rs diff --git a/packages/rs-platform-wallet/src/lib.rs b/packages/rs-platform-wallet/src/lib.rs index 13005d9c445..cb5b9290680 100644 --- a/packages/rs-platform-wallet/src/lib.rs +++ b/packages/rs-platform-wallet/src/lib.rs @@ -126,3 +126,8 @@ pub use key_wallet_manager; // the crate can pass it to `ManagedIdentity` mutation methods // (`set_dashpay_profile`, `record_dashpay_payment`, `add_identity`, …). pub use wallet::persister::WalletPersister; + +#[cfg(feature = "shielded")] +pub use wallet::shielded::tips::{ + is_shielded_identity_account, shielded_identity_account_index, SHIELDED_IDENTITY_ACCOUNT_BASE, +}; diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index c67a5462f70..5103ad84ea1 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -55,6 +55,7 @@ use { super::shielded::store::{ PendingRedrive, PrivateContactPayment, PrivatePaymentEntry, PrivatePaymentRecord, }, + super::shielded::tips::PreparedTipAccount, super::shielded::{ FileBackedShieldedStore, NetworkShieldedCoordinator, OrchardKeySet, ShieldedStore, }, @@ -434,6 +435,23 @@ pub struct PlatformWallet { /// wallet handles share the one lock. #[cfg(feature = "shielded")] pub(crate) shield_guard: Arc>, + /// Serializes changes to the bound shielded account set. A bind + /// replaces the registration with the accounts it was given, so a + /// caller that reads the current set and binds an augmented copy (tip + /// preparation) must not interleave with another bind, or the other + /// bind's ordinary accounts are dropped and their live state purged. + /// Taken at the top of every bind entry point and by + /// `shielded_add_account`, before `shield_guard`. + #[cfg(feature = "shielded")] + pub(crate) shielded_config_lock: Arc>, + /// The tip account each identity's address was last prepared for in + /// this session, after its slot was proved with the seed. Publication + /// of a wallet tip address requires it, so an address is published only + /// for the identity and slot it was prepared for, and invalidated by a + /// coordinator Clear. `Arc` so cloned wallet handles share it. + #[cfg(feature = "shielded")] + pub(crate) prepared_tip_accounts: + Arc>>, /// Set once this wallet has been removed from the manager, to stop /// a handle that outlives the removal from binding shielded state /// back onto the coordinator. Callers resolve an @@ -833,6 +851,10 @@ impl PlatformWallet { #[cfg(feature = "shielded")] shield_guard: Arc::new(tokio::sync::Mutex::new(())), #[cfg(feature = "shielded")] + shielded_config_lock: Arc::new(tokio::sync::Mutex::new(())), + #[cfg(feature = "shielded")] + prepared_tip_accounts: Arc::new(std::sync::Mutex::new(BTreeMap::new())), + #[cfg(feature = "shielded")] shielded_detached: Arc::new(std::sync::atomic::AtomicBool::new(false)), #[cfg(feature = "shielded")] private_payment_restores: Arc::default(), @@ -871,6 +893,19 @@ impl PlatformWallet { seed: &[u8], accounts: &[u32], coordinator: &Arc, + ) -> Result<(), PlatformWalletError> { + let _config = self.shielded_config_lock.lock().await; + self.bind_shielded_locked(seed, accounts, coordinator).await + } + + /// [`Self::bind_shielded`] for a caller already holding + /// `shielded_config_lock`. + #[cfg(feature = "shielded")] + pub(crate) async fn bind_shielded_locked( + &self, + seed: &[u8], + accounts: &[u32], + coordinator: &Arc, ) -> Result<(), PlatformWalletError> { use super::shielded::{AccountViewingKeys, OrchardKeySet, SubwalletId}; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; @@ -897,12 +932,29 @@ impl PlatformWallet { // snapshot predates a Clear. let snapshot_generation = coordinator.clear_generation(); let network = self.sdk.network; + let mut accounts: std::collections::BTreeSet = accounts.iter().copied().collect(); + accounts.extend(self.discovered_tip_accounts().await?); + // Keep retired tip accounts scanning, including identities removed from + // the local manager after their address was published. + let start = self + .persister + .load() + .map_err(|e| PlatformWalletError::Persistence(e.to_string()))?; + accounts.extend( + start + .shielded + .viewing_keys + .keys() + .filter(|id| { + id.wallet_id == self.wallet_id + && super::shielded::is_shielded_identity_account(id.account_index) + }) + .map(|id| id.account_index), + ); let mut account_views: std::collections::BTreeMap = std::collections::BTreeMap::new(); - for &account in accounts { - // `accounts` may contain duplicates; the BTreeMap - // dedups by definition. The full keyset (with its - // `SpendAuthorizingKey`) is dropped at the end of + for &account in &accounts { + // The full keyset (with its `SpendAuthorizingKey`) is dropped at the end of // this iteration — only the viewing half survives. let ks = OrchardKeySet::from_seed(seed, network, account)?; account_views.insert(account, ks.viewing_keys()); @@ -920,11 +972,6 @@ impl PlatformWallet { // treat it like the malformed-row case below: surface it rather // than silently mixing two keys' state. The recovery is a // shielded Clear, which drops both sides at once. - let start = self.persister.load().map_err(|e| { - PlatformWalletError::ShieldedBuildError(format!( - "persister load failed while binding shielded viewing keys: {e}" - )) - })?; for (account, views) in &account_views { let id = SubwalletId::new(self.wallet_id, *account); if let Some(persisted) = start.shielded.viewing_keys.get(&id) { @@ -989,6 +1036,7 @@ impl PlatformWallet { coordinator: &Arc, ) -> Result { use super::shielded::{AccountViewingKeys, SubwalletId}; + let _config = self.shielded_config_lock.lock().await; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; let capabilities = self.persister.persistence_capabilities(); if !capabilities.contains(required) { @@ -1014,9 +1062,25 @@ impl PlatformWallet { "persister load failed while rebinding shielded viewing keys: {e}" )) })?; + let mut accounts: std::collections::BTreeSet = accounts.iter().copied().collect(); + // Newly discovered identities require their tip accounts too. Missing + // FVKs must trigger seed-backed binding: skipping them would report a + // successful restart while silently omitting recoverable tip history. + accounts.extend(self.discovered_tip_accounts().await?); + accounts.extend( + start + .shielded + .viewing_keys + .keys() + .filter(|id| { + id.wallet_id == self.wallet_id + && super::shielded::is_shielded_identity_account(id.account_index) + }) + .map(|id| id.account_index), + ); let mut account_views: std::collections::BTreeMap = std::collections::BTreeMap::new(); - for &account in accounts { + for &account in &accounts { let id = SubwalletId::new(self.wallet_id, account); let Some(fvk_bytes) = start.shielded.viewing_keys.get(&id) else { return Ok(false); @@ -1288,6 +1352,9 @@ impl PlatformWallet { ))); } self.ensure_shielded_attached()?; + // Serialized with the binds: tip preparation rebinds a snapshot of + // the account set, which would drop an account inserted mid-way. + let _config = self.shielded_config_lock.lock().await; // Everything that calls into the host — the snapshot read below // and the viewing-key write further down — stays OUTSIDE the key // slot's lock. A host callback invoked while this write guard is @@ -2905,6 +2972,10 @@ impl Clone for PlatformWallet { #[cfg(feature = "shielded")] shield_guard: self.shield_guard.clone(), #[cfg(feature = "shielded")] + shielded_config_lock: self.shielded_config_lock.clone(), + #[cfg(feature = "shielded")] + prepared_tip_accounts: self.prepared_tip_accounts.clone(), + #[cfg(feature = "shielded")] shielded_detached: self.shielded_detached.clone(), #[cfg(feature = "shielded")] private_payment_restores: self.private_payment_restores.clone(), diff --git a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index e224586420b..69386a16905 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -251,6 +251,11 @@ pub struct NetworkShieldedCoordinator { /// other way round. lifecycle: tokio::sync::Mutex<()>, + /// Clear waits for operations that bind/flush viewing keys or spend from + /// extracted notes. Separate from `lifecycle`, which bind takes internally. + /// Lock order: `operation_admission` → `lifecycle` → `store`. + operation_admission: RwLock<()>, + /// Wallets whose per-subwallet store state has been successfully /// hydrated from the host's persisted snapshot this session /// (`restore_for_wallet` completed without error after the @@ -420,6 +425,7 @@ impl NetworkShieldedCoordinator { progress_handler: std::sync::Mutex::new(None), tree_progress_handler: std::sync::Mutex::new(None), lifecycle: tokio::sync::Mutex::new(()), + operation_admission: RwLock::new(()), hydrated: RwLock::new(std::collections::BTreeSet::new()), clear_generation: std::sync::atomic::AtomicU64::new(0), } @@ -679,6 +685,12 @@ impl NetworkShieldedCoordinator { } } + /// Acquire before wallet placement/configuration locks; retain through persistence + /// and the operation result so Clear cannot delete state while it is still in use. + pub(crate) async fn operation_guard(&self) -> tokio::sync::RwLockReadGuard<'_, ()> { + self.operation_admission.read().await + } + /// [`register_wallet`](Self::register_wallet)'s body, with the /// lifecycle mutex already held by the caller's transaction. async fn register_locked( @@ -1222,6 +1234,7 @@ impl NetworkShieldedCoordinator { /// stays populated, and the next cold resync would gate-skip every /// re-downloaded position against the stale `tree_size`. pub async fn clear(&self) -> Result<(), crate::error::PlatformWalletError> { + let _operations = self.operation_admission.write().await; // Held across the whole Clear, store reset included, so no bind // install can be halfway through its transaction here: one that // already registered would otherwise restore its pre-Clear diff --git a/packages/rs-platform-wallet/src/wallet/shielded/mod.rs b/packages/rs-platform-wallet/src/wallet/shielded/mod.rs index a535e52fff4..f1d57e23a0d 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/mod.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/mod.rs @@ -52,6 +52,10 @@ pub mod prover; pub mod seed_pool; pub mod store; pub mod sync; +pub mod tips; +pub use tips::{ + is_shielded_identity_account, shielded_identity_account_index, SHIELDED_IDENTITY_ACCOUNT_BASE, +}; #[cfg(test)] mod viewing_key_bind_tests; #[cfg(test)] diff --git a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs new file mode 100644 index 00000000000..ec71267745f --- /dev/null +++ b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs @@ -0,0 +1,322 @@ +//! Dedicated DashPay identity shielded accounts. Account selection is wallet policy, not +//! consensus: external Orchard addresses remain valid profile values. +//! +//! Reserve the upper half of the ZIP-32 account space for identities. A wallet-owned +//! identity at index i uses account 0x40000000 + i. Ordinary accounts use the +//! lower half. This mapping never depends on a username, the current profile, +//! or local allocation history: identity discovery followed by shielded bind +//! reconstructs it even after publication was removed. Address rotation uses +//! another diversifier in the same account, so all old addresses remain covered. + +use super::NetworkShieldedCoordinator; +use crate::wallet::platform_wallet::PlatformWallet; +use crate::PlatformWalletError; +use dpp::identity::accessors::IdentityGettersV0; +use dpp::prelude::Identifier; +use std::sync::{Arc, PoisonError, Weak}; + +pub(crate) struct PreparedTipAccount { + account: u32, + coordinator: Weak, + clear_generation: u64, +} + +impl PreparedTipAccount { + /// The prepared account, while the preparation stands: its coordinator is alive + /// and has not been cleared since. + pub(crate) fn standing_account(&self) -> Option { + self.coordinator + .upgrade() + .is_some_and(|coordinator| coordinator.clear_generation() == self.clear_generation) + .then_some(self.account) + } +} + +pub const SHIELDED_IDENTITY_ACCOUNT_BASE: u32 = 0x4000_0000; + +pub fn shielded_identity_account_index(identity_index: u32) -> Result { + if identity_index >= SHIELDED_IDENTITY_ACCOUNT_BASE { + return Err(PlatformWalletError::ShieldedKeyDerivation( + "Identity index exceeds the identity shielded account range".to_string(), + )); + } + Ok(SHIELDED_IDENTITY_ACCOUNT_BASE + identity_index) +} + +pub fn is_shielded_identity_account(account: u32) -> bool { + (SHIELDED_IDENTITY_ACCOUNT_BASE..0x8000_0000).contains(&account) +} + +/// Prove `identity_index` with the seed: the identity must carry the MASTER +/// authentication key this wallet derives at that index (the key discovery and +/// registration both use). Fails closed when no key matches, including when +/// no public keys were restored. +fn verify_identity_index( + seed: &[u8], + network: key_wallet::Network, + identity_index: u32, + identity: &dpp::identity::Identity, +) -> Result<(), PlatformWalletError> { + use crate::wallet::identity::network::{ + derive_identity_auth_key_hash_from_master, MASTER_KEY_INDEX, + }; + use dpp::identity::identity_public_key::methods::hash::IdentityPublicKeyHashMethodsV0; + + let master = key_wallet::bip32::ExtendedPrivKey::new_master(network, seed) + .map_err(|e| PlatformWalletError::ShieldedKeyDerivation(e.to_string()))?; + let expected = derive_identity_auth_key_hash_from_master( + &master, + network, + identity_index, + MASTER_KEY_INDEX, + )?; + let verified = identity + .public_keys() + .values() + .any(|key| key.public_key_hash().ok() == Some(expected)); + if verified { + Ok(()) + } else { + Err(PlatformWalletError::InvalidIdentityData(format!( + "Identity index {identity_index} is not verified for this wallet; \ + rediscover the identity before using its tip account" + ))) + } +} + +impl PlatformWallet { + pub(crate) async fn discovered_tip_accounts(&self) -> Result, PlatformWalletError> { + let wm = self.wallet_manager.read().await; + let info = wm + .get_wallet_info(&self.wallet_id()) + .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id())))?; + // An identity outside this convention can still use ordinary accounts + // or publish an external address; it must not block wallet-wide sync. + Ok(info + .identity_manager + .managed_identities() + .filter(|identity| identity.wallet_id == Some(self.wallet_id())) + .filter_map(|identity| identity.identity_index) + .filter_map(|index| shielded_identity_account_index(index).ok()) + .collect()) + } + + /// Prepare a dedicated receiving account without publishing anything. Full + /// bind persists its viewing key and registers it with the coordinator before + /// callers can publish the returned address. Repeated calls are idempotent. + /// Requires an existing shielded bind so preparing a tip account cannot + /// silently replace the host's ordinary account configuration. + pub async fn prepare_shielded_identity_address( + &self, + seed: &[u8], + identity_id: &Identifier, + coordinator: &Arc, + ) -> Result<[u8; 43], PlatformWalletError> { + // Do not publish a key from a mis-associated mnemonic. A first bind + // has no persisted FVK against which to detect a wrong seed. + let root = key_wallet::wallet::root_extended_keys::RootExtendedPrivKey::new_master(seed) + .map_err(|e| PlatformWalletError::ShieldedKeyDerivation(e.to_string()))?; + let public = root.to_root_extended_pub_key(); + drop(root); + let scoped_id = key_wallet::Wallet::compute_wallet_id_from_root_extended_pub_key( + &public, + Some(self.sdk.network), + ); + let legacy_id = + key_wallet::Wallet::compute_wallet_id_from_root_extended_pub_key(&public, None); + if self.wallet_id() != scoped_id && self.wallet_id() != legacy_id { + return Err(PlatformWalletError::ShieldedKeyDerivation( + "The supplied seed does not belong to this wallet".to_string(), + )); + } + let _lifecycle = self.core().generation_payment_guard().await; + if !self.core().is_current_generation().await { + return Err(PlatformWalletError::WalletNotFound(hex::encode( + self.wallet_id(), + ))); + } + let _operation = coordinator.operation_guard().await; + let account = self.verified_tip_account(seed, identity_id).await?; + // Read the bound set and bind the augmented copy as one step: a bind + // replaces the registration, so another bind landing in between + // would have its ordinary accounts dropped by this stale snapshot. + let _config = self.shielded_config_lock.lock().await; + let clear_generation = coordinator.clear_generation(); + let mut accounts = self.shielded_account_indices().await; + if accounts.is_empty() { + return Err(PlatformWalletError::ShieldedNotBound); + } + #[cfg(test)] + test_hooks::after_snapshot(self).await; + accounts.push(account); + self.bind_shielded_locked(seed, &accounts, coordinator) + .await?; + self.persister() + .flush() + .map_err(|e| PlatformWalletError::Persistence(e.to_string()))?; + let address = self + .shielded_default_address(account) + .await + .ok_or(PlatformWalletError::ShieldedNotBound)?; + if coordinator.clear_generation() != clear_generation { + return Err(PlatformWalletError::ShieldedNotBound); + } + self.prepared_tip_accounts + .lock() + .unwrap_or_else(PoisonError::into_inner) + .insert( + *identity_id, + PreparedTipAccount { + account, + coordinator: Arc::downgrade(coordinator), + clear_generation, + }, + ); + Ok(address) + } + + /// The shielded account prepared for `identity_id` in this session + /// ([`Self::prepare_shielded_identity_address`]), while the preparation + /// stands: its coordinator is alive and has not been cleared since. + /// `None` otherwise. A recorded identity index alone is not proof of the + /// account, so this is what tells a caller that the seed proved it. + pub fn prepared_shielded_identity_account(&self, identity_id: &Identifier) -> Option { + self.prepared_tip_accounts + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get(identity_id) + .and_then(PreparedTipAccount::standing_account) + } + + /// The tip account of a wallet-owned identity's slot, proved with the + /// seed. A restored index is metadata, and hosts that cannot represent + /// "unknown" restore a placeholder 0 for wallet-attached identities, so + /// the seed must reproduce the identity's MASTER key at the recorded index + /// before the account that index selects is used for it. + async fn verified_tip_account( + &self, + seed: &[u8], + identity_id: &Identifier, + ) -> Result { + let wm = self.wallet_manager.read().await; + let info = wm + .get_wallet_info(&self.wallet_id()) + .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id())))?; + let identity = info + .identity_manager + .managed_identity(identity_id) + .ok_or(PlatformWalletError::IdentityNotFound(*identity_id))?; + if identity.wallet_id != Some(self.wallet_id()) { + return Err(PlatformWalletError::InvalidIdentityData( + "Tip account requires a wallet-owned identity".to_string(), + )); + } + let index = identity.identity_index.ok_or_else(|| { + PlatformWalletError::InvalidIdentityData( + "Tip account requires a recoverable identity index".to_string(), + ) + })?; + verify_identity_index(seed, self.sdk.network, index, &identity.identity)?; + shielded_identity_account_index(index) + } +} + +/// Test-only pause point between tip preparation's account snapshot and +/// its bind, keyed by wallet instance (shared by its clones) so parallel +/// tests built from the same mnemonic do not interfere. +#[cfg(test)] +pub(crate) mod test_hooks { + use super::PlatformWallet; + use std::collections::HashMap; + use std::sync::{Arc, Mutex}; + use tokio::sync::oneshot; + + type Hook = (oneshot::Sender<()>, oneshot::Receiver<()>); + #[derive(Clone, Copy, PartialEq, Eq, Hash)] + enum Stage { + Snapshot, + } + static HOOKS: Mutex>> = Mutex::new(None); + + fn key(wallet: &PlatformWallet) -> usize { + Arc::as_ptr(&wallet.shielded_config_lock) as usize + } + + fn pause( + wallet: &PlatformWallet, + stage: Stage, + ) -> (oneshot::Receiver<()>, oneshot::Sender<()>) { + let (reached_tx, reached_rx) = oneshot::channel(); + let (resume_tx, resume_rx) = oneshot::channel(); + HOOKS + .lock() + .unwrap() + .get_or_insert_with(HashMap::new) + .insert((key(wallet), stage), (reached_tx, resume_rx)); + (reached_rx, resume_tx) + } + + async fn reach(wallet: &PlatformWallet, stage: Stage) { + let hook = HOOKS + .lock() + .unwrap() + .as_mut() + .and_then(|hooks| hooks.remove(&(key(wallet), stage))); + if let Some((reached, resume)) = hook { + let _ = reached.send(()); + let _ = resume.await; + } + } + + pub(crate) fn pause_after_snapshot( + wallet: &PlatformWallet, + ) -> (oneshot::Receiver<()>, oneshot::Sender<()>) { + pause(wallet, Stage::Snapshot) + } + + pub(crate) async fn after_snapshot(wallet: &PlatformWallet) { + reach(wallet, Stage::Snapshot).await; + } +} + +#[cfg(test)] +mod tests { + use super::super::OrchardKeySet; + use super::*; + use key_wallet::Network; + + #[test] + fn should_derive_distinct_recoverable_tip_accounts() { + let seed = [42u8; 64]; + let ordinary = OrchardKeySet::from_seed(&seed, Network::Testnet, 0).unwrap(); + let index = shielded_identity_account_index(0).unwrap(); + let tip = OrchardKeySet::from_seed(&seed, Network::Testnet, index).unwrap(); + let restored = OrchardKeySet::from_seed( + &seed, + Network::Testnet, + shielded_identity_account_index(0).unwrap(), + ) + .unwrap(); + let other = OrchardKeySet::from_seed( + &seed, + Network::Testnet, + shielded_identity_account_index(1).unwrap(), + ) + .unwrap(); + assert_ne!( + ordinary.full_viewing_key.to_bytes(), + tip.full_viewing_key.to_bytes() + ); + assert_ne!( + tip.default_address.to_raw_address_bytes(), + other.default_address.to_raw_address_bytes() + ); + assert_eq!( + tip.full_viewing_key.to_bytes(), + restored.full_viewing_key.to_bytes() + ); + assert!(!is_shielded_identity_account(0)); + assert!(is_shielded_identity_account(index)); + assert!(shielded_identity_account_index(SHIELDED_IDENTITY_ACCOUNT_BASE).is_err()); + } +} diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 0f2b2cadada..6e29c634a92 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -38,6 +38,8 @@ use crate::wallet::shielded::{ #[derive(Default)] struct CapturingPersistence { stored: Mutex>, + durability_events: Mutex>, + fail_flush: std::sync::atomic::AtomicBool, serve: Mutex>>, serve_subwallets: Mutex>, load_calls: Mutex, @@ -112,11 +114,18 @@ impl PlatformWalletPersistence for CapturingPersistence { _wallet_id: WalletId, changeset: PlatformWalletChangeSet, ) -> Result<(), PersistenceError> { + self.durability_events.lock().unwrap().push("store"); self.stored.lock().expect("stored lock").push(changeset); Ok(()) } fn flush(&self, _wallet_id: WalletId) -> Result<(), PersistenceError> { + self.durability_events.lock().unwrap().push("flush"); + if self.fail_flush.load(std::sync::atomic::Ordering::SeqCst) { + return Err(PersistenceError::backend(std::io::Error::other( + "injected flush failure", + ))); + } Ok(()) } @@ -1162,3 +1171,630 @@ async fn fresh_address_and_own_check_follow_the_bound_account() { ); assert_eq!(wallet.is_own_shielded_address(1, &fresh).await, None); } + +/// The MASTER authentication key this seed derives for `identity_index` on +/// testnet, as an identity public-key map: what discovery and registration +/// leave on a wallet-owned identity, and what tip preparation verifies. +fn seed_master_key( + seed: &[u8], + identity_index: u32, +) -> BTreeMap { + use dpp::identity::identity_public_key::v0::IdentityPublicKeyV0; + use dpp::identity::{IdentityPublicKey, KeyType, Purpose, SecurityLevel}; + use dpp::platform_value::BinaryData; + let master = key_wallet::bip32::ExtendedPrivKey::new_master(Network::Testnet, seed).unwrap(); + let derived = crate::wallet::identity::network::derive_ecdsa_identity_auth_keypair_from_master( + &master, + Network::Testnet, + identity_index, + crate::wallet::identity::network::MASTER_KEY_INDEX, + ) + .unwrap(); + BTreeMap::from([( + 0, + IdentityPublicKey::V0(IdentityPublicKeyV0 { + id: 0, + purpose: Purpose::AUTHENTICATION, + security_level: SecurityLevel::MASTER, + contract_bounds: None, + key_type: KeyType::ECDSA_SECP256K1, + read_only: false, + data: BinaryData::new(derived.public_key.to_vec()), + disabled_at: None, + }), + )]) +} + +/// Cold reload of a host that cannot store "index unknown": a wallet-attached +/// identity whose index was lost comes back at placeholder slot 0 while +/// identity 0's tip account (and its funds) is retained and bound. Tip +/// preparation must refuse it instead of handing out identity 0's address, +/// and must accept it once rediscovery proves its real index. +#[tokio::test] +async fn should_refuse_tip_account_for_a_placeholder_index_until_it_is_verified() { + use crate::shielded_identity_account_index; + use dpp::identity::{Identity, IdentityV0}; + use dpp::prelude::Identifier; + let phrase = crate::test_support::MESSAGE_SIGNING_TEST_MNEMONIC; + let seed = key_wallet::Mnemonic::from_phrase(phrase) + .unwrap() + .to_seed(""); + let (wallet_manager, wallet_id, _, _) = + crate::test_support::mnemonic_wallet_manager(phrase).await; + let generation = wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .unwrap() + .generation + .clone(); + let spv = Arc::new(crate::spv::SpvRuntime::new( + Arc::clone(&wallet_manager), + Arc::new(crate::events::PlatformEventManager::new(Vec::new())), + )); + let sdk = dash_sdk::SdkBuilder::new_mock() + .with_network(Network::Testnet) + .build() + .unwrap(); + let persister = Arc::new(CapturingPersistence::default()); + let wallet = PlatformWallet::new( + Arc::new(sdk), + wallet_id, + wallet_manager, + generation, + Arc::new(tokio::sync::Notify::new()), + persister.clone(), + Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), + ); + // Restored at the host's placeholder 0, but its keys are identity 5's. + let id = Identifier::from([0x55; 32]); + { + let mut wm = wallet.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager + .add_identity( + Identity::V0(IdentityV0 { + id, + public_keys: seed_master_key(&seed[..], 5), + balance: 0, + revision: 0, + }), + 0, + wallet.wallet_id(), + wallet.persister(), + ) + .unwrap(); + } + let coordinator = coordinator_at(&temp_dir("tips_placeholder_index")); + let identity_zero_tips = shielded_identity_account_index(0).unwrap(); + wallet + .bind_shielded(&seed[..], &[0, identity_zero_tips], &coordinator) + .await + .unwrap(); + let identity_zero_address = wallet + .shielded_default_address(identity_zero_tips) + .await + .unwrap(); + + let refused = wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await; + assert!( + matches!(refused, Err(crate::PlatformWalletError::InvalidIdentityData(ref msg)) if msg.contains("not verified")), + "{refused:?}" + ); + + // Rediscovery proves index 5, and the identity is stored at that slot. + { + let mut wm = wallet.wallet_manager.write().await; + let manager = &mut wm + .get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager; + let identity = manager.remove_identity(&id, wallet.persister()).unwrap(); + manager + .add_identity(identity, 5, wallet.wallet_id(), wallet.persister()) + .unwrap(); + } + let address = wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await + .unwrap(); + assert_ne!(address, identity_zero_address); + assert_eq!( + wallet + .shielded_default_address(shielded_identity_account_index(5).unwrap()) + .await + .unwrap(), + address + ); + assert_eq!( + wallet.prepared_shielded_identity_account(&id), + Some(shielded_identity_account_index(5).unwrap()) + ); +} + +/// A profile is not a recovery record. Identity discovery alone must restore +/// the reserved account, even when the owner removed the tip address entirely. +#[tokio::test] +async fn should_restore_tip_account_from_identity_discovery_and_register_for_sync() { + use crate::shielded_identity_account_index; + use dpp::identity::{Identity, IdentityV0}; + use dpp::prelude::Identifier; + let phrase = crate::test_support::MESSAGE_SIGNING_TEST_MNEMONIC; + let seed = key_wallet::Mnemonic::from_phrase(phrase) + .unwrap() + .to_seed(""); + let id = Identifier::from([0x33; 32]); + let account = shielded_identity_account_index(3).unwrap(); + let mut original = None; + for session in 0..2 { + let persister = Arc::new(CapturingPersistence::default()); + let (wallet_manager, wallet_id, _, _) = + crate::test_support::mnemonic_wallet_manager(phrase).await; + let generation = wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .unwrap() + .generation + .clone(); + let spv = Arc::new(crate::spv::SpvRuntime::new( + Arc::clone(&wallet_manager), + Arc::new(crate::events::PlatformEventManager::new(Vec::new())), + )); + let sdk = dash_sdk::SdkBuilder::new_mock() + .with_network(Network::Testnet) + .build() + .unwrap(); + let wallet = PlatformWallet::new( + Arc::new(sdk), + wallet_id, + wallet_manager, + generation, + Arc::new(tokio::sync::Notify::new()), + persister.clone(), + Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), + ); + { + let mut wm = wallet.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager + .add_identity( + Identity::V0(IdentityV0 { + id, + public_keys: seed_master_key(&seed[..], 3), + balance: 0, + revision: 0, + }), + 3, + wallet.wallet_id(), + wallet.persister(), + ) + .unwrap(); + } + { + let mut wm = wallet.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager + .add_identity( + Identity::V0(IdentityV0 { + id: Identifier::from([0x44; 32]), + public_keys: BTreeMap::new(), + balance: 0, + revision: 0, + }), + crate::SHIELDED_IDENTITY_ACCOUNT_BASE, + wallet.wallet_id(), + wallet.persister(), + ) + .unwrap(); + } + let coordinator = coordinator_at(&temp_dir(&format!("tips_restore_{session}"))); + // Fresh database, no viewing keys, no published profile, and caller + // only knows about ordinary account zero. + assert!(!wallet + .bind_shielded_from_persisted(&[0], &coordinator) + .await + .unwrap()); + // Upgrading an existing wallet can leave account zero persisted while + // identity discovery introduces a tip account without an FVK. Seedless + // success here would suppress the host's seed fallback and lose scans + // for that identity's tip history. + let ordinary = super::OrchardKeySet::from_seed(&seed[..], Network::Testnet, 0).unwrap(); + persister.serve_viewing_keys(BTreeMap::from([( + SubwalletId::new(wallet.wallet_id(), 0), + ordinary.full_viewing_key.to_bytes().to_vec(), + )])); + assert!(!wallet + .bind_shielded_from_persisted(&[0], &coordinator) + .await + .unwrap()); + assert!(!wallet.is_shielded_bound().await); + assert!(wallet + .prepare_shielded_identity_address(&[0x11; 64], &id, &coordinator) + .await + .is_err()); + assert!(matches!( + wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await, + Err(crate::PlatformWalletError::ShieldedNotBound) + )); + assert!(!wallet.is_shielded_bound().await); + assert!(persister.captured_viewing_keys().is_empty()); + assert!(coordinator.registered_subwallets().await.is_empty()); + wallet + .bind_shielded(&seed[..], &[0], &coordinator) + .await + .unwrap(); + assert!(coordinator + .registered_subwallets() + .await + .contains(&SubwalletId::new(wallet.wallet_id(), account))); + assert!(wallet + .prepare_shielded_identity_address(&[0x11; 64], &id, &coordinator) + .await + .is_err()); + // A queued FVK is insufficient: no publishable address may escape a + // failed durability barrier, even though bind installed keys in memory. + persister.durability_events.lock().unwrap().clear(); + persister + .fail_flush + .store(true, std::sync::atomic::Ordering::SeqCst); + assert!(matches!( + wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await, + Err(crate::PlatformWalletError::Persistence(_)) + )); + assert_eq!( + *persister.durability_events.lock().unwrap(), + ["store", "flush"] + ); + assert!(wallet.shielded_default_address(account).await.is_some()); + persister + .fail_flush + .store(false, std::sync::atomic::Ordering::SeqCst); + persister.durability_events.lock().unwrap().clear(); + let address = wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await + .unwrap(); + assert!(wallet + .prepare_shielded_identity_address( + &seed[..], + &Identifier::from([0x44; 32]), + &coordinator + ) + .await + .is_err()); + assert_eq!( + *persister.durability_events.lock().unwrap(), + ["store", "flush"] + ); + assert_ne!(wallet.shielded_default_address(0).await.unwrap(), address); + assert_eq!( + wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await + .unwrap(), + address + ); + assert!(persister + .captured_viewing_keys() + .contains_key(&SubwalletId::new(wallet.wallet_id(), account))); + if let Some(first) = original { + assert_eq!(first, address); + } else { + original = Some(address); + } + } +} + +#[tokio::test] +async fn should_rebind_retired_tip_accounts_without_profile_or_identity() { + let seed = [0x42; 64]; + let account = crate::shielded_identity_account_index(7).unwrap(); + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let keys = super::OrchardKeySet::from_seed(&seed[..], Network::Testnet, account).unwrap(); + let ordinary = super::OrchardKeySet::from_seed(&seed[..], Network::Testnet, 0).unwrap(); + persister.serve_viewing_keys(BTreeMap::from([ + ( + SubwalletId::new(wallet.wallet_id(), 0), + ordinary.full_viewing_key.to_bytes().to_vec(), + ), + ( + SubwalletId::new(wallet.wallet_id(), account), + keys.full_viewing_key.to_bytes().to_vec(), + ), + ])); + let coordinator = coordinator_at(&temp_dir("retired_tips")); + assert!(wallet + .bind_shielded_from_persisted(&[0], &coordinator) + .await + .unwrap()); + assert_eq!( + wallet.shielded_default_address(account).await.unwrap(), + keys.default_address.to_raw_address_bytes() + ); + assert!(coordinator + .registered_subwallets() + .await + .contains(&SubwalletId::new(wallet.wallet_id(), account))); +} + +/// A host rebind passes only ordinary accounts (FFI and host SDKs cap the +/// request at 64 entries); Rust adds every persisted tip account itself. With +/// more than 64 tip accounts on record, a `[0]` request must still bind them +/// all, both seedlessly and from the seed, and the resulting +/// `shielded_account_indices` snapshot exceeds the request cap, which is why +/// hosts must not echo it back as a bind request. +#[tokio::test] +async fn should_bind_more_than_64_persisted_tip_accounts_from_an_ordinary_request() { + let seed = [0x42; 64]; + let tips: Vec = (0..70) + .map(|index| crate::shielded_identity_account_index(index).unwrap()) + .collect(); + for (tag, seeded) in [("many_tips_persisted", false), ("many_tips_seeded", true)] { + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let mut rows = BTreeMap::new(); + for &account in std::iter::once(&0).chain(tips.iter()) { + let keys = + super::OrchardKeySet::from_seed(&seed[..], wallet.sdk().network, account).unwrap(); + rows.insert( + SubwalletId::new(wallet.wallet_id(), account), + keys.full_viewing_key.to_bytes().to_vec(), + ); + } + persister.serve_viewing_keys(rows); + let coordinator = coordinator_at(&temp_dir(tag)); + if seeded { + wallet + .bind_shielded(&seed[..], &[0], &coordinator) + .await + .unwrap(); + } else { + assert!(wallet + .bind_shielded_from_persisted(&[0], &coordinator) + .await + .unwrap()); + } + let bound = wallet.shielded_account_indices().await; + assert_eq!(bound.len(), 71, "{tag}"); + assert!( + bound.len() > 64, + "{tag}: the snapshot exceeds the request cap" + ); + let ordinary: Vec = bound + .iter() + .copied() + .filter(|&account| !super::is_shielded_identity_account(account)) + .collect(); + assert_eq!(ordinary, vec![0], "{tag}"); + let registered = coordinator.registered_subwallets().await; + for &account in &tips { + assert!( + registered.contains(&SubwalletId::new(wallet.wallet_id(), account)), + "{tag}: tip account {account} registered for sync" + ); + } + } +} + +/// A wallet bound to account 0 whose identity at index 2 can prepare a tip +/// address, for racing preparation against other account-set writers. +async fn tip_race_fixture( + tag: &str, +) -> ( + PlatformWallet, + [u8; 64], + dpp::prelude::Identifier, + Arc, +) { + use dpp::identity::{Identity, IdentityV0}; + use dpp::prelude::Identifier; + let phrase = crate::test_support::MESSAGE_SIGNING_TEST_MNEMONIC; + let seed = key_wallet::Mnemonic::from_phrase(phrase) + .unwrap() + .to_seed(""); + let (wallet_manager, wallet_id, _, _) = + crate::test_support::mnemonic_wallet_manager(phrase).await; + let generation = wallet_manager + .read() + .await + .get_wallet_info(&wallet_id) + .unwrap() + .generation + .clone(); + let spv = Arc::new(crate::spv::SpvRuntime::new( + Arc::clone(&wallet_manager), + Arc::new(crate::events::PlatformEventManager::new(Vec::new())), + )); + let mock_dir = temp_dir(tag); + std::fs::create_dir_all(&mock_dir).unwrap(); + let sdk = dash_sdk::SdkBuilder::new_mock() + .with_network(Network::Testnet) + .with_dump_dir(&mock_dir) + .with_version(dpp::version::PlatformVersion::latest()) + .build() + .unwrap(); + let persister = Arc::new(CapturingPersistence::default()); + let wallet = PlatformWallet::new( + Arc::new(sdk), + wallet_id, + wallet_manager, + generation, + Arc::new(tokio::sync::Notify::new()), + persister.clone(), + Arc::new(crate::broadcaster::SpvBroadcaster::new(spv)), + Arc::new(std::sync::atomic::AtomicBool::new(false)), + crate::changeset::DurableCursors::default(), + ); + let id = Identifier::from([0x66; 32]); + { + let mut wm = wallet.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager + .add_identity( + Identity::V0(IdentityV0 { + id, + public_keys: seed_master_key(&seed[..], 2), + balance: 0, + revision: 0, + }), + 2, + wallet.wallet_id(), + wallet.persister(), + ) + .unwrap(); + } + let coordinator = coordinator_at(&temp_dir(tag)); + wallet + .bind_shielded(&seed[..], &[0], &coordinator) + .await + .unwrap(); + (wallet, *seed, id, coordinator) +} + +/// Pause tip preparation right after its account snapshot, run `writer` +/// against the same wallet, and require the writer to wait for preparation. +async fn race_tip_preparation( + wallet: &PlatformWallet, + seed: [u8; 64], + id: dpp::prelude::Identifier, + coordinator: &Arc, + writer: F, +) where + F: FnOnce(PlatformWallet) -> Fut, + Fut: std::future::Future> + Send + 'static, +{ + let (reached, resume) = super::tips::test_hooks::pause_after_snapshot(wallet); + let preparing = { + let wallet = wallet.clone(); + let coordinator = Arc::clone(coordinator); + tokio::spawn(async move { + wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await + }) + }; + reached.await.expect("preparation reached its snapshot"); + let mut writing = tokio::spawn(writer(wallet.clone())); + // Unserialized, the writer finishes now and the resumed preparation then + // rebinds its stale {0} plus the tip account, dropping account 1. + let finished_early = + tokio::time::timeout(std::time::Duration::from_millis(500), &mut writing).await; + assert!( + finished_early.is_err(), + "an account-set writer must wait for tip preparation's read-modify-write" + ); + resume.send(()).unwrap(); + preparing.await.unwrap().expect("tip address prepared"); + writing.await.unwrap().expect("account-set writer succeeds"); +} + +/// Tip preparation reads the bound account set and binds it plus the tip +/// account. A bind of ordinary accounts landing between those two steps must +/// not be undone by the stale snapshot: preparation is paused right after its +/// snapshot while another task binds accounts 0 and 1, and account 1 must +/// still be bound and registered once both finish. +#[tokio::test] +async fn should_keep_an_ordinary_account_bound_during_tip_preparation() { + use crate::shielded_identity_account_index; + let (wallet, seed, id, coordinator) = tip_race_fixture("tips_concurrent_bind").await; + let tip = shielded_identity_account_index(2).unwrap(); + let bind_coordinator = Arc::clone(&coordinator); + race_tip_preparation(&wallet, seed, id, &coordinator, move |wallet| async move { + wallet + .bind_shielded(&seed[..], &[0, 1], &bind_coordinator) + .await + }) + .await; + + let bound = wallet.shielded_account_indices().await; + for account in [0, 1, tip] { + assert!( + bound.contains(&account), + "account {account} bound: {bound:?}" + ); + } + let registered = coordinator.registered_subwallets().await; + for account in [0, 1, tip] { + assert!( + registered.contains(&SubwalletId::new(wallet.wallet_id(), account)), + "account {account} registered" + ); + } +} + +/// `shielded_add_account` writes the same account set, so it must not land +/// between preparation's snapshot and its bind either, or that bind replaces +/// the key map without the added account. +#[tokio::test] +async fn should_keep_an_added_account_during_tip_preparation() { + use crate::shielded_identity_account_index; + let (wallet, seed, id, coordinator) = tip_race_fixture("tips_concurrent_add").await; + let tip = shielded_identity_account_index(2).unwrap(); + race_tip_preparation(&wallet, seed, id, &coordinator, move |wallet| async move { + wallet.shielded_add_account(&seed[..], 1).await + }) + .await; + + let bound = wallet.shielded_account_indices().await; + for account in [0, 1, tip] { + assert!( + bound.contains(&account), + "account {account} bound: {bound:?}" + ); + } +} + +/// A Clear drops the coordinator's state for the account, so the preparation no +/// longer stands, and a rebind of the ordinary accounts does not bring it back: the +/// identity's account is proved and prepared again. +#[tokio::test] +async fn should_require_fresh_tip_preparation_after_clear_even_after_rebind() { + let (wallet, seed, id, coordinator) = tip_race_fixture("tips_clear").await; + let address = wallet + .prepare_shielded_identity_address(&seed, &id, &coordinator) + .await + .unwrap(); + let account = wallet + .prepared_shielded_identity_account(&id) + .expect("the preparation stands"); + assert_eq!( + wallet.shielded_default_address(account).await, + Some(address) + ); + + coordinator.clear().await.unwrap(); + assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + wallet + .bind_shielded(&seed, &[0], &coordinator) + .await + .unwrap(); + assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + + assert_eq!( + wallet + .prepare_shielded_identity_address(&seed, &id, &coordinator) + .await + .unwrap(), + address + ); + assert_eq!( + wallet.prepared_shielded_identity_account(&id), + Some(account) + ); +} From 148a215e63b62c984634240a28a63b5030c40fe5 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 16:04:00 +0700 Subject: [PATCH 2/7] fix(platform-wallet): refuse an identity account preparation on a coordinator of another network `prepare_shielded_identity_address` derives the account's keys for the wallet's network and binds them to the coordinator it is given. A coordinator of another network would scan the wrong chain while the address was returned and recorded as prepared: the bind does not compare the networks, and the check in the private payment restoration that follows it only logs. The preparation now compares the two networks first and refuses a mismatch before it takes a lock, binds, stores or records anything. Co-Authored-By: Claude Fable 5.2 --- .../src/wallet/shielded/tips.rs | 10 ++++++ .../wallet/shielded/viewing_key_bind_tests.rs | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs index ec71267745f..6adbcb442b9 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs @@ -112,6 +112,16 @@ impl PlatformWallet { identity_id: &Identifier, coordinator: &Arc, ) -> Result<[u8; 43], PlatformWalletError> { + // The account's keys are derived for this wallet's network. Bound to + // a coordinator of another network they would scan the wrong chain, + // while the address was handed out and recorded as prepared. + if coordinator.network() != self.sdk.network { + return Err(PlatformWalletError::InvalidParameter(format!( + "the shielded coordinator is for {:?}, this wallet for {:?}", + coordinator.network(), + self.sdk.network + ))); + } // Do not publish a key from a mis-associated mnemonic. A first bind // has no persisted FVK against which to detect a wrong seed. let root = key_wallet::wallet::root_extended_keys::RootExtendedPrivKey::new_master(seed) diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 6e29c634a92..8fe70dbd289 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -1760,6 +1760,39 @@ async fn should_keep_an_added_account_during_tip_preparation() { } } +/// The identity account's keys are derived for the wallet's network, so a +/// coordinator of another network is refused before anything is bound, +/// stored or recorded as prepared. +#[tokio::test] +async fn should_refuse_tip_preparation_on_a_coordinator_of_another_network() { + let (wallet, seed, id, _coordinator) = tip_race_fixture("tips_other_network").await; + let dir = temp_dir("tips_other_network_mainnet"); + std::fs::create_dir_all(&dir).expect("create temp dir"); + let db_path = dir.join("tree.sqlite"); + let store = FileBackedShieldedStore::open_path(&db_path, 100).expect("open file store"); + let mainnet = Arc::new(NetworkShieldedCoordinator::new( + Arc::new(dash_sdk::Sdk::new_mock()), + Network::Mainnet, + db_path, + store, + )); + let bound_before = wallet.shielded_account_indices().await; + + let refused = wallet + .prepare_shielded_identity_address(&seed, &id, &mainnet) + .await; + + assert!( + matches!( + refused, + Err(crate::PlatformWalletError::InvalidParameter(_)) + ), + "{refused:?}" + ); + assert_eq!(wallet.shielded_account_indices().await, bound_before); + assert_eq!(wallet.prepared_shielded_identity_account(&id), None); +} + /// A Clear drops the coordinator's state for the account, so the preparation no /// longer stands, and a rebind of the ordinary accounts does not bring it back: the /// identity's account is proved and prepared again. From 223c3155b375ad783482c7185cbd4fb56a546ffa Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 18:10:51 +0700 Subject: [PATCH 3/7] fix(sdk): the example apps count only account 0 as the spendable shielded balance A bind now also binds the shielded account of each identity the wallet holds, so a wallet's notes are no longer all account 0's. The example apps spend account 0 (`account: 0`) but summed every unspent note of the wallet as what a shielded send or a shielded identity funding can spend: funds on an identity account made a send look funded that account 0 could not pay. - Swift: the send screen and the identity creation screen query the unspent notes of account 0 (`shieldedSourcePredicate`, `shieldedFundingPredicate`). - Kotlin: the send screen reads `observeUnspentNotesByWalletAccount( walletId, 0)`, a new DAO query with its test. The wallet detail and wallet list totals stay wallet-wide: they show a total and spend nothing. These are #5288's host changes for the same reason, unchanged. Co-authored-by: pasta Co-Authored-By: Claude Fable 5.2 --- .../ui/wallet/SendTransactionScreen.kt | 5 ++-- .../dashsdk/persistence/dao/ShieldedDao.kt | 6 +++++ .../dashsdk/persistence/DashDatabaseTest.kt | 26 +++++++++++++++++++ .../Core/Views/SendTransactionView.swift | 12 ++++++--- .../Views/CreateIdentityView.swift | 14 +++++++--- 5 files changed, 53 insertions(+), 10 deletions(-) diff --git a/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/wallet/SendTransactionScreen.kt b/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/wallet/SendTransactionScreen.kt index e1c133c12f2..a2a9664b029 100644 --- a/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/wallet/SendTransactionScreen.kt +++ b/packages/kotlin-sdk/KotlinExampleApp/app/src/main/java/org/dashfoundation/example/ui/wallet/SendTransactionScreen.kt @@ -192,12 +192,11 @@ fun SendTransactionScreen( } } - // Shielded credits — unspent-note sum from Room, only on a shielded - // build (same source WalletDetailScreen's Shielded row reads). + // Ordinary shielded sends spend account zero. val hasShielded = remember { Sdk.hasShielded() } val shieldedBalance by remember(walletIdHex) { if (hasShielded) { - container.database.shieldedDao().observeUnspentNotesByWallet(walletId) + container.database.shieldedDao().observeUnspentNotesByWalletAccount(walletId, 0) .map { notes -> notes.sumOf { it.value } } } else { MutableStateFlow(0L) diff --git a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt index 9e7ed9141e6..a5fbee6aa97 100644 --- a/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt +++ b/packages/kotlin-sdk/sdk/src/main/kotlin/org/dashfoundation/dashsdk/persistence/dao/ShieldedDao.kt @@ -53,6 +53,12 @@ interface ShieldedDao { @Query("SELECT * FROM shielded_notes WHERE walletId = :walletId AND isSpent = 0") fun observeUnspentNotesByWallet(walletId: ByteArray): Flow> + @Query("SELECT * FROM shielded_notes WHERE walletId = :walletId AND accountIndex = :accountIndex AND isSpent = 0") + fun observeUnspentNotesByWalletAccount( + walletId: ByteArray, + accountIndex: Int, + ): Flow> + @Upsert suspend fun upsertNote(note: ShieldedNoteEntity) diff --git a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt index ac1ae6f77ae..268df9d62de 100644 --- a/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt +++ b/packages/kotlin-sdk/sdk/src/test/kotlin/org/dashfoundation/dashsdk/persistence/DashDatabaseTest.kt @@ -5,6 +5,7 @@ import kotlinx.coroutines.flow.first import kotlinx.coroutines.test.runTest import org.dashfoundation.dashsdk.persistence.entities.AccountEntity import org.dashfoundation.dashsdk.persistence.entities.IdentityEntity +import org.dashfoundation.dashsdk.persistence.entities.ShieldedNoteEntity import org.dashfoundation.dashsdk.persistence.entities.ShieldedViewingKeyEntity import org.dashfoundation.dashsdk.persistence.entities.TokenBalanceEntity import org.dashfoundation.dashsdk.persistence.entities.TokenEntity @@ -42,6 +43,31 @@ class DashDatabaseTest { db.close() } + @Test + fun ordinarySendBalanceIncludesOnlyUnspentAccountZeroNotes() = runTest { + val dao = db.shieldedDao() + fun note(id: Int, account: Int, value: Long, spent: Boolean = false, wallet: ByteArray = walletId) = + ShieldedNoteEntity( + nullifier = ByteArray(32) { id.toByte() }, + walletId = wallet, + accountIndex = account, + position = id.toLong(), + cmx = ByteArray(32), + blockHeight = 1, + isSpent = spent, + value = value, + noteData = ByteArray(115), + ) + dao.upsertNote(note(1, 0x40000000, 900)) + assertEquals(0L, dao.observeUnspentNotesByWalletAccount(walletId, 0).first().sumOf { it.value }) + dao.upsertNote(note(2, 0, 100)) + dao.upsertNote(note(3, 0, 200)) + dao.upsertNote(note(4, 1, 500)) + dao.upsertNote(note(5, 0, 700, spent = true)) + dao.upsertNote(note(6, 0, 800, wallet = ByteArray(32) { 2 })) + assertEquals(300L, dao.observeUnspentNotesByWalletAccount(walletId, 0).first().sumOf { it.value }) + } + @Test fun walletRoundTripsThroughUpsertAndFlow() = runTest { val wallet = WalletEntity( diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/SendTransactionView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/SendTransactionView.swift index de48fdf33bb..08f8aa7d1b0 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/SendTransactionView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Core/Views/SendTransactionView.swift @@ -52,7 +52,7 @@ struct SendTransactionView: View { /// credits". @Query private var syncStates: [PersistentPlatformAddressesSyncState] - /// This wallet's unspent shielded (Orchard) notes. Summed into + /// This wallet's unspent account-0 shielded (Orchard) notes. Summed into /// `shieldedBalance` below so the shielded source row reflects THIS /// wallet's own pool, not the single-mirror `shieldedService`. @Query private var shieldedNotes: [PersistentShieldedNote] @@ -71,10 +71,16 @@ struct SendTransactionView: View { } ) _shieldedNotes = Query( - filter: PersistentShieldedNote.unspentPredicate(walletId: walletId) + filter: Self.shieldedSourcePredicate(walletId: walletId) ) } + static func shieldedSourcePredicate(walletId: Data) -> Predicate { + #Predicate { + $0.walletId == walletId && $0.accountIndex == 0 && $0.isSpent == false + } + } + var body: some View { // Snapshot Core balance once per render — `coreBalance` goes // through a blocking FFI call (`accountBalances(for:)`); the @@ -486,7 +492,7 @@ struct SendTransactionView: View { .reduce(0) { $0 + $1.confirmed } } - /// Per-wallet shielded balance: sum of THIS wallet's unspent + /// Account-0 shielded balance: sum of THIS wallet's unspent /// `PersistentShieldedNote` values (Rust pushes note rows via the /// shielded persister). Reads SwiftData rather than the /// single-mirror `shieldedService.shieldedBalance`, so the shielded diff --git a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/CreateIdentityView.swift b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/CreateIdentityView.swift index 5c5d8974477..cf4c3084c19 100644 --- a/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/CreateIdentityView.swift +++ b/packages/swift-sdk/SwiftExampleApp/SwiftExampleApp/Views/CreateIdentityView.swift @@ -248,7 +248,7 @@ struct CreateIdentityView: View { @Query(sort: [SortDescriptor(\PersistentAssetLock.updatedAt, order: .reverse)]) private var allAssetLocks: [PersistentAssetLock] - /// All wallets' unspent shielded notes. The shielded funding + /// All wallets' unspent account-0 shielded notes. The shielded funding /// picker spans multiple wallets, so this uses the all-wallets /// predicate and `shieldedPoolBalance(for:)` scopes by walletId in /// memory — a single-walletId `@Query` couldn't serve every row @@ -256,9 +256,15 @@ struct CreateIdentityView: View { /// `modelContext.fetch`) makes the funding option and denomination /// list update live: rows arriving while this sheet is open /// re-render the view. - @Query(filter: PersistentShieldedNote.unspentPredicate) + @Query(filter: CreateIdentityView.shieldedFundingPredicate) private var unspentShieldedNotes: [PersistentShieldedNote] + static var shieldedFundingPredicate: Predicate { + #Predicate { + $0.accountIndex == 0 && $0.isSpent == false + } + } + // MARK: - Selection state /// The source wallet selection. `nil` encodes "pick nothing yet"; @@ -1820,8 +1826,8 @@ struct CreateIdentityView: View { && shieldedFallbackAddressBytes(for: walletId) != nil } - /// Per-wallet shielded pool balance: sum of `walletId`'s unspent - /// `PersistentShieldedNote` values, filtered in memory from the + /// Per-wallet shielded funding balance: sum of `walletId`'s unspent + /// account-0 `PersistentShieldedNote` values, filtered in memory from the /// `unspentShieldedNotes` `@Query` (rather than the single-mirror /// `shieldedService.shieldedBalance`). Correct for ANY loaded /// wallet, not just the mirror's `firstWallet`. From fa5d0221c9beb707c9359cc4227911dc56a068ad Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 18:10:51 +0700 Subject: [PATCH 4/7] fix(platform-wallet): identity account seed proof, Clear ordering and bind network check Review fixes for the identity shielded account. - The seed proof of an identity's index accepted any key carrying the derived hash. Only an ECDSA MASTER authentication key counts now: a hash-only key, or a key of another purpose or level, can carry the hash on an identity whose master key is another wallet's or another index's. A disabled master key still counts, since rotating it does not change the index the identity was created at. - A preparation sampled the coordinator's clear generation after it was admitted. One that waited behind a Clear therefore started from the post-Clear generation, while the host wipes its own rows only after that Clear returns: its bind restored the notes the Clear removed. The generation is sampled before the first wait and the preparation is refused if it changed by admission. - Both bind routes and the preparation refuse a coordinator of another network than the wallet's, through one check (`NetworkShieldedCoordinator::ensure_network`), before anything is read, derived, stored or registered. The bind test fixture built its wallet on the bare mock SDK, a mainnet one, against testnet coordinators; it is on the testnet mock SDK now. - Both binds report a failed load with `from_load_failure` and the preparation a failed flush with `from_flush_failure`. - An identity without a recorded index returns `IdentityIndexNotSet`. - An identity whose index has no identity account is logged when a bind skips it. - The two account-writer race tests run on a paused clock, so their timeout fires only once nothing else is runnable. - The bind docs say that identity accounts are added to the request. Co-Authored-By: Claude Fable 5.2 --- .../src/wallet/platform_wallet.rs | 37 ++++- .../src/wallet/shielded/coordinator.rs | 17 ++ .../src/wallet/shielded/tips.rs | 60 ++++--- .../wallet/shielded/viewing_key_bind_tests.rs | 154 ++++++++++++++++-- 4 files changed, 226 insertions(+), 42 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 5103ad84ea1..01e3a5f1998 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -883,7 +883,21 @@ impl PlatformWallet { /// shielded wallet (e.g. after a network switch). /// /// `accounts` must be non-empty; pass `&[0]` for the - /// single-account default. + /// single-account default. They are the ordinary accounts to bind, + /// not the whole bound set. The bind adds to them: + /// + /// - the identity account (`shielded_identity_account_index`) of + /// every identity this wallet holds with a recorded index; + /// - every identity account with a persisted viewing key, so the + /// account of an identity removed since keeps scanning. + /// + /// The bound set can therefore be larger than the request, and each + /// added account is one more to scan. Pass the ordinary accounts + /// again on the next bind, never the bound set: it can hold more + /// accounts than the host bindings accept in one request. + /// + /// Fails, before anything is derived, stored or registered, for a + /// `coordinator` of another network than this wallet's. /// /// [`bind_shielded_from_persisted`]: Self::bind_shielded_from_persisted /// [`SpendingKey::from_zip32_seed`]: grovedb_commitment_tree::SpendingKey::from_zip32_seed @@ -908,6 +922,7 @@ impl PlatformWallet { coordinator: &Arc, ) -> Result<(), PlatformWalletError> { use super::shielded::{AccountViewingKeys, OrchardKeySet, SubwalletId}; + coordinator.ensure_network(self.sdk.network)?; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; let capabilities = self.persister.persistence_capabilities(); if !capabilities.contains(required) { @@ -939,7 +954,7 @@ impl PlatformWallet { let start = self .persister .load() - .map_err(|e| PlatformWalletError::Persistence(e.to_string()))?; + .map_err(PlatformWalletError::from_load_failure)?; accounts.extend( start .shielded @@ -1029,6 +1044,14 @@ impl PlatformWallet { /// rows); the caller then falls back to the seed path. A persisted /// row that fails to decode is an error, not a fallback — silent /// re-resolution would mask persistence corruption. + /// + /// `accounts` are the ordinary accounts, as for + /// [`bind_shielded`](Self::bind_shielded): the same identity accounts + /// are added to them, so the bound set can be larger than the + /// request. One of those without a persisted viewing key (an identity + /// found since the last seed-backed bind) also returns `Ok(false)`. + /// A `coordinator` of another network than this wallet's is refused + /// the same way. #[cfg(feature = "shielded")] pub async fn bind_shielded_from_persisted( &self, @@ -1036,6 +1059,7 @@ impl PlatformWallet { coordinator: &Arc, ) -> Result { use super::shielded::{AccountViewingKeys, SubwalletId}; + coordinator.ensure_network(self.sdk.network)?; let _config = self.shielded_config_lock.lock().await; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; let capabilities = self.persister.persistence_capabilities(); @@ -1057,11 +1081,10 @@ impl PlatformWallet { // Sampled before the load so the install can tell that the // snapshot predates a Clear. let snapshot_generation = coordinator.clear_generation(); - let start = self.persister.load().map_err(|e| { - PlatformWalletError::ShieldedBuildError(format!( - "persister load failed while rebinding shielded viewing keys: {e}" - )) - })?; + let start = self + .persister + .load() + .map_err(PlatformWalletError::from_load_failure)?; let mut accounts: std::collections::BTreeSet = accounts.iter().copied().collect(); // Newly discovered identities require their tip accounts too. Missing // FVKs must trigger seed-backed binding: skipping them would report a diff --git a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs index 69386a16905..d5e1997d297 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/coordinator.rs @@ -447,6 +447,23 @@ impl NetworkShieldedCoordinator { self.network } + /// Refuse a wallet of another network. A bind derives the wallet's + /// viewing keys for the wallet's network: registered on this coordinator + /// they would be scanned for on another network's chain, so the bind + /// would succeed and the wallet would never see a note. + pub(crate) fn ensure_network( + &self, + wallet_network: dashcore::Network, + ) -> Result<(), PlatformWalletError> { + if self.network != wallet_network { + return Err(PlatformWalletError::InvalidParameter(format!( + "the shielded coordinator is for {:?}, this wallet for {:?}", + self.network, wallet_network + ))); + } + Ok(()) + } + /// Install (or replace) the per-chunk progress handler. The /// callback runs from inside `sync_shielded_notes`'s chunk loop /// — once per ~2048 notes processed — so keep it cheap. Used by diff --git a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs index 6adbcb442b9..fea5934d915 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs @@ -51,6 +51,12 @@ pub fn is_shielded_identity_account(account: u32) -> bool { /// authentication key this wallet derives at that index (the key discovery and /// registration both use). Fails closed when no key matches, including when /// no public keys were restored. +/// +/// Only an ECDSA MASTER authentication key counts. The hash alone proves +/// nothing: a hash-only key (`ECDSA_HASH160`) or a key of another purpose or +/// level can carry it on an identity whose master key is another wallet's or +/// another index's. A disabled key still counts: rotating the master key +/// does not change the index the identity was created at. fn verify_identity_index( seed: &[u8], network: key_wallet::Network, @@ -60,7 +66,9 @@ fn verify_identity_index( use crate::wallet::identity::network::{ derive_identity_auth_key_hash_from_master, MASTER_KEY_INDEX, }; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0; use dpp::identity::identity_public_key::methods::hash::IdentityPublicKeyHashMethodsV0; + use dpp::identity::{KeyType, Purpose, SecurityLevel}; let master = key_wallet::bip32::ExtendedPrivKey::new_master(network, seed) .map_err(|e| PlatformWalletError::ShieldedKeyDerivation(e.to_string()))?; @@ -70,10 +78,12 @@ fn verify_identity_index( identity_index, MASTER_KEY_INDEX, )?; - let verified = identity - .public_keys() - .values() - .any(|key| key.public_key_hash().ok() == Some(expected)); + let verified = identity.public_keys().values().any(|key| { + key.key_type() == KeyType::ECDSA_SECP256K1 + && key.purpose() == Purpose::AUTHENTICATION + && key.security_level() == SecurityLevel::MASTER + && key.public_key_hash().ok() == Some(expected) + }); if verified { Ok(()) } else { @@ -96,8 +106,19 @@ impl PlatformWallet { .identity_manager .managed_identities() .filter(|identity| identity.wallet_id == Some(self.wallet_id())) - .filter_map(|identity| identity.identity_index) - .filter_map(|index| shielded_identity_account_index(index).ok()) + .filter_map(|identity| { + let index = identity.identity_index?; + let account = shielded_identity_account_index(index); + if account.is_err() { + tracing::warn!( + identity = %identity.identity.id(), + identity_index = index, + "identity index is outside the identity shielded account range; \ + its shielded account is not bound" + ); + } + account.ok() + }) .collect()) } @@ -115,13 +136,12 @@ impl PlatformWallet { // The account's keys are derived for this wallet's network. Bound to // a coordinator of another network they would scan the wrong chain, // while the address was handed out and recorded as prepared. - if coordinator.network() != self.sdk.network { - return Err(PlatformWalletError::InvalidParameter(format!( - "the shielded coordinator is for {:?}, this wallet for {:?}", - coordinator.network(), - self.sdk.network - ))); - } + coordinator.ensure_network(self.sdk.network)?; + // Sampled before the first wait. A Clear that runs while this waits + // for admission below has bumped it by the time this is admitted, and + // the host wipes its own rows only after that Clear returns: the bind + // here would load them and restore the notes the Clear removed. + let clear_generation = coordinator.clear_generation(); // Do not publish a key from a mis-associated mnemonic. A first bind // has no persisted FVK against which to detect a wrong seed. let root = key_wallet::wallet::root_extended_keys::RootExtendedPrivKey::new_master(seed) @@ -146,12 +166,14 @@ impl PlatformWallet { ))); } let _operation = coordinator.operation_guard().await; + if coordinator.clear_generation() != clear_generation { + return Err(PlatformWalletError::ShieldedNotBound); + } let account = self.verified_tip_account(seed, identity_id).await?; // Read the bound set and bind the augmented copy as one step: a bind // replaces the registration, so another bind landing in between // would have its ordinary accounts dropped by this stale snapshot. let _config = self.shielded_config_lock.lock().await; - let clear_generation = coordinator.clear_generation(); let mut accounts = self.shielded_account_indices().await; if accounts.is_empty() { return Err(PlatformWalletError::ShieldedNotBound); @@ -163,7 +185,7 @@ impl PlatformWallet { .await?; self.persister() .flush() - .map_err(|e| PlatformWalletError::Persistence(e.to_string()))?; + .map_err(PlatformWalletError::from_flush_failure)?; let address = self .shielded_default_address(account) .await @@ -221,11 +243,9 @@ impl PlatformWallet { "Tip account requires a wallet-owned identity".to_string(), )); } - let index = identity.identity_index.ok_or_else(|| { - PlatformWalletError::InvalidIdentityData( - "Tip account requires a recoverable identity index".to_string(), - ) - })?; + let index = identity + .identity_index + .ok_or(PlatformWalletError::IdentityIndexNotSet(*identity_id))?; verify_identity_index(seed, self.sdk.network, index, &identity.identity)?; shielded_identity_account_index(index) } diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 8fe70dbd289..779615a8eb2 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -164,12 +164,16 @@ fn temp_dir(tag: &str) -> PathBuf { } fn coordinator_at(dir: &std::path::Path) -> Arc { + coordinator_for(dir, Network::Testnet) +} + +fn coordinator_for(dir: &std::path::Path, network: Network) -> Arc { std::fs::create_dir_all(dir).expect("create temp dir"); let db_path = dir.join("tree.sqlite"); let store = FileBackedShieldedStore::open_path(&db_path, 100).expect("open file store"); Arc::new(NetworkShieldedCoordinator::new( Arc::new(dash_sdk::Sdk::new_mock()), - Network::Testnet, + network, db_path, store, )) @@ -184,7 +188,15 @@ where { let (wallet_manager, wallet_id, balance, _signer) = funded_wallet_manager(StandardAccountType::BIP44Account).await; - let sdk = Arc::new(dash_sdk::Sdk::new_mock()); + // On the wallet manager's and the coordinators' network: the bare mock + // SDK is a mainnet one, and a bind refuses a coordinator of another + // network than the SDK's. + let sdk = Arc::new( + dash_sdk::SdkBuilder::new_mock() + .with_network(Network::Testnet) + .build() + .expect("mock sdk"), + ); let spv = Arc::new(crate::spv::SpvRuntime::new( Arc::clone(&wallet_manager), Arc::new(crate::events::PlatformEventManager::new(Vec::new())), @@ -1453,7 +1465,7 @@ async fn should_restore_tip_account_from_identity_discovery_and_register_for_syn wallet .prepare_shielded_identity_address(&seed[..], &id, &coordinator) .await, - Err(crate::PlatformWalletError::Persistence(_)) + Err(crate::PlatformWalletError::PersisterFlush(_)) )); assert_eq!( *persister.durability_events.lock().unwrap(), @@ -1693,6 +1705,12 @@ async fn race_tip_preparation( let mut writing = tokio::spawn(writer(wallet.clone())); // Unserialized, the writer finishes now and the resumed preparation then // rebinds its stale {0} plus the tip account, dropping account 1. + // + // The callers run on a paused clock, which is load-bearing: the timeout + // fires only once nothing else is runnable, so it reads "the runtime had + // no work left and the writer still had not finished". On a real clock a + // writer that is merely slow to be scheduled passes it too, with the + // lock removed. let finished_early = tokio::time::timeout(std::time::Duration::from_millis(500), &mut writing).await; assert!( @@ -1709,7 +1727,7 @@ async fn race_tip_preparation( /// not be undone by the stale snapshot: preparation is paused right after its /// snapshot while another task binds accounts 0 and 1, and account 1 must /// still be bound and registered once both finish. -#[tokio::test] +#[tokio::test(flavor = "current_thread", start_paused = true)] async fn should_keep_an_ordinary_account_bound_during_tip_preparation() { use crate::shielded_identity_account_index; let (wallet, seed, id, coordinator) = tip_race_fixture("tips_concurrent_bind").await; @@ -1741,7 +1759,7 @@ async fn should_keep_an_ordinary_account_bound_during_tip_preparation() { /// `shielded_add_account` writes the same account set, so it must not land /// between preparation's snapshot and its bind either, or that bind replaces /// the key map without the added account. -#[tokio::test] +#[tokio::test(flavor = "current_thread", start_paused = true)] async fn should_keep_an_added_account_during_tip_preparation() { use crate::shielded_identity_account_index; let (wallet, seed, id, coordinator) = tip_race_fixture("tips_concurrent_add").await; @@ -1766,16 +1784,7 @@ async fn should_keep_an_added_account_during_tip_preparation() { #[tokio::test] async fn should_refuse_tip_preparation_on_a_coordinator_of_another_network() { let (wallet, seed, id, _coordinator) = tip_race_fixture("tips_other_network").await; - let dir = temp_dir("tips_other_network_mainnet"); - std::fs::create_dir_all(&dir).expect("create temp dir"); - let db_path = dir.join("tree.sqlite"); - let store = FileBackedShieldedStore::open_path(&db_path, 100).expect("open file store"); - let mainnet = Arc::new(NetworkShieldedCoordinator::new( - Arc::new(dash_sdk::Sdk::new_mock()), - Network::Mainnet, - db_path, - store, - )); + let mainnet = coordinator_for(&temp_dir("tips_other_network_mainnet"), Network::Mainnet); let bound_before = wallet.shielded_account_indices().await; let refused = wallet @@ -1793,6 +1802,121 @@ async fn should_refuse_tip_preparation_on_a_coordinator_of_another_network() { assert_eq!(wallet.prepared_shielded_identity_account(&id), None); } +/// A bind derives its viewing keys for the wallet's network, so both bind +/// routes refuse a coordinator of another network before anything is read, +/// derived, stored or registered. +#[tokio::test] +async fn should_refuse_a_bind_on_a_coordinator_of_another_network() { + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let mainnet = coordinator_for(&temp_dir("bind_other_network"), Network::Mainnet); + + let seeded = wallet.bind_shielded(&[0x42u8; 64], &[0], &mainnet).await; + assert!( + matches!(seeded, Err(crate::PlatformWalletError::InvalidParameter(_))), + "{seeded:?}" + ); + let seedless = wallet.bind_shielded_from_persisted(&[0], &mainnet).await; + assert!( + matches!( + seedless, + Err(crate::PlatformWalletError::InvalidParameter(_)) + ), + "{seedless:?}" + ); + + assert!(wallet.shielded_account_indices().await.is_empty()); + assert!(mainnet.registered_subwallets().await.is_empty()); + assert!(persister.stored.lock().unwrap().is_empty()); + assert_eq!(*persister.load_calls.lock().unwrap(), 0); +} + +/// The seed proof accepts only the identity's ECDSA MASTER authentication +/// key. The derived key's hash on a hash-only key, or the derived key itself +/// below MASTER, says nothing about the index the identity was created at. +#[tokio::test] +async fn should_refuse_tip_preparation_when_the_derived_key_is_not_the_master_key() { + use dpp::identity::identity_public_key::methods::hash::IdentityPublicKeyHashMethodsV0; + use dpp::identity::{Identity, IdentityPublicKey, IdentityV0, KeyType, SecurityLevel}; + use dpp::platform_value::BinaryData; + use dpp::prelude::Identifier; + let (wallet, seed, _, coordinator) = tip_race_fixture("tips_not_master_key").await; + + // Index 3: the derived key's hash, carried by a hash-only key. + let mut hash_only = seed_master_key(&seed[..], 3); + let derived_hash = hash_only[&0].public_key_hash().unwrap(); + let Some(IdentityPublicKey::V0(key)) = hash_only.get_mut(&0) else { + panic!("the helper builds a V0 key"); + }; + key.key_type = KeyType::ECDSA_HASH160; + key.data = BinaryData::new(derived_hash.to_vec()); + assert_eq!(hash_only[&0].public_key_hash().unwrap(), derived_hash); + // Index 4: the derived key itself, below MASTER. + let mut below_master = seed_master_key(&seed[..], 4); + let Some(IdentityPublicKey::V0(key)) = below_master.get_mut(&0) else { + panic!("the helper builds a V0 key"); + }; + key.security_level = SecurityLevel::HIGH; + + for (id, index, public_keys) in [ + (Identifier::from([0x71; 32]), 3, hash_only), + (Identifier::from([0x72; 32]), 4, below_master), + ] { + { + let mut wm = wallet.wallet_manager.write().await; + wm.get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager + .add_identity( + Identity::V0(IdentityV0 { + id, + public_keys, + balance: 0, + revision: 0, + }), + index, + wallet.wallet_id(), + wallet.persister(), + ) + .unwrap(); + } + let refused = wallet + .prepare_shielded_identity_address(&seed, &id, &coordinator) + .await; + assert!( + matches!(refused, Err(crate::PlatformWalletError::InvalidIdentityData(ref msg)) if msg.contains("not verified")), + "index {index}: {refused:?}" + ); + assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + } +} + +/// A Clear that is admitted while a preparation waits for admission wins. The +/// host wipes its own rows only after that Clear returns, so a preparation +/// admitted right behind it would bind from the rows the Clear removed: it is +/// refused and nothing is prepared. +#[tokio::test] +async fn should_refuse_a_tip_preparation_admitted_behind_a_clear() { + let (wallet, seed, id, coordinator) = tip_race_fixture("tips_behind_clear").await; + // Admission is held, so the Clear queues for it and the preparation + // queues behind the Clear (the lock is fair: a waiting writer goes + // before a later reader). Releasing it admits the Clear first. + let admission = coordinator.operation_guard().await; + let (cleared, prepared, ()) = tokio::join!( + coordinator.clear(), + wallet.prepare_shielded_identity_address(&seed, &id, &coordinator), + async move { drop(admission) }, + ); + + cleared.unwrap(); + assert!( + matches!(prepared, Err(crate::PlatformWalletError::ShieldedNotBound)), + "{prepared:?}" + ); + assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + assert!(coordinator.registered_subwallets().await.is_empty()); +} + /// A Clear drops the coordinator's state for the account, so the preparation no /// longer stands, and a rebind of the ordinary accounts does not bring it back: the /// identity's account is proved and prepared again. From 0d5c4ac0415366199ce8919bd7f592c8024dcccf Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Fri, 13 Nov 2026 21:35:36 +0700 Subject: [PATCH 5/7] fix(platform-wallet): a shielded bind holds the wallet's lifecycle gate A bind checked once that the wallet was still attached, then read the host snapshot, derived the keys and stored the viewing keys. A removal could complete between the check and the store. The host deletes its rows once the removal returns, so the viewing key was then written for a deleted wallet and stayed on disk. `bind_shielded`, `bind_shielded_from_persisted` and `shielded_add_account` now hold the generation's lifecycle gate from the attachment check until they return. A removal waits for a bind in flight, and a bind admitted behind a removal is refused before it stores anything. Tip preparation already holds the gate, so it calls `bind_shielded_locked_unfenced`, which does not take it again. The same change as 56bb77d5cb in #5288, with tests for both orders. Co-authored-by: pasta Co-Authored-By: Claude Fable 5.2 --- .../src/wallet/platform_wallet.rs | 38 ++++- .../src/wallet/shielded/tips.rs | 2 +- .../wallet/shielded/viewing_key_bind_tests.rs | 145 ++++++++++++++++++ 3 files changed, 178 insertions(+), 7 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 01e3a5f1998..786c196be87 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -441,7 +441,8 @@ pub struct PlatformWallet { /// preparation) must not interleave with another bind, or the other /// bind's ordinary accounts are dropped and their live state purged. /// Taken at the top of every bind entry point and by - /// `shielded_add_account`, before `shield_guard`. + /// `shielded_add_account`, after the generation's lifecycle gate and + /// before `shield_guard`. #[cfg(feature = "shielded")] pub(crate) shielded_config_lock: Arc>, /// The tip account each identity's address was last prepared for in @@ -899,6 +900,11 @@ impl PlatformWallet { /// Fails, before anything is derived, stored or registered, for a /// `coordinator` of another network than this wallet's. /// + /// Holds the wallet generation's lifecycle gate from the attachment + /// check until the keys are stored and installed, so a removal of the + /// wallet waits for a bind in flight, and a bind admitted after a + /// removal stores nothing. A caller must not hold the gate. + /// /// [`bind_shielded_from_persisted`]: Self::bind_shielded_from_persisted /// [`SpendingKey::from_zip32_seed`]: grovedb_commitment_tree::SpendingKey::from_zip32_seed #[cfg(feature = "shielded")] @@ -908,14 +914,18 @@ impl PlatformWallet { accounts: &[u32], coordinator: &Arc, ) -> Result<(), PlatformWalletError> { + // Fence generation teardown across attachment validation, host + // viewing-key persistence, and coordinator/handle installation. + let _lifecycle = self.core().generation_payment_guard().await; let _config = self.shielded_config_lock.lock().await; - self.bind_shielded_locked(seed, accounts, coordinator).await + self.bind_shielded_locked_unfenced(seed, accounts, coordinator) + .await } /// [`Self::bind_shielded`] for a caller already holding - /// `shielded_config_lock`. + /// `shielded_config_lock` and the generation lifecycle guard. #[cfg(feature = "shielded")] - pub(crate) async fn bind_shielded_locked( + pub(crate) async fn bind_shielded_locked_unfenced( &self, seed: &[u8], accounts: &[u32], @@ -1051,7 +1061,8 @@ impl PlatformWallet { /// request. One of those without a persisted viewing key (an identity /// found since the last seed-backed bind) also returns `Ok(false)`. /// A `coordinator` of another network than this wallet's is refused - /// the same way. + /// the same way, and the wallet generation's lifecycle gate is held + /// the same way: a caller must not hold it. #[cfg(feature = "shielded")] pub async fn bind_shielded_from_persisted( &self, @@ -1060,6 +1071,9 @@ impl PlatformWallet { ) -> Result { use super::shielded::{AccountViewingKeys, SubwalletId}; coordinator.ensure_network(self.sdk.network)?; + // Keep the generation alive from attachment validation through the + // persisted snapshot read and coordinator/handle installation. + let _lifecycle = self.core().generation_payment_guard().await; let _config = self.shielded_config_lock.lock().await; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; let capabilities = self.persister.persistence_capabilities(); @@ -1356,6 +1370,10 @@ impl PlatformWallet { /// historical funds for a freshly-added account should drop /// the commitment-tree DB and call [`bind_shielded`] again /// with the full account list. + /// + /// Holds the wallet generation's lifecycle gate as + /// [`bind_shielded`](Self::bind_shielded) does: a caller must not + /// hold it. #[cfg(feature = "shielded")] pub async fn shielded_add_account( &self, @@ -1374,10 +1392,13 @@ impl PlatformWallet { missing.bits(), ))); } - self.ensure_shielded_attached()?; // Serialized with the binds: tip preparation rebinds a snapshot of // the account set, which would drop an account inserted mid-way. + // The lifecycle gate spans attachment validation through host + // persistence and the final handle mutation. + let _lifecycle = self.core().generation_payment_guard().await; let _config = self.shielded_config_lock.lock().await; + self.ensure_shielded_attached()?; // Everything that calls into the host — the snapshot read below // and the viewing-key write further down — stays OUTSIDE the key // slot's lock. A host callback invoked while this write guard is @@ -1470,6 +1491,11 @@ impl PlatformWallet { /// user deleted, with the mnemonic already gone. An FVK discloses /// every incoming and outgoing note of its account, so the bind /// paths check here first, before writing anything. + /// + /// On its own this is a point-in-time observation. The bind paths and + /// `shielded_add_account` hold the generation's lifecycle gate from + /// this check through their host write, and the removal sets the flag + /// under the gate's exclusive side, so it cannot complete in between. #[cfg(feature = "shielded")] fn ensure_shielded_attached(&self) -> Result<(), PlatformWalletError> { if self diff --git a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs index fea5934d915..6dc0199628a 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs @@ -181,7 +181,7 @@ impl PlatformWallet { #[cfg(test)] test_hooks::after_snapshot(self).await; accounts.push(account); - self.bind_shielded_locked(seed, &accounts, coordinator) + self.bind_shielded_locked_unfenced(seed, &accounts, coordinator) .await?; self.persister() .flush() diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 779615a8eb2..79be825a12f 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -705,6 +705,151 @@ async fn bind_after_wallet_removal_is_refused() { ); } +/// What `PlatformWalletManager::remove_wallet` does to this handle's +/// shielded side once it holds `teardown`, the lifecycle gate of the +/// wallet's generation. +async fn finish_removal( + wallet: &PlatformWallet, + coordinator: &NetworkShieldedCoordinator, + teardown: tokio::sync::OwnedRwLockWriteGuard<()>, +) { + wallet.mark_shielded_detached(); + coordinator.unregister_wallet(wallet.wallet_id()).await; + drop(teardown); +} + +/// A bind that starts while the wallet's removal is under way stores no +/// viewing key. The attachment check alone is a point-in-time observation: +/// the removal takes the lifecycle gate and only then marks the handle +/// detached, so a bind checking in between would pass and store its key +/// after the host deleted the wallet's rows. The bind waits at the gate +/// instead, and is refused once the removal returns. +#[tokio::test] +async fn should_store_no_viewing_key_for_a_bind_admitted_behind_a_removal() { + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let coordinator = coordinator_at(&temp_dir("bind_behind_removal")); + let seed = [0x42u8; 64]; + + // The removal holds the gate and has not marked the handle yet. + let teardown = wallet.generation().teardown_guard().await; + let mut bind = std::pin::pin!(wallet.bind_shielded(&seed, &[0], &coordinator)); + assert!( + futures::poll!(bind.as_mut()).is_pending(), + "the bind must wait for the removal under way" + ); + finish_removal(&wallet, &coordinator, teardown).await; + + let err = bind.await.expect_err("a removed wallet must not bind"); + assert!( + matches!(err, crate::PlatformWalletError::WalletNotFound(_)), + "{err:?}" + ); + assert_eq!( + persister.stored_count(), + 0, + "no viewing key may be stored once the removal returned" + ); + assert!(coordinator.registered_subwallets().await.is_empty()); +} + +/// The same for `shielded_add_account`, the other writer of a viewing-key +/// row: admitted behind a removal, it stores nothing and adds no account. +#[tokio::test] +async fn should_store_no_viewing_key_for_an_account_added_behind_a_removal() { + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let coordinator = coordinator_at(&temp_dir("add_account_behind_removal")); + let seed = [0x42u8; 64]; + wallet + .bind_shielded(&seed, &[0], &coordinator) + .await + .expect("bind succeeds"); + let stored_before = persister.stored_count(); + + let teardown = wallet.generation().teardown_guard().await; + let mut add = std::pin::pin!(wallet.shielded_add_account(&seed, 1)); + assert!( + futures::poll!(add.as_mut()).is_pending(), + "the account add must wait for the removal under way" + ); + finish_removal(&wallet, &coordinator, teardown).await; + + let err = add + .await + .expect_err("a removed wallet must not add accounts"); + assert!( + matches!(err, crate::PlatformWalletError::WalletNotFound(_)), + "{err:?}" + ); + assert_eq!( + persister.stored_count(), + stored_before, + "no viewing key may be stored once the removal returned" + ); + assert_eq!(wallet.shielded_account_indices().await, vec![0]); +} + +/// A removal waits for a bind that already passed its attachment check, so +/// the bind's viewing-key write lands before the removal returns and the +/// host deletes the wallet's rows, never after. +/// +/// Parks a bind inside the persister load, which follows the check and +/// precedes the write, and asserts the removal cannot take the gate. The +/// bind then finishes with the removal queued behind it, which it could +/// not if anything in it took the gate a second time. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_hold_a_removal_until_a_bind_in_flight_returns() { + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let coordinator = coordinator_at(&temp_dir("removal_behind_bind")); + let seed = [0x42u8; 64]; + + let release = persister.gate_next_load(); + let entries_before = persister.load_entries(); + let binder = { + let (wallet, coordinator) = (wallet.clone(), Arc::clone(&coordinator)); + tokio::spawn(async move { wallet.bind_shielded(&seed, &[0], &coordinator).await }) + }; + for _ in 0..400 { + if persister.load_entries() > entries_before { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(5)).await; + } + assert!( + persister.load_entries() > entries_before, + "the bind never reached the persister load" + ); + + let mut removal = std::pin::pin!(wallet.generation().teardown_guard()); + assert!( + futures::poll!(removal.as_mut()).is_pending(), + "the removal must wait for the bind in flight" + ); + + release.send(()).expect("release the parked load"); + binder + .await + .expect("bind task") + .expect("a bind admitted before the removal completes"); + let stored = persister.stored_count(); + assert!( + persister + .captured_viewing_keys() + .contains_key(&SubwalletId::new(wallet.wallet_id(), 0)), + "the bind stored its viewing key before the removal" + ); + + finish_removal(&wallet, &coordinator, removal.await).await; + assert_eq!( + persister.stored_count(), + stored, + "nothing is stored once the removal returned" + ); + assert!(coordinator.registered_subwallets().await.is_empty()); +} + /// `shielded_add_account` is the other writer of a subwallet's /// viewing-key row, so it owes the same refusal as the bind paths. An /// account missing from the in-memory map can still have durable rows From 563ed1b989d3520b9dcff293396673e66a6a3bb6 Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sat, 14 Nov 2026 16:59:03 +0700 Subject: [PATCH 6/7] fix(platform-wallet): binds wait for a Clear, and a prepared account follows its identity's slot Three gaps around the identity shielded account. A bind did not hold the coordinator's operation admission, so a Clear could complete between the bind's host load and its host store. The host wipes its rows once the Clear returns, and the bind then wrote its viewing keys after that wipe. `bind_shielded` and `bind_shielded_from_persisted` now hold the admission from before their load until they return, so a Clear waits for a bind in flight. They sample the clear generation before they wait: a bind admitted behind a Clear registers the wallet and restores nothing, because the rows it reads may be the ones the host is about to wipe. The preparation proved the identity's slot, released the wallet-manager lock and bound the account that slot selects. An identity removed and added back at another slot in between was recorded, and given the address, for the account of the slot it had left. The preparation now proves the slot again as it records, under the wallet-manager lock that every slot change takes for writing, and refuses when the account differs. `prepared_shielded_identity_account` reports an account only while the identity still holds the slot that selects it; it is async now, since it reads the wallet manager. Removing the wallet left its prepared accounts in place, so a handle that outlived the removal still reported them. The removal drops them. Co-Authored-By: Claude Fable 5.2 --- .../src/wallet/platform_wallet.rs | 58 +++- .../src/wallet/shielded/tips.rs | 57 +++- .../wallet/shielded/viewing_key_bind_tests.rs | 312 +++++++++++++++--- 3 files changed, 354 insertions(+), 73 deletions(-) diff --git a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs index 786c196be87..c2771b2c077 100644 --- a/packages/rs-platform-wallet/src/wallet/platform_wallet.rs +++ b/packages/rs-platform-wallet/src/wallet/platform_wallet.rs @@ -442,14 +442,15 @@ pub struct PlatformWallet { /// bind's ordinary accounts are dropped and their live state purged. /// Taken at the top of every bind entry point and by /// `shielded_add_account`, after the generation's lifecycle gate and - /// before `shield_guard`. + /// the coordinator's operation admission, and before `shield_guard`. #[cfg(feature = "shielded")] pub(crate) shielded_config_lock: Arc>, /// The tip account each identity's address was last prepared for in /// this session, after its slot was proved with the seed. Publication /// of a wallet tip address requires it, so an address is published only /// for the identity and slot it was prepared for, and invalidated by a - /// coordinator Clear. `Arc` so cloned wallet handles share it. + /// coordinator Clear and by the wallet's removal. `Arc` so cloned wallet + /// handles share it. #[cfg(feature = "shielded")] pub(crate) prepared_tip_accounts: Arc>>, @@ -905,6 +906,13 @@ impl PlatformWallet { /// wallet waits for a bind in flight, and a bind admitted after a /// removal stores nothing. A caller must not hold the gate. /// + /// Holds the coordinator's operation admission the same way, so a + /// Clear waits for a bind in flight and the host, which wipes its own + /// rows once the Clear returns, wipes the viewing keys the bind stored. + /// A bind that waited for a Clear registers the wallet and restores + /// nothing: the rows it reads may be the ones the host is about to + /// wipe. A caller must not hold the admission either. + /// /// [`bind_shielded_from_persisted`]: Self::bind_shielded_from_persisted /// [`SpendingKey::from_zip32_seed`]: grovedb_commitment_tree::SpendingKey::from_zip32_seed #[cfg(feature = "shielded")] @@ -914,25 +922,36 @@ impl PlatformWallet { accounts: &[u32], coordinator: &Arc, ) -> Result<(), PlatformWalletError> { + coordinator.ensure_network(self.sdk.network)?; + // Sampled before the first wait, so the install can tell that the + // snapshot predates a Clear this bind waited for. + let snapshot_generation = coordinator.clear_generation(); // Fence generation teardown across attachment validation, host // viewing-key persistence, and coordinator/handle installation. let _lifecycle = self.core().generation_payment_guard().await; + // And a Clear across the same span: one that completed between the + // host load and the host store would have this bind write viewing + // keys after the host wiped its rows. + let _operation = coordinator.operation_guard().await; let _config = self.shielded_config_lock.lock().await; - self.bind_shielded_locked_unfenced(seed, accounts, coordinator) + self.bind_shielded_locked_unfenced(seed, accounts, coordinator, snapshot_generation) .await } - /// [`Self::bind_shielded`] for a caller already holding - /// `shielded_config_lock` and the generation lifecycle guard. + /// [`Self::bind_shielded`] for a caller that checked the coordinator's + /// network and already holds the generation lifecycle guard, the + /// coordinator's operation admission and `shielded_config_lock`. + /// `snapshot_generation` is the coordinator's clear generation, sampled + /// before the caller waited for the admission. #[cfg(feature = "shielded")] pub(crate) async fn bind_shielded_locked_unfenced( &self, seed: &[u8], accounts: &[u32], coordinator: &Arc, + snapshot_generation: u64, ) -> Result<(), PlatformWalletError> { use super::shielded::{AccountViewingKeys, OrchardKeySet, SubwalletId}; - coordinator.ensure_network(self.sdk.network)?; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; let capabilities = self.persister.persistence_capabilities(); if !capabilities.contains(required) { @@ -953,9 +972,6 @@ impl PlatformWallet { // dropped must not persist viewing keys the host can no longer // delete (see `ensure_shielded_attached`). self.ensure_shielded_attached()?; - // Sampled before the load below so the install can tell that the - // snapshot predates a Clear. - let snapshot_generation = coordinator.clear_generation(); let network = self.sdk.network; let mut accounts: std::collections::BTreeSet = accounts.iter().copied().collect(); accounts.extend(self.discovered_tip_accounts().await?); @@ -1061,8 +1077,9 @@ impl PlatformWallet { /// request. One of those without a persisted viewing key (an identity /// found since the last seed-backed bind) also returns `Ok(false)`. /// A `coordinator` of another network than this wallet's is refused - /// the same way, and the wallet generation's lifecycle gate is held - /// the same way: a caller must not hold it. + /// the same way, and the wallet generation's lifecycle gate and the + /// coordinator's operation admission are held the same way: a caller + /// must not hold either. #[cfg(feature = "shielded")] pub async fn bind_shielded_from_persisted( &self, @@ -1071,9 +1088,14 @@ impl PlatformWallet { ) -> Result { use super::shielded::{AccountViewingKeys, SubwalletId}; coordinator.ensure_network(self.sdk.network)?; + // Sampled before the first wait, so the install can tell that the + // snapshot predates a Clear this bind waited for. + let snapshot_generation = coordinator.clear_generation(); // Keep the generation alive from attachment validation through the // persisted snapshot read and coordinator/handle installation. let _lifecycle = self.core().generation_payment_guard().await; + // A Clear waits for this bind, as for a seed bind. + let _operation = coordinator.operation_guard().await; let _config = self.shielded_config_lock.lock().await; let required = crate::changeset::PersistenceCapabilities::SHIELDED_FVK_RESTART; let capabilities = self.persister.persistence_capabilities(); @@ -1092,9 +1114,6 @@ impl PlatformWallet { )); } self.ensure_shielded_attached()?; - // Sampled before the load so the install can tell that the - // snapshot predates a Clear. - let snapshot_generation = coordinator.clear_generation(); let start = self .persister .load() @@ -1520,10 +1539,21 @@ impl PlatformWallet { /// bind either commit fully before the unregister purges it, or see /// the flag and refuse. Setting it afterwards would leave the /// window this closes. + /// + /// Also drops the identity accounts prepared in this session. A removed + /// wallet is no longer registered or persisted, so a handle that + /// outlives the removal must not report an account as prepared. The + /// removal runs under the lifecycle gate's exclusive side and a + /// preparation records under its shared side, so none is recorded + /// afterwards. #[cfg(feature = "shielded")] pub(crate) fn mark_shielded_detached(&self) { self.shielded_detached .store(true, std::sync::atomic::Ordering::Release); + self.prepared_tip_accounts + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .clear(); } /// Bound ZIP-32 account indices on the shielded sub-wallet, diff --git a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs index 6dc0199628a..c9e16d298eb 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/tips.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/tips.rs @@ -9,10 +9,11 @@ //! another diversifier in the same account, so all old addresses remain covered. use super::NetworkShieldedCoordinator; -use crate::wallet::platform_wallet::PlatformWallet; +use crate::wallet::platform_wallet::{PlatformWallet, PlatformWalletInfo}; use crate::PlatformWalletError; use dpp::identity::accessors::IdentityGettersV0; use dpp::prelude::Identifier; +use key_wallet_manager::WalletManager; use std::sync::{Arc, PoisonError, Weak}; pub(crate) struct PreparedTipAccount { @@ -181,7 +182,7 @@ impl PlatformWallet { #[cfg(test)] test_hooks::after_snapshot(self).await; accounts.push(account); - self.bind_shielded_locked_unfenced(seed, &accounts, coordinator) + self.bind_shielded_locked_unfenced(seed, &accounts, coordinator, clear_generation) .await?; self.persister() .flush() @@ -193,6 +194,20 @@ impl PlatformWallet { if coordinator.clear_generation() != clear_generation { return Err(PlatformWalletError::ShieldedNotBound); } + // The wallet-manager lock was released after the proof above, so the + // identity can have been removed and added back at another slot while + // the account was bound. Prove its slot again and record under that + // lock, which every change of a slot takes for writing: what is + // recorded is the account of the slot the identity holds as it is + // recorded. + let wm = self.wallet_manager.read().await; + if self.verified_tip_account_in(&wm, seed, identity_id)? != account { + return Err(PlatformWalletError::InvalidIdentityData( + "The identity moved to another slot while its tip account was prepared; \ + prepare its tip address again" + .to_string(), + )); + } self.prepared_tip_accounts .lock() .unwrap_or_else(PoisonError::into_inner) @@ -204,20 +219,39 @@ impl PlatformWallet { clear_generation, }, ); + drop(wm); Ok(address) } /// The shielded account prepared for `identity_id` in this session /// ([`Self::prepare_shielded_identity_address`]), while the preparation - /// stands: its coordinator is alive and has not been cleared since. - /// `None` otherwise. A recorded identity index alone is not proof of the - /// account, so this is what tells a caller that the seed proved it. - pub fn prepared_shielded_identity_account(&self, identity_id: &Identifier) -> Option { + /// stands: its coordinator is alive and has not been cleared since, the + /// wallet has not been removed, and the identity still holds the slot + /// that selects the account. `None` otherwise. A recorded identity index + /// alone is not proof of the account, so this is what tells a caller + /// that the seed proved it. + /// + /// An identity removed and added back at another slot after its + /// preparation has another account: the preparation of the old one no + /// longer stands, and the identity is prepared again. + pub async fn prepared_shielded_identity_account( + &self, + identity_id: &Identifier, + ) -> Option { + let wm = self.wallet_manager.read().await; + let current = wm + .get_wallet_info(&self.wallet_id())? + .identity_manager + .managed_identity(identity_id) + .filter(|identity| identity.wallet_id == Some(self.wallet_id())) + .and_then(|identity| identity.identity_index) + .and_then(|index| shielded_identity_account_index(index).ok())?; self.prepared_tip_accounts .lock() .unwrap_or_else(PoisonError::into_inner) .get(identity_id) .and_then(PreparedTipAccount::standing_account) + .filter(|prepared| *prepared == current) } /// The tip account of a wallet-owned identity's slot, proved with the @@ -231,6 +265,17 @@ impl PlatformWallet { identity_id: &Identifier, ) -> Result { let wm = self.wallet_manager.read().await; + self.verified_tip_account_in(&wm, seed, identity_id) + } + + /// [`Self::verified_tip_account`] under a wallet-manager lock the caller + /// holds. + fn verified_tip_account_in( + &self, + wm: &WalletManager, + seed: &[u8], + identity_id: &Identifier, + ) -> Result { let info = wm .get_wallet_info(&self.wallet_id()) .ok_or_else(|| PlatformWalletError::WalletNotFound(hex::encode(self.wallet_id())))?; diff --git a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs index 79be825a12f..e0069e2adb2 100644 --- a/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs +++ b/packages/rs-platform-wallet/src/wallet/shielded/viewing_key_bind_tests.rs @@ -912,23 +912,21 @@ async fn add_account_rejects_a_viewing_key_change_for_a_persisted_account() { ); } -/// A bind that read the host snapshot before a Clear must not put that -/// snapshot back afterwards. +/// A bind that waited for a Clear must not restore the host snapshot it +/// then reads. /// -/// The snapshot has to be read before the install transaction opens (the -/// seedless path reconstructs its keys from it, and a host callback must -/// not run under the coordinator's lifecycle mutex), so serializing the -/// transaction alone cannot cover this: Clear takes the mutex, finishes, -/// and the host then wipes its own rows — while a bind still holds the -/// pre-Clear notes and watermark in hand. Restoring them resurrects -/// history the user deleted and re-arms a watermark that reports -/// caught-up, so the cold rebuild from index 0 that Clear promises never -/// runs. -#[tokio::test(flavor = "multi_thread", worker_threads = 4)] -async fn bind_does_not_restore_a_snapshot_that_predates_a_clear() { +/// The host wipes its own rows only once the Clear returns, so a bind +/// admitted right behind the Clear can still read the pre-Clear notes and +/// watermark. Restoring them resurrects history the user deleted and +/// re-arms a watermark that reports caught-up, so the cold rebuild from +/// index 0 that Clear promises never runs. The bind samples the clear +/// generation before it waits, so its install sees the Clear and restores +/// nothing. +#[tokio::test] +async fn should_not_restore_a_snapshot_for_a_bind_admitted_behind_a_clear() { let persister = Arc::new(CapturingPersistence::default()); let wallet = platform_wallet_with(Arc::clone(&persister)).await; - let coordinator = coordinator_at(&temp_dir("clear_during_bind")); + let coordinator = coordinator_at(&temp_dir("bind_behind_clear")); let id = SubwalletId::new(wallet.wallet_id(), 0); // Pre-Clear host state: one note and a high watermark. @@ -951,44 +949,27 @@ async fn bind_does_not_restore_a_snapshot_that_predates_a_clear() { ); persister.serve_subwallets(snapshot); - // Park the bind inside load(): it has sampled the clear generation - // and is about to read the pre-Clear snapshot. - let release = persister.gate_next_load(); - let bind = { - let wallet = wallet.clone(); - let coordinator = Arc::clone(&coordinator); - tokio::spawn(async move { - wallet - .bind_shielded(&[0x42u8; 64], &[0], &coordinator) - .await - }) - }; - // Wait for the bind to actually REACH the gate. Without this - // handshake the Clear below could land before the bind samples the - // generation — a different ordering, which the fix is not about and - // which would make this test pass either way. - for _ in 0..400 { - if persister.load_entries() > 0 { - break; - } - tokio::time::sleep(std::time::Duration::from_millis(5)).await; - } + // Admission is held, so the Clear queues for it and the bind queues + // behind the Clear (the lock is fair: a waiting writer goes before a + // later reader). Releasing it admits the Clear first, and the bind + // then reads the rows the host has not wiped yet. + let admission = coordinator.operation_guard().await; + let (cleared, bound, ()) = tokio::join!( + coordinator.clear(), + wallet.bind_shielded(&[0x42u8; 64], &[0], &coordinator), + async move { drop(admission) }, + ); + cleared.expect("clear succeeds"); + bound.expect("bind succeeds"); assert!( - persister.load_entries() > 0, - "bind never reached the persister load" + persister.load_calls() > 0, + "the bind read the host snapshot" ); - // The user taps Clear and it completes — store purged, registries - // dropped — then the host wipes its own rows. - coordinator.clear().await.expect("clear succeeds"); - - release.send(()).expect("release the parked load"); - bind.await.expect("bind task").expect("bind succeeds"); - let store = coordinator.store().read().await; assert!( store.get_all_notes(id).unwrap().is_empty(), - "a snapshot read before the Clear must not be restored after it — those notes \ + "a snapshot read behind the Clear must not be restored after it: those notes \ are exactly what the user asked to delete" ); assert_eq!( @@ -999,6 +980,108 @@ async fn bind_does_not_restore_a_snapshot_that_predates_a_clear() { ); } +/// Park `bind` inside its host load, and require a Clear started meanwhile +/// to wait until the bind has returned. +async fn assert_clear_waits_for_bind( + persister: &CapturingPersistence, + coordinator: &NetworkShieldedCoordinator, + bind: F, +) -> F::Output +where + F: std::future::Future + Send + 'static, + F::Output: Send + 'static, +{ + let entries_before = persister.load_entries(); + let release = persister.gate_next_load(); + let bind = tokio::spawn(bind); + for _ in 0..400 { + if persister.load_entries() > entries_before { + break; + } + tokio::time::sleep(std::time::Duration::from_millis(5)).await; + } + assert!( + persister.load_entries() > entries_before, + "the bind never reached the persister load" + ); + + let mut clear = std::pin::pin!(coordinator.clear()); + assert!( + futures::poll!(clear.as_mut()).is_pending(), + "the Clear must wait for the bind in flight" + ); + release.send(()).expect("release the parked load"); + let bound = bind.await.expect("bind task"); + clear.await.expect("clear succeeds"); + bound +} + +/// A Clear waits for a bind in flight. The host wipes its own rows once the +/// Clear returns, so a Clear that completed while a bind sat between its +/// host load and its host store would have the bind write its viewing keys +/// after that wipe: rows for shielded state the user cleared. Waiting puts +/// the bind's write before the Clear, and so before the wipe. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_hold_a_clear_until_a_bind_in_flight_returns() { + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let coordinator = coordinator_at(&temp_dir("clear_waits_for_bind")); + + let bind = { + let wallet = wallet.clone(); + let coordinator = Arc::clone(&coordinator); + async move { + wallet + .bind_shielded(&[0x42u8; 64], &[0], &coordinator) + .await + } + }; + assert_clear_waits_for_bind(&persister, &coordinator, bind) + .await + .expect("bind succeeds"); + assert!( + !persister.captured_viewing_keys().is_empty(), + "the bind stored its viewing key, and did so before the Clear ran" + ); + assert!( + coordinator.registered_subwallets().await.is_empty(), + "the Clear ran after the bind and dropped its registration" + ); +} + +/// The seedless bind is held the same way: it restores the notes and +/// watermark of the snapshot it loaded, so a Clear completing while it sat +/// in that load must wait as well. +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] +async fn should_hold_a_clear_until_a_persisted_bind_in_flight_returns() { + let persister = Arc::new(CapturingPersistence::default()); + let wallet = platform_wallet_with(Arc::clone(&persister)).await; + let coordinator = coordinator_at(&temp_dir("clear_waits_for_persisted_bind")); + wallet + .bind_shielded(&[0x42u8; 64], &[0], &coordinator) + .await + .expect("seed bind succeeds"); + persister.serve_viewing_keys(persister.captured_viewing_keys()); + + let bind = { + let wallet = wallet.clone(); + let coordinator = Arc::clone(&coordinator); + async move { + wallet + .bind_shielded_from_persisted(&[0], &coordinator) + .await + } + }; + let found = assert_clear_waits_for_bind(&persister, &coordinator, bind) + .await + .expect("persisted bind succeeds"); + assert!(found, "the bind found its persisted key"); + assert!( + coordinator.registered_subwallets().await.is_empty(), + "the Clear ran after the bind and dropped its registration" + ); +} + /// Two binds of one wallet must not interleave: neither may publish its /// registration while the other's transaction is open. That is what /// keeps the handle's key slot and the coordinator's registration from @@ -1469,7 +1552,7 @@ async fn should_refuse_tip_account_for_a_placeholder_index_until_it_is_verified( address ); assert_eq!( - wallet.prepared_shielded_identity_account(&id), + wallet.prepared_shielded_identity_account(&id).await, Some(shielded_identity_account_index(5).unwrap()) ); } @@ -1944,7 +2027,7 @@ async fn should_refuse_tip_preparation_on_a_coordinator_of_another_network() { "{refused:?}" ); assert_eq!(wallet.shielded_account_indices().await, bound_before); - assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); } /// A bind derives its viewing keys for the wallet's network, so both bind @@ -2032,7 +2115,7 @@ async fn should_refuse_tip_preparation_when_the_derived_key_is_not_the_master_ke matches!(refused, Err(crate::PlatformWalletError::InvalidIdentityData(ref msg)) if msg.contains("not verified")), "index {index}: {refused:?}" ); - assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); } } @@ -2058,7 +2141,7 @@ async fn should_refuse_a_tip_preparation_admitted_behind_a_clear() { matches!(prepared, Err(crate::PlatformWalletError::ShieldedNotBound)), "{prepared:?}" ); - assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); assert!(coordinator.registered_subwallets().await.is_empty()); } @@ -2074,6 +2157,7 @@ async fn should_require_fresh_tip_preparation_after_clear_even_after_rebind() { .unwrap(); let account = wallet .prepared_shielded_identity_account(&id) + .await .expect("the preparation stands"); assert_eq!( wallet.shielded_default_address(account).await, @@ -2081,12 +2165,12 @@ async fn should_require_fresh_tip_preparation_after_clear_even_after_rebind() { ); coordinator.clear().await.unwrap(); - assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); wallet .bind_shielded(&seed, &[0], &coordinator) .await .unwrap(); - assert_eq!(wallet.prepared_shielded_identity_account(&id), None); + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); assert_eq!( wallet @@ -2096,7 +2180,129 @@ async fn should_require_fresh_tip_preparation_after_clear_even_after_rebind() { address ); assert_eq!( - wallet.prepared_shielded_identity_account(&id), + wallet.prepared_shielded_identity_account(&id).await, Some(account) ); } + +/// Move the fixture's identity from slot 2 to slot 3, as its removal and a +/// rediscovery at another index do. It then carries the MASTER key the seed +/// derives at each of the two slots, so the seed proves either one. +async fn move_identity_to_slot_three( + wallet: &PlatformWallet, + seed: &[u8], + id: &dpp::prelude::Identifier, +) { + use dpp::identity::accessors::IdentityGettersV0; + use dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeySettersV0; + let mut second_master = seed_master_key(seed, 3) + .remove(&0) + .expect("the master key derived at slot 3"); + second_master.set_id(1); + let mut wm = wallet.wallet_manager.write().await; + let manager = &mut wm + .get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager; + let mut identity = manager.remove_identity(id, wallet.persister()).unwrap(); + identity.add_public_key(second_master); + manager + .add_identity(identity, 3, wallet.wallet_id(), wallet.persister()) + .unwrap(); +} + +/// The preparation proves the identity's slot, releases the wallet-manager +/// lock, and binds the account that slot selects. An identity moved to +/// another slot in between has another account, so the preparation must +/// neither hand out nor record the address of the one it bound. +#[tokio::test] +async fn should_refuse_a_tip_preparation_whose_identity_moved_while_it_bound() { + use crate::shielded_identity_account_index; + let (wallet, seed, id, coordinator) = tip_race_fixture("tips_identity_moved").await; + let (reached, resume) = super::tips::test_hooks::pause_after_snapshot(&wallet); + let preparing = { + let wallet = wallet.clone(); + let coordinator = Arc::clone(&coordinator); + tokio::spawn(async move { + wallet + .prepare_shielded_identity_address(&seed[..], &id, &coordinator) + .await + }) + }; + reached.await.expect("preparation reached its snapshot"); + move_identity_to_slot_three(&wallet, &seed, &id).await; + resume.send(()).unwrap(); + + let refused = preparing.await.unwrap(); + assert!( + matches!(refused, Err(crate::PlatformWalletError::InvalidIdentityData(ref msg)) if msg.contains("moved to another slot")), + "{refused:?}" + ); + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); + + // Prepared again, the identity gets the account of the slot it holds. + let address = wallet + .prepare_shielded_identity_address(&seed, &id, &coordinator) + .await + .unwrap(); + let account = shielded_identity_account_index(3).unwrap(); + assert_eq!( + wallet.shielded_default_address(account).await, + Some(address) + ); + assert_eq!( + wallet.prepared_shielded_identity_account(&id).await, + Some(account) + ); +} + +/// A preparation stands for the slot the identity held when it was made. +/// Moved to another slot, the identity has another account, and the old +/// one must not be reported as prepared for it; nor for an identity the +/// wallet no longer holds. +#[tokio::test] +async fn should_not_report_a_prepared_account_for_an_identity_that_left_its_slot() { + use crate::shielded_identity_account_index; + let (wallet, seed, id, coordinator) = tip_race_fixture("tips_identity_left_slot").await; + wallet + .prepare_shielded_identity_address(&seed, &id, &coordinator) + .await + .unwrap(); + assert_eq!( + wallet.prepared_shielded_identity_account(&id).await, + Some(shielded_identity_account_index(2).unwrap()) + ); + + move_identity_to_slot_three(&wallet, &seed, &id).await; + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); + + wallet + .wallet_manager + .write() + .await + .get_wallet_info_mut(&wallet.wallet_id()) + .unwrap() + .identity_manager + .remove_identity(&id, wallet.persister()) + .unwrap(); + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); +} + +/// A removed wallet is no longer registered or persisted, so a handle that +/// outlives the removal must not report an account as prepared. +#[tokio::test] +async fn should_drop_prepared_accounts_when_the_wallet_is_removed() { + let (wallet, seed, id, coordinator) = tip_race_fixture("tips_wallet_removed").await; + wallet + .prepare_shielded_identity_address(&seed, &id, &coordinator) + .await + .unwrap(); + assert!(wallet + .prepared_shielded_identity_account(&id) + .await + .is_some()); + + let teardown = wallet.generation().teardown_guard().await; + finish_removal(&wallet, &coordinator, teardown).await; + assert_eq!(wallet.prepared_shielded_identity_account(&id).await, None); +} From 322e40b11e35a99aacd5c2dc85ced300e72f950a Mon Sep 17 00:00:00 2001 From: Quantum Explorer Date: Sun, 15 Nov 2026 14:34:59 +0700 Subject: [PATCH 7/7] style(drive-abci): format a test comment that fails `cargo fmt` on v5.1-dev Two doc comment lines in `is_allowed.rs` came in unindented with #5910, so the format step of the Rust tests fails for every pull request on `v5.1-dev`. Formatted with `cargo fmt --all`; no other change. Co-Authored-By: Claude Fable 5.2 --- .../state_transition/processor/traits/is_allowed.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs index 2780f26f314..53e6c463d0c 100644 --- a/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs +++ b/packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/is_allowed.rs @@ -815,8 +815,8 @@ mod tests { use dpp::tests::fixtures::get_data_contract_fixture; use platform_version::TryIntoPlatformVersioned; -/// The versions that refuse a tradeable token: the last whose software cannot decode -/// the mode, and protocol version 15, which decodes it but creates none of its state. + /// The versions that refuse a tradeable token: the last whose software cannot decode + /// the mode, and protocol version 15, which decodes it but creates none of its state. fn protocol_versions_before_tradeable() -> [&'static PlatformVersion; 2] { [ PlatformVersion::get(14).expect("expected protocol version 14"),