diff --git a/Cargo.lock b/Cargo.lock index 103411e..9b2c951 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1531,7 +1531,6 @@ dependencies = [ "byteorder", "chrono", "data-encoding", - "dirs 2.0.2", "ed25519-dalek", "futures 0.3.31", "grin_api", diff --git a/config/src/config.rs b/config/src/config.rs index 6eb5b20..9855eb7 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -43,7 +43,8 @@ pub const API_SECRET_FILE_NAME: &str = ".foreign_api_secret"; /// Owner API secret pub const OWNER_API_SECRET_FILE_NAME: &str = ".owner_api_secret"; -fn get_wallet_path( +/// Function to get wallet dir and create dirs if not existing +pub fn get_wallet_path( chain_type: &global::ChainTypes, create_path: bool, ) -> Result { @@ -68,8 +69,8 @@ fn get_wallet_path( } } -// Smart function to find the most likely .api_secret location for the node -fn get_node_path( +/// Smart function to find the most likely .api_secret location for the node +pub fn get_node_path( data_path: Option, chain_type: &global::ChainTypes, ) -> Result { @@ -110,6 +111,7 @@ fn get_node_path( node_path } +/// Checks if config in current working dir fn check_config_current_dir(path: &str) -> Option { let p = env::current_dir(); let mut c = match p { @@ -176,6 +178,7 @@ fn check_api_secret_file( } /// Handles setup and detection of paths for wallet +// Use config file in a) current directory as template, or b) in top path, or c) .grin home pub fn initial_setup_wallet( chain_type: &global::ChainTypes, data_path: Option, @@ -186,47 +189,57 @@ pub fn initial_setup_wallet( fs::create_dir_all(p)?; } } - // Use config file in a) current directory, or b) in top path, or c) .grin home + + // Get wallet data_dir path, create it if it does not exist + let wallet_path = match data_path { + Some(p) => { + let mut abs_wallet_path = std::env::current_dir()?; + abs_wallet_path.push(p); + abs_wallet_path + } + None => get_wallet_path(chain_type, create_path)?, + }; + + // Get path to the node dir(s), first try top dir, if no node api_secret return home./grin + let node_path = get_node_path(Some(wallet_path.clone()), chain_type)?; + + // Get path to the newwly to be created config file + let mut config_path = wallet_path.clone(); + config_path.push(WALLET_CONFIG_FILE_NAME); + + // Check if config exists in working dir, if so, use it as template for newly created config let (path, config) = if let Some(p) = check_config_current_dir(WALLET_CONFIG_FILE_NAME) { let mut path = p.clone(); path.pop(); - (path, GlobalWalletConfig::new(p.to_str().unwrap())?) + let mut config = GlobalWalletConfig::new(p.to_str().unwrap())?; + // Use template config, update data_dir, network and api secrets + config.config_file_path = Some(config_path); + config.update_paths(&wallet_path, &node_path); + (path, config) } else { - let wallet_path = match data_path { - Some(p) => p, - None => get_wallet_path(chain_type, create_path)?, - }; - - // Get path to wallet and node dir(s) - let node_path = get_node_path(Some(wallet_path.clone()), chain_type)?; - - // Get path to default config file - let mut config_path = wallet_path.clone(); - config_path.push(WALLET_CONFIG_FILE_NAME); - // Return defaults config updated with node and wallet dir and chain dir match config_path.clone().exists() { false => { let mut default_config = GlobalWalletConfig::for_chain(chain_type); default_config.config_file_path = Some(config_path); - // Update paths relative to current dir, assumes node secret is in user home + // Update paths relative to current dir default_config.update_paths(&wallet_path, &node_path); // Write config file, otherwise defaults will be writen - default_config - .write_to_file( - &default_config - .config_file_path - .clone() - .unwrap() - .to_str() - .unwrap(), - false, - None, - None, - ) - .unwrap_or_else(|e| { - panic!("Error creating config file: {}", e); - }); + // default_config + // .write_to_file( + // &default_config + // .config_file_path + // .clone() + // .unwrap() + // .to_str() + // .unwrap(), + // false, + // None, + // None, + // ) + // .unwrap_or_else(|e| { + // panic!("Error creating config file: {}", e); + // }); (wallet_path, default_config) } diff --git a/controller/src/command.rs b/controller/src/command.rs index 0f3e1de..4fa781e 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -16,8 +16,8 @@ use crate::api::TLSConfig; use crate::apiwallet::{try_slatepack_sync_workflow, Owner}; -use crate::config::{TorConfig, WalletConfig}; -use crate::core::core; +use crate::config::{TorConfig, WalletConfig, WALLET_CONFIG_FILE_NAME}; +use crate::core::{core, global}; use crate::error::Error; use crate::impls::PathToSlatepack; use crate::impls::SlateGetter as _; @@ -82,13 +82,17 @@ where K: keychain::Keychain + 'static, { // Assume global chain type has already been initialized. + let chain_type = global::get_chain_type(); + let mut w_lock = owner_api.wallet_inst.lock(); let p = w_lock.lc_provider()?; - // Config creation was moved to main, only create new if config is ot in arguments - // let chain_type = global::get_chain_type(); - // args.config, config: &WalletConfig) { - // p.create_config(&chain_type, WALLET_CONFIG_FILE_NAME, None, None, None)?; - // } + p.create_config( + &chain_type, + WALLET_CONFIG_FILE_NAME, + Some(args.config), + None, + None, + )?; p.create_wallet( None, args.recovery_phrase, diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index d0cc606..759d34d 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -87,11 +87,12 @@ where None => None, }, }; - let wallet = match wallet_config { - Some(w) => w, + // Check if config was provided, if not load default and set update to "true" + let (wallet, update) = match wallet_config { + Some(w) => (w, false), None => match default_config.members.as_ref() { - Some(m) => m.clone().wallet, - None => WalletConfig::default(), + Some(m) => (m.clone().wallet, true), + None => (WalletConfig::default(), true), }, }; let tor = match tor_config { @@ -137,10 +138,15 @@ where return Ok(()); } - let mut abs_path = std::env::current_dir()?; - abs_path.push(self.data_dir.clone()); + let mut abs_path_node = get_node_path?; + abs_path_node.push(self.data_dir.clone()); + let mut absolute_path_wallet = std::env::current_dir()?; + absolute_path_wallet.push(self.data_dir.clone()); - default_config.update_paths(&abs_path); + // if no config provided, update defaults + if update == true { + default_config.update_paths(&abs_path_node, &absolute_path_wallet); + }; let res = default_config.write_to_file(config_file_name.to_str().unwrap(), false, None, None); if let Err(e) = res {