diff --git a/key-wallet/src/managed_account/address_pool.rs b/key-wallet/src/managed_account/address_pool.rs index f0b426bc9..2f504fa8b 100644 --- a/key-wallet/src/managed_account/address_pool.rs +++ b/key-wallet/src/managed_account/address_pool.rs @@ -471,6 +471,12 @@ impl AddressPool { Ok(new_addresses) } + /// Generate every address up to and including `index` + pub fn generate_up_to(&mut self, index: u32, key_source: &KeySource) -> Result> { + let next = self.highest_generated.map_or(0, |highest| highest + 1); + self.generate_addresses((index + 1).saturating_sub(next), key_source, true) + } + /// Generate a specific address at an index pub(crate) fn generate_address_at_index( &mut self, @@ -1363,6 +1369,27 @@ mod tests { assert_eq!(pool.addresses.len(), 10); } + #[test] + fn test_generate_up_to() { + let base_path = DerivationPath::from(vec![ChildNumber::from_normal_idx(0).unwrap()]); + let key_source = test_key_source(); + let mut pool = AddressPool::new( + base_path, + AddressPoolType::External, + 5, + Network::Testnet, + &key_source, + ) + .unwrap(); + + assert_eq!(pool.generate_up_to(29, &key_source).unwrap().len(), 25); + assert_eq!(pool.highest_generated, Some(29)); + assert_eq!(pool.addresses.len(), 30); + + assert!(pool.generate_up_to(10, &key_source).unwrap().is_empty()); + assert_eq!(pool.highest_generated, Some(29)); + } + #[test] fn test_address_usage() { let base_path = DerivationPath::from(vec![ChildNumber::from_normal_idx(0).unwrap()]); diff --git a/key-wallet/src/wallet/managed_wallet_info/mod.rs b/key-wallet/src/wallet/managed_wallet_info/mod.rs index 67d927da4..4938ebec9 100644 --- a/key-wallet/src/wallet/managed_wallet_info/mod.rs +++ b/key-wallet/src/wallet/managed_wallet_info/mod.rs @@ -614,6 +614,30 @@ impl ManagedWalletInfo { address } + + /// Derive both pools of every CoinJoin account up to and including `last_index`. + /// + /// Call it on fresh wallet state, before the first sync from birth: blocks + /// already processed are not scanned again for the new addresses. + pub fn extend_coinjoin_pools( + &mut self, + wallet: &Wallet, + last_index: u32, + ) -> crate::error::Result<()> { + for (index, account) in self.accounts.coinjoin_accounts.iter_mut() { + let key_source = wallet.key_source_for_account(crate::AccountType::CoinJoin { + index: *index, + }); + + for pool in account.managed_account_type_mut().address_pools_mut() { + pool.generate_up_to(last_index, &key_source)?; + } + + account.bump_monitor_revision(); + } + + Ok(()) + } } /// Re-export types from account module for convenience