Skip to content

Commit 8ac392d

Browse files
committed
Ensure we always startup with a rustls CryptoProvider
The `rustls` library recently introduced this weird behavior where they expect users to, apart from configuring the respective feature, also explictly call `CryptoProvider::install_default`. Otherwise they'd simply panic at runtime whenever the first network call requiring TLS would be made. While we already made a change upstream at `rust-electrum-client`, we also make sure here that we definitely, always, absolutley are sure that we have a `CryptoProvider` set on startup.
1 parent 241b12b commit 8ac392d

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ bdk_electrum = { version = "0.23.0", default-features = false, features = ["use-
6767
bdk_wallet = { version = "2.0.0", default-features = false, features = ["std", "keys-bip39"]}
6868

6969
reqwest = { version = "0.12", default-features = false, features = ["json", "rustls-tls"] }
70+
rustls = { version = "0.23", default-features = false }
7071
rusqlite = { version = "0.31.0", features = ["bundled"] }
7172
bitcoin = "0.32.4"
7273
bip39 = "2.0.0"

src/builder.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1051,6 +1051,8 @@ fn build_with_store_internal(
10511051
liquidity_source_config: Option<&LiquiditySourceConfig>, seed_bytes: [u8; 64],
10521052
logger: Arc<Logger>, kv_store: Arc<DynStore>,
10531053
) -> Result<Node, BuildError> {
1054+
optionally_install_rustls_cryptoprovider();
1055+
10541056
if let Err(err) = may_announce_channel(&config) {
10551057
if config.announcement_addresses.is_some() {
10561058
log_error!(logger, "Announcement addresses were set but some required configuration options for node announcement are missing: {}", err);
@@ -1663,6 +1665,28 @@ fn build_with_store_internal(
16631665
})
16641666
}
16651667

1668+
fn optionally_install_rustls_cryptoprovider() {
1669+
// Acquire a global Mutex, ensuring that only one process at a time install the provider. This
1670+
// is mostly required for running tests concurrently.
1671+
static INIT_CRYPTO: Once = Once::new();
1672+
1673+
INIT_CRYPTO.call_once(|| {
1674+
// Ensure we always install a `CryptoProvider` for `rustls` if it was somehow not previously installed by now.
1675+
if rustls::crypto::CryptoProvider::get_default().is_none() {
1676+
let res = rustls::crypto::aws_lc_rs::default_provider()
1677+
.install_default()
1678+
.or(rustls::crypto::ring::default_provider().install_default());
1679+
debug_assert!(res.is_ok(), "We need to install a CryptoProvider");
1680+
}
1681+
1682+
// Refuse to startup without TLS support. Better to catch it now than even later at runtime.
1683+
assert!(
1684+
rustls::crypto::CryptoProvider::get_default().is_some(),
1685+
"We need to have a CryptoProvider"
1686+
);
1687+
});
1688+
}
1689+
16661690
/// Sets up the node logger.
16671691
fn setup_logger(
16681692
log_writer_config: &Option<LogWriterConfig>, config: &Config,

0 commit comments

Comments
 (0)