Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WalletExt to extend Wallet for ease-of-use #83

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@
#![warn(missing_docs)]
use core::fmt;
use std::collections::BTreeMap;
#[cfg(feature = "wallet")]
use std::collections::HashSet;

use bdk_chain::{
keychain_txout::KeychainTxOutIndex,
Expand All @@ -183,7 +185,9 @@ pub use kyoto::{DisconnectedHeader, FailurePayload};

pub use kyoto::ClientSender as EventSender;
use kyoto::{IndexedBlock, NodeMessage, RejectReason};
pub use kyoto::{NodeState, Receiver, SyncUpdate, TxBroadcast, TxBroadcastPolicy, Txid, Warning};
pub use kyoto::{
NodeState, Receiver, ScriptBuf, SyncUpdate, TxBroadcast, TxBroadcastPolicy, Txid, Warning,
};

#[cfg(all(feature = "wallet", feature = "rusqlite"))]
pub mod builder;
Expand Down Expand Up @@ -487,6 +491,29 @@ pub enum LogLevel {
Warning,
}

/// Extend the functionality of [`Wallet`](bdk_wallet) for interoperablility
/// with the light client.
#[cfg(feature = "wallet")]
pub trait WalletExt {
/// Collect relevant scripts for addition to the node. Peeks scripts
/// `lookahead` + `last_revealed_index` for each keychain.
fn peek_revealed_plus_lookahead(&self) -> Box<dyn Iterator<Item = ScriptBuf>>;
}

#[cfg(feature = "wallet")]
impl WalletExt for bdk_wallet::Wallet {
fn peek_revealed_plus_lookahead(&self) -> Box<dyn Iterator<Item = ScriptBuf>> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering from an API perspective if this would make sense if implemented in bdk_wallet with a more abstract name, something like all_spks or just an iterator of all the derived addresses the wallet knows about including unrevealed. Then again, our peeking at the lookahead is somewhat off-label from the regular address API so probably better to leave it an extension trait.

let mut spks: HashSet<ScriptBuf> = HashSet::new();
for keychain in [KeychainKind::External, KeychainKind::Internal] {
let last_revealed = self.spk_index().last_revealed_index(keychain).unwrap_or(0);
let lookahead_index = last_revealed + self.spk_index().lookahead();
for index in 0..=lookahead_index {
spks.insert(self.peek_address(keychain, index).script_pubkey());
}
}
Box::new(spks.into_iter())
}
}
trait StringExt {
fn into_string(self) -> String;
}
Expand Down
Loading