Skip to content

Commit

Permalink
Generate API secret in wallet --here mode (#2547)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignopeverell authored Feb 9, 2019
1 parent 49ca8eb commit d8605a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 4 additions & 4 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf, ConfigError> {
// Check if grin dir exists
Expand Down Expand Up @@ -79,7 +79,7 @@ fn check_config_current_dir(path: &str) -> Option<PathBuf> {
}

/// 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())
Expand All @@ -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();
Expand Down
10 changes: 9 additions & 1 deletion src/bin/cmd/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
}

0 comments on commit d8605a4

Please sign in to comment.