Skip to content

Commit

Permalink
feat(client): add EventSenderExt to work with Wallet directly
Browse files Browse the repository at this point in the history
  • Loading branch information
rustaceanrob committed Dec 16, 2024
1 parent 5792ade commit 4e54637
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
5 changes: 3 additions & 2 deletions examples/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -71,6 +71,7 @@ async fn main() -> anyhow::Result<()> {
"Broadcast minimum fee rate: {}",
receiver.broadcast_minimum()
);
sender.add_revealed_scripts(&wallet).await?;
}
}
}
36 changes: 36 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;

use bdk_chain::{
keychain_txout::KeychainTxOutIndex,
local_chain::{self, CheckPoint, LocalChain},
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 4e54637

Please sign in to comment.