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
6 changes: 3 additions & 3 deletions key-wallet-ffi/src/managed_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ mod tests {

// Create managed wallet info from the wallet heap-allocated like C would do
let wallet_rust = unsafe { &(*wallet).wallet };
let managed_info = ManagedWalletInfo::from_wallet(wallet_rust);
let managed_info = ManagedWalletInfo::from_wallet(wallet_rust, 0);
let ffi_managed = Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)));

// Test get_next_receive_address with valid pointers
Expand Down Expand Up @@ -581,7 +581,7 @@ mod tests {

// We need to work with the existing wallet structure
// Create managed wallet info from the existing wallet
let mut managed_info = ManagedWalletInfo::from_wallet(wallet_arc);
let mut managed_info = ManagedWalletInfo::from_wallet(wallet_arc, 0);

let network = key_wallet::Network::Testnet;

Expand Down Expand Up @@ -759,7 +759,7 @@ mod tests {

// Create managed wallet info
let wallet_arc = unsafe { &(*wallet_ptr).wallet };
let mut managed_info = ManagedWalletInfo::from_wallet(wallet_arc);
let mut managed_info = ManagedWalletInfo::from_wallet(wallet_arc, 0);

// Set some test balance values
managed_info.balance = WalletCoreBalance::new(1000000, 50000, 10000, 25000);
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ pub unsafe extern "C" fn wallet_check_transaction(
use key_wallet::transaction_checking::WalletTransactionChecker;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;

let mut managed_info = ManagedWalletInfo::from_wallet(wallet.inner());
let mut managed_info = ManagedWalletInfo::from_wallet(wallet.inner(), 0);

// Check the transaction - wallet is always required now
let wallet_mut = unwrap_or_return!(wallet.inner_mut(), error);
Expand Down
2 changes: 1 addition & 1 deletion key-wallet-ffi/src/transaction_checking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub unsafe extern "C" fn wallet_create_managed_wallet(
error: *mut FFIError,
) -> *mut FFIManagedWalletInfo {
let wallet = deref_ptr!(wallet, error);
let managed_info = ManagedWalletInfo::from_wallet(wallet.inner());
let managed_info = ManagedWalletInfo::from_wallet(wallet.inner(), 0);
Box::into_raw(Box::new(FFIManagedWalletInfo::new(managed_info)))
}

Expand Down
2 changes: 1 addition & 1 deletion key-wallet-manager/src/accessors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod tests {
fn insert_wallet_rejects_duplicate() {
let mut manager: WalletManager<ManagedWalletInfo> = WalletManager::new(Network::Testnet);
let wallet = build_wallet();
let info = ManagedWalletInfo::from_wallet(&wallet);
let info = ManagedWalletInfo::from_wallet(&wallet, 0);

let id =
manager.insert_wallet(wallet.clone(), info.clone()).expect("first insert succeeds");
Expand Down
24 changes: 9 additions & 15 deletions key-wallet-manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {

// Create managed wallet info from the wallet to properly initialize accounts
// This ensures the ManagedAccountCollection is synchronized with the Wallet's accounts
let mut managed_info = T::from_wallet(&wallet);
managed_info.set_birth_height(birth_height);
let mut managed_info = T::from_wallet(&wallet, birth_height);
managed_info.set_first_loaded_at(current_timestamp());

// The wallet already has accounts created according to the provided options
Expand Down Expand Up @@ -276,8 +275,7 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
})?;

// Add the wallet to the manager
let mut managed_info = T::from_wallet(&final_wallet);
managed_info.set_birth_height(birth_height);
let mut managed_info = T::from_wallet(&final_wallet, birth_height);
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, final_wallet);
Expand Down Expand Up @@ -311,8 +309,7 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
}

// Create managed wallet info
let mut managed_info = T::from_wallet(&wallet);
managed_info.set_birth_height(self.last_processed_height());
let mut managed_info = T::from_wallet(&wallet, self.last_processed_height());
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, wallet);
Expand Down Expand Up @@ -352,8 +349,7 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
}

// Create managed wallet info
let mut managed_info = T::from_wallet(&wallet);
managed_info.set_birth_height(self.last_processed_height());
let mut managed_info = T::from_wallet(&wallet, self.last_processed_height());
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, wallet);
Expand Down Expand Up @@ -400,8 +396,7 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
}

// Create managed wallet info
let mut managed_info = T::from_wallet(&wallet);
managed_info.set_birth_height(self.last_processed_height());
let mut managed_info = T::from_wallet(&wallet, self.last_processed_height());
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, wallet);
Expand Down Expand Up @@ -442,11 +437,10 @@ impl<T: WalletInfoInterface + Send + Sync + 'static> WalletManager<T> {
return Err(WalletError::WalletExists(wallet_id));
}

