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 committed by Sosthene
parent a071d404d8
commit 3898a835b0
3 changed files with 22 additions and 9 deletions

View File

@ -16,6 +16,7 @@ pub struct Config {
pub blindbit_url: String,
pub zmq_url: String,
pub data_dir: String,
pub cookie_path: Option<String>,
}
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)

View File

@ -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<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 {
Some(rpcwallet) => rpc_url.push_str(&rpcwallet),
None => (),
@ -101,11 +101,20 @@ fn rpc_connect(rpcwallet: Option<String>, 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<String>, rpc_url: String, network: Network) -> Result<Self> {
let mut rpc = rpc_connect(rpcwallet, network, rpc_url)?;
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, 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<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
Self: Sized;

View File

@ -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) => {