Skip to content
Closed
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
3 changes: 2 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use dash_sdk::dapi_client::AddressList;
use dash_sdk::dpp::dashcore::Network;
use dash_sdk::sdk::Uri;
use serde::Deserialize;
use zeroize::{Zeroize, ZeroizeOnDrop};

#[derive(Debug, Deserialize, Clone)]
pub struct Config {
Expand All @@ -26,7 +27,7 @@ pub enum ConfigError {
NoValidConfigs,
}

#[derive(Debug, Deserialize, Clone)]
#[derive(Debug, Deserialize, Clone, Zeroize, ZeroizeOnDrop)]
pub struct NetworkConfig {
/// Hostname of the Dash Platform node to connect to
pub dapi_addresses: String,
Expand Down
1 change: 1 addition & 0 deletions src/database/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ impl Database {
identities: HashMap::new(),
utxos: HashMap::new(),
is_main,
network: *network,
},
);

Expand Down
3 changes: 3 additions & 0 deletions src/model/qualified_identity/encrypted_key_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use bincode::error::{DecodeError, EncodeError};
use bincode::{BorrowDecode, Decode, Encode};
use dash_sdk::dashcore_rpc::dashcore::bip32::DerivationPath;
use dash_sdk::dpp::dashcore::bip32::ChildNumber;
use dash_sdk::dpp::dashcore::Network;
use dash_sdk::dpp::identity::identity_public_key::accessors::v0::IdentityPublicKeyGettersV0;
use dash_sdk::dpp::identity::{KeyID, Purpose, SecurityLevel};
use std::collections::{BTreeMap, BTreeSet};
Expand Down Expand Up @@ -277,6 +278,7 @@ impl KeyStorage {
&self,
key: &(PrivateKeyTarget, KeyID),
wallets: &[Arc<RwLock<Wallet>>],
network: Network,
) -> Result<Option<(QualifiedIdentityPublicKey, [u8; 32])>, String> {
self.private_keys
.get(key)
Expand All @@ -296,6 +298,7 @@ impl KeyStorage {
wallets,
*wallet_seed_hash,
derivation_path,
network,
)?
.ok_or(format!(
"Wallet for key at derivation path {} not present, we have {} wallets",
Expand Down
15 changes: 15 additions & 0 deletions src/model/qualified_identity/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,20 @@ impl Signer for QualifiedIdentity {
identity_public_key: &IdentityPublicKey,
data: &[u8],
) -> Result<BinaryData, ProtocolError> {
// Detect network from wallet's known addresses
let network = self
.associated_wallets
.values()
.find_map(|wallet| {
let wallet = wallet.read().unwrap();
wallet
.known_addresses
.keys()
.next()
.map(|addr| *addr.network())
})
.unwrap_or(Network::Dash); // Default to mainnet if no addresses found

let (_, private_key) = self
.private_keys
.get_resolve(
Expand All @@ -176,6 +190,7 @@ impl Signer for QualifiedIdentity {
.cloned()
.collect::<Vec<_>>()
.as_slice(),
network,
)
.map_err(ProtocolError::Generic)?
.ok_or(ProtocolError::Generic(format!(
Expand Down
14 changes: 9 additions & 5 deletions src/model/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use dash_sdk::dpp::dashcore::hashes::Hash;
use dash_sdk::dpp::fee::Credits;
use dash_sdk::dpp::prelude::AssetLockProof;
use dash_sdk::platform::Identity;
use zeroize::Zeroize;
use zeroize::{Zeroize, ZeroizeOnDrop};

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
Expand Down Expand Up @@ -122,6 +122,7 @@ impl PartialEq for WalletArcRef {
#[derive(Debug, Clone, PartialEq)]
pub struct Wallet {
pub wallet_seed: WalletSeed,
pub network: Network,
pub uses_password: bool,
pub master_bip44_ecdsa_extended_public_key: ExtendedPubKey,
pub address_balances: BTreeMap<Address, u64>,
Expand All @@ -148,7 +149,8 @@ pub enum WalletSeed {
Open(OpenWalletSeed),
Closed(ClosedWalletSeed),
}
#[derive(Debug, Clone, PartialEq)]

#[derive(Debug, Clone, PartialEq, Zeroize, ZeroizeOnDrop)]
pub struct OpenKeyItem<const N: usize> {
pub seed: [u8; N],
pub wallet_info: ClosedKeyItem,
Expand All @@ -157,7 +159,7 @@ pub struct OpenKeyItem<const N: usize> {
// Type alias for OpenWalletSeed with a fixed seed size of 64 bytes
pub type OpenWalletSeed = OpenKeyItem<64>;

#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone, PartialEq, Zeroize)]
pub struct ClosedKeyItem {
pub seed_hash: WalletSeedHash, // SHA-256 hash of the seed
pub encrypted_seed: Vec<u8>,
Expand Down Expand Up @@ -326,6 +328,7 @@ impl Wallet {
slice: &[Arc<RwLock<Wallet>>],
wallet_seed_hash: WalletSeedHash,
derivation_path: &DerivationPath,
network: Network,
) -> Result<Option<[u8; 32]>, String> {
for wallet in slice {
// Attempt to read the wallet from the RwLock
Expand All @@ -334,7 +337,7 @@ impl Wallet {
if wallet_ref.seed_hash() == wallet_seed_hash {
// Attempt to derive the private key using the provided derivation path
let extended_private_key = derivation_path
.derive_priv_ecdsa_for_master_seed(wallet_ref.seed_bytes()?, Network::Dash)
.derive_priv_ecdsa_for_master_seed(wallet_ref.seed_bytes()?, network)
.map_err(|e| e.to_string())?;
return Ok(Some(extended_private_key.private_key.secret_bytes()));
}
Expand All @@ -346,9 +349,10 @@ impl Wallet {
pub fn private_key_at_derivation_path(
&self,
derivation_path: &DerivationPath,
network: Network,
) -> Result<PrivateKey, String> {
let extended_private_key = derivation_path
.derive_priv_ecdsa_for_master_seed(self.seed_bytes()?, Network::Dash)
.derive_priv_ecdsa_for_master_seed(self.seed_bytes()?, network)
.map_err(|e| e.to_string())?;
Ok(extended_private_key.to_priv())
}
Expand Down
2 changes: 2 additions & 0 deletions src/ui/identities/keys/key_info_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ impl ScreenLike for KeyInfoScreen {
self.selected_wallet.as_ref().unwrap().read().unwrap();
match wallet.private_key_at_derivation_path(
&derivation_path.derivation_path,
self.app_context.network,
) {
Ok(private_key) => {
let private_key_wif = private_key.to_wif();
Expand Down Expand Up @@ -334,6 +335,7 @@ impl ScreenLike for KeyInfoScreen {
self.selected_wallet.as_ref().unwrap().read().unwrap();
match wallet.private_key_at_derivation_path(
&derivation_path.derivation_path,
self.app_context.network,
) {
Ok(private_key) => {
let private_key_wif = private_key.to_wif();
Expand Down
2 changes: 2 additions & 0 deletions src/ui/wallets/add_new_wallet_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl AddNewWalletScreen {
.expect("Failed to create master ECDSA extended private key");
let bip44_root_derivation_path: DerivationPath = match self.app_context.network {
Network::Dash => DerivationPath::from(DASH_BIP44_ACCOUNT_0_PATH_MAINNET.as_slice()),
// All other networks use the testnet derivation path
_ => DerivationPath::from(DASH_BIP44_ACCOUNT_0_PATH_TESTNET.as_slice()),
};
let secp = Secp256k1::new();
Expand All @@ -136,6 +137,7 @@ impl AddNewWalletScreen {
password_hint: None, // Set a password hint if needed
},
}),
network: self.app_context.network,
uses_password,
master_bip44_ecdsa_extended_public_key,
address_balances: Default::default(),
Expand Down
1 change: 1 addition & 0 deletions src/ui/wallets/import_wallet_screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl ImportWalletScreen {
},
}),
uses_password,
network: self.app_context.network,
master_bip44_ecdsa_extended_public_key,
address_balances: Default::default(),
known_addresses: Default::default(),
Expand Down