wallet.fetch_index(KeychainKind) can mutate the database which can lead to unexpected results.
For example, the two following snippets give different addresses with a fresh wallet:
dbg!(wallet.get_address(New).unwrap().to_string());
wallet.fetch_index(KeychainKind::External).unwrap();
dbg!(wallet.get_address(New).unwrap().to_string());
In the 2nd case, since no index exists yet in the database, fetch_index follows the else path which fetches and increments the index:
fn fetch_index(&self, keychain: KeychainKind) -> Result<u32, Error> {
let (descriptor, keychain) = self._get_descriptor_for_keychain(keychain);
let index = match descriptor.is_deriveable() {
false => Some(0),
true => self.database.borrow_mut().get_last_index(keychain)?,
};
if let Some(i) = index {
Ok(i)
} else {
// HERE
self.fetch_and_increment_index(keychain)
}
}
Perhaps fetch_index should just return None for fresh wallets?
Would replacing fetch_index with fetch_next_derivation_index fix this?
Thoughts/other solutions?
wallet.fetch_index(KeychainKind)can mutate the database which can lead to unexpected results.For example, the two following snippets give different addresses with a fresh wallet:
In the 2nd case, since no index exists yet in the database,
fetch_indexfollows theelsepath which fetches and increments the index:Perhaps
fetch_indexshould just returnNonefor fresh wallets?Would replacing
fetch_indexwithfetch_next_derivation_indexfix this?Thoughts/other solutions?