Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions key-wallet/src/managed_account/managed_core_funds_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self, Error> {
Comment thread
ZocoLini marked this conversation as resolved.
Resolved
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<Self, Error> {
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<Self, Error> {
ManagedCoreKeysAccount::from_eddsa_account(account).map(Self::wrap)
}

fn wrap(keys: ManagedCoreKeysAccount) -> Self {
Expand Down
46 changes: 17 additions & 29 deletions key-wallet/src/managed_account/managed_core_keys_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Self> {
// 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<Self> {
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<Self> {
// 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.
Expand Down
18 changes: 9 additions & 9 deletions key-wallet/src/wallet/managed_wallet_info/managed_accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OwnedManagedCoreAccount> {
match account.account_type {
AccountType::Standard {
..
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -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()) {
Expand Down