// Copyright 2018 The Grin Developers // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Public types for config modules use std::fmt; use std::io; use std::path::PathBuf; use crate::core::global::ChainTypes; use crate::util::LoggingConfig; /// Command-line wallet configuration #[derive(Debug, Clone, Serialize, Deserialize, PartialEq)] pub struct WalletConfig { /// Chain parameters (default to Mainnet if none at the moment) pub chain_type: Option, /// The api interface/ip_address that this api server (i.e. this wallet) will run /// by default this is 127.0.0.1 (and will not accept connections from external clients) pub api_listen_interface: String, /// The port this wallet will run on pub api_listen_port: u16, /// The port this wallet's owner API will run on pub owner_api_listen_port: Option, /// Location of the secret for basic auth on the Owner API pub api_secret_path: Option, /// Location of the node api secret for basic auth on the Grin API pub node_api_secret_path: Option, /// The api address of a running server node against which transaction inputs /// will be checked during send pub check_node_api_http_addr: String, /// Whether to include foreign API endpoints on the Owner API pub owner_api_include_foreign: Option, /// The directory in which wallet files are stored pub data_file_dir: String, /// If Some(true), don't cache commits alongside output data /// speed improvement, but your commits are in the database pub no_commit_cache: Option, /// TLS certificate file pub tls_certificate_file: Option, /// TLS certificate private key file pub tls_certificate_key: Option, /// Whether to use the black background color scheme for command line /// if enabled, wallet command output color will be suitable for black background terminal pub dark_background_color_scheme: Option, /// The exploding lifetime (minutes) for keybase notification on coins received pub keybase_notify_ttl: Option, } impl Default for WalletConfig { fn default() -> WalletConfig { WalletConfig { chain_type: Some(ChainTypes::Mainnet), api_listen_interface: "127.0.0.1".to_string(), 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(".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), keybase_notify_ttl: Some(1440), } } } impl WalletConfig { /// API Listen address pub fn api_listen_addr(&self) -> String { format!("{}:{}", self.api_listen_interface, self.api_listen_port) } /// Default listener port pub fn default_owner_api_listen_port() -> u16 { 3420 } /// Use value from config file, defaulting to sensible value if missing. pub fn owner_api_listen_port(&self) -> u16 { self.owner_api_listen_port .unwrap_or(WalletConfig::default_owner_api_listen_port()) } /// Owner API listen address pub fn owner_api_listen_addr(&self) -> String { format!("127.0.0.1:{}", self.owner_api_listen_port()) } } /// Error type wrapping config errors. #[derive(Debug)] pub enum ConfigError { /// Error with parsing of config file ParseError(String, String), /// Error with fileIO while reading config file FileIOError(String, String), /// No file found FileNotFoundError(String), /// Error serializing config values SerializationError(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) } } } } impl From for ConfigError { fn from(error: io::Error) -> ConfigError { ConfigError::FileIOError( String::from(""), String::from(format!("Error loading config file: {}", error)), ) } } /// Wallet should be split into a separate configuration file #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct GlobalWalletConfig { /// Keep track of the file we've read pub config_file_path: Option, /// Wallet members pub members: Option, } /// Wallet internal members #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct GlobalWalletConfigMembers { /// Wallet configuration #[serde(default)] pub wallet: WalletConfig, /// Logging config pub logging: Option, }