use std::fmt;
use std::io;
use std::path::PathBuf;
use crate::core::global::ChainTypes;
use crate::util::logger::LoggingConfig;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct WalletConfig {
pub chain_type: Option<ChainTypes>,
pub api_listen_port: u16,
pub owner_api_listen_port: Option<u16>,
pub api_secret_path: Option<String>,
pub node_api_secret_path: Option<String>,
pub check_node_api_http_addr: String,
pub owner_api_include_foreign: Option<bool>,
pub data_file_dir: String,
pub no_commit_cache: Option<bool>,
pub tls_certificate_file: Option<String>,
pub tls_certificate_key: Option<String>,
pub dark_background_color_scheme: Option<bool>,
pub accept_fee_base: Option<u64>,
}
impl Default for WalletConfig {
fn default() -> WalletConfig {
WalletConfig {
chain_type: Some(ChainTypes::Mainnet),
api_listen_port: 3415,
owner_api_listen_port: Some(WalletConfig::default_owner_api_listen_port()),
api_secret_path: Some(".owner_api_secret".to_string()),
node_api_secret_path: Some(".foreign_api_secret".to_string()),
check_node_api_http_addr: "http://127.0.0.1:3413".to_string(),
owner_api_include_foreign: Some(false),
data_file_dir: ".".to_string(),
no_commit_cache: Some(false),
tls_certificate_file: None,
tls_certificate_key: None,
dark_background_color_scheme: Some(true),
accept_fee_base: None,
}
}
}
impl WalletConfig {
pub fn api_listen_addr(&self) -> String {
format!("127.0.0.1:{}", self.api_listen_port)
}
pub fn default_owner_api_listen_port() -> u16 {
3420
}
pub fn default_accept_fee_base() -> u64 {
500_000
}
pub fn owner_api_listen_port(&self) -> u16 {
self.owner_api_listen_port
.unwrap_or_else(WalletConfig::default_owner_api_listen_port)
}
pub fn owner_api_listen_addr(&self) -> String {
format!("127.0.0.1:{}", self.owner_api_listen_port())
}
pub fn accept_fee_base(&self) -> u64 {
self.accept_fee_base
.unwrap_or_else(|| WalletConfig::default_accept_fee_base())
}
}
#[derive(Debug)]
pub enum ConfigError {
ParseError(String, String),
FileIOError(String, String),
FileNotFoundError(String),
SerializationError(String),
PathNotFoundError(String),
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
ConfigError::ParseError(ref file_name, ref message) => write!(
f,
"Error parsing configuration file at {} - {}",
file_name, message
),
ConfigError::FileIOError(ref file_name, ref message) => {
write!(f, "{} {}", message, file_name)
}
ConfigError::FileNotFoundError(ref file_name) => {
write!(f, "Configuration file not found: {}", file_name)
}
ConfigError::SerializationError(ref message) => {
write!(f, "Error serializing configuration: {}", message)
}
ConfigError::PathNotFoundError(ref message) => write!(f, "Path not found: {}", message),
}
}
}
impl From<io::Error> for ConfigError {
fn from(error: io::Error) -> ConfigError {
ConfigError::FileIOError(
String::from(""),
format!("Error loading config file: {}", error),
)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TorConfig {
pub skip_send_attempt: Option<bool>,
pub use_tor_listener: bool,
pub socks_proxy_addr: String,
pub send_config_dir: String,
#[serde(default)]
pub bridge: TorBridgeConfig,
#[serde(default)]
pub proxy: TorProxyConfig,
}
impl Default for TorConfig {
fn default() -> TorConfig {
TorConfig {
skip_send_attempt: Some(false),
use_tor_listener: true,
socks_proxy_addr: "127.0.0.1:59050".to_owned(),
send_config_dir: ".".into(),
bridge: TorBridgeConfig::default(),
proxy: TorProxyConfig::default(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TorBridgeConfig {
pub bridge_line: Option<String>,
pub client_option: Option<String>,
}
impl Default for TorBridgeConfig {
fn default() -> TorBridgeConfig {
TorBridgeConfig {
bridge_line: None,
client_option: None,
}
}
}
impl fmt::Display for TorBridgeConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TorProxyConfig {
pub transport: Option<String>,
pub address: Option<String>,
pub username: Option<String>,
pub password: Option<String>,
pub allowed_port: Option<Vec<u16>>,
}
impl Default for TorProxyConfig {
fn default() -> TorProxyConfig {
TorProxyConfig {
transport: None,
address: None,
username: None,
password: None,
allowed_port: None,
}
}
}
impl fmt::Display for TorProxyConfig {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?}", self)
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GlobalWalletConfig {
pub config_file_path: Option<PathBuf>,
pub members: Option<GlobalWalletConfigMembers>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GlobalWalletConfigMembers {
#[serde(default)]
pub config_file_version: Option<u32>,
#[serde(default)]
pub wallet: WalletConfig,
pub tor: Option<TorConfig>,
pub logging: Option<LoggingConfig>,
}