From 0f9e89edd97b52a4c7ed01b638a982deab3fb155 Mon Sep 17 00:00:00 2001 From: Nicolas Cantu Date: Fri, 22 Aug 2025 14:06:12 +0200 Subject: [PATCH] Fix cookie path issue: add cookie_path config option and use it in rpc_connect --- src/config.rs | 2 ++ src/daemon.rs | 27 ++++++++++++++++++--------- src/main.rs | 2 ++ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index 496bfe3..31cfd5d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,7 @@ pub struct Config { pub blindbit_url: String, pub zmq_url: String, pub data_dir: String, + pub cookie_path: Option, } impl Config { @@ -74,6 +75,7 @@ impl Config { data_dir: file_content .remove("data_dir") .unwrap_or_else(|| ".4nk".to_string()), + cookie_path: file_content.remove("cookie_path").map(|s| s.to_owned()), }; Ok(config) diff --git a/src/daemon.rs b/src/daemon.rs index 63a5168..6164bba 100644 --- a/src/daemon.rs +++ b/src/daemon.rs @@ -90,7 +90,7 @@ fn read_cookie(path: &Path) -> Result<(String, String)> { Ok((parts[0].to_owned(), parts[1].to_owned())) } -fn rpc_connect(rpcwallet: Option, network: Network, mut rpc_url: String) -> Result { +fn rpc_connect(rpcwallet: Option, network: Network, mut rpc_url: String, cookie_path: Option) -> Result { match rpcwallet { Some(rpcwallet) => rpc_url.push_str(&rpcwallet), None => (), @@ -101,11 +101,20 @@ fn rpc_connect(rpcwallet: Option, network: Network, mut rpc_url: String) let builder = jsonrpc::simple_http::SimpleHttpTransport::builder() .url(&rpc_url)? .timeout(Duration::from_secs(30)); - let home = env::var("HOME")?; - let mut cookie_path = PathBuf::from_str(&home)?; - cookie_path.push(".bitcoin"); - cookie_path.push(network.to_core_arg()); - cookie_path.push(".cookie"); + + let cookie_path = match cookie_path { + Some(path) => path, + None => { + // Fallback to default path + let home = env::var("HOME")?; + let mut default_path = PathBuf::from_str(&home)?; + default_path.push(".bitcoin"); + default_path.push(network.to_core_arg()); + default_path.push(".cookie"); + default_path + } + }; + let daemon_auth = SensitiveAuth(Auth::CookieFile(cookie_path)); let builder = match daemon_auth.get_auth() { Auth::None => builder, @@ -126,8 +135,8 @@ pub struct Daemon { } impl RpcCall for Daemon { - fn connect(rpcwallet: Option, rpc_url: String, network: Network) -> Result { - let mut rpc = rpc_connect(rpcwallet, network, rpc_url)?; + fn connect(rpcwallet: Option, rpc_url: String, network: Network, cookie_path: Option) -> Result { + let mut rpc = rpc_connect(rpcwallet, network, rpc_url, cookie_path)?; loop { match rpc_poll(&mut rpc, false) { @@ -390,7 +399,7 @@ impl RpcCall for Daemon { } pub(crate) trait RpcCall: Send + Sync + std::fmt::Debug { - fn connect(rpcwallet: Option, rpc_url: String, network: Network) -> Result + fn connect(rpcwallet: Option, rpc_url: String, network: Network, cookie_path: Option) -> Result where Self: Sized; diff --git a/src/main.rs b/src/main.rs index 433b76e..4042c6d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -438,10 +438,12 @@ async fn main() -> Result<()> { const RETRY_DELAY_MS: u64 = 2000; // 2 seconds initial delay let daemon = loop { + let cookie_path = config.cookie_path.as_ref().map(|p| PathBuf::from(p)); match Daemon::connect( config.core_wallet.clone(), config.core_url.clone(), config.network, + cookie_path, ) { Ok(daemon) => break daemon, Err(e) => {