Skip to content

Commit

Permalink
wip: add optional crypto provider to client config
Browse files Browse the repository at this point in the history
  • Loading branch information
oleonardolima committed Jul 28, 2024
1 parent b415b5c commit 743b0ae
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
10 changes: 7 additions & 3 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,14 @@ impl ClientType {
config.validate_domain(),
socks5,
config.timeout(),
config.crypto_provider(),
)?,
None => RawClient::new_ssl(
url.as_str(),
config.validate_domain(),
config.timeout(),
config.crypto_provider(),
)?,
None => {
RawClient::new_ssl(url.as_str(), config.validate_domain(), config.timeout())?
}
};

Ok(ClientType::SSL(client))
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use std::time::Duration;

use rustls::crypto::CryptoProvider;

/// Configuration for an electrum client
///
/// Refer to [`Client::from_config`] and [`ClientType::from_config`].
Expand All @@ -12,6 +14,7 @@ pub struct Config {
socks5: Option<Socks5Config>,
/// timeout in seconds, default None (depends on TcpStream default)
timeout: Option<Duration>,
crypto_provider: Option<CryptoProvider>,
/// number of retry if any error, default 1
retry: u8,
/// when ssl, validate the domain, default true
Expand Down Expand Up @@ -135,6 +138,10 @@ impl Config {
pub fn builder() -> ConfigBuilder {
ConfigBuilder::new()
}

pub fn crypto_provider(&self) -> Option<CryptoProvider> {
self.crypto_provider
}
}

impl Default for Config {
Expand All @@ -144,6 +151,7 @@ impl Default for Config {
timeout: None,
retry: 1,
validate_domain: true,
crypto_provider: None,
}
}
}
30 changes: 26 additions & 4 deletions src/raw_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use bitcoin::{Script, Txid};
#[cfg(feature = "use-openssl")]
use openssl::ssl::{SslConnector, SslMethod, SslStream, SslVerifyMode};

use rustls::crypto::aws_lc_rs::default_provider;
use rustls::crypto::{self, CryptoProvider};
#[cfg(all(
any(
feature = "default",
Expand Down Expand Up @@ -368,6 +370,7 @@ impl RawClient<ElectrumSslStream> {
socket_addrs: A,
validate_domain: bool,
timeout: Option<Duration>,
crypto_provider: Option<CryptoProvider>,
) -> Result<Self, Error> {
debug!(
"new_ssl socket_addrs.domain():{:?} validate_domain:{} timeout:{:?}",
Expand All @@ -378,16 +381,28 @@ impl RawClient<ElectrumSslStream> {
if validate_domain {
socket_addrs.domain().ok_or(Error::MissingDomain)?;
}

let crypto_provider = match crypto_provider {
Some(provider) => provider,
None => {
#[cfg(feature = "use-rustls")]
crypto::aws_lc_rs::default_provider();

#[cfg(feature = "use-rustls-ring")]
crypto::ring::default_provider()
}
};

match timeout {
Some(timeout) => {
let stream = connect_with_total_timeout(socket_addrs.clone(), timeout)?;
stream.set_read_timeout(Some(timeout))?;
stream.set_write_timeout(Some(timeout))?;
Self::new_ssl_from_stream(socket_addrs, validate_domain, stream)
Self::new_ssl_from_stream(socket_addrs, validate_domain, stream, crypto_provider)
}
None => {
let stream = TcpStream::connect(socket_addrs.clone())?;
Self::new_ssl_from_stream(socket_addrs, validate_domain, stream)
Self::new_ssl_from_stream(socket_addrs, validate_domain, stream, crypto_provider)
}
}
}
Expand All @@ -397,10 +412,11 @@ impl RawClient<ElectrumSslStream> {
socket_addr: A,
validate_domain: bool,
tcp_stream: TcpStream,
crypto_provider: CryptoProvider,
) -> Result<Self, Error> {
use std::convert::TryFrom;

let builder = ClientConfig::builder();
let builder = ClientConfig::builder_with_provider(crypto_provider.into());

let config = if validate_domain {
socket_addr.domain().ok_or(Error::MissingDomain)?;
Expand Down Expand Up @@ -480,6 +496,7 @@ impl RawClient<ElectrumProxyStream> {
validate_domain: bool,
proxy: &crate::Socks5Config,
timeout: Option<Duration>,
crypto_provider: Option<CryptoProvider>,
) -> Result<RawClient<ElectrumSslStream>, Error> {
let target = target_addr.to_target_addr()?;

Expand All @@ -496,7 +513,12 @@ impl RawClient<ElectrumProxyStream> {
stream.get_mut().set_read_timeout(timeout)?;
stream.get_mut().set_write_timeout(timeout)?;

RawClient::new_ssl_from_stream(target, validate_domain, stream.into_inner())
RawClient::new_ssl_from_stream(
target,
validate_domain,
stream.into_inner(),
crypto_provider,
)
}
}

Expand Down

0 comments on commit 743b0ae

Please sign in to comment.