Wallet init -t and -h working, as well as with info, always detect grin-wallet.toml in working dir, same as old wallet

This commit is contained in:
Anynomous
2025-09-18 11:42:04 +02:00
parent 2bea5b6826
commit 6d99a431dd
+18 -25
View File
@@ -21,7 +21,6 @@ use crate::types::{
}; };
use crate::types::{TorConfig, WalletConfig}; use crate::types::{TorConfig, WalletConfig};
use crate::util::logger::LoggingConfig; use crate::util::logger::LoggingConfig;
use log::warn;
use rand::distributions::{Alphanumeric, Distribution}; use rand::distributions::{Alphanumeric, Distribution};
use rand::thread_rng; use rand::thread_rng;
use std::env; use std::env;
@@ -50,15 +49,15 @@ pub fn get_wallet_path(
chain_type: &global::ChainTypes, chain_type: &global::ChainTypes,
create_path: bool, create_path: bool,
) -> Result<PathBuf, ConfigError> { ) -> Result<PathBuf, ConfigError> {
// A - If not a new wallet, check if wallet dir exists // A - Detect grin-wallet.toml in working dir
let mut config_path = std::env::current_dir()?; let mut config_path = std::env::current_dir()?;
config_path.push(WALLET_CONFIG_FILE_NAME); config_path.push(WALLET_CONFIG_FILE_NAME);
if create_path == false & config_path.exists() { if create_path == false && config_path.exists() {
config_path.pop(); config_path.pop();
warn!("Detected 'wallet.toml' in working dir - opening associated wallet"); println!("Detected 'wallet.toml' in working dir - opening associated wallet");
return Ok(config_path); return Ok(config_path);
}; };
// B - chose working directory Check if grin dir exists // B - Select home directory
let mut wallet_path = match dirs::home_dir() { let mut wallet_path = match dirs::home_dir() {
Some(p) => p, Some(p) => p,
None => PathBuf::new(), None => PathBuf::new(),
@@ -69,6 +68,7 @@ pub fn get_wallet_path(
if !wallet_path.exists() && create_path { if !wallet_path.exists() && create_path {
fs::create_dir_all(wallet_path.clone())?; fs::create_dir_all(wallet_path.clone())?;
} }
// Throw an error if the path still does not exist
if !wallet_path.exists() { if !wallet_path.exists() {
Err(ConfigError::PathNotFoundError(String::from( Err(ConfigError::PathNotFoundError(String::from(
wallet_path.to_str().unwrap(), wallet_path.to_str().unwrap(),
@@ -197,13 +197,14 @@ pub fn initial_setup_wallet(
mut data_path: Option<PathBuf>, mut data_path: Option<PathBuf>,
create_path: bool, create_path: bool,
) -> Result<GlobalWalletConfig, ConfigError> { ) -> Result<GlobalWalletConfig, ConfigError> {
// Fixing the unput path when run with -here or -t (top-dir)
// - Fix top-dir path to compensate for bug on Linux to handle "\" // - Fix top-dir path to compensate for bug on Linux to handle "\"
// - Convert top-dir path to be always absolute for config generation // - Convert top-dir path to be always absolute for config generation
// - Fix for Windows 10/11 to strip the '\\?\' prefix added to the path
if let Some(p) = &data_path { if let Some(p) = &data_path {
if let Some(p_str) = p.to_str() { if let Some(p_str) = p.to_str() {
let fixed_str = p_str.replace("\\", "/"); let fixed_str = p_str.replace("\\", "/");
let fixed_path = PathBuf::from(fixed_str); let fixed_path = PathBuf::from(fixed_str);
// If a new wallet, check if dir exist, if needed create it
if create_path { if create_path {
fs::create_dir_all(&fixed_path)?; fs::create_dir_all(&fixed_path)?;
} }
@@ -212,7 +213,6 @@ pub fn initial_setup_wallet(
} else { } else {
env::current_dir()?.join(&fixed_path).canonicalize()? env::current_dir()?.join(&fixed_path).canonicalize()?
}; };
// Fix for Windows to strip the '\\?\'prefix added to the path
let absolute_path = let absolute_path =
std::path::PathBuf::from(absolute_path.to_str().unwrap().replace(r"\\?\", "")); std::path::PathBuf::from(absolute_path.to_str().unwrap().replace(r"\\?\", ""));
data_path = Some(absolute_path); // Store the updated path data_path = Some(absolute_path); // Store the updated path
@@ -224,36 +224,27 @@ pub fn initial_setup_wallet(
Some(p) => p, Some(p) => p,
None => get_wallet_path(chain_type, create_path)?, None => get_wallet_path(chain_type, create_path)?,
}; };
println!("wallet path {}", wallet_path.display()); println!("wallet path: {}", wallet_path.display());
// Get path to the node directory, // Get path to the node directory,
let node_path = get_node_path(Some(wallet_path.clone()), chain_type)?; let node_path = get_node_path(Some(wallet_path.clone()), chain_type)?;
// Get path to the newly to be created config file // Get config path and data path
let mut config_path = wallet_path.clone(); let mut config_path = wallet_path.clone();
config_path.push(WALLET_CONFIG_FILE_NAME); config_path.push(WALLET_CONFIG_FILE_NAME);
let mut data_dir = wallet_path.clone(); let mut data_dir = wallet_path.clone();
data_dir.push(GRIN_WALLET_DIR); data_dir.push(GRIN_WALLET_DIR);
// Check if a config exists in theworking dir, if so load it // Check if a config exists in theworking dir, if so load it
let (path, config) = if let Some(p) = check_config_current_dir(WALLET_CONFIG_FILE_NAME) { let (path, config) = match config_path.clone().exists() {
let mut path = p.clone();
path.pop();
let mut config = GlobalWalletConfig::new(p.to_str().unwrap())?;
// If loaded and run with 'init', use te config as template, update node and wallet dir
if create_path == true {
config.config_file_path = Some(config_path);
config.update_paths(&wallet_path, &node_path);
}
(path, config)
} else {
match config_path.clone().exists() {
// If the config does not exist, load default and updated node and wallet dir // If the config does not exist, load default and updated node and wallet dir
false => { false => {
let mut default_config = GlobalWalletConfig::for_chain(chain_type); let mut default_config = GlobalWalletConfig::for_chain(chain_type);
default_config.config_file_path = Some(config_path.clone()); default_config.config_file_path = Some(config_path.clone());
default_config.update_paths(&wallet_path, &node_path); default_config.update_paths(&wallet_path, &node_path);
// Write config file // Write config file
let res = let res =
default_config.write_to_file(config_path.to_str().unwrap(), false, None, None); default_config.write_to_file(config_path.to_str().unwrap(), false, None, None);
if let Err(e) = res { if let Err(e) = res {
let msg = format!( let msg = format!(
"Error creating config file as ({}): {}", "Error creating config file as ({}): {}",
@@ -262,12 +253,14 @@ pub fn initial_setup_wallet(
); );
return Err(ConfigError::SerializationError(msg)); return Err(ConfigError::SerializationError(msg));
} }
(wallet_path, default_config) (wallet_path, default_config)
} }
// If only the config exists, but no wallet data, load the cofig and proceed
// Return config if not run with init
true => { true => {
// If both config and data_dir exist, throw an error // If run with init and seed does not yet exists, continue, else throw error
if data_dir.exists() { if data_dir.exists() && create_path == true {
let msg = format!( let msg = format!(
"{} already exists in the target directory ({}). Please remove it first", "{} already exists in the target directory ({}). Please remove it first",
config_path.to_str().unwrap(), config_path.to_str().unwrap(),
@@ -279,8 +272,8 @@ pub fn initial_setup_wallet(
(wallet_path, config) (wallet_path, config)
} }
} }
}
}; };
// Check API secrets, if ok, return config // Check API secrets, if ok, return config
check_api_secret_file(chain_type, Some(path.clone()), OWNER_API_SECRET_FILE_NAME)?; check_api_secret_file(chain_type, Some(path.clone()), OWNER_API_SECRET_FILE_NAME)?;
check_api_secret_file(chain_type, Some(path), API_SECRET_FILE_NAME)?; check_api_secret_file(chain_type, Some(path), API_SECRET_FILE_NAME)?;