|
| 1 | +use ldk_node::bitcoin::Network; |
| 2 | +use ldk_node::lightning::ln::msgs::SocketAddress; |
| 3 | +use serde::{Deserialize, Serialize}; |
| 4 | +use std::net::SocketAddr; |
| 5 | +use std::path::Path; |
| 6 | +use std::str::FromStr; |
| 7 | +use std::{fs, io}; |
| 8 | + |
| 9 | +/// Configuration for LDK Server. |
| 10 | +#[derive(PartialEq, Eq, Debug)] |
| 11 | +pub struct Config { |
| 12 | + pub listening_addr: SocketAddress, |
| 13 | + pub network: Network, |
| 14 | + pub rest_service_addr: SocketAddr, |
| 15 | + pub storage_dir_path: String, |
| 16 | + pub bitcoind_rpc_addr: SocketAddr, |
| 17 | + pub bitcoind_rpc_user: String, |
| 18 | + pub bitcoind_rpc_password: String, |
| 19 | +} |
| 20 | + |
| 21 | +impl TryFrom<JsonConfig> for Config { |
| 22 | + type Error = io::Error; |
| 23 | + |
| 24 | + fn try_from(json_config: JsonConfig) -> io::Result<Self> { |
| 25 | + let listening_addr = |
| 26 | + SocketAddress::from_str(&json_config.listening_address).map_err(|e| { |
| 27 | + io::Error::new( |
| 28 | + io::ErrorKind::InvalidInput, |
| 29 | + format!("Invalid listening address configured: {}", e), |
| 30 | + ) |
| 31 | + })?; |
| 32 | + let rest_service_addr = |
| 33 | + SocketAddr::from_str(&json_config.rest_service_address).map_err(|e| { |
| 34 | + io::Error::new( |
| 35 | + io::ErrorKind::InvalidInput, |
| 36 | + format!("Invalid rest service address configured: {}", e), |
| 37 | + ) |
| 38 | + })?; |
| 39 | + |
| 40 | + let bitcoind_rpc_addr = |
| 41 | + SocketAddr::from_str(&json_config.bitcoind_rpc_address).map_err(|e| { |
| 42 | + io::Error::new( |
| 43 | + io::ErrorKind::InvalidInput, |
| 44 | + format!("Invalid bitcoind RPC address configured: {}", e), |
| 45 | + ) |
| 46 | + })?; |
| 47 | + |
| 48 | + Ok(Config { |
| 49 | + listening_addr, |
| 50 | + network: json_config.network, |
| 51 | + rest_service_addr, |
| 52 | + storage_dir_path: json_config.storage_dir_path, |
| 53 | + bitcoind_rpc_addr, |
| 54 | + bitcoind_rpc_user: json_config.bitcoind_rpc_user, |
| 55 | + bitcoind_rpc_password: json_config.bitcoind_rpc_password, |
| 56 | + }) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +/// Configuration loaded from a JSON file. |
| 61 | +#[derive(Deserialize, Serialize)] |
| 62 | +pub struct JsonConfig { |
| 63 | + listening_address: String, |
| 64 | + network: Network, |
| 65 | + rest_service_address: String, |
| 66 | + storage_dir_path: String, |
| 67 | + bitcoind_rpc_address: String, |
| 68 | + bitcoind_rpc_user: String, |
| 69 | + bitcoind_rpc_password: String, |
| 70 | +} |
| 71 | + |
| 72 | +/// Loads the configuration from a JSON file at the given path. |
| 73 | +pub fn load_config<P: AsRef<Path>>(config_path: P) -> io::Result<Config> { |
| 74 | + let file_contents = fs::read_to_string(config_path.as_ref()).map_err(|e| { |
| 75 | + io::Error::new( |
| 76 | + e.kind(), |
| 77 | + format!("Failed to read config file '{}': {}", config_path.as_ref().display(), e), |
| 78 | + ) |
| 79 | + })?; |
| 80 | + |
| 81 | + let json_string = remove_json_comments(file_contents.as_str()); |
| 82 | + let json_config: JsonConfig = serde_json::from_str(&json_string).map_err(|e| { |
| 83 | + io::Error::new( |
| 84 | + io::ErrorKind::InvalidData, |
| 85 | + format!("Config file contains invalid JSON format: {}", e), |
| 86 | + ) |
| 87 | + })?; |
| 88 | + Ok(Config::try_from(json_config)?) |
| 89 | +} |
| 90 | + |
| 91 | +fn remove_json_comments(s: &str) -> String { |
| 92 | + s.lines() |
| 93 | + .map(|line| if let Some(pos) = line.find("//") { &line[..pos] } else { line }) |
| 94 | + .collect::<Vec<&str>>() |
| 95 | + .join("\n") |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod tests { |
| 100 | + use super::*; |
| 101 | + use ldk_node::{bitcoin::Network, lightning::ln::msgs::SocketAddress}; |
| 102 | + use std::str::FromStr; |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_read_json_config_from_file() { |
| 106 | + let storage_path = std::env::temp_dir(); |
| 107 | + let config_file_name = "config.json"; |
| 108 | + |
| 109 | + let json_config = r#"{ |
| 110 | + "listening_address": "localhost:3001", |
| 111 | + "network": "regtest", |
| 112 | + "rest_service_address": "127.0.0.1:3002", |
| 113 | + "storage_dir_path": "/tmp", |
| 114 | + "bitcoind_rpc_address":"127.0.0.1:8332", // comment-1 |
| 115 | + "bitcoind_rpc_user": "bitcoind-testuser", |
| 116 | + "bitcoind_rpc_password": "bitcoind-testpassword", |
| 117 | + "unknown_key": "random-value" |
| 118 | + // comment-2 |
| 119 | + }"#; |
| 120 | + |
| 121 | + fs::write(storage_path.join(config_file_name), json_config).unwrap(); |
| 122 | + |
| 123 | + assert_eq!( |
| 124 | + load_config(storage_path.join(config_file_name)).unwrap(), |
| 125 | + Config { |
| 126 | + listening_addr: SocketAddress::from_str("localhost:3001").unwrap(), |
| 127 | + network: Network::Regtest, |
| 128 | + rest_service_addr: SocketAddr::from_str("127.0.0.1:3002").unwrap(), |
| 129 | + storage_dir_path: "/tmp".to_string(), |
| 130 | + bitcoind_rpc_addr: SocketAddr::from_str("127.0.0.1:8332").unwrap(), |
| 131 | + bitcoind_rpc_user: "bitcoind-testuser".to_string(), |
| 132 | + bitcoind_rpc_password: "bitcoind-testpassword".to_string(), |
| 133 | + } |
| 134 | + ) |
| 135 | + } |
| 136 | +} |
0 commit comments