Skip to content

Commit e7792c4

Browse files
authored
refactor: Single network Wallet and ManagedWalletInfo (#271)
1 parent 6fe9408 commit e7792c4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1930
-3172
lines changed

dash-spv-ffi/tests/test_wallet_manager.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ mod tests {
6363
.create_wallet_from_mnemonic_return_serialized_bytes(
6464
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
6565
"",
66-
&[Network::Dash],
66+
Network::Dash,
6767
None,
6868
WalletAccountCreationOptions::Default,
6969
false,

dash-spv/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
306306
let wallet_id = wallet_manager.create_wallet_from_mnemonic(
307307
mnemonic_phrase.as_str(),
308308
"",
309-
&[network],
309+
network,
310310
None,
311311
key_wallet::wallet::initialization::WalletAccountCreationOptions::default(),
312312
)?;
@@ -473,7 +473,7 @@ async fn run_client<S: dash_spv::storage::StorageManager + Send + Sync + 'static
473473

474474
// Derive a conservative incoming total by summing tx outputs to our addresses.
475475
let incoming_sum = if let Some(ns) = mgr.get_network_state(network_for_logger) {
476-
let addrs = mgr.monitored_addresses(network_for_logger);
476+
let addrs = mgr.monitored_addresses();
477477
let addr_set: std::collections::HashSet<_> = addrs.into_iter().collect();
478478
let mut sum_incoming: u64 = 0;
479479
for rec in ns.transactions.values() {
@@ -593,7 +593,7 @@ async fn run_client<S: dash_spv::storage::StorageManager + Send + Sync + 'static
593593
// Display current wallet addresses
594594
{
595595
let wallet_lock = wallet.read().await;
596-
let monitored = wallet_lock.monitored_addresses(config.network);
596+
let monitored = wallet_lock.monitored_addresses();
597597
if !monitored.is_empty() {
598598
tracing::info!("Wallet monitoring {} addresses:", monitored.len());
599599
for (i, addr) in monitored.iter().take(10).enumerate() {
@@ -635,7 +635,7 @@ async fn run_client<S: dash_spv::storage::StorageManager + Send + Sync + 'static
635635
// Check filters for matches if wallet has addresses before starting monitoring
636636
let should_check_filters = {
637637
let wallet_lock = wallet.read().await;
638-
let monitored = wallet_lock.monitored_addresses(config.network);
638+
let monitored = wallet_lock.monitored_addresses();
639639
!monitored.is_empty() && !matches.get_flag("no-filters")
640640
};
641641

key-wallet-ffi/FFI_API.md

Lines changed: 44 additions & 44 deletions
Large diffs are not rendered by default.

key-wallet-ffi/include/key_wallet_ffi.h

Lines changed: 13 additions & 63 deletions
Large diffs are not rendered by default.

key-wallet-ffi/src/account.rs

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::os::raw::c_uint;
44
use std::sync::Arc;
55

66
use crate::error::{FFIError, FFIErrorCode};
7-
use crate::types::{FFIAccountResult, FFIAccountType, FFINetwork, FFINetworks, FFIWallet};
7+
use crate::types::{FFIAccountResult, FFIAccountType, FFINetworks, FFIWallet};
88
#[cfg(feature = "bls")]
99
use key_wallet::account::BLSAccount;
1010
#[cfg(feature = "eddsa")]
@@ -81,7 +81,6 @@ impl FFIEdDSAAccount {
8181
#[no_mangle]
8282
pub unsafe extern "C" fn wallet_get_account(
8383
wallet: *const FFIWallet,
84-
network: FFINetwork,
8584
account_index: c_uint,
8685
account_type: FFIAccountType,
8786
) -> FFIAccountResult {
@@ -90,15 +89,9 @@ pub unsafe extern "C" fn wallet_get_account(
9089
}
9190

9291
let wallet = &*wallet;
93-
let network_rust: key_wallet::Network = network.into();
94-
9592
let account_type_rust = account_type.to_account_type(account_index);
9693

97-
match wallet
98-
.inner()
99-
.accounts_on_network(network_rust)
100-
.and_then(|account_collection| account_collection.account_of_type(account_type_rust))
101-
{
94+
match wallet.inner().accounts.account_of_type(account_type_rust) {
10295
Some(account) => {
10396
let ffi_account = FFIAccount::new(account);
10497
FFIAccountResult::success(Box::into_raw(Box::new(ffi_account)))
@@ -118,26 +111,20 @@ pub unsafe extern "C" fn wallet_get_account(
118111
#[no_mangle]
119112
pub unsafe extern "C" fn wallet_get_top_up_account_with_registration_index(
120113
wallet: *const FFIWallet,
121-
network: FFINetwork,
122114
registration_index: c_uint,
123115
) -> FFIAccountResult {
124116
if wallet.is_null() {
125117
return FFIAccountResult::error(FFIErrorCode::InvalidInput, "Wallet is null".to_string());
126118
}
127119

128120
let wallet = &*wallet;
129-
let network_rust: key_wallet::Network = network.into();
130121

131122
// This function is specifically for IdentityTopUp accounts
132123
let account_type = key_wallet::AccountType::IdentityTopUp {
133124
registration_index,
134125
};
135126

136-
match wallet
137-
.inner()
138-
.accounts_on_network(network_rust)
139-
.and_then(|account_collection| account_collection.account_of_type(account_type))
140-
{
127+
match wallet.inner().accounts.account_of_type(account_type) {
141128
Some(account) => {
142129
let ffi_account = FFIAccount::new(account);
143130
FFIAccountResult::success(Box::into_raw(Box::new(ffi_account)))
@@ -565,7 +552,6 @@ pub unsafe extern "C" fn eddsa_account_get_is_watch_only(account: *const FFIEdDS
565552
#[no_mangle]
566553
pub unsafe extern "C" fn wallet_get_account_count(
567554
wallet: *const FFIWallet,
568-
network: FFINetwork,
569555
error: *mut FFIError,
570556
) -> c_uint {
571557
if wallet.is_null() {
@@ -574,23 +560,14 @@ pub unsafe extern "C" fn wallet_get_account_count(
574560
}
575561

576562
let wallet = &*wallet;
577-
let network: key_wallet::Network = network.into();
578-
579-
match wallet.inner().accounts.get(&network) {
580-
Some(accounts) => {
581-
FFIError::set_success(error);
582-
let count = accounts.standard_bip44_accounts.len()
583-
+ accounts.standard_bip32_accounts.len()
584-
+ accounts.coinjoin_accounts.len()
585-
+ accounts.identity_registration.is_some() as usize
586-
+ accounts.identity_topup.len();
587-
count as c_uint
588-
}
589-
None => {
590-
FFIError::set_success(error);
591-
0
592-
}
593-
}
563+
let accounts = &wallet.inner().accounts;
564+
FFIError::set_success(error);
565+
let count = accounts.standard_bip44_accounts.len()
566+
+ accounts.standard_bip32_accounts.len()
567+
+ accounts.coinjoin_accounts.len()
568+
+ accounts.identity_registration.is_some() as usize
569+
+ accounts.identity_topup.len();
570+
count as c_uint
594571
}
595572

596573
#[cfg(test)]

key-wallet-ffi/src/account_collection.rs

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use std::ptr;
1010
use crate::account::FFIAccount;
1111
use crate::error::{FFIError, FFIErrorCode};
1212
use crate::types::FFIWallet;
13-
use crate::FFINetwork;
1413

1514
/// Opaque handle to an account collection
1615
pub struct FFIAccountCollection {
@@ -84,7 +83,6 @@ pub struct FFIAccountCollectionSummary {
8483
#[no_mangle]
8584
pub unsafe extern "C" fn wallet_get_account_collection(
8685
wallet: *const FFIWallet,
87-
network: FFINetwork,
8886
error: *mut FFIError,
8987
) -> *mut FFIAccountCollection {
9088
if wallet.is_null() {
@@ -93,26 +91,9 @@ pub unsafe extern "C" fn wallet_get_account_collection(
9391
}
9492

9593
let wallet = &*wallet;
96-
let network_rust: key_wallet::Network = network.into();
97-
98-
match wallet.inner().accounts_on_network(network_rust) {
99-
Some(collection) => {
100-
let ffi_collection = FFIAccountCollection::new(collection);
101-
Box::into_raw(Box::new(ffi_collection))
102-
}
103-
None => {
104-
FFIError::set_error(
105-
error,
106-
FFIErrorCode::NotFound,
107-
format!(
108-
"No accounts found for network {:?}, wallet has networks {:?}",
109-
network_rust,
110-
wallet.inner().networks_supported()
111-
),
112-
);
113-
ptr::null_mut()
114-
}
115-
}
94+
FFIError::set_success(error);
95+
let ffi_collection = FFIAccountCollection::new(&wallet.inner().accounts);
96+
Box::into_raw(Box::new(ffi_collection))
11697
}
11798

11899
/// Free an account collection handle
@@ -1100,8 +1081,7 @@ mod tests {
11001081
assert!(!wallet.is_null());
11011082

11021083
// Get account collection
1103-
let collection =
1104-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1084+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
11051085
assert!(!collection.is_null());
11061086

11071087
// Check that we have some accounts
@@ -1155,8 +1135,7 @@ mod tests {
11551135
assert!(!wallet.is_null());
11561136

11571137
// Get account collection
1158-
let collection =
1159-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1138+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
11601139
assert!(!collection.is_null());
11611140

11621141
// Check for provider operator keys account (BLS)
@@ -1202,8 +1181,7 @@ mod tests {
12021181
assert!(!wallet.is_null());
12031182

12041183
// Get account collection
1205-
let collection =
1206-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1184+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
12071185
assert!(!collection.is_null());
12081186

12091187
// Check for provider platform keys account (EdDSA)
@@ -1273,8 +1251,7 @@ mod tests {
12731251
assert!(!wallet.is_null());
12741252

12751253
// Get account collection
1276-
let collection =
1277-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1254+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
12781255
assert!(!collection.is_null());
12791256

12801257
// Get the summary
@@ -1326,8 +1303,7 @@ mod tests {
13261303
assert!(!wallet.is_null());
13271304

13281305
// Get account collection
1329-
let collection =
1330-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1306+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
13311307

13321308
// With SpecificAccounts and empty lists, collection might be null or empty
13331309
if collection.is_null() {
@@ -1411,8 +1387,7 @@ mod tests {
14111387
assert!(!wallet.is_null());
14121388

14131389
// Get account collection
1414-
let collection =
1415-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1390+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
14161391
assert!(!collection.is_null());
14171392

14181393
// Get the summary data
@@ -1498,8 +1473,7 @@ mod tests {
14981473
assert!(!wallet.is_null());
14991474

15001475
// Get account collection
1501-
let collection =
1502-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1476+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
15031477

15041478
// With AllAccounts but empty lists, collection should still exist
15051479
if collection.is_null() {
@@ -1569,8 +1543,7 @@ mod tests {
15691543
assert!(!wallet.is_null());
15701544

15711545
// Get account collection
1572-
let collection =
1573-
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
1546+
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
15741547
assert!(!collection.is_null());
15751548

15761549
// Get multiple summaries to test memory management

key-wallet-ffi/src/account_tests.rs

Lines changed: 5 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ mod tests {
1010

1111
#[test]
1212
fn test_wallet_get_account_null_wallet() {
13-
let result = unsafe {
14-
wallet_get_account(ptr::null(), FFINetwork::Testnet, 0, FFIAccountType::StandardBIP44)
15-
};
13+
let result = unsafe { wallet_get_account(ptr::null(), 0, FFIAccountType::StandardBIP44) };
1614

1715
assert!(result.account.is_null());
1816
assert_ne!(result.error_code, 0);
@@ -44,9 +42,7 @@ mod tests {
4442
};
4543

4644
// Try to get the default account (should exist)
47-
let result = unsafe {
48-
wallet_get_account(wallet, FFINetwork::Testnet, 0, FFIAccountType::StandardBIP44)
49-
};
45+
let result = unsafe { wallet_get_account(wallet, 0, FFIAccountType::StandardBIP44) };
5046

5147
// Note: Since the account may not exist yet (depends on wallet creation logic),
5248
// we just check that the call doesn't return an error for invalid parameters
@@ -76,8 +72,7 @@ mod tests {
7672
fn test_wallet_get_account_count_null_wallet() {
7773
let mut error = FFIError::success();
7874

79-
let count =
80-
unsafe { wallet_get_account_count(ptr::null(), FFINetwork::Testnet, &mut error) };
75+
let count = unsafe { wallet_get_account_count(ptr::null(), &mut error) };
8176

8277
assert_eq!(count, 0);
8378
assert_eq!(error.code, FFIErrorCode::InvalidInput);
@@ -100,7 +95,7 @@ mod tests {
10095
)
10196
};
10297

103-
let count = unsafe { wallet_get_account_count(wallet, FFINetwork::Testnet, &mut error) };
98+
let count = unsafe { wallet_get_account_count(wallet, &mut error) };
10499

105100
// Should have at least one default account
106101
assert!(count >= 1);
@@ -112,42 +107,6 @@ mod tests {
112107
}
113108
}
114109

115-
#[test]
116-
fn test_wallet_get_account_count_empty_network() {
117-
let mut error = FFIError::success();
118-
119-
// Create a wallet
120-
let mnemonic = CString::new("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap();
121-
let passphrase = CString::new("").unwrap();
122-
123-
let wallet = unsafe {
124-
wallet::wallet_create_from_mnemonic(
125-
mnemonic.as_ptr(),
126-
passphrase.as_ptr(),
127-
FFINetworks::TestnetFlag,
128-
&mut error,
129-
)
130-
};
131-
132-
// Try to get account count for a different network (Mainnet)
133-
let count = unsafe {
134-
wallet_get_account_count(
135-
wallet,
136-
FFINetwork::Dash, // Different network
137-
&mut error,
138-
)
139-
};
140-
141-
// Should return 0 for network with no accounts
142-
assert_eq!(count, 0);
143-
assert_eq!(error.code, FFIErrorCode::Success);
144-
145-
// Clean up
146-
unsafe {
147-
wallet::wallet_free(wallet);
148-
}
149-
}
150-
151110
#[test]
152111
fn test_account_type_values() {
153112
// Test FFIAccountType enum values
@@ -185,9 +144,7 @@ mod tests {
185144
assert_eq!(error.code, FFIErrorCode::Success);
186145

187146
// Get an account
188-
let result = unsafe {
189-
wallet_get_account(wallet, FFINetwork::Testnet, 0, FFIAccountType::StandardBIP44)
190-
};
147+
let result = unsafe { wallet_get_account(wallet, 0, FFIAccountType::StandardBIP44) };
191148

192149
if !result.account.is_null() {
193150
// Test all the getter functions

0 commit comments

Comments
 (0)