Skip to content

Commit b4ac740

Browse files
committed
Add option to include headers in Esplora config
This is useful if the esplora server has a form of authentication in front of it
1 parent 57e1fb1 commit b4ac740

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/builder.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ const LSPS_HARDENED_CHILD_INDEX: u32 = 577;
8787
enum ChainDataSourceConfig {
8888
Esplora {
8989
server_url: String,
90+
headers: HashMap<String, String>,
9091
sync_config: Option<EsploraSyncConfig>,
9192
},
9293
Electrum {
@@ -294,9 +295,27 @@ impl NodeBuilder {
294295
/// information.
295296
pub fn set_chain_source_esplora(
296297
&mut self, server_url: String, sync_config: Option<EsploraSyncConfig>,
298+
) -> &mut Self {
299+
self.chain_data_source_config = Some(ChainDataSourceConfig::Esplora {
300+
server_url,
301+
headers: Default::default(),
302+
sync_config,
303+
});
304+
self
305+
}
306+
307+
/// Configures the [`Node`] instance to source its chain data from the given Esplora server.
308+
/// The given `headers` will be included in all requests to the Esplora server, typically used for
309+
/// authentication purposes.
310+
///
311+
/// If no `sync_config` is given, default values are used. See [`EsploraSyncConfig`] for more
312+
/// information.
313+
pub fn set_chain_source_esplora_with_headers(
314+
&mut self, server_url: String, headers: HashMap<String, String>,
315+
sync_config: Option<EsploraSyncConfig>,
297316
) -> &mut Self {
298317
self.chain_data_source_config =
299-
Some(ChainDataSourceConfig::Esplora { server_url, sync_config });
318+
Some(ChainDataSourceConfig::Esplora { server_url, headers, sync_config });
300319
self
301320
}
302321

@@ -754,6 +773,19 @@ impl ArcedNodeBuilder {
754773
self.inner.write().unwrap().set_chain_source_esplora(server_url, sync_config);
755774
}
756775

776+
/// Configures the [`Node`] instance to source its chain data from the given Esplora server.
777+
/// The given `headers` will be included in all requests to the Esplora server, typically used for
778+
/// authentication purposes.
779+
///
780+
/// If no `sync_config` is given, default values are used. See [`EsploraSyncConfig`] for more
781+
/// information.
782+
pub fn set_chain_source_esplora_with_headers(
783+
&self, server_url: String, headers: HashMap<String, String>,
784+
sync_config: Option<EsploraSyncConfig>,
785+
) {
786+
self.inner.write().unwrap().set_chain_source_esplora_with_headers(server_url, headers, sync_config);
787+
}
788+
757789
/// Configures the [`Node`] instance to source its chain data from the given Electrum server.
758790
///
759791
/// If no `sync_config` is given, default values are used. See [`ElectrumSyncConfig`] for more
@@ -1117,10 +1149,11 @@ fn build_with_store_internal(
11171149
));
11181150

11191151
let chain_source = match chain_data_source_config {
1120-
Some(ChainDataSourceConfig::Esplora { server_url, sync_config }) => {
1152+
Some(ChainDataSourceConfig::Esplora { server_url, headers, sync_config }) => {
11211153
let sync_config = sync_config.unwrap_or(EsploraSyncConfig::default());
11221154
Arc::new(ChainSource::new_esplora(
11231155
server_url.clone(),
1156+
headers.clone(),
11241157
sync_config,
11251158
Arc::clone(&wallet),
11261159
Arc::clone(&fee_estimator),
@@ -1187,6 +1220,7 @@ fn build_with_store_internal(
11871220
let sync_config = EsploraSyncConfig::default();
11881221
Arc::new(ChainSource::new_esplora(
11891222
server_url.clone(),
1223+
HashMap::new(),
11901224
sync_config,
11911225
Arc::clone(&wallet),
11921226
Arc::clone(&fee_estimator),

src/chain/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,21 +233,31 @@ pub(crate) enum ChainSource {
233233

234234
impl ChainSource {
235235
pub(crate) fn new_esplora(
236-
server_url: String, sync_config: EsploraSyncConfig, onchain_wallet: Arc<Wallet>,
237-
fee_estimator: Arc<OnchainFeeEstimator>, tx_broadcaster: Arc<Broadcaster>,
238-
kv_store: Arc<DynStore>, config: Arc<Config>, logger: Arc<Logger>,
239-
node_metrics: Arc<RwLock<NodeMetrics>>,
236+
server_url: String, headers: HashMap<String, String>, sync_config: EsploraSyncConfig,
237+
onchain_wallet: Arc<Wallet>, fee_estimator: Arc<OnchainFeeEstimator>,
238+
tx_broadcaster: Arc<Broadcaster>, kv_store: Arc<DynStore>, config: Arc<Config>,
239+
logger: Arc<Logger>, node_metrics: Arc<RwLock<NodeMetrics>>,
240240
) -> Self {
241241
// FIXME / TODO: We introduced this to make `bdk_esplora` work separately without updating
242242
// `lightning-transaction-sync`. We should revert this as part of of the upgrade to LDK 0.2.
243243
let mut client_builder_0_11 = esplora_client_0_11::Builder::new(&server_url);
244244
client_builder_0_11 = client_builder_0_11.timeout(DEFAULT_ESPLORA_CLIENT_TIMEOUT_SECS);
245+
246+
for (header_name, header_value) in &headers {
247+
client_builder_0_11 = client_builder_0_11.header(header_name, header_value);
248+
}
249+
245250
let esplora_client_0_11 = client_builder_0_11.build_async().unwrap();
246251
let tx_sync =
247252
Arc::new(EsploraSyncClient::from_client(esplora_client_0_11, Arc::clone(&logger)));
248253

249254
let mut client_builder = esplora_client::Builder::new(&server_url);
250255
client_builder = client_builder.timeout(DEFAULT_ESPLORA_CLIENT_TIMEOUT_SECS);
256+
257+
for (header_name, header_value) in &headers {
258+
client_builder = client_builder.header(header_name, header_value);
259+
}
260+
251261
let esplora_client = client_builder.build_async().unwrap();
252262

253263
let onchain_wallet_sync_status = Mutex::new(WalletSyncStatus::Completed);

0 commit comments

Comments
 (0)