diff --git a/examples/wallet.rs b/examples/wallet.rs index 4e8b640..a810c2d 100644 --- a/examples/wallet.rs +++ b/examples/wallet.rs @@ -2,7 +2,7 @@ use std::net::{IpAddr, Ipv4Addr}; use bdk_kyoto::builder::{LightClientBuilder, ServiceFlags, TrustedPeer}; use bdk_kyoto::logger::TraceLogger; -use bdk_kyoto::LightClient; +use bdk_kyoto::{EventSenderExt, LightClient}; use bdk_wallet::bitcoin::Network; use bdk_wallet::{KeychainKind, Wallet}; @@ -32,7 +32,7 @@ async fn main() -> anyhow::Result<()> { // The light client builder handles the logic of inserting the SPKs let LightClient { - sender: _, + sender, mut receiver, node, } = LightClientBuilder::new(&wallet) @@ -71,6 +71,7 @@ async fn main() -> anyhow::Result<()> { "Broadcast minimum fee rate: {}", receiver.broadcast_minimum() ); + sender.add_revealed_scripts(&wallet).await?; } } } diff --git a/src/lib.rs b/src/lib.rs index d7beb72..21ad6a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,8 +159,13 @@ #![warn(missing_docs)] use core::fmt; +#[cfg(feature = "wallet")] +use core::{future::Future, pin::Pin}; use std::collections::BTreeMap; +#[cfg(feature = "wallet")] +type FutureResult<'a, T, E> = Pin> + Send + 'a>>; + use bdk_chain::{ keychain_txout::KeychainTxOutIndex, local_chain::{self, CheckPoint, LocalChain}, @@ -487,6 +492,37 @@ pub enum LogLevel { Warning, } +/// Extend the [`EventSender`] functionality to work conveniently with a [`Wallet`](bdk_wallet). +#[cfg(feature = "wallet")] +pub trait EventSenderExt { + /// Add all revealed scripts to the node to monitor. + fn add_revealed_scripts<'a>( + &'a self, + wallet: &'a bdk_wallet::Wallet, + ) -> FutureResult<'a, (), kyoto::ClientError>; +} + +impl EventSenderExt for EventSender { + fn add_revealed_scripts<'a>( + &'a self, + wallet: &'a bdk_wallet::Wallet, + ) -> FutureResult<'a, (), kyoto::ClientError> { + async fn _add_revealed( + sender: &EventSender, + wallet: &bdk_wallet::Wallet, + ) -> Result<(), kyoto::ClientError> { + for keychain in [KeychainKind::External, KeychainKind::Internal] { + let scripts = wallet.spk_index().revealed_keychain_spks(keychain); + for (_, script) in scripts { + sender.add_script(script).await?; + } + } + Ok(()) + } + Box::pin(_add_revealed(self, wallet)) + } +} + trait StringExt { fn into_string(self) -> String; }