// Create managed wallet info from the imported wallet
let mut managed_info = T::from_wallet(&wallet);

// Use the current height as the birth height since we don't know when it was originally created
managed_info.set_birth_height(self.last_processed_height());
// Create managed wallet info from the imported wallet, using the manager's
// current aggregated last-processed height as the fallback birth height
// since the serialized form does not preserve it.
let mut managed_info = T::from_wallet(&wallet, self.last_processed_height());
managed_info.set_first_loaded_at(current_timestamp());

self.wallets.insert(wallet_id, wallet);
Expand Down
27 changes: 16 additions & 11 deletions key-wallet-manager/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,36 +181,41 @@ fn test_block_height_tracking() {
.create_wallet_from_mnemonic(
&mnemonic2.to_string(),
"",
0,
5000,
WalletAccountCreationOptions::Default,
)
.unwrap();

assert_eq!(manager.wallet_count(), 2);

// Both wallets initialized with `synced_height = birth_height - 1 = 0`,
// so neither has been processed past genesis.
for wallet_info in manager.get_all_wallet_infos().values() {
assert_eq!(wallet_info.last_processed_height(), 0);
assert_eq!(wallet_info.synced_height(), 0);
}
// wallet1 was created with birth_height = 0 so its sync checkpoint stays at 0;
// wallet2 was created with birth_height = 5000 and is seeded to birth_height - 1.
let wallet_info1 = manager.get_wallet_info(&wallet_id1).unwrap();
let wallet_info2 = manager.get_wallet_info(&wallet_id2).unwrap();
assert_eq!(wallet_info1.last_processed_height(), 0);
assert_eq!(wallet_info1.synced_height(), 0);
assert_eq!(wallet_info2.last_processed_height(), 4999);
assert_eq!(wallet_info2.synced_height(), 4999);
// Manager aggregates: synced_height is the min, last_processed_height is the max.
assert_eq!(manager.synced_height(), 0);
assert_eq!(manager.last_processed_height(), 4999);

// Per-wallet last-processed updates only touch the addressed wallet.
manager.update_wallet_last_processed_height(&wallet_id1, 12345);
assert_eq!(manager.last_processed_height(), 12345);
let wallet_info1 = manager.get_wallet_info(&wallet_id1).unwrap();
let wallet_info2 = manager.get_wallet_info(&wallet_id2).unwrap();
assert_eq!(wallet_info1.last_processed_height(), 12345);
assert_eq!(wallet_info2.last_processed_height(), 0);
assert_eq!(wallet_info2.last_processed_height(), 4999);

// Per-wallet synced-height updates only touch the addressed wallet.
manager.update_wallet_synced_height(&wallet_id1, 12000);
let wallet_info1 = manager.get_wallet_info(&wallet_id1).unwrap();
let wallet_info2 = manager.get_wallet_info(&wallet_id2).unwrap();
assert_eq!(wallet_info1.synced_height(), 12000);
assert_eq!(wallet_info2.synced_height(), 0);
// Aggregate `synced_height()` is `min` across wallets, so wallet 2 holds it at 0.
assert_eq!(manager.synced_height(), 0);
assert_eq!(wallet_info2.synced_height(), 4999);
// Aggregate `synced_height()` is `min` across wallets, so wallet 2 holds it at 4999.
assert_eq!(manager.synced_height(), 4999);

// Advance wallet 2 too. Aggregate min jumps to wallet 2's new value.
manager.update_wallet_synced_height(&wallet_id2, 11000);
Expand Down
7 changes: 7 additions & 0 deletions key-wallet-manager/tests/test_serialized_wallets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[cfg(test)]
mod tests {
use key_wallet::wallet::initialization::WalletAccountCreationOptions;
use key_wallet::wallet::managed_wallet_info::wallet_info_interface::WalletInfoInterface;
use key_wallet::wallet::managed_wallet_info::ManagedWalletInfo;
use key_wallet::Network;
use key_wallet_manager::WalletManager;
Expand All @@ -26,6 +27,12 @@ mod tests {
assert!(!bytes.is_empty());
println!("Full wallet ID: {}", hex::encode(wallet_id));

// The wallet's sync checkpoint should be seeded to birth_height - 1.
let info = manager.get_wallet_info(&wallet_id).unwrap();
assert_eq!(info.birth_height(), 100_000);
assert_eq!(info.synced_height(), 99_999);
assert_eq!(info.last_processed_height(), 99_999);

// Test 2: Create watch-only wallet (no private keys)
let mut manager2 = WalletManager::<ManagedWalletInfo>::new(Network::Testnet);
let result = manager2.create_wallet_from_mnemonic_return_serialized_bytes(
Expand Down
2 changes: 1 addition & 1 deletion key-wallet/src/test_utils/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl TestWalletContext {
let wallet = Wallet::new_random(Network::Testnet, WalletAccountCreationOptions::Default)
.expect("Should create wallet");
let mut managed_wallet =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

let xpub = wallet
.accounts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async fn test_identity_registration_account_routing() {
wallet.add_account(account_type, None).expect("Failed to add account to wallet");

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get the identity registration account
let account = wallet
Expand Down Expand Up @@ -155,7 +155,7 @@ async fn test_normal_payment_to_identity_address_not_detected() {
let mut wallet = Wallet::new_random(network, WalletAccountCreationOptions::Default)
.expect("Failed to create wallet with default options");
let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

let account = wallet
.accounts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ async fn test_provider_registration_transaction_routing_check_owner_only() {
.expect("Failed to create wallet with default options");

let mut other_managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string());
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string(), 0);

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get addresses from provider accounts
let managed_owner = managed_wallet_info
Expand Down Expand Up @@ -271,10 +271,10 @@ async fn test_provider_registration_transaction_routing_check_voting_only() {
.expect("Failed to create wallet with default options");

let mut other_managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string());
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string(), 0);

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get addresses from provider accounts
let owner_address = other_managed_wallet_info
Expand Down Expand Up @@ -406,10 +406,10 @@ async fn test_provider_registration_transaction_routing_check_operator_only() {
.expect("Failed to create wallet with default options");

let mut other_managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string());
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string(), 0);

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get addresses from provider accounts
let owner_address = other_managed_wallet_info
Expand Down Expand Up @@ -587,10 +587,10 @@ async fn test_provider_registration_transaction_routing_check_platform_only() {
.expect("Failed to create wallet with default options");

let mut other_managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string());
ManagedWalletInfo::from_wallet_with_name(&other_wallet, "Other".to_string(), 0);

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get addresses from provider accounts
let owner_address = other_managed_wallet_info
Expand Down Expand Up @@ -785,7 +785,7 @@ async fn test_provider_update_registrar_with_voting_and_operator() {
.expect("Failed to create wallet with default options");

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get voting address
let voting_address = managed_wallet_info
Expand Down Expand Up @@ -858,7 +858,7 @@ async fn test_provider_revocation_classification_and_routing() {
.expect("Failed to create wallet with default options");

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get a standard address for collateral return
let account = wallet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async fn test_transaction_routing_to_bip32_account() {
wallet.add_account(account_type, None).expect("Failed to add account to wallet");

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get the account's xpub for address derivation
let account = wallet
Expand Down Expand Up @@ -159,7 +159,7 @@ async fn test_transaction_routing_to_coinjoin_account() {
wallet.add_account(account_type, None).expect("Failed to add account to wallet");

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get the account's xpub
let account = wallet
Expand Down Expand Up @@ -258,7 +258,7 @@ async fn test_transaction_affects_multiple_accounts() {
wallet.add_account(account_type, None).expect("Failed to add account to wallet");

let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get addresses from different accounts

Expand Down Expand Up @@ -362,7 +362,7 @@ fn test_next_address_method_restrictions() {
let wallet = Wallet::new_random(Network::Testnet, WalletAccountCreationOptions::Default)
.expect("Failed to create wallet with default options");
let mut managed_wallet_info =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Test that standard BIP44 accounts reject next_address
{
Expand Down
8 changes: 4 additions & 4 deletions key-wallet/src/transaction_checking/wallet_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod tests {
.expect("Should create wallet");

let mut managed_wallet =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Create a transaction to an external address
let dummy_address = Address::p2pkh(
Expand Down Expand Up @@ -311,7 +311,7 @@ mod tests {
.expect("Should add identity registration account");

let mut managed_wallet =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

// Get BIP32 account address - scope the immutable borrow
let (bip32_xpub, bip32_address) = {
Expand Down Expand Up @@ -1029,7 +1029,7 @@ mod tests {
.expect("Should add second BIP44 account");

let mut managed_wallet =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

let xpub0 = wallet
.accounts
Expand Down Expand Up @@ -1638,7 +1638,7 @@ mod tests {
.expect("add coinjoin");

let mut managed_wallet =
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);

let xpub =
wallet.accounts.coinjoin_accounts.get(&0).expect("coinjoin account").account_xpub;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ mod tests {
fn test_wallet_and_info() -> (Wallet, ManagedWalletInfo) {
let wallet =
Wallet::new_random(Network::Testnet, WalletAccountCreationOptions::Default).unwrap();
let info = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string());
let info = ManagedWalletInfo::from_wallet_with_name(&wallet, "Test".to_string(), 0);
(wallet, info)
}

Expand Down
Loading
Loading