use std::error::Error;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct Config {
pub bitcoin_network: String,
pub bitcoin_rpc_url: String,
pub bitcoin_rpc_user: Option<String>,
pub bitcoin_rpc_pass: Option<String>,
pub bitcoin_data_dir: Option<String>,
pub wallet_path: Option<String>,
pub lightning_implementation: Option<String>,
pub lightning_node_pubkey: Option<String>,
pub lightning_listen_addr: Option<String>,
pub lightning_data_dir: Option<String>,
pub liquid_rpc_url: Option<String>,
pub liquid_rpc_user: Option<String>,
pub liquid_rpc_pass: Option<String>,
pub liquid_network: Option<String>,
pub liquid_data_dir: Option<String>,
pub web5_config: crate::web5::Web5Config,
pub features: HashMap<String, bool>,
}
impl Default for Config {
fn default() -> Self {
let mut features = HashMap::new();
features.insert("taproot".to_string(), true);
features.insert("lightning".to_string(), false);
features.insert("dlc".to_string(), false);
features.insert("liquid".to_string(), false);
features.insert("web5".to_string(), true);
Self {
bitcoin_network: "testnet".to_string(),
bitcoin_rpc_url: "http://localhost:18332".to_string(),
bitcoin_rpc_user: None,
bitcoin_rpc_pass: None,
bitcoin_data_dir: None,
wallet_path: None,
lightning_implementation: Some("ldk".to_string()),
lightning_node_pubkey: None,
lightning_listen_addr: None,
lightning_data_dir: None,
liquid_rpc_url: Some("http://localhost:7041".to_string()),
liquid_rpc_user: None,
liquid_rpc_pass: None,
liquid_network: Some("liquidtestnet".to_string()),
liquid_data_dir: None,
web5_config: crate::web5::Web5Config::default(),
features,
}
}
}
impl Config {
pub fn from_env() -> Self {
let mut config = Config::default();
if let Ok(network) = std::env::var("BITCOIN_NETWORK") {
config.bitcoin_network = network;
}
if let Ok(rpc_url) = std::env::var("BITCOIN_RPC_URL") {
config.bitcoin_rpc_url = rpc_url;
}
if let Ok(rpc_user) = std::env::var("BITCOIN_RPC_USER") {
config.bitcoin_rpc_user = Some(rpc_user);
}
if let Ok(rpc_pass) = std::env::var("BITCOIN_RPC_PASS") {
config.bitcoin_rpc_pass = Some(rpc_pass);
}
if let Ok(data_dir) = std::env::var("BITCOIN_DATA_DIR") {
config.bitcoin_data_dir = Some(data_dir);
}
if let Ok(wallet_path) = std::env::var("WALLET_PATH") {
config.wallet_path = Some(wallet_path);
}
if let Ok(lightning_impl) = std::env::var("LIGHTNING_IMPLEMENTATION") {
config.lightning_implementation = Some(lightning_impl);
}
if let Ok(lightning_pubkey) = std::env::var("LIGHTNING_NODE_PUBKEY") {
config.lightning_node_pubkey = Some(lightning_pubkey);
}
if let Ok(lightning_addr) = std::env::var("LIGHTNING_LISTEN_ADDR") {
config.lightning_listen_addr = Some(lightning_addr);
}
if let Ok(lightning_dir) = std::env::var("LIGHTNING_DATA_DIR") {
config.lightning_data_dir = Some(lightning_dir);
}
if let Ok(liquid_rpc_url) = std::env::var("LIQUID_RPC_URL") {
config.liquid_rpc_url = Some(liquid_rpc_url);
}
if let Ok(liquid_rpc_user) = std::env::var("LIQUID_RPC_USER") {
config.liquid_rpc_user = Some(liquid_rpc_user);
}
if let Ok(liquid_rpc_pass) = std::env::var("LIQUID_RPC_PASS") {
config.liquid_rpc_pass = Some(liquid_rpc_pass);
}
if let Ok(liquid_network) = std::env::var("LIQUID_NETWORK") {
config.liquid_network = Some(liquid_network);
}
if let Ok(liquid_data_dir) = std::env::var("LIQUID_DATA_DIR") {
config.liquid_data_dir = Some(liquid_data_dir);
}
if let Ok(did_method) = std::env::var("WEB5_DID_METHOD") {
config.web5_config.did_method = did_method;
}
if let Ok(dwn_endpoint) = std::env::var("WEB5_DWN_ENDPOINT") {
config.web5_config.dwn_url = Some(dwn_endpoint);
}
if let Ok(features_str) = std::env::var("ENABLED_FEATURES") {
for feature in features_str.split(',') {
let feature = feature.trim();
if !feature.is_empty() {
config.features.insert(feature.to_string(), true);
}
}
}
config
}
pub fn is_feature_enabled(&self, feature: &str) -> bool {
self.features.get(feature).copied().unwrap_or(false)
}
pub fn set_feature(&mut self, feature: &str, enabled: bool) {
self.features.insert(feature.to_string(), enabled);
}
pub fn get_bitcoin_implementation_type(&self) -> crate::bitcoin::interface::BitcoinImplementationType {
crate::bitcoin::interface::BitcoinImplementationType::Rust
}
pub fn is_liquid_enabled(&self) -> bool {
self.is_feature_enabled("liquid")
}
pub fn is_web5_enabled(&self) -> bool {
self.is_feature_enabled("web5")
}
}
pub fn test_config() -> Config {
let mut config = Config::default();
config.bitcoin_network = "regtest".to_string();
config.bitcoin_rpc_url = "http://localhost:18443".to_string();
config.liquid_network = Some("liquidregtest".to_string());
config.liquid_rpc_url = Some("http://localhost:18884".to_string());
config
}