Skip to content

Commit dfdcd83

Browse files
committed
feat: add Wallet::with_custom_params API
So that advanced users can specify custom genesis_hash and lookahead or enable spk_cache.
1 parent e468a1b commit dfdcd83

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

wallet/src/wallet/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,50 @@ where
354354
}
355355
}
356356

357+
/// Construct a new [`Wallet`] with the given `keyring`, `genesis_hash` and `lookahead`.
358+
///
359+
/// The `genesis_hash` (if not specified) will be inferred from `keyring.network` and
360+
/// `DEFAULT_LOOKAHEAD` will be used in case `lookahead` is not specified.
361+
pub fn with_custom_params(
362+
mut keyring: KeyRing<K>,
363+
genesis_hash: Option<BlockHash>,
364+
lookahead: Option<u32>,
365+
use_spk_cache: bool,
366+
) -> Self {
367+
let network = keyring.network;
368+
let genesis_inferred = bitcoin::constants::genesis_block(network).block_hash();
369+
370+
let (chain, chain_changeset) =
371+
LocalChain::from_genesis_hash(genesis_hash.unwrap_or(genesis_inferred));
372+
373+
let mut index =
374+
KeychainTxOutIndex::new(lookahead.unwrap_or(DEFAULT_LOOKAHEAD), use_spk_cache);
375+
376+
let descriptors = core::mem::take(&mut keyring.descriptors);
377+
for (keychain, desc) in descriptors {
378+
let _inserted = index
379+
.insert_descriptor(keychain, desc)
380+
.expect("err: failed to insert descriptor");
381+
assert!(_inserted);
382+
}
383+
384+
let tx_graph = KeychainTxGraph::new(index);
385+
386+
let stage = ChangeSet {
387+
keyring: keyring.initial_changeset(),
388+
local_chain: chain_changeset,
389+
tx_graph: bdk_chain::tx_graph::ChangeSet::default(),
390+
indexer: bdk_chain::keychain_txout::ChangeSet::default(),
391+
};
392+
393+
Self {
394+
keyring,
395+
chain,
396+
tx_graph,
397+
stage,
398+
}
399+
}
400+
357401
/// Reveal the next address of the default `keychain`.
358402
///
359403
/// This is equivalent to calling [`Self::reveal_next_address`] with the default `keychain` as

0 commit comments

Comments
 (0)