diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index 137aecc3..9d782967 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -447,7 +447,12 @@ impl Wallet { let genesis_hash = params .genesis_hash .unwrap_or(genesis_block(network).block_hash()); - let (chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash); + let (mut chain, chain_changeset) = LocalChain::from_genesis_hash(genesis_hash); + if let Some(birthday) = params.birthday { + chain + .apply_update(CheckPoint::new(birthday)) + .expect("wallet birthday overrides genesis hash."); + } let (descriptor, mut descriptor_keymap) = (params.descriptor)(&secp, network_kind)?; check_wallet_descriptor(&descriptor)?; diff --git a/src/wallet/params.rs b/src/wallet/params.rs index 4868074b..b4cd6a41 100644 --- a/src/wallet/params.rs +++ b/src/wallet/params.rs @@ -1,6 +1,6 @@ use alloc::boxed::Box; -use bdk_chain::keychain_txout::DEFAULT_LOOKAHEAD; +use bdk_chain::{keychain_txout::DEFAULT_LOOKAHEAD, BlockId}; use bitcoin::{BlockHash, Network, NetworkKind}; use miniscript::descriptor::KeyMap; @@ -65,6 +65,7 @@ pub struct CreateParams { pub(crate) change_descriptor_keymap: KeyMap, pub(crate) network: Network, pub(crate) genesis_hash: Option, + pub(crate) birthday: Option, pub(crate) lookahead: u32, pub(crate) use_spk_cache: bool, } @@ -88,6 +89,7 @@ impl CreateParams { change_descriptor_keymap: KeyMap::default(), network: Network::Bitcoin, genesis_hash: None, + birthday: None, lookahead: DEFAULT_LOOKAHEAD, use_spk_cache: false, } @@ -110,6 +112,7 @@ impl CreateParams { change_descriptor_keymap: KeyMap::default(), network: Network::Bitcoin, genesis_hash: None, + birthday: None, lookahead: DEFAULT_LOOKAHEAD, use_spk_cache: false, } @@ -135,6 +138,7 @@ impl CreateParams { change_descriptor_keymap: KeyMap::default(), network: Network::Bitcoin, genesis_hash: None, + birthday: None, lookahead: DEFAULT_LOOKAHEAD, use_spk_cache: false, } @@ -162,6 +166,13 @@ impl CreateParams { self } + /// Begin wallet scanning from the specified birthday. Particularly useful for block-based + /// scanning sources. + pub fn birthday(mut self, birthday: BlockId) -> Self { + self.birthday = Some(birthday); + self + } + /// Use a custom `lookahead` value. /// /// The `lookahead` defines a number of script pubkeys to derive over and above the last