2019-08-07 12:17:11 -07:00
|
|
|
// Wallet settings that can be configured for long-term use
|
|
|
|
use serde_derive::{Deserialize, Serialize};
|
2020-01-31 18:27:37 -08:00
|
|
|
use std::{
|
|
|
|
fs::{create_dir_all, File},
|
|
|
|
io::{self, Write},
|
|
|
|
path::Path,
|
|
|
|
};
|
2020-03-09 12:29:31 -07:00
|
|
|
use url::Url;
|
2019-08-07 12:17:11 -07:00
|
|
|
|
|
|
|
lazy_static! {
|
|
|
|
pub static ref CONFIG_FILE: Option<String> = {
|
|
|
|
dirs::home_dir().map(|mut path| {
|
2019-10-04 15:13:21 -07:00
|
|
|
path.extend(&[".config", "solana", "cli", "config.yml"]);
|
2019-08-07 12:17:11 -07:00
|
|
|
path.to_str().unwrap().to_string()
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-03-09 12:29:31 -07:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
2019-08-07 12:17:11 -07:00
|
|
|
pub struct Config {
|
2020-03-09 12:14:17 -07:00
|
|
|
pub json_rpc_url: String,
|
2020-02-29 08:39:07 -08:00
|
|
|
pub websocket_url: String,
|
2019-11-23 08:55:43 -08:00
|
|
|
pub keypair_path: String,
|
2019-08-07 12:17:11 -07:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:29:31 -07:00
|
|
|
impl Default for Config {
|
|
|
|
fn default() -> Self {
|
|
|
|
let keypair_path = {
|
|
|
|
let mut keypair_path = dirs::home_dir().expect("home directory");
|
|
|
|
keypair_path.extend(&[".config", "solana", "id.json"]);
|
|
|
|
keypair_path.to_str().unwrap().to_string()
|
|
|
|
};
|
|
|
|
let json_rpc_url = "http://127.0.0.1:8899".to_string();
|
|
|
|
let websocket_url = Self::compute_websocket_url(&json_rpc_url);
|
|
|
|
|
2019-08-07 12:17:11 -07:00
|
|
|
Self {
|
2020-03-09 12:29:31 -07:00
|
|
|
json_rpc_url,
|
|
|
|
websocket_url,
|
|
|
|
keypair_path,
|
2019-08-07 12:17:11 -07:00
|
|
|
}
|
|
|
|
}
|
2020-03-09 12:29:31 -07:00
|
|
|
}
|
2019-08-07 12:17:11 -07:00
|
|
|
|
2020-03-09 12:29:31 -07:00
|
|
|
impl Config {
|
2019-08-07 12:17:11 -07:00
|
|
|
pub fn load(config_file: &str) -> Result<Self, io::Error> {
|
|
|
|
let file = File::open(config_file.to_string())?;
|
|
|
|
let config = serde_yaml::from_reader(file)
|
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?;
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn save(&self, config_file: &str) -> Result<(), io::Error> {
|
|
|
|
let serialized = serde_yaml::to_string(self)
|
|
|
|
.map_err(|err| io::Error::new(io::ErrorKind::Other, format!("{:?}", err)))?;
|
|
|
|
|
|
|
|
if let Some(outdir) = Path::new(&config_file).parent() {
|
|
|
|
create_dir_all(outdir)?;
|
|
|
|
}
|
|
|
|
let mut file = File::create(config_file)?;
|
|
|
|
file.write_all(&serialized.into_bytes())?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-03-09 12:29:31 -07:00
|
|
|
|
|
|
|
pub fn compute_websocket_url(json_rpc_url: &str) -> String {
|
|
|
|
let json_rpc_url: Option<Url> = json_rpc_url.parse().ok();
|
|
|
|
if json_rpc_url.is_none() {
|
|
|
|
return "".to_string();
|
|
|
|
}
|
|
|
|
let json_rpc_url = json_rpc_url.unwrap();
|
|
|
|
let is_secure = json_rpc_url.scheme().to_ascii_lowercase() == "https";
|
|
|
|
let mut ws_url = json_rpc_url.clone();
|
|
|
|
ws_url
|
|
|
|
.set_scheme(if is_secure { "wss" } else { "ws" })
|
|
|
|
.expect("unable to set scheme");
|
|
|
|
let ws_port = match json_rpc_url.port() {
|
|
|
|
Some(port) => port + 1,
|
|
|
|
None => {
|
|
|
|
if is_secure {
|
|
|
|
8901
|
|
|
|
} else {
|
|
|
|
8900
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
ws_url.set_port(Some(ws_port)).expect("unable to set port");
|
|
|
|
ws_url.to_string()
|
|
|
|
}
|
2019-08-07 12:17:11 -07:00
|
|
|
}
|