From d8605a4d516e800f680363ad295db70849f51704 Mon Sep 17 00:00:00 2001 From: Ignotus Peverell Date: Sat, 9 Feb 2019 11:03:15 -0800 Subject: [PATCH] Generate API secret in wallet --here mode (#2547) --- config/src/config.rs | 8 ++++---- src/bin/cmd/config.rs | 10 +++++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/config/src/config.rs b/config/src/config.rs index 3ec8e78f7c..252f43c1b5 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -46,7 +46,7 @@ const GRIN_HOME: &'static str = ".grin"; const GRIN_CHAIN_DIR: &'static str = "chain_data"; /// Wallet data directory pub const GRIN_WALLET_DIR: &'static str = "wallet_data"; -const API_SECRET_FILE_NAME: &'static str = ".api_secret"; +pub const API_SECRET_FILE_NAME: &'static str = ".api_secret"; fn get_grin_path(chain_type: &global::ChainTypes) -> Result { // Check if grin dir exists @@ -79,7 +79,7 @@ fn check_config_current_dir(path: &str) -> Option { } /// Create file with api secret -fn init_api_secret(api_secret_path: &PathBuf) -> Result<(), ConfigError> { +pub fn init_api_secret(api_secret_path: &PathBuf) -> Result<(), ConfigError> { let mut api_secret_file = File::create(api_secret_path)?; let api_secret: String = Alphanumeric .sample_iter(&mut thread_rng()) @@ -89,8 +89,8 @@ fn init_api_secret(api_secret_path: &PathBuf) -> Result<(), ConfigError> { Ok(()) } -/// // Check if file contains a secret and nothing else -fn check_api_secret(api_secret_path: &PathBuf) -> Result<(), ConfigError> { +/// Check if file contains a secret and nothing else +pub fn check_api_secret(api_secret_path: &PathBuf) -> Result<(), ConfigError> { let api_secret_file = File::open(api_secret_path)?; let buf_reader = BufReader::new(api_secret_file); let mut lines_iter = buf_reader.lines(); diff --git a/src/bin/cmd/config.rs b/src/bin/cmd/config.rs index 8405fb5102..61b4a0544b 100644 --- a/src/bin/cmd/config.rs +++ b/src/bin/cmd/config.rs @@ -13,7 +13,7 @@ // limitations under the License. /// Grin configuration file output command -use crate::config::{GlobalConfig, GlobalWalletConfig, GRIN_WALLET_DIR}; +use crate::config::{config, GlobalConfig, GlobalWalletConfig, GRIN_WALLET_DIR}; use crate::core::global; use std::env; @@ -79,4 +79,12 @@ pub fn config_command_wallet(chain_type: &global::ChainTypes, file_name: &str) { "File {} configured and created", config_file_name.to_str().unwrap(), ); + + let mut api_secret_path = current_dir.clone(); + api_secret_path.push(config::API_SECRET_FILE_NAME); + if !api_secret_path.exists() { + config::init_api_secret(&api_secret_path).unwrap(); + } else { + config::check_api_secret(&api_secret_path).unwrap(); + } }