Skip to content

fetch_index can mutate the database and increment the index #701

@nickfarrow

Description

@nickfarrow

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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions