diff --git a/api/Cargo.toml b/api/Cargo.toml index 4492e30..bd6ee3c 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -13,6 +13,7 @@ readme = "README.md" [dependencies] axum = { workspace = true } +ini = { package = "rust-ini", version = "0.21.3" } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } stealth-bitcoincore = { path = "../bitcoincore" } diff --git a/api/src/main.rs b/api/src/main.rs index 2559956..b744f2f 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf}; use std::sync::Arc; use std::time::Duration; +use ini::Ini; use stealth_api::app_with_gateway; use stealth_bitcoincore::{read_cookie_file, BitcoinCoreRpc}; use stealth_engine::gateway::BlockchainGateway; @@ -165,40 +166,9 @@ fn cookie_candidates(bitcoin_dir: &Path, port: u16) -> Vec { } fn read_bitcoin_conf_credentials() -> Option<(String, String)> { - let conf_path = PathBuf::from("bitcoin.conf"); - let conf = std::fs::read_to_string(conf_path).ok()?; - - let mut user: Option = None; - let mut pass: Option = None; - - for raw_line in conf.lines() { - let line = raw_line.trim(); - if line.is_empty() - || line.starts_with('#') - || line.starts_with(';') - || line.starts_with('[') - { - continue; - } - - let Some((raw_key, raw_value)) = line.split_once('=') else { - continue; - }; - let key = raw_key.trim(); - let value = raw_value.trim(); - if value.is_empty() { - continue; - } - - match key { - "rpcuser" => user = Some(value.to_owned()), - "rpcpassword" => pass = Some(value.to_owned()), - _ => {} - } - } - - match (user, pass) { - (Some(user), Some(pass)) => Some((user, pass)), - _ => None, - } + let conf = Ini::load_from_file("bitcoin.conf").ok()?; + let section = conf.general_section(); + let user = section.get("rpcuser")?; + let pass = section.get("rpcpassword")?; + Some((user.to_owned(), pass.to_owned())) }