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..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,11 +6,13 @@ //! 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")] 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 +234,47 @@ impl ManagedCoreKeysAccount { } /// Create a `ManagedCoreKeysAccount` from an [`Account`](super::super::Account). - pub fn from_account(account: &super::super::Account) -> Self { - let key_source = address_pool::KeySource::Public(account.account_xpub); + pub fn from_account(account: &super::super::Account) -> Result { + // 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, - ) - .unwrap_or_else(|_| { - let no_key_source = address_pool::KeySource::NoKeySource; - ManagedAccountType::from_account_type( - account.account_type, - account.network, - &no_key_source, - ) - .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; - ManagedAccountType::from_account_type( - account.account_type, - account.network, - &no_key_source, - ) - .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()) {