From f690eb31fddfe1fcbbaa9188fe10264c335ecb38 Mon Sep 17 00:00:00 2001 From: dorianvp Date: Mon, 9 Dec 2024 14:41:46 -0300 Subject: [PATCH] fix(load_clientconfig): parse empty uri strings correctly --- zingolib/src/config.rs | 82 ++++++++++++++++++++++++++++++------------ 1 file changed, 59 insertions(+), 23 deletions(-) diff --git a/zingolib/src/config.rs b/zingolib/src/config.rs index 85cb7a420..fd3e87abb 100644 --- a/zingolib/src/config.rs +++ b/zingolib/src/config.rs @@ -71,21 +71,28 @@ pub fn load_clientconfig( monitor_mempool: bool, ) -> std::io::Result { use std::net::ToSocketAddrs; - match format!( - "{}:{}", - lightwallet_uri.host().unwrap(), - lightwallet_uri.port().unwrap() - ) - .to_socket_addrs() - { - Ok(_) => { - info!("Connected to {}", lightwallet_uri); - } - Err(e) => { - info!("Couldn't resolve server: {}", e); + + let host = lightwallet_uri.host(); + let port = lightwallet_uri.port(); + + if host.is_none() || port.is_none() { + info!("Using offline mode"); + } else { + match format!( + "{}:{}", + lightwallet_uri.host().unwrap(), + lightwallet_uri.port().unwrap() + ) + .to_socket_addrs() + { + Ok(_) => { + info!("Connected to {}", lightwallet_uri); + } + Err(e) => { + info!("Couldn't resolve server: {}", e); + } } } - info!("Connected to {}", lightwallet_uri); // Create a Light Client Config let config = ZingoConfig { @@ -105,18 +112,23 @@ pub fn load_clientconfig( /// TODO: Add Doc Comment Here! pub fn construct_lightwalletd_uri(server: Option) -> http::Uri { match server { - Some(s) => { - let mut s = if s.starts_with("http") { + Some(s) => match s.is_empty() { + true => { + return http::Uri::default(); + } + false => { + let mut s = if s.starts_with("http") { + s + } else { + "http://".to_string() + &s + }; + let uri: http::Uri = s.parse().unwrap(); + if uri.port().is_none() { + s += ":9067"; + } s - } else { - "http://".to_string() + &s - }; - let uri: http::Uri = s.parse().unwrap(); - if uri.port().is_none() { - s += ":9067"; } - s - } + }, None => DEFAULT_LIGHTWALLETD_SERVER.to_string(), } .parse() @@ -733,6 +745,30 @@ impl ActivationHeights { mod tests { + /// Validate that the load_clientconfig function creates a valid config from an empty uri + #[tokio::test] + async fn test_load_clientconfig() { + rustls::crypto::ring::default_provider() + .install_default() + .expect("Ring to work as a default"); + tracing_subscriber::fmt().init(); + + let valid_uri = crate::config::construct_lightwalletd_uri(Some("".to_string())); + + let temp_dir = tempfile::TempDir::new().unwrap(); + + let temp_path = temp_dir.path().to_path_buf(); + + let valid_config = crate::config::load_clientconfig( + valid_uri.clone(), + Some(temp_path), + crate::config::ChainType::Mainnet, + true, + ); + + assert_eq!(valid_config.is_ok(), true); + } + #[tokio::test] async fn test_load_clientconfig_serverless() { rustls::crypto::ring::default_provider()