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 17, 2024
1 parent 71fc288 commit c182d50
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
15 changes: 8 additions & 7 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()
Expand All @@ -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?;
}
}
}
38 changes: 38 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<dyn Future<Output = Result<T, E>> + Send + 'a>>;

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

0 comments on commit c182d50

Please sign in to comment.