Skip to content

Commit bafa77d

Browse files
committed
feat(client): add EventSenderExt to work with Wallet directly
1 parent 5792ade commit bafa77d

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

examples/wallet.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::net::{IpAddr, Ipv4Addr};
22

33
use bdk_kyoto::builder::{LightClientBuilder, ServiceFlags, TrustedPeer};
44
use bdk_kyoto::logger::TraceLogger;
5-
use bdk_kyoto::LightClient;
5+
use bdk_kyoto::{EventSenderExt, LightClient};
66
use bdk_wallet::bitcoin::Network;
77
use bdk_wallet::{KeychainKind, Wallet};
88

@@ -32,7 +32,7 @@ async fn main() -> anyhow::Result<()> {
3232

3333
// The light client builder handles the logic of inserting the SPKs
3434
let LightClient {
35-
sender: _,
35+
sender,
3636
mut receiver,
3737
node,
3838
} = LightClientBuilder::new(&wallet)
@@ -71,6 +71,7 @@ async fn main() -> anyhow::Result<()> {
7171
"Broadcast minimum fee rate: {}",
7272
receiver.broadcast_minimum()
7373
);
74+
sender.add_revealed_scripts(&wallet).await?;
7475
}
7576
}
7677
}

src/lib.rs

+36
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,13 @@
159159
160160
#![warn(missing_docs)]
161161
use core::fmt;
162+
#[cfg(feature = "wallet")]
163+
use core::{future::Future, pin::Pin};
162164
use std::collections::BTreeMap;
163165

166+
#[cfg(feature = "wallet")]
167+
type FutureResult<'a, T, E> = Pin<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;
168+
164169
use bdk_chain::{
165170
keychain_txout::KeychainTxOutIndex,
166171
local_chain::{self, CheckPoint, LocalChain},
@@ -487,6 +492,37 @@ pub enum LogLevel {
487492
Warning,
488493
}
489494

495+
/// Extend the [`EventSender`] functionality to work conviently with a [`Wallet`](bdk_wallet).
496+
#[cfg(feature = "wallet")]
497+
pub trait EventSenderExt {
498+
/// Add all revealed scripts to the node to monitor.
499+
fn add_revealed_scripts<'a>(
500+
&'a self,
501+
wallet: &'a bdk_wallet::Wallet,
502+
) -> FutureResult<(), kyoto::ClientError>;
503+
}
504+
505+
impl EventSenderExt for EventSender {
506+
fn add_revealed_scripts<'a>(
507+
&'a self,
508+
wallet: &'a bdk_wallet::Wallet,
509+
) -> FutureResult<(), kyoto::ClientError> {
510+
async fn _add_revealed(
511+
sender: &EventSender,
512+
wallet: &bdk_wallet::Wallet,
513+
) -> Result<(), kyoto::ClientError> {
514+
for keychain in [KeychainKind::External, KeychainKind::Internal] {
515+
let scripts = wallet.spk_index().revealed_keychain_spks(keychain);
516+
for (_, script) in scripts {
517+
sender.add_script(script).await?;
518+
}
519+
}
520+
Ok(())
521+
}
522+
Box::pin(_add_revealed(&self, wallet))
523+
}
524+
}
525+
490526
trait StringExt {
491527
fn into_string(self) -> String;
492528
}

0 commit comments

Comments
 (0)