Added auto detection of wallet-dir in current directory, fix for loading config in current dir

This commit is contained in:
Anynomouss
2025-05-23 16:08:57 +02:00
parent da0050172b
commit d3625a6faf
+18 -11
View File
@@ -48,7 +48,14 @@ 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> {
// Check if grin dir exists // 1) A If not new wallet, check if walelt exist in working dir
let mut wallet_path = std::env::current_dir()?;
wallet_path.push(GRIN_WALLET_DIR);
if create_path == false & wallet_path.exists() {
wallet_path.pop();
return Ok(wallet_path);
};
// 1) B, chose working direcotry Check if grin dir exists
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(),
@@ -59,7 +66,6 @@ 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())?;
} }
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(),
@@ -215,22 +221,23 @@ pub fn initial_setup_wallet(
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 config exists in working dir, if so, use it as template for newly created config // 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) = if let Some(p) = check_config_current_dir(WALLET_CONFIG_FILE_NAME) {
let mut path = p.clone(); let mut path = p.clone();
path.pop(); path.pop();
let mut config = 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 // If loaded an run with 'init', use te config as template, update node and wallet dir
config.config_file_path = Some(config_path); if create_path == true {
config.update_paths(&wallet_path, &node_path); config.config_file_path = Some(config_path);
config.update_paths(&wallet_path, &node_path);
}
(path, config) (path, config)
} else { } else {
match config_path.clone().exists() { match config_path.clone().exists() {
// If config does not exist 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());
// Update paths relative to current dir
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 =
@@ -245,7 +252,7 @@ pub fn initial_setup_wallet(
} }
(wallet_path, default_config) (wallet_path, default_config)
} }
// If only the config exists, but no wallet data, just load the cofig and proceed // If only the config exists, but no wallet data, load the cofig and proceed
true => { true => {
// If both config and data_dir exist, throw an error // If both config and data_dir exist, throw an error
if data_dir.exists() { if data_dir.exists() {
@@ -256,8 +263,8 @@ pub fn initial_setup_wallet(
); );
return Err(ConfigError::SerializationError(msg)); return Err(ConfigError::SerializationError(msg));
} else { } else {
let existing_config = GlobalWalletConfig::new(config_path.to_str().unwrap())?; let config = GlobalWalletConfig::new(config_path.to_str().unwrap())?;
(wallet_path, existing_config) (wallet_path, config)
} }
} }
} }