diff --git a/examples/wallet.rs b/examples/wallet.rs index 00520fb..cc0ccfa 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() @@ -58,19 +58,20 @@ async fn main() -> anyhow::Result<()> { wallet.apply_update(update)?; tracing::info!("Tx count: {}", wallet.transactions().count()); tracing::info!("Balance: {}", wallet.balance().total().to_sat()); - let last_revealed = wallet.derivation_index(KeychainKind::External).unwrap(); - tracing::info!("Last revealed External: {}", last_revealed); + let last_revealed = wallet.derivation_index(KeychainKind::External); + tracing::info!("Last revealed External: {:?}", last_revealed); tracing::info!( - "Last revealed Internal: {}", - wallet.derivation_index(KeychainKind::Internal).unwrap() + "Last revealed Internal: {:?}", + wallet.derivation_index(KeychainKind::Internal) ); tracing::info!("Local chain tip: {}", wallet.local_chain().tip().height()); - let next = wallet.peek_address(KeychainKind::External, last_revealed + 1); + let next = wallet.reveal_next_address(KeychainKind::External).address; tracing::info!("Next receiving address: {next}"); tracing::info!( "Broadcast minimum fee rate: {}", receiver.broadcast_minimum() ); + sender.add_revealed_scripts(&wallet).await?; } } } diff --git a/src/lib.rs b/src/lib.rs index ddb357c..8feccc4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -159,10 +159,15 @@ #![warn(missing_docs)] use core::fmt; +#[cfg(feature = "wallet")] +use core::{future::Future, pin::Pin}; use std::collections::BTreeMap; #[cfg(feature = "wallet")] use std::collections::HashSet; +#[cfg(feature = "wallet")] +type FutureResult<'a, T, E> = Pin> + Send + 'a>>; + use bdk_chain::{ keychain_txout::KeychainTxOutIndex, local_chain::{self, CheckPoint, LocalChain}, @@ -514,6 +519,39 @@ impl WalletExt for bdk_wallet::Wallet { Box::new(spks.into_iter()) } } + +/// 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>; +} + +#[cfg(feature = "wallet")] +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; }