From 2bdf1256df51ed10c8e873393fd918598ef6fe14 Mon Sep 17 00:00:00 2001 From: yeastplume Date: Wed, 17 Jul 2024 11:37:34 +0000 Subject: [PATCH] Resolve socket addresses from config file --- src/config.rs | 22 +++++++++++----------- src/node.rs | 7 +++++-- src/wallet.rs | 5 ++++- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/config.rs b/src/config.rs index b5466cd..31a2ee5 100644 --- a/src/config.rs +++ b/src/config.rs @@ -30,11 +30,11 @@ pub struct ServerConfig { /// socket address the server listener should bind to pub addr: SocketAddr, /// foreign api address of the grin node - pub grin_node_url: SocketAddr, + pub grin_node_url: String, /// path to file containing api secret for the grin node pub grin_node_secret_path: Option, /// owner api address of the grin wallet - pub wallet_owner_url: SocketAddr, + pub wallet_owner_url: String, /// path to file containing secret for the grin wallet's owner api pub wallet_owner_secret_path: Option, /// public key of the previous mix/swap server (e.g. N_1 if this is N_2) @@ -180,9 +180,9 @@ struct RawConfig { nonce: String, interval_s: u32, addr: SocketAddr, - grin_node_url: SocketAddr, + grin_node_url: String, grin_node_secret_path: Option, - wallet_owner_url: SocketAddr, + wallet_owner_url: String, wallet_owner_secret_path: Option, #[serde(with = "grin_onion::crypto::dalek::option_dalek_pubkey_serde", default)] prev_server: Option, @@ -204,9 +204,9 @@ pub fn write_config( nonce: encrypted.nonce, interval_s: server_config.interval_s, addr: server_config.addr, - grin_node_url: server_config.grin_node_url, + grin_node_url: server_config.grin_node_url.clone(), grin_node_secret_path: server_config.grin_node_secret_path.clone(), - wallet_owner_url: server_config.wallet_owner_url, + wallet_owner_url: server_config.wallet_owner_url.clone(), wallet_owner_secret_path: server_config.wallet_owner_secret_path.clone(), prev_server: server_config.prev_server.clone(), next_server: server_config.next_server.clone(), @@ -268,16 +268,16 @@ pub fn wallet_owner_secret_path(chain_type: &ChainTypes) -> PathBuf { path } -pub fn grin_node_url(chain_type: &ChainTypes) -> SocketAddr { +pub fn grin_node_url(chain_type: &ChainTypes) -> String { if *chain_type == ChainTypes::Testnet { - "127.0.0.1:13413".parse().unwrap() + "127.0.0.1:13413".into() } else { - "127.0.0.1:3413".parse().unwrap() + "127.0.0.1:3413".into() } } -pub fn wallet_owner_url(_chain_type: &ChainTypes) -> SocketAddr { - "127.0.0.1:3420".parse().unwrap() +pub fn wallet_owner_url(_chain_type: &ChainTypes) -> String { + "127.0.0.1:3420".into() } #[cfg(test)] diff --git a/src/node.rs b/src/node.rs index 2bf55c5..677842b 100644 --- a/src/node.rs +++ b/src/node.rs @@ -1,4 +1,5 @@ use std::net::SocketAddr; +use std::net::ToSocketAddrs; use std::sync::Arc; use async_trait::async_trait; @@ -142,9 +143,11 @@ pub struct HttpGrinNode { const ENDPOINT: &str = "/v2/foreign"; impl HttpGrinNode { - pub fn new(node_url: &SocketAddr, node_api_secret: &Option) -> HttpGrinNode { + pub fn new(node_url: &str, node_api_secret: &Option) -> HttpGrinNode { + let mut addrs_iter = node_url.to_socket_addrs().unwrap(); + let node_url = addrs_iter.next().unwrap(); HttpGrinNode { - node_url: node_url.to_owned(), + node_url, node_api_secret: node_api_secret.to_owned(), } } diff --git a/src/wallet.rs b/src/wallet.rs index 3e63535..6d65781 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -1,4 +1,5 @@ use std::net::SocketAddr; +use std::net::ToSocketAddrs; use async_trait::async_trait; use grin_core::core::Output; @@ -54,11 +55,13 @@ pub struct ECDHPubkey { impl HttpWallet { /// Calls the 'open_wallet' using the RPC API. pub async fn async_open_wallet( - wallet_owner_url: &SocketAddr, + wallet_owner_url: &str, wallet_owner_secret: &Option, wallet_pass: &ZeroingString, ) -> Result { info!("Opening wallet at {}", wallet_owner_url); + let mut addrs_iter = wallet_owner_url.to_socket_addrs().unwrap(); + let wallet_owner_url = addrs_iter.next().unwrap(); let shared_key = HttpWallet::async_init_secure_api(&wallet_owner_url, &wallet_owner_secret).await?; let open_wallet_params = json!({