Skip to content

Commit 4c52f3e

Browse files
feat(wallet): make wallet compatible with sync/full-scan structures
* Changed `Wallet::apply_update` to also take in anything that implements `Into<Update>`. This allows us to directly apply a `FullScanResult` or `SyncResult`. * Added `start_full_scan` and `start_sync_with_revealed_spks` methods to `Wallet`. Co-authored-by: Steve Myers <[email protected]>
1 parent cdfec5f commit 4c52f3e

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

crates/bdk/src/wallet/mod.rs

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bdk_chain::{
2626
local_chain::{
2727
self, ApplyHeaderError, CannotConnectError, CheckPoint, CheckPointIter, LocalChain,
2828
},
29+
spk_client::{FullScanRequest, FullScanResult, SyncRequest, SyncResult},
2930
tx_graph::{CanonicalTx, TxGraph},
3031
Append, BlockId, ChainPosition, ConfirmationTime, ConfirmationTimeHeightAnchor, FullTxOut,
3132
IndexedTxGraph, Persist, PersistBackend,
@@ -110,6 +111,26 @@ pub struct Update {
110111
pub chain: Option<CheckPoint>,
111112
}
112113

114+
impl From<FullScanResult<KeychainKind>> for Update {
115+
fn from(value: FullScanResult<KeychainKind>) -> Self {
116+
Self {
117+
last_active_indices: value.last_active_indices,
118+
graph: value.graph_update,
119+
chain: Some(value.chain_update),
120+
}
121+
}
122+
}
123+
124+
impl From<SyncResult> for Update {
125+
fn from(value: SyncResult) -> Self {
126+
Self {
127+
last_active_indices: BTreeMap::new(),
128+
graph: value.graph_update,
129+
chain: Some(value.chain_update),
130+
}
131+
}
132+
}
133+
113134
/// The changes made to a wallet by applying an [`Update`].
114135
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize, Default)]
115136
pub struct ChangeSet {
@@ -2262,7 +2283,8 @@ impl Wallet {
22622283
/// transactions related to your wallet into it.
22632284
///
22642285
/// [`commit`]: Self::commit
2265-
pub fn apply_update(&mut self, update: Update) -> Result<(), CannotConnectError> {
2286+
pub fn apply_update(&mut self, update: impl Into<Update>) -> Result<(), CannotConnectError> {
2287+
let update = update.into();
22662288
let mut changeset = match update.chain {
22672289
Some(chain_update) => ChangeSet::from(self.chain.apply_update(chain_update)?),
22682290
None => ChangeSet::default(),
@@ -2387,6 +2409,31 @@ impl Wallet {
23872409
}
23882410
}
23892411

2412+
/// Methods to construct sync/full-scan requests for spk-based chain sources.
2413+
impl Wallet {
2414+
/// Create a partial [`SyncRequest`] for this wallet for all revealed spks.
2415+
///
2416+
/// This is the first step when performing a spk-based wallet partial sync, the returned
2417+
/// [`SyncRequest`] collects all revealed script pubkeys from the wallet keychain needed to
2418+
/// start a blockchain sync with a spk based blockchain client.
2419+
pub fn start_sync_with_revealed_spks(&self) -> SyncRequest {
2420+
SyncRequest::from_chain_tip(self.chain.tip())
2421+
.populate_with_revealed_spks(&self.indexed_graph.index, ..)
2422+
}
2423+
2424+
/// Create a [`FullScanRequest] for this wallet.
2425+
///
2426+
/// This is the first step when performing a spk-based wallet full scan, the returned
2427+
/// [`FullScanRequest] collects iterators for the wallet's keychain script pub keys needed to
2428+
/// start a blockchain full scan with a spk based blockchain client.
2429+
///
2430+
/// This operation is generally only used when importing or restoring a previously used wallet
2431+
/// in which the list of used scripts is not known.
2432+
pub fn start_full_scan(&self) -> FullScanRequest<KeychainKind> {
2433+
FullScanRequest::from_keychain_txout_index(self.chain.tip(), &self.indexed_graph.index)
2434+
}
2435+
}
2436+
23902437
impl AsRef<bdk_chain::tx_graph::TxGraph<ConfirmationTimeHeightAnchor>> for Wallet {
23912438
fn as_ref(&self) -> &bdk_chain::tx_graph::TxGraph<ConfirmationTimeHeightAnchor> {
23922439
self.indexed_graph.graph()

0 commit comments

Comments
 (0)