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
2 changes: 1 addition & 1 deletion dash-spv-ffi/tests/test_wallet_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ mod tests {
.create_wallet_from_mnemonic_return_serialized_bytes(
"abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about",
"",
&[Network::Dash],
Network::Dash,
None,
WalletAccountCreationOptions::Default,
false,
Expand Down
8 changes: 4 additions & 4 deletions dash-spv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ async fn run() -> Result<(), Box<dyn std::error::Error>> {
let wallet_id = wallet_manager.create_wallet_from_mnemonic(
"enemy check owner stumble unaware debris suffer peanut good fabric bleak outside",
"",
&[network],
network,
None,
key_wallet::wallet::initialization::WalletAccountCreationOptions::default(),
)?;
Expand Down Expand Up @@ -495,7 +495,7 @@ async fn run_client<S: dash_spv::storage::StorageManager + Send + Sync + 'static

// Derive a conservative incoming total by summing tx outputs to our addresses.
let incoming_sum = if let Some(ns) = mgr.get_network_state(network_for_logger) {
let addrs = mgr.monitored_addresses(network_for_logger);
let addrs = mgr.monitored_addresses();
let addr_set: std::collections::HashSet<_> = addrs.into_iter().collect();
let mut sum_incoming: u64 = 0;
for rec in ns.transactions.values() {
Expand Down Expand Up @@ -615,7 +615,7 @@ async fn run_client<S: dash_spv::storage::StorageManager + Send + Sync + 'static
// Display current wallet addresses
{
let wallet_lock = wallet.read().await;
let monitored = wallet_lock.monitored_addresses(config.network);
let monitored = wallet_lock.monitored_addresses();
if !monitored.is_empty() {
tracing::info!("Wallet monitoring {} addresses:", monitored.len());
for (i, addr) in monitored.iter().take(10).enumerate() {
Expand Down Expand Up @@ -657,7 +657,7 @@ async fn run_client<S: dash_spv::storage::StorageManager + Send + Sync + 'static
// Check filters for matches if wallet has addresses before starting monitoring
let should_check_filters = {
let wallet_lock = wallet.read().await;
let monitored = wallet_lock.monitored_addresses(config.network);
let monitored = wallet_lock.monitored_addresses();
!monitored.is_empty() && !matches.get_flag("no-filters")
};

Expand Down
88 changes: 44 additions & 44 deletions key-wallet-ffi/FFI_API.md

Large diffs are not rendered by default.

76 changes: 13 additions & 63 deletions key-wallet-ffi/include/key_wallet_ffi.h

Large diffs are not rendered by default.

45 changes: 11 additions & 34 deletions key-wallet-ffi/src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::os::raw::c_uint;
use std::sync::Arc;

use crate::error::{FFIError, FFIErrorCode};
use crate::types::{FFIAccountResult, FFIAccountType, FFINetwork, FFINetworks, FFIWallet};
use crate::types::{FFIAccountResult, FFIAccountType, FFINetworks, FFIWallet};
#[cfg(feature = "bls")]
use key_wallet::account::BLSAccount;
#[cfg(feature = "eddsa")]
Expand Down Expand Up @@ -81,7 +81,6 @@ impl FFIEdDSAAccount {
#[no_mangle]
pub unsafe extern "C" fn wallet_get_account(
wallet: *const FFIWallet,
network: FFINetwork,
account_index: c_uint,
account_type: FFIAccountType,
) -> FFIAccountResult {
Expand All @@ -90,15 +89,9 @@ pub unsafe extern "C" fn wallet_get_account(
}

let wallet = &*wallet;
let network_rust: key_wallet::Network = network.into();

let account_type_rust = account_type.to_account_type(account_index);

match wallet
.inner()
.accounts_on_network(network_rust)
.and_then(|account_collection| account_collection.account_of_type(account_type_rust))
{
match wallet.inner().accounts.account_of_type(account_type_rust) {
Some(account) => {
let ffi_account = FFIAccount::new(account);
FFIAccountResult::success(Box::into_raw(Box::new(ffi_account)))
Expand All @@ -118,26 +111,20 @@ pub unsafe extern "C" fn wallet_get_account(
#[no_mangle]
pub unsafe extern "C" fn wallet_get_top_up_account_with_registration_index(
wallet: *const FFIWallet,
network: FFINetwork,
registration_index: c_uint,
) -> FFIAccountResult {
if wallet.is_null() {
return FFIAccountResult::error(FFIErrorCode::InvalidInput, "Wallet is null".to_string());
}

let wallet = &*wallet;
let network_rust: key_wallet::Network = network.into();

// This function is specifically for IdentityTopUp accounts
let account_type = key_wallet::AccountType::IdentityTopUp {
registration_index,
};

match wallet
.inner()
.accounts_on_network(network_rust)
.and_then(|account_collection| account_collection.account_of_type(account_type))
{
match wallet.inner().accounts.account_of_type(account_type) {
Some(account) => {
let ffi_account = FFIAccount::new(account);
FFIAccountResult::success(Box::into_raw(Box::new(ffi_account)))
Expand Down Expand Up @@ -565,7 +552,6 @@ pub unsafe extern "C" fn eddsa_account_get_is_watch_only(account: *const FFIEdDS
#[no_mangle]
pub unsafe extern "C" fn wallet_get_account_count(
wallet: *const FFIWallet,
network: FFINetwork,
error: *mut FFIError,
) -> c_uint {
if wallet.is_null() {
Expand All @@ -574,23 +560,14 @@ pub unsafe extern "C" fn wallet_get_account_count(
}

let wallet = &*wallet;
let network: key_wallet::Network = network.into();

match wallet.inner().accounts.get(&network) {
Some(accounts) => {
FFIError::set_success(error);
let count = accounts.standard_bip44_accounts.len()
+ accounts.standard_bip32_accounts.len()
+ accounts.coinjoin_accounts.len()
+ accounts.identity_registration.is_some() as usize
+ accounts.identity_topup.len();
count as c_uint
}
None => {
FFIError::set_success(error);
0
}
}
let accounts = &wallet.inner().accounts;
FFIError::set_success(error);
let count = accounts.standard_bip44_accounts.len()
+ accounts.standard_bip32_accounts.len()
+ accounts.coinjoin_accounts.len()
+ accounts.identity_registration.is_some() as usize
+ accounts.identity_topup.len();
count as c_uint
}

#[cfg(test)]
Expand Down
49 changes: 11 additions & 38 deletions key-wallet-ffi/src/account_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::ptr;
use crate::account::FFIAccount;
use crate::error::{FFIError, FFIErrorCode};
use crate::types::FFIWallet;
use crate::FFINetwork;

/// Opaque handle to an account collection
pub struct FFIAccountCollection {
Expand Down Expand Up @@ -84,7 +83,6 @@ pub struct FFIAccountCollectionSummary {
#[no_mangle]
pub unsafe extern "C" fn wallet_get_account_collection(
wallet: *const FFIWallet,
network: FFINetwork,
error: *mut FFIError,
) -> *mut FFIAccountCollection {
if wallet.is_null() {
Expand All @@ -93,26 +91,9 @@ pub unsafe extern "C" fn wallet_get_account_collection(
}

let wallet = &*wallet;
let network_rust: key_wallet::Network = network.into();

match wallet.inner().accounts_on_network(network_rust) {
Some(collection) => {
let ffi_collection = FFIAccountCollection::new(collection);
Box::into_raw(Box::new(ffi_collection))
}
None => {
FFIError::set_error(
error,
FFIErrorCode::NotFound,
format!(
"No accounts found for network {:?}, wallet has networks {:?}",
network_rust,
wallet.inner().networks_supported()
),
);
ptr::null_mut()
}
}
FFIError::set_success(error);
let ffi_collection = FFIAccountCollection::new(&wallet.inner().accounts);
Box::into_raw(Box::new(ffi_collection))
}

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

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
assert!(!collection.is_null());

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

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
assert!(!collection.is_null());

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

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
assert!(!collection.is_null());

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

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
assert!(!collection.is_null());

// Get the summary
Expand Down Expand Up @@ -1326,8 +1303,7 @@ mod tests {
assert!(!wallet.is_null());

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());

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

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
assert!(!collection.is_null());

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

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());

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

// Get account collection
let collection =
wallet_get_account_collection(wallet, FFINetwork::Testnet, ptr::null_mut());
let collection = wallet_get_account_collection(wallet, ptr::null_mut());
assert!(!collection.is_null());

// Get multiple summaries to test memory management
Expand Down
53 changes: 5 additions & 48 deletions key-wallet-ffi/src/account_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ mod tests {

#[test]
fn test_wallet_get_account_null_wallet() {
let result = unsafe {
wallet_get_account(ptr::null(), FFINetwork::Testnet, 0, FFIAccountType::StandardBIP44)
};
let result = unsafe { wallet_get_account(ptr::null(), 0, FFIAccountType::StandardBIP44) };

assert!(result.account.is_null());
assert_ne!(result.error_code, 0);
Expand Down Expand Up @@ -44,9 +42,7 @@ mod tests {
};

// Try to get the default account (should exist)
let result = unsafe {
wallet_get_account(wallet, FFINetwork::Testnet, 0, FFIAccountType::StandardBIP44)
};
let result = unsafe { wallet_get_account(wallet, 0, FFIAccountType::StandardBIP44) };

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

let count =
unsafe { wallet_get_account_count(ptr::null(), FFINetwork::Testnet, &mut error) };
let count = unsafe { wallet_get_account_count(ptr::null(), &mut error) };

assert_eq!(count, 0);
assert_eq!(error.code, FFIErrorCode::InvalidInput);
Expand All @@ -100,7 +95,7 @@ mod tests {
)
};

let count = unsafe { wallet_get_account_count(wallet, FFINetwork::Testnet, &mut error) };
let count = unsafe { wallet_get_account_count(wallet, &mut error) };

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

#[test]
fn test_wallet_get_account_count_empty_network() {
let mut error = FFIError::success();

// Create a wallet
let mnemonic = CString::new("abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about").unwrap();
let passphrase = CString::new("").unwrap();

let wallet = unsafe {
wallet::wallet_create_from_mnemonic(
mnemonic.as_ptr(),
passphrase.as_ptr(),
FFINetworks::TestnetFlag,
&mut error,
)
};

// Try to get account count for a different network (Mainnet)
let count = unsafe {
wallet_get_account_count(
wallet,
FFINetwork::Dash, // Different network
&mut error,
)
};

// Should return 0 for network with no accounts
assert_eq!(count, 0);
assert_eq!(error.code, FFIErrorCode::Success);

// Clean up
unsafe {
wallet::wallet_free(wallet);
}
}

#[test]
fn test_account_type_values() {
// Test FFIAccountType enum values
Expand Down Expand Up @@ -185,9 +144,7 @@ mod tests {
assert_eq!(error.code, FFIErrorCode::Success);

// Get an account
let result = unsafe {
wallet_get_account(wallet, FFINetwork::Testnet, 0, FFIAccountType::StandardBIP44)
};
let result = unsafe { wallet_get_account(wallet, 0, FFIAccountType::StandardBIP44) };

if !result.account.is_null() {
// Test all the getter functions
Expand Down
Loading