Skip to content

Commit

Permalink
feat(wallet): add sync_request and full_scan_request functions
Browse files Browse the repository at this point in the history
  • Loading branch information
notmandatory committed Feb 22, 2024
1 parent 557ad35 commit 9c200c3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 3 additions & 5 deletions crates/bdk/src/wallet/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ impl fmt::Display for MiniscriptPsbtError {
impl std::error::Error for MiniscriptPsbtError {}

#[derive(Debug)]
/// Error returned from [`TxBuilder::finish`]
/// Error returned by [`TxBuilder::finish`]
///
/// [`TxBuilder::finish`]: crate::wallet::tx_builder::TxBuilder::finish
/// [`TxBuilder::finish`]: super::tx_builder::TxBuilder::finish
pub enum CreateTxError<P> {
/// There was a problem with the descriptors passed in
Descriptor(DescriptorError),
Expand Down Expand Up @@ -246,9 +246,7 @@ impl<P> From<coin_selection::Error> for CreateTxError<P> {
impl<P: core::fmt::Display + core::fmt::Debug> std::error::Error for CreateTxError<P> {}

#[derive(Debug)]
/// Error returned from [`Wallet::build_fee_bump`]
///
/// [`Wallet::build_fee_bump`]: super::Wallet::build_fee_bump
/// Error returned by [`Wallet::build_fee_bump`]
pub enum BuildFeeBumpError {
/// Happens when trying to spend an UTXO that is not in the internal database
UnknownUtxo(OutPoint),
Expand Down
26 changes: 26 additions & 0 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ use core::ops::Deref;
use descriptor::error::Error as DescriptorError;
use miniscript::psbt::{PsbtExt, PsbtInputExt, PsbtInputSatisfier};

use bdk_chain::spk_client::{FullScanRequest, SyncRequest};
use bdk_chain::tx_graph::CalculateFeeError;

pub mod coin_selection;
Expand Down Expand Up @@ -2488,6 +2489,31 @@ impl<D> Wallet<D> {
.batch_insert_relevant_unconfirmed(unconfirmed_txs);
self.persist.stage(ChangeSet::from(indexed_graph_changeset));
}

/// Create a [`SyncRequest`] for this wallet for all revealed spks.
///
/// This is the first step when performing a spk-based wallet sync, the returned [`SyncRequest`] collects
/// all revealed script pub keys from the wallet keychain needed to start a blockchain sync with a spk based
/// blockchain client.
pub fn sync_revealed_spks_request(&self) -> SyncRequest {
let chain_tip = self.local_chain().tip();
self.spk_index().sync_revealed_spks_request(chain_tip)
}

/// Create a [`FullScanRequest] for this wallet.
///
/// This is the first step when performing a spk-based wallet full scan, the returned [`FullScanRequest]
/// collects iterators for the wallet's keychain script pub keys needed to start a blockchain full scan
/// with a spk based blockchain client.
///
/// This operation is generally only used when importing or restoring a previously used wallet
/// in which the list of used scripts is not known.
pub fn full_scan_request(
&self,
) -> FullScanRequest<KeychainKind, impl Iterator<Item = (u32, ScriptBuf)> + Clone> {
let chain_tip = self.local_chain().tip();
self.spk_index().full_scan_request(chain_tip)
}
}

impl<D> AsRef<bdk_chain::tx_graph::TxGraph<ConfirmationTimeHeightAnchor>> for Wallet<D> {
Expand Down

0 comments on commit 9c200c3

Please sign in to comment.