Skip to content

Commit a9d1fb9

Browse files
committed
feat(electrum): add JWT authentication support via ConfigBuilder
1 parent a161ee2 commit a9d1fb9

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

crates/electrum/src/bdk_electrum_client.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,44 @@ impl<E: ElectrumApi> BdkElectrumClient<E> {
603603
}
604604
}
605605

606+
impl BdkElectrumClient<electrum_client::Client> {
607+
/// Creates a new BDK Electrum client from a URL with custom configuration.
608+
///
609+
/// This is a convenience method for creating an Electrum client with custom settings
610+
/// such as JWT authorization providers, timeouts, or proxy settings.
611+
///
612+
/// # Example with JWT Authentication
613+
///
614+
/// ```no_run
615+
/// # use bdk_electrum::{BdkElectrumClient, electrum_client};
616+
/// # use std::sync::Arc;
617+
/// let config = electrum_client::ConfigBuilder::new()
618+
/// .authorization_provider(Some(Arc::new(|| {
619+
/// // Your token provider that fetches/refreshes JWT
620+
/// Some("Bearer your-jwt-token".to_string())
621+
/// })))
622+
/// .timeout(Some(30))
623+
/// .build();
624+
///
625+
/// let client = BdkElectrumClient::from_config("tcp://server:50001", config)?;
626+
/// # Ok::<(), electrum_client::Error>(())
627+
/// ```
628+
///
629+
/// # Arguments
630+
///
631+
/// * `url` - Electrum server URL (e.g., "tcp://host:port" or "ssl://host:port")
632+
/// * `config` - Configuration created via [`electrum_client::ConfigBuilder`]
633+
///
634+
/// [`electrum_client::ConfigBuilder`]: crate::electrum_client::ConfigBuilder
635+
pub fn from_config(
636+
url: &str,
637+
config: electrum_client::Config,
638+
) -> Result<Self, electrum_client::Error> {
639+
let client = electrum_client::Client::from_config(url, config)?;
640+
Ok(Self::new(client))
641+
}
642+
}
643+
606644
/// Return a [`CheckPoint`] of the latest tip, that connects with `prev_tip`. The latest blocks are
607645
/// fetched to construct checkpoint updates with the proper [`BlockHash`] in case of re-org.
608646
fn fetch_tip_and_latest_blocks(

crates/electrum/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,31 @@
1111
//! where the range of possibly used scripts is not known. In this case it is necessary to scan all
1212
//! keychain scripts until a number (the "stop gap") of unused scripts is discovered.
1313
//!
14+
//! # JWT Authentication
15+
//!
16+
//! If your Electrum server requires JWT authentication (e.g., behind an API gateway),
17+
//! you can use the [`AuthProvider`] to dynamically inject tokens:
18+
//!
19+
//! ```no_run
20+
//! # use bdk_electrum::{BdkElectrumClient, electrum_client};
21+
//! # use std::sync::Arc;
22+
//! let config = electrum_client::ConfigBuilder::new()
23+
//! .authorization_provider(Some(Arc::new(|| {
24+
//! Some("Bearer your-jwt-token".to_string())
25+
//! })))
26+
//! .build();
27+
//!
28+
//! let client = electrum_client::Client::from_config("tcp://server:50001", config)?;
29+
//! let bdk_client = BdkElectrumClient::new(client);
30+
//! # Ok::<(), electrum_client::Error>(())
31+
//! ```
32+
//!
1433
//! Refer to [`example_electrum`] for a complete example.
1534
//!
1635
//! [`example_electrum`]: https://github.com/bitcoindevkit/bdk/tree/master/examples/example_electrum
1736
//! [`SyncResponse`]: bdk_core::spk_client::SyncResponse
1837
//! [`FullScanResponse`]: bdk_core::spk_client::FullScanResponse
38+
//! [`AuthProvider`]: electrum_client::config::AuthProvider
1939
#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
2040
#![warn(missing_docs)]
2141

@@ -24,3 +44,6 @@ pub use bdk_electrum_client::*;
2444

2545
pub use bdk_core;
2646
pub use electrum_client;
47+
48+
// Re-export commonly used electrum_client config types for convenience
49+
pub use electrum_client::{Config, ConfigBuilder};

0 commit comments

Comments
 (0)