mod output_ids;
mod outputs;
use std::collections::HashSet;
use crate::{
client::secret::SecretManage,
wallet::account::{operations::syncing::SyncOptions, types::address::AddressWithUnspentOutputs, Account},
};
impl<S: 'static + SecretManage> Account<S>
where
crate::wallet::Error: From<S::Error>,
{
pub(crate) async fn get_addresses_to_sync(
&self,
options: &SyncOptions,
) -> crate::wallet::Result<Vec<AddressWithUnspentOutputs>> {
log::debug!("[SYNC] get_addresses_to_sync");
let mut addresses_before_syncing = self.addresses().await?;
if !options.addresses.is_empty() {
let mut specific_addresses_to_sync = HashSet::new();
for bech32_address in &options.addresses {
match addresses_before_syncing.iter().find(|a| &a.address == bech32_address) {
Some(address) => {
specific_addresses_to_sync.insert(address.clone());
}
None => {
return Err(crate::wallet::Error::AddressNotFoundInAccount(*bech32_address));
}
}
}
addresses_before_syncing = specific_addresses_to_sync.into_iter().collect();
} else if options.address_start_index != 0 || options.address_start_index_internal != 0 {
addresses_before_syncing.retain(|a| {
if a.internal {
a.key_index >= options.address_start_index_internal
} else {
a.key_index >= options.address_start_index
}
});
}
let addresses_with_unspent_outputs = self.addresses_with_unspent_outputs().await?;
let mut addresses_with_old_output_ids = Vec::new();
for address in addresses_before_syncing {
let mut output_ids = Vec::new();
if let Some(address_with_unspent_outputs) = addresses_with_unspent_outputs
.iter()
.find(|a| a.address == address.address)
{
output_ids = address_with_unspent_outputs.output_ids.to_vec();
}
addresses_with_old_output_ids.push(AddressWithUnspentOutputs {
address: address.address,
key_index: address.key_index,
internal: address.internal,
output_ids,
})
}
Ok(addresses_with_old_output_ids)
}
}