From c0e90f0288e28bd3f86286c4715aa4358561611f Mon Sep 17 00:00:00 2001 From: Borja Castellano Date: Mon, 9 Nov 2026 10:43:47 -0800 Subject: [PATCH 1/2] fix(key-wallet)!: return errors from managed account constructors `from_account`, `from_bls_account` and `from_eddsa_account` on `ManagedCoreKeysAccount` panicked through `expect` when `ManagedAccountType::from_account_type` failed even with `NoKeySource`. They now return `Result` on both `ManagedCoreKeysAccount` and `ManagedCoreFundsAccount`, and the `add_managed_*` callers propagate the error. --- .../managed_account_collection.rs | 1 + .../managed_core_funds_account.rs | 13 ++++---- .../managed_core_keys_account.rs | 32 ++++++++----------- .../managed_wallet_info/managed_accounts.rs | 18 +++++------ 4 files changed, 31 insertions(+), 33 deletions(-) diff --git a/key-wallet/src/managed_account/managed_account_collection.rs b/key-wallet/src/managed_account/managed_account_collection.rs index 51742dd52..a6afeef5f 100644 --- a/key-wallet/src/managed_account/managed_account_collection.rs +++ b/key-wallet/src/managed_account/managed_account_collection.rs @@ -1169,6 +1169,7 @@ mod dashpay_funding_scope_tests { account_xpub: xpub(), is_watch_only: matches!(account_type, AccountType::DashpayExternalAccount { .. }), }) + .unwrap() } /// A DashPay **external** account is watch-only — its addresses derive from diff --git a/key-wallet/src/managed_account/managed_core_funds_account.rs b/key-wallet/src/managed_account/managed_core_funds_account.rs index 7eb199933..1b3296573 100644 --- a/key-wallet/src/managed_account/managed_core_funds_account.rs +++ b/key-wallet/src/managed_account/managed_core_funds_account.rs @@ -15,6 +15,7 @@ use crate::account::BLSAccount; #[cfg(feature = "eddsa")] use crate::account::EdDSAAccount; use crate::account::TransactionRecord; +use crate::error::Error; use crate::managed_account::address_pool; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::managed_account::managed_account_type::ManagedAccountType; @@ -135,20 +136,20 @@ impl ManagedCoreFundsAccount { } /// Create a `ManagedCoreFundsAccount` from an [`Account`](super::super::Account). - pub fn from_account(account: &super::super::Account) -> Self { - Self::wrap(ManagedCoreKeysAccount::from_account(account)) + pub fn from_account(account: &super::super::Account) -> Result { + ManagedCoreKeysAccount::from_account(account).map(Self::wrap) } /// Create a `ManagedCoreFundsAccount` from a [`BLSAccount`]. #[cfg(feature = "bls")] - pub fn from_bls_account(account: &BLSAccount) -> Self { - Self::wrap(ManagedCoreKeysAccount::from_bls_account(account)) + pub fn from_bls_account(account: &BLSAccount) -> Result { + ManagedCoreKeysAccount::from_bls_account(account).map(Self::wrap) } /// Create a `ManagedCoreFundsAccount` from an [`EdDSAAccount`]. #[cfg(feature = "eddsa")] - pub fn from_eddsa_account(account: &EdDSAAccount) -> Self { - Self::wrap(ManagedCoreKeysAccount::from_eddsa_account(account)) + pub fn from_eddsa_account(account: &EdDSAAccount) -> Result { + ManagedCoreKeysAccount::from_eddsa_account(account).map(Self::wrap) } fn wrap(keys: ManagedCoreKeysAccount) -> Self { diff --git a/key-wallet/src/managed_account/managed_core_keys_account.rs b/key-wallet/src/managed_account/managed_core_keys_account.rs index f3e7aea37..874c78882 100644 --- a/key-wallet/src/managed_account/managed_core_keys_account.rs +++ b/key-wallet/src/managed_account/managed_core_keys_account.rs @@ -11,6 +11,7 @@ use crate::account::BLSAccount; #[cfg(feature = "eddsa")] use crate::account::EdDSAAccount; use crate::account::TransactionRecord; +use crate::error::Result; use crate::managed_account::address_pool; use crate::managed_account::managed_account_trait::ManagedAccountTrait; use crate::managed_account::managed_account_type::ManagedAccountType; @@ -232,61 +233,56 @@ impl ManagedCoreKeysAccount { } /// Create a `ManagedCoreKeysAccount` from an [`Account`](super::super::Account). - pub fn from_account(account: &super::super::Account) -> Self { + pub fn from_account(account: &super::super::Account) -> Result { let key_source = address_pool::KeySource::Public(account.account_xpub); let managed_type = ManagedAccountType::from_account_type( account.account_type, account.network, &key_source, ) - .unwrap_or_else(|_| { - let no_key_source = address_pool::KeySource::NoKeySource; + .or_else(|_| { ManagedAccountType::from_account_type( account.account_type, account.network, - &no_key_source, + &address_pool::KeySource::NoKeySource, ) - .expect("Should succeed with NoKeySource") - }); + })?; - Self::new(managed_type, account.network) + Ok(Self::new(managed_type, account.network)) } /// Create a `ManagedCoreKeysAccount` from a [`BLSAccount`]. #[cfg(feature = "bls")] - pub fn from_bls_account(account: &BLSAccount) -> Self { + pub fn from_bls_account(account: &BLSAccount) -> Result { let key_source = address_pool::KeySource::BLSPublic(account.bls_public_key.clone()); let managed_type = ManagedAccountType::from_account_type( account.account_type, account.network, &key_source, ) - .unwrap_or_else(|_| { - let no_key_source = address_pool::KeySource::NoKeySource; + .or_else(|_| { ManagedAccountType::from_account_type( account.account_type, account.network, - &no_key_source, + &address_pool::KeySource::NoKeySource, ) - .expect("Should succeed with NoKeySource") - }); + })?; - Self::new(managed_type, account.network) + Ok(Self::new(managed_type, account.network)) } /// Create a `ManagedCoreKeysAccount` from an [`EdDSAAccount`]. #[cfg(feature = "eddsa")] - pub fn from_eddsa_account(account: &EdDSAAccount) -> Self { + pub fn from_eddsa_account(account: &EdDSAAccount) -> Result { // EdDSA requires hardened derivation, so we cannot generate addresses without the private key. let key_source = address_pool::KeySource::NoKeySource; let managed_type = ManagedAccountType::from_account_type( account.account_type, account.network, &key_source, - ) - .expect("Should succeed with NoKeySource"); + )?; - Self::new(managed_type, account.network) + Ok(Self::new(managed_type, account.network)) } /// Record a new transaction for this keys account. diff --git a/key-wallet/src/wallet/managed_wallet_info/managed_accounts.rs b/key-wallet/src/wallet/managed_wallet_info/managed_accounts.rs index 498172e68..df04f2063 100644 --- a/key-wallet/src/wallet/managed_wallet_info/managed_accounts.rs +++ b/key-wallet/src/wallet/managed_wallet_info/managed_accounts.rs @@ -18,7 +18,7 @@ use crate::wallet::Wallet; /// Wrap an [`Account`] as the right [`OwnedManagedCoreAccount`] variant for /// its [`AccountType`] — funds-bearing for Standard / CoinJoin / DashPay, /// keys-only for identity / asset-lock / provider variants. -fn owned_from_account(account: &Account) -> OwnedManagedCoreAccount { +fn owned_from_account(account: &Account) -> Result { match account.account_type { AccountType::Standard { .. @@ -31,8 +31,8 @@ fn owned_from_account(account: &Account) -> OwnedManagedCoreAccount { } | AccountType::DashpayExternalAccount { .. - } => ManagedCoreFundsAccount::from_account(account).into(), - _ => ManagedCoreKeysAccount::from_account(account).into(), + } => ManagedCoreFundsAccount::from_account(account).map(Into::into), + _ => ManagedCoreKeysAccount::from_account(account).map(Into::into), } } @@ -66,7 +66,7 @@ impl ManagedAccountOperations for ManagedWalletInfo { })?; // Wrap as the right managed-account variant for the account type. - let managed_account = owned_from_account(account); + let managed_account = owned_from_account(account)?; // Check if managed account already exists if self.accounts.contains_managed_account_type(managed_account.managed_account_type()) { @@ -101,7 +101,7 @@ impl ManagedAccountOperations for ManagedWalletInfo { let account = Account::new(None, account_type, account_xpub, self.network)?; // Wrap as the right managed-account variant for the account type. - let managed_account = owned_from_account(&account); + let managed_account = owned_from_account(&account)?; // Check if managed account already exists if self.accounts.contains_managed_account_type(managed_account.managed_account_type()) { @@ -149,7 +149,7 @@ impl ManagedAccountOperations for ManagedWalletInfo { })?; // ProviderOperatorKeys is always a keys-only variant. - let managed_account = ManagedCoreKeysAccount::from_bls_account(bls_account); + let managed_account = ManagedCoreKeysAccount::from_bls_account(bls_account)?; // Check if managed account already exists if self.accounts.contains_managed_account_type(managed_account.managed_type()) { @@ -185,7 +185,7 @@ impl ManagedAccountOperations for ManagedWalletInfo { BLSAccount::from_public_key_bytes(None, account_type, bls_public_key, self.network)?; // ProviderOperatorKeys is always a keys-only variant. - let managed_account = ManagedCoreKeysAccount::from_bls_account(&bls_account); + let managed_account = ManagedCoreKeysAccount::from_bls_account(&bls_account)?; // Check if managed account already exists if self.accounts.contains_managed_account_type(managed_account.managed_type()) { @@ -234,7 +234,7 @@ impl ManagedAccountOperations for ManagedWalletInfo { })?; // ProviderPlatformKeys is always a keys-only variant. - let managed_account = ManagedCoreKeysAccount::from_eddsa_account(eddsa_account); + let managed_account = ManagedCoreKeysAccount::from_eddsa_account(eddsa_account)?; // Check if managed account already exists if self.accounts.contains_managed_account_type(managed_account.managed_type()) { @@ -275,7 +275,7 @@ impl ManagedAccountOperations for ManagedWalletInfo { // Create the ManagedAccount from the EdDSA Account // ProviderPlatformKeys is always a keys-only variant. - let managed_account = ManagedCoreKeysAccount::from_eddsa_account(&eddsa_account); + let managed_account = ManagedCoreKeysAccount::from_eddsa_account(&eddsa_account)?; // Check if managed account already exists if self.accounts.contains_managed_account_type(managed_account.managed_type()) { From f3111ed9f3cafff79b55eb4533cdf77cc18935d2 Mon Sep 17 00:00:00 2001 From: Borja Castellano Date: Wed, 11 Nov 2026 12:34:55 -0800 Subject: [PATCH 2/2] fix(key-wallet): propagate derivation errors in managed accounts `from_account` and `from_bls_account` retried with `NoKeySource` on any derivation error, hiding it and returning an account without addresses. `ProviderPlatformKeys` now uses `NoKeySource` directly since an xpub cannot derive its hardened pool, and every other error is returned. --- .../managed_core_keys_account.rs | 26 +++++++------------ 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/key-wallet/src/managed_account/managed_core_keys_account.rs b/key-wallet/src/managed_account/managed_core_keys_account.rs index 874c78882..08a20543d 100644 --- a/key-wallet/src/managed_account/managed_core_keys_account.rs +++ b/key-wallet/src/managed_account/managed_core_keys_account.rs @@ -6,6 +6,7 @@ //! special-purpose flows (identity registration, asset locks, masternode provider //! keys) rather than to hold and spend Dash directly. +use crate::account::AccountType; #[cfg(feature = "bls")] use crate::account::BLSAccount; #[cfg(feature = "eddsa")] @@ -234,19 +235,17 @@ impl ManagedCoreKeysAccount { /// Create a `ManagedCoreKeysAccount` from an [`Account`](super::super::Account). pub fn from_account(account: &super::super::Account) -> Result { - let key_source = address_pool::KeySource::Public(account.account_xpub); + // An xpub cannot derive the hardened pool of `ProviderPlatformKeys`. + let key_source = if account.account_type == AccountType::ProviderPlatformKeys { + address_pool::KeySource::NoKeySource + } else { + address_pool::KeySource::Public(account.account_xpub) + }; let managed_type = ManagedAccountType::from_account_type( account.account_type, account.network, &key_source, - ) - .or_else(|_| { - ManagedAccountType::from_account_type( - account.account_type, - account.network, - &address_pool::KeySource::NoKeySource, - ) - })?; + )?; Ok(Self::new(managed_type, account.network)) } @@ -259,14 +258,7 @@ impl ManagedCoreKeysAccount { account.account_type, account.network, &key_source, - ) - .or_else(|_| { - ManagedAccountType::from_account_type( - account.account_type, - account.network, - &address_pool::KeySource::NoKeySource, - ) - })?; + )?; Ok(Self::new(managed_type, account.network)) }