Skip to content

Commit

Permalink
chain: Return Option instead of panicking for...
Browse files Browse the repository at this point in the history
...missing keychains

Changelog:
- `KeychainTxOutIndex::reveal_next_spk` returns Option
- `KeychainTxOutIndex::next_unused_spk` returns Option
- `KeychainTxOutIndex::unbounded_spk_iter` returns Option
- `KeychainTxOutIndex::next_index` returns Option
- `KeychainTxOutIndex::reveal_to_target` returns Option
- `KeychainTxOutIndex::revealed_keychain_spks` returns Option
- `KeychainTxOutIndex::unused_keychain_spks` returns Option
- `KeychainTxOutIndex::last_revealed_index` returns Option
- `KeychainTxOutIndex::keychain_outpoints` returns Option
- `KeychainTxOutIndex::keychain_outpoints_in_range` returns Option
- `KeychainTxOutIndex::last_used_index` returns None if the keychain
  has never been used, or if it doesn't exist
  -
  • Loading branch information
danielabrozzoni committed Feb 26, 2024
1 parent d006a60 commit d3f6b5b
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 201 deletions.
31 changes: 24 additions & 7 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -828,15 +828,21 @@ impl<D> Wallet<D> {
let txout_index = &mut self.indexed_graph.index;
let (index, spk, changeset) = match address_index {
AddressIndex::New => {
let ((index, spk), index_changeset) = txout_index.reveal_next_spk(&keychain);
let ((index, spk), index_changeset) = txout_index
.reveal_next_spk(&keychain)
.expect("Keychain exists (we called map_keychain)");
(index, spk.into(), Some(index_changeset))
}
AddressIndex::LastUnused => {
let ((index, spk), index_changeset) = txout_index.next_unused_spk(&keychain);
let ((index, spk), index_changeset) = txout_index
.next_unused_spk(&keychain)
.expect("Keychain exists (we called map_keychain)");
(index, spk.into(), Some(index_changeset))
}
AddressIndex::Peek(mut peek_index) => {
let mut spk_iter = txout_index.unbounded_spk_iter(&keychain);
let mut spk_iter = txout_index
.unbounded_spk_iter(&keychain)
.expect("Keychain exists (we called map_keychain)");
if !spk_iter.descriptor().has_wildcard() {
peek_index = 0;
}
Expand Down Expand Up @@ -935,7 +941,10 @@ impl<D> Wallet<D> {
keychain: KeychainKind,
) -> impl Iterator<Item = (u32, ScriptBuf)> + Clone {
let keychain = self.map_keychain(keychain);
self.indexed_graph.index.unbounded_spk_iter(&keychain)
self.indexed_graph
.index
.unbounded_spk_iter(&keychain)
.expect("Must exist (we called map_keychain)")
}

/// Returns the utxo owned by this wallet corresponding to `outpoint` if it exists in the
Expand Down Expand Up @@ -1569,8 +1578,11 @@ impl<D> Wallet<D> {
Some(ref drain_recipient) => drain_recipient.clone(),
None => {
let change_keychain = self.map_keychain(KeychainKind::Internal);
let ((index, spk), index_changeset) =
self.indexed_graph.index.next_unused_spk(&change_keychain);
let ((index, spk), index_changeset) = self
.indexed_graph
.index
.next_unused_spk(&change_keychain)
.expect("Keychain exists (we called map_keychain)");
let spk = spk.into();
self.indexed_graph.index.mark_used(change_keychain, index);
self.persist
Expand Down Expand Up @@ -2042,7 +2054,12 @@ impl<D> Wallet<D> {

/// The index of the next address that you would get if you were to ask the wallet for a new address
pub fn next_derivation_index(&self, keychain: KeychainKind) -> u32 {
self.indexed_graph.index.next_index(&keychain).0
let keychain = self.map_keychain(keychain);
self.indexed_graph
.index
.next_index(&keychain)
.expect("Keychain must exist (we called map_keychain)")
.0
}

/// Informs the wallet that you no longer intend to broadcast a tx that was built from it.
Expand Down
Loading

0 comments on commit d3f6b5b

Please sign in to comment.