Fix cookie path issue: add cookie_path config option and use it in rpc_connect

This commit is contained in:
Nicolas Cantu 2025-08-22 14:06:12 +02:00
parent b46529682c
commit 0f9e89edd9
3 changed files with 22 additions and 9 deletions

View File

@ -16,6 +16,7 @@ pub struct Config {
pub blindbit_url: String, pub blindbit_url: String,
pub zmq_url: String, pub zmq_url: String,
pub data_dir: String, pub data_dir: String,
pub cookie_path: Option<String>,
} }
impl Config { impl Config {
@ -74,6 +75,7 @@ impl Config {
data_dir: file_content data_dir: file_content
.remove("data_dir") .remove("data_dir")
.unwrap_or_else(|| ".4nk".to_string()), .unwrap_or_else(|| ".4nk".to_string()),
cookie_path: file_content.remove("cookie_path").map(|s| s.to_owned()),
}; };
Ok(config) Ok(config)

View File

@ -90,7 +90,7 @@ fn read_cookie(path: &Path) -> Result<(String, String)> {
Ok((parts[0].to_owned(), parts[1].to_owned())) Ok((parts[0].to_owned(), parts[1].to_owned()))
} }
fn rpc_connect(rpcwallet: Option<String>, network: Network, mut rpc_url: String) -> Result<Client> { fn rpc_connect(rpcwallet: Option<String>, network: Network, mut rpc_url: String, cookie_path: Option<PathBuf>) -> Result<Client> {
match rpcwallet { match rpcwallet {
Some(rpcwallet) => rpc_url.push_str(&rpcwallet), Some(rpcwallet) => rpc_url.push_str(&rpcwallet),
None => (), None => (),
@ -101,11 +101,20 @@ fn rpc_connect(rpcwallet: Option<String>, network: Network, mut rpc_url: String)
let builder = jsonrpc::simple_http::SimpleHttpTransport::builder() let builder = jsonrpc::simple_http::SimpleHttpTransport::builder()
.url(&rpc_url)? .url(&rpc_url)?
.timeout(Duration::from_secs(30)); .timeout(Duration::from_secs(30));
let home = env::var("HOME")?;
let mut cookie_path = PathBuf::from_str(&home)?; let cookie_path = match cookie_path {
cookie_path.push(".bitcoin"); Some(path) => path,
cookie_path.push(network.to_core_arg()); None => {
cookie_path.push(".cookie"); // 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 daemon_auth = SensitiveAuth(Auth::CookieFile(cookie_path));
let builder = match daemon_auth.get_auth() { let builder = match daemon_auth.get_auth() {
Auth::None => builder, Auth::None => builder,
@ -126,8 +135,8 @@ pub struct Daemon {
} }
impl RpcCall for Daemon { impl RpcCall for Daemon {
fn connect(rpcwallet: Option<String>, rpc_url: String, network: Network) -> Result<Self> { fn connect(rpcwallet: Option<String>, rpc_url: String, network: Network, cookie_path: Option<PathBuf>) -> Result<Self> {
let mut rpc = rpc_connect(rpcwallet, network, rpc_url)?; let mut rpc = rpc_connect(rpcwallet, network, rpc_url, cookie_path)?;
loop { loop {
match rpc_poll(&mut rpc, false) { match rpc_poll(&mut rpc, false) {
@ -390,7 +399,7 @@ impl RpcCall for Daemon {
} }
pub(crate) trait RpcCall: Send + Sync + std::fmt::Debug { pub(crate) trait RpcCall: Send + Sync + std::fmt::Debug {
fn connect(rpcwallet: Option<String>, rpc_url: String, network: Network) -> Result<Self> fn connect(rpcwallet: Option<String>, rpc_url: String, network: Network, cookie_path: Option<PathBuf>) -> Result<Self>
where where
Self: Sized; Self: Sized;

View File

@ -438,10 +438,12 @@ async fn main() -> Result<()> {
const RETRY_DELAY_MS: u64 = 2000; // 2 seconds initial delay const RETRY_DELAY_MS: u64 = 2000; // 2 seconds initial delay
let daemon = loop { let daemon = loop {
let cookie_path = config.cookie_path.as_ref().map(|p| PathBuf::from(p));
match Daemon::connect( match Daemon::connect(
config.core_wallet.clone(), config.core_wallet.clone(),
config.core_url.clone(), config.core_url.clone(),
config.network, config.network,
cookie_path,
) { ) {
Ok(daemon) => break daemon, Ok(daemon) => break daemon,
Err(e) => { Err(e) => {