diff --git a/config/src/config.rs b/config/src/config.rs index 9666ccb..1ceaef6 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -177,8 +177,11 @@ fn check_api_secret_file( } } -/// Handles setup and detection of paths for wallet -/// Use config file in a) current dir as template, b) in top path, or c) .grin home +/// Initial wallet setup doess the following +/// Try in thiss order a) current dir as template, b) in top path, or c) .grin home +/// - load default config values +/// - update the wallet and node dir to the correct paths +/// - If the config exists, but the wallet data dir does not, load config and continue wallet generation pub fn initial_setup_wallet( chain_type: &global::ChainTypes, data_path: Option, @@ -203,10 +206,11 @@ pub fn initial_setup_wallet( // 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 + // Get path to the newly to be created config file let mut config_path = wallet_path.clone(); config_path.push(WALLET_CONFIG_FILE_NAME); - + let mut data_dir = wallet_path.clone(); + data_dir.push(GRIN_WALLET_DIR); // 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(); @@ -217,22 +221,40 @@ pub fn initial_setup_wallet( config.update_paths(&wallet_path, &node_path); (path, config) } else { - // Return defaults config updated with node and wallet dir and chain dir match config_path.clone().exists() { + // If config does not exist updated node and wallet dir false => { let mut default_config = GlobalWalletConfig::for_chain(chain_type); - default_config.config_file_path = Some(config_path); + default_config.config_file_path = Some(config_path.clone()); // Update paths relative to current dir default_config.update_paths(&wallet_path, &node_path); + // Write config file + let res = + default_config.write_to_file(config_path.to_str().unwrap(), false, None, None); + if let Err(e) = res { + let msg = format!( + "Error creating config file as ({}): {}", + config_path.to_str().unwrap(), + e + ); + return Err(ConfigError::SerializationError(msg)); + } (wallet_path, default_config) } + // If only the config exists, but no wallet data, just load the cofig and proceed true => { - let mut path = wallet_path.clone(); - path.pop(); - ( - path, - GlobalWalletConfig::new(config_path.to_str().unwrap())?, - ) + // If both config and data_dir exist, throw an error + if data_dir.exists() { + let msg = format!( + "{} already exists in the target directory ({}). Please remove it first", + config_path.to_str().unwrap(), + data_dir.to_str().unwrap(), + ); + return Err(ConfigError::SerializationError(msg)); + } else { + let existing_config = GlobalWalletConfig::new(config_path.to_str().unwrap())?; + (wallet_path, existing_config) + } } } }; @@ -326,20 +348,21 @@ impl GlobalWalletConfig { /// Update paths pub fn update_paths(&mut self, wallet_home: &PathBuf, node_home: &PathBuf) { - let mut wallet_path = wallet_home.clone(); - wallet_path.push(GRIN_WALLET_DIR); - self.members.as_mut().unwrap().wallet.data_file_dir = - wallet_path.to_str().unwrap().to_owned(); - let mut secret_path = wallet_home.clone(); - secret_path.push(OWNER_API_SECRET_FILE_NAME); - self.members.as_mut().unwrap().wallet.api_secret_path = - Some(secret_path.to_str().unwrap().to_owned()); + let mut data_file_dir = wallet_home.clone(); let mut node_secret_path = node_home.clone(); + let mut secret_path = wallet_home.clone(); + let mut log_path = wallet_home.clone(); + let tor_path = wallet_home.clone(); node_secret_path.push(API_SECRET_FILE_NAME); + data_file_dir.push(GRIN_WALLET_DIR); + secret_path.push(OWNER_API_SECRET_FILE_NAME); + log_path.push(WALLET_LOG_FILE_NAME); + self.members.as_mut().unwrap().wallet.data_file_dir = + data_file_dir.to_str().unwrap().to_owned(); self.members.as_mut().unwrap().wallet.node_api_secret_path = Some(node_secret_path.to_str().unwrap().to_owned()); - let mut log_path = wallet_home.clone(); - log_path.push(WALLET_LOG_FILE_NAME); + self.members.as_mut().unwrap().wallet.api_secret_path = + Some(secret_path.to_str().unwrap().to_owned()); self.members .as_mut() .unwrap() @@ -347,7 +370,6 @@ impl GlobalWalletConfig { .as_mut() .unwrap() .log_file_path = log_path.to_str().unwrap().to_owned(); - let tor_path = wallet_home.clone(); self.members .as_mut() .unwrap() diff --git a/controller/src/command.rs b/controller/src/command.rs index 1d1c082..1e883dd 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -85,6 +85,7 @@ where let chain_type = global::get_chain_type(); let mut w_lock = owner_api.wallet_inst.lock(); let p = w_lock.lc_provider()?; + println!("Checkpoint"); p.create_config( &chain_type, WALLET_CONFIG_FILE_NAME, diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index 45435da..a50b470 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -133,7 +133,7 @@ where return Err(Error::Lifecycle(msg)); } - // just leave as is if file exists but there's no data dir + // If config exists but the datadir not yet exists, create data dir return ok if config_file_name.exists() { return Ok(()); } diff --git a/src/bin/grin-wallet.rs b/src/bin/grin-wallet.rs index 69e8923..75b9d27 100644 --- a/src/bin/grin-wallet.rs +++ b/src/bin/grin-wallet.rs @@ -82,12 +82,12 @@ fn real_main() -> i32 { let mut current_dir = None; let mut create_path = false; - if args.is_present("top_level_dir") { let res = args.value_of("top_level_dir"); match res { Some(d) => { - current_dir = Some(PathBuf::from(d.replace("/", "\\"))); + let d = d.to_owned().replace("/", "\\"); + current_dir = Some(PathBuf::from(d)); } None => { warn!("Argument --top_level_dir needs a value. Defaulting to current directory") @@ -151,8 +151,8 @@ fn real_main() -> i32 { .unwrap() .clone(), ); - global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base()); + global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base()); let wallet_config = config.clone().members.unwrap().wallet; let node_client = HTTPNodeClient::new(&wallet_config.check_node_api_http_addr, None).unwrap(); cmd::wallet_command(&args, config, node_client) diff --git a/src/cmd/wallet_args.rs b/src/cmd/wallet_args.rs index 866db6c..205cc0e 100644 --- a/src/cmd/wallet_args.rs +++ b/src/cmd/wallet_args.rs @@ -24,7 +24,7 @@ use grin_core as core; use grin_core::core::amount_to_hr_string; use grin_keychain as keychain; use grin_wallet_api::Owner; -use grin_wallet_config::{config_file_exists, TorConfig, WalletConfig}; +use grin_wallet_config::{TorConfig, WalletConfig}; use grin_wallet_controller::{command, Error}; use grin_wallet_impls::{DefaultLCProvider, DefaultWalletImpl}; use grin_wallet_libwallet::{self, Slate, SlatepackAddress, SlatepackArmor}; @@ -332,7 +332,7 @@ pub fn parse_init_args( config: &WalletConfig, g_args: &command::GlobalArgs, args: &ArgMatches, - test_mode: bool, + _test_mode: bool, ) -> Result where DefaultWalletImpl<'static, C>: WalletInst<'static, L, C, K>, @@ -340,10 +340,6 @@ where C: NodeClient + 'static, K: keychain::Keychain + 'static, { - if config_file_exists(&config.data_file_dir) && !test_mode { - return Err(ParseError::WalletExists(config.data_file_dir.clone())); - } - let list_length = match args.is_present("short_wordlist") { false => 32, true => 16, @@ -1004,7 +1000,7 @@ where node_client.set_node_api_secret(global_wallet_args.node_api_secret.clone()); // legacy hack to avoid the need for changes in existing grin-wallet.toml files - // remove `wallet_data` from end of path as + // remove `wallet_data` from end of path // new lifecycle provider assumes grin_wallet.toml is in root of data directory let mut top_level_wallet_dir = PathBuf::from(wallet_config.clone().data_file_dir); if top_level_wallet_dir.ends_with(GRIN_WALLET_DIR) {