From 82e61dd28a8a4946faf2a3262fa639832d4591ca Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Fri, 24 Jan 2025 13:45:55 +0100 Subject: [PATCH 01/10] Added an extra warning message when failing to reach the node, work in progress on changing default dir for node api ssecret 'foreign_api_secret' --- config/src/config.rs | 25 +++++++++++++++++++++++-- impls/src/node_clients/http.rs | 1 + 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/config/src/config.rs b/config/src/config.rs index d11298078..9db501e52 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -83,6 +83,22 @@ fn check_config_current_dir(path: &str) -> Option { None } +/// Checks if the node api secret in wallet dir, else return default path +fn check_node_api_exists(path: &str) -> Option { + let p = env::current_dir(); + let mut c = match p { + Ok(c) => c, + Err(_) => { + return None; + } + }; + c.push(path); + if c.exists() { + return Some(c); + } + None +} + /// Whether a config file exists at the given directory pub fn config_file_exists(path: &str) -> bool { let mut path = PathBuf::from(path); @@ -279,8 +295,13 @@ impl GlobalWalletConfig { Some(secret_path.to_str().unwrap().to_owned()); let mut node_secret_path = wallet_home.clone(); node_secret_path.push(API_SECRET_FILE_NAME); - self.members.as_mut().unwrap().wallet.node_api_secret_path = - Some(node_secret_path.to_str().unwrap().to_owned()); + // Set node API secret path to the wallet dir if it exists, else keep default home dir + // if node_secret_path.exists(){ + // self.members.as_mut().unwrap().wallet.node_api_secret_path = + // Some(node_secret_path.to_str().unwrap().to_owned()); + // println!("It node path does actually exists") + // } + let mut log_path = wallet_home.clone(); log_path.push(WALLET_LOG_FILE_NAME); self.members diff --git a/impls/src/node_clients/http.rs b/impls/src/node_clients/http.rs index 83e04882b..cd293f95e 100644 --- a/impls/src/node_clients/http.rs +++ b/impls/src/node_clients/http.rs @@ -129,6 +129,7 @@ impl NodeClient for HTTPNodeClient { }); } else { error!("Unable to contact Node to get version info: {}", e); + warn!("Warning: You might need to correct 'node_api_secret_path' in your 'grin-wallet.toml' file"); return None; } } From 4fb0b7b387f5c29316e9c90b985e6c360cccbcdf Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Thu, 30 Jan 2025 16:25:41 +0100 Subject: [PATCH 02/10] In the middle of debugging hell, default overwrite any updates to the config made earlier, its never written to a file when running init --- Cargo.lock | 3 +- config/src/config.rs | 85 ++++++++++++++++------------------ impls/Cargo.toml | 1 + impls/src/lifecycle/default.rs | 10 ++-- src/bin/grin-wallet.rs | 10 ++-- 5 files changed, 56 insertions(+), 53 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bee516a4d..103411eee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] name = "addr2line" @@ -1531,6 +1531,7 @@ 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 9db501e52..327f16bd8 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -35,7 +35,7 @@ use crate::util::logger::LoggingConfig; /// Wallet configuration file name pub const WALLET_CONFIG_FILE_NAME: &str = "grin-wallet.toml"; const WALLET_LOG_FILE_NAME: &str = "grin-wallet.log"; -const GRIN_HOME: &str = ".grin"; +pub const GRIN_HOME: &str = ".grin"; /// Wallet data directory pub const GRIN_WALLET_DIR: &str = "wallet_data"; /// Node API secret @@ -43,28 +43,28 @@ 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_grin_path( +fn get_grin_wallet_path( chain_type: &global::ChainTypes, create_path: bool, ) -> Result { // Check if grin dir exists - let mut grin_path = match dirs::home_dir() { + let mut grin_wallet_path = match dirs::home_dir() { Some(p) => p, None => PathBuf::new(), }; - grin_path.push(GRIN_HOME); - grin_path.push(chain_type.shortname()); + grin_wallet_path.push(GRIN_HOME); + grin_wallet_path.push(chain_type.shortname()); // Create if the default path doesn't exist - if !grin_path.exists() && create_path { - fs::create_dir_all(grin_path.clone())?; + if !grin_wallet_path.exists() && create_path { + fs::create_dir_all(grin_wallet_path.clone())?; } - if !grin_path.exists() { + if !grin_wallet_path.exists() { Err(ConfigError::PathNotFoundError(String::from( - grin_path.to_str().unwrap(), + grin_wallet_path.to_str().unwrap(), ))) } else { - Ok(grin_path) + Ok(grin_wallet_path) } } @@ -83,22 +83,6 @@ fn check_config_current_dir(path: &str) -> Option { None } -/// Checks if the node api secret in wallet dir, else return default path -fn check_node_api_exists(path: &str) -> Option { - let p = env::current_dir(); - let mut c = match p { - Ok(c) => c, - Err(_) => { - return None; - } - }; - c.push(path); - if c.exists() { - return Some(c); - } - None -} - /// Whether a config file exists at the given directory pub fn config_file_exists(path: &str) -> bool { let mut path = PathBuf::from(path); @@ -136,11 +120,11 @@ fn check_api_secret_file( data_path: Option, file_name: &str, ) -> Result<(), ConfigError> { - let grin_path = match data_path { + let grin_wallet_path = match data_path { Some(p) => p, - None => get_grin_path(chain_type, false)?, + None => get_grin_wallet_path(chain_type, false)?, }; - let mut api_secret_path = grin_path; + let mut api_secret_path = grin_wallet_path; api_secret_path.push(file_name); if !api_secret_path.exists() { init_api_secret(&api_secret_path) @@ -167,13 +151,14 @@ pub fn initial_setup_wallet( (path, GlobalWalletConfig::new(p.to_str().unwrap())?) } else { // Check if grin dir exists - let grin_path = match data_path { + let node_path = get_grin_wallet_path(chain_type, create_path)?; + let grin_wallet_path = match data_path { Some(p) => p, - None => get_grin_path(chain_type, create_path)?, + None => node_path.clone(), }; // Get path to default config file - let mut config_path = grin_path.clone(); + let mut config_path = grin_wallet_path.clone(); config_path.push(WALLET_CONFIG_FILE_NAME); // Return defaults if file doesn't exist @@ -181,9 +166,21 @@ pub fn initial_setup_wallet( false => { let mut default_config = GlobalWalletConfig::for_chain(chain_type); default_config.config_file_path = Some(config_path); - // update paths relative to current dir - default_config.update_paths(&grin_path); - (grin_path, default_config) + // update paths relative to current dir, assumes node secret is in user home + default_config.update_paths(&grin_wallet_path, &node_path); + // Write config file, otherwise default will writen + default_config.write_to_file( + &default_config + .config_file_path + .clone() + .unwrap() + .to_str() + .unwrap(), + false, + None, + None, + ); + (grin_wallet_path, default_config) } true => { let mut path = config_path.clone(); @@ -195,9 +192,10 @@ pub fn initial_setup_wallet( } } }; - 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)?; + // println!("{:#?}",&config.members.clone().unwrap().wallet.node_api_secret_path); + // println!("CONFIG APPEARS TO BE OK"); Ok(config) } @@ -284,7 +282,7 @@ impl GlobalWalletConfig { } /// Update paths - pub fn update_paths(&mut self, wallet_home: &PathBuf) { + 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 = @@ -293,15 +291,12 @@ impl GlobalWalletConfig { 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 node_secret_path = wallet_home.clone(); + let mut node_secret_path = node_home.clone(); node_secret_path.push(API_SECRET_FILE_NAME); - // Set node API secret path to the wallet dir if it exists, else keep default home dir - // if node_secret_path.exists(){ - // self.members.as_mut().unwrap().wallet.node_api_secret_path = - // Some(node_secret_path.to_str().unwrap().to_owned()); - // println!("It node path does actually exists") - // } - + dbg!(&node_secret_path); + self.members.as_mut().unwrap().wallet.node_api_secret_path = + Some(node_secret_path.to_str().unwrap().to_owned()); + dbg!(&self.members.as_mut().unwrap().wallet.node_api_secret_path); let mut log_path = wallet_home.clone(); log_path.push(WALLET_LOG_FILE_NAME); self.members diff --git a/impls/Cargo.toml b/impls/Cargo.toml index 7689d6179..4dd380f41 100644 --- a/impls/Cargo.toml +++ b/impls/Cargo.toml @@ -19,6 +19,7 @@ serde_derive = "1" serde_json = "1" log = "0.4" ring = "0.16" +dirs = "2.0" uuid = { version = "0.8", features = ["serde", "v4"] } chrono = { version = "0.4.11", features = ["serde"] } lazy_static = "1" diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index d0cc60636..eb696f2ab 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -13,7 +13,6 @@ // limitations under the License. //! Default wallet lifecycle provider - use crate::config::{ config, GlobalWalletConfig, GlobalWalletConfigMembers, TorConfig, WalletConfig, GRIN_WALLET_DIR, }; @@ -137,10 +136,13 @@ where return Ok(()); } - let mut abs_path = std::env::current_dir()?; - abs_path.push(self.data_dir.clone()); + // let mut abs_path_node = std::env::current_dir()?; + // let mut abs_path_wallet = std::env::current_dir()?; + // abs_path_wallet.push(self.data_dir.clone()); + // abs_path_node.push(self.data_dir.clone()); + // default_config.update_paths(&abs_path_wallet,&abs_path_node); + // println!("here, updating and writing config in default"); - default_config.update_paths(&abs_path); let res = default_config.write_to_file(config_file_name.to_str().unwrap(), false, None, None); if let Err(e) = res { diff --git a/src/bin/grin-wallet.rs b/src/bin/grin-wallet.rs index 3a192fd8d..50e745598 100644 --- a/src/bin/grin-wallet.rs +++ b/src/bin/grin-wallet.rs @@ -110,6 +110,7 @@ fn real_main() -> i32 { // Load relevant config, try and load a wallet config file // Use defaults for configuration if config file not found anywhere + dbg!(¤t_dir, &create_path); let mut config = match config::initial_setup_wallet(&chain_type, current_dir, create_path) { Ok(c) => c, Err(e) => match e { @@ -138,7 +139,7 @@ fn real_main() -> i32 { "Using wallet configuration file at {}", config.config_file_path.as_ref().unwrap().to_str().unwrap() ); - + println!("here3"); log_build_info(); global::init_global_chain_type( @@ -152,11 +153,14 @@ fn real_main() -> i32 { .unwrap() .clone(), ); - + println!("here4"); global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base()); let wallet_config = config.clone().members.unwrap().wallet; + println!("here5"); let node_client = HTTPNodeClient::new(&wallet_config.check_node_api_http_addr, None).unwrap(); - + println!("here6"); + //dbg!(&config.check_node_api_http_addr, None).unwrap(); + println!("here7"); cmd::wallet_command(&args, config, node_client) } From 0573393e8da93c2a43197cb07c9609cf59ee4388 Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Wed, 5 Feb 2025 20:54:33 +0100 Subject: [PATCH 03/10] More debugging, next trace the overwrite of the config --- config/src/config.rs | 92 ++++++++++++++++++++++++++++++++---------- src/bin/grin-wallet.rs | 8 +--- 2 files changed, 72 insertions(+), 28 deletions(-) diff --git a/config/src/config.rs b/config/src/config.rs index 327f16bd8..01af83cf0 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -43,28 +43,28 @@ 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_grin_wallet_path( +fn get_grin_path( chain_type: &global::ChainTypes, create_path: bool, ) -> Result { // Check if grin dir exists - let mut grin_wallet_path = match dirs::home_dir() { + let mut grin_path = match dirs::home_dir() { Some(p) => p, None => PathBuf::new(), }; - grin_wallet_path.push(GRIN_HOME); - grin_wallet_path.push(chain_type.shortname()); + grin_path.push(GRIN_HOME); + grin_path.push(chain_type.shortname()); // Create if the default path doesn't exist - if !grin_wallet_path.exists() && create_path { - fs::create_dir_all(grin_wallet_path.clone())?; + if !grin_path.exists() && create_path { + fs::create_dir_all(grin_path.clone())?; } - if !grin_wallet_path.exists() { + if !grin_path.exists() { Err(ConfigError::PathNotFoundError(String::from( - grin_wallet_path.to_str().unwrap(), + grin_path.to_str().unwrap(), ))) } else { - Ok(grin_wallet_path) + Ok(grin_path) } } @@ -120,11 +120,11 @@ fn check_api_secret_file( data_path: Option, file_name: &str, ) -> Result<(), ConfigError> { - let grin_wallet_path = match data_path { + let grin_path = match data_path { Some(p) => p, - None => get_grin_wallet_path(chain_type, false)?, + None => get_grin_path(chain_type, false)?, }; - let mut api_secret_path = grin_wallet_path; + let mut api_secret_path = grin_path; api_secret_path.push(file_name); if !api_secret_path.exists() { init_api_secret(&api_secret_path) @@ -144,21 +144,23 @@ pub fn initial_setup_wallet( fs::create_dir_all(p)?; } } - // Use config file if current directory if it exists, .grin home otherwise + // Use config file in current directory if it exists, .grin home otherwise let (path, config) = if let Some(p) = check_config_current_dir(WALLET_CONFIG_FILE_NAME) { let mut path = p.clone(); path.pop(); + dbg!("This means the config file already exists!"); (path, GlobalWalletConfig::new(p.to_str().unwrap())?) } else { // Check if grin dir exists - let node_path = get_grin_wallet_path(chain_type, create_path)?; - let grin_wallet_path = match data_path { + let node_path = get_grin_path(chain_type, false)?; + dbg!("in init function 1", &node_path); + let grin_path = match data_path { Some(p) => p, None => node_path.clone(), }; // Get path to default config file - let mut config_path = grin_wallet_path.clone(); + let mut config_path = grin_path.clone(); config_path.push(WALLET_CONFIG_FILE_NAME); // Return defaults if file doesn't exist @@ -167,8 +169,30 @@ pub fn initial_setup_wallet( 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 - default_config.update_paths(&grin_wallet_path, &node_path); - // Write config file, otherwise default will writen + dbg!( + "This is the node_path before asking update_paths {}", + &node_path + ); + default_config.update_paths(&grin_path, &node_path); + dbg!( + "This path, after this line write to config file before default will do so: {}", + &default_config + .members + .as_mut() + .unwrap() + .wallet + .node_api_secret_path + ); + // Write config file, otherwise defaults will be writen + dbg!( + "This path, after this line write to config file before default will do so: {}", + &default_config + .members + .as_mut() + .unwrap() + .wallet + .node_api_secret_path + ); default_config.write_to_file( &default_config .config_file_path @@ -180,11 +204,29 @@ pub fn initial_setup_wallet( None, None, ); - (grin_wallet_path, default_config) + + // After writing the updated config, load it as strig + let mut path = default_config.config_file_path.clone(); + path.pop(); + dbg!( + "Here we load from the cofig we just wrote {}", + &config_path.clone().exists() + ); + ( + path.unwrap(), + GlobalWalletConfig::new(config_path.to_str().unwrap())?, + ) + + // Before it would retuur + // (grin_path, default_config) } true => { let mut path = config_path.clone(); path.pop(); + dbg!( + "This means the file exists already.... arm on in match in connfig init {}", + &config_path.clone().exists() + ); ( path, GlobalWalletConfig::new(config_path.to_str().unwrap())?, @@ -194,7 +236,10 @@ pub fn initial_setup_wallet( }; 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)?; - // println!("{:#?}",&config.members.clone().unwrap().wallet.node_api_secret_path); + println!( + "{:#?}", + &config.members.clone().unwrap().wallet.node_api_secret_path + ); // println!("CONFIG APPEARS TO BE OK"); Ok(config) } @@ -293,10 +338,13 @@ impl GlobalWalletConfig { Some(secret_path.to_str().unwrap().to_owned()); let mut node_secret_path = node_home.clone(); node_secret_path.push(API_SECRET_FILE_NAME); - dbg!(&node_secret_path); + dbg!("Voor update, path to node to set to:{}", &node_secret_path); self.members.as_mut().unwrap().wallet.node_api_secret_path = Some(node_secret_path.to_str().unwrap().to_owned()); - dbg!(&self.members.as_mut().unwrap().wallet.node_api_secret_path); + dbg!( + "Na update, path to node:{}", + &self.members.as_mut().unwrap().wallet.node_api_secret_path + ); let mut log_path = wallet_home.clone(); log_path.push(WALLET_LOG_FILE_NAME); self.members diff --git a/src/bin/grin-wallet.rs b/src/bin/grin-wallet.rs index 50e745598..097440795 100644 --- a/src/bin/grin-wallet.rs +++ b/src/bin/grin-wallet.rs @@ -110,7 +110,6 @@ fn real_main() -> i32 { // Load relevant config, try and load a wallet config file // Use defaults for configuration if config file not found anywhere - dbg!(¤t_dir, &create_path); let mut config = match config::initial_setup_wallet(&chain_type, current_dir, create_path) { Ok(c) => c, Err(e) => match e { @@ -142,6 +141,7 @@ fn real_main() -> i32 { println!("here3"); log_build_info(); + dbg!(&config.members.as_mut().unwrap().wallet.node_api_secret_path); global::init_global_chain_type( config .members @@ -153,14 +153,10 @@ fn real_main() -> i32 { .unwrap() .clone(), ); - println!("here4"); global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base()); let wallet_config = config.clone().members.unwrap().wallet; - println!("here5"); + dbg!(&config.members.as_mut().unwrap().wallet.node_api_secret_path); let node_client = HTTPNodeClient::new(&wallet_config.check_node_api_http_addr, None).unwrap(); - println!("here6"); - //dbg!(&config.check_node_api_http_addr, None).unwrap(); - println!("here7"); cmd::wallet_command(&args, config, node_client) } From 80586cc175987a2ccd2eae97e824ac2dbed78551 Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Thu, 13 Feb 2025 17:11:38 +0100 Subject: [PATCH 04/10] Added get get_node_path function, seperating wallet dir and node_dir -compiling but some warnings --- config/src/comments.rs | 2 +- config/src/config.rs | 117 ++++++++++++++++----------------- controller/src/command.rs | 3 +- impls/src/lifecycle/default.rs | 3 +- 4 files changed, 62 insertions(+), 63 deletions(-) diff --git a/config/src/comments.rs b/config/src/comments.rs index 4ae9f1b73..4c6d9cde8 100644 --- a/config/src/comments.rs +++ b/config/src/comments.rs @@ -74,7 +74,7 @@ fn comments() -> HashMap { .to_string(), ); retval.insert( - "node_api_secret_path".to_string(), + "node_api_secret_path".to_string().replace("/", "\\"), " #location of the node api secret for basic auth on the Grin API " diff --git a/config/src/config.rs b/config/src/config.rs index 01af83cf0..ba8c961ff 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -14,7 +14,6 @@ //! Configuration file management -use dirs; use rand::distributions::{Alphanumeric, Distribution}; use rand::thread_rng; use std::env; @@ -43,7 +42,7 @@ 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_grin_path( +fn get_wallet_path( chain_type: &global::ChainTypes, create_path: bool, ) -> Result { @@ -68,6 +67,48 @@ fn get_grin_path( } } +// Smart function to find the most likely .api_secret location for the node +fn get_node_path( + data_path: Option, + chain_type: &global::ChainTypes, +) -> Result { + let node_path = match data_path { + // 1) A If top dir provided and api_secret exist, return top dir + Some(path) => { + let mut node_path = path; + node_path.push(GRIN_HOME); + node_path.push(chain_type.shortname()); + node_path.push(API_SECRET_FILE_NAME); + if node_path.exists() { + node_path.pop(); + Ok(node_path) + + // 1) B If top dir exists, but no config there, return home dir + } else { + let mut node_path = match dirs::home_dir() { + Some(p) => p, + None => PathBuf::new(), + }; + node_path.push(GRIN_HOME); + node_path.push(chain_type.shortname()); + Ok(node_path) + } + } + + // 2) If there is no top_dir provided, always return home dir + None => { + let mut node_path = match dirs::home_dir() { + Some(p) => p, + None => PathBuf::new(), + }; + node_path.push(GRIN_HOME); + node_path.push(chain_type.shortname()); + Ok(node_path) + } + }; + node_path +} + fn check_config_current_dir(path: &str) -> Option { let p = env::current_dir(); let mut c = match p { @@ -122,7 +163,7 @@ fn check_api_secret_file( ) -> Result<(), ConfigError> { let grin_path = match data_path { Some(p) => p, - None => get_grin_path(chain_type, false)?, + None => get_node_path(data_path, chain_type)?, }; let mut api_secret_path = grin_path; api_secret_path.push(file_name); @@ -144,55 +185,32 @@ pub fn initial_setup_wallet( fs::create_dir_all(p)?; } } - // Use config file in current directory if it exists, .grin home otherwise + // Use config file in a) current directory, or b) in top path, or c) .grin home let (path, config) = if let Some(p) = check_config_current_dir(WALLET_CONFIG_FILE_NAME) { let mut path = p.clone(); path.pop(); - dbg!("This means the config file already exists!"); (path, GlobalWalletConfig::new(p.to_str().unwrap())?) } else { - // Check if grin dir exists - let node_path = get_grin_path(chain_type, false)?; - dbg!("in init function 1", &node_path); - let grin_path = match data_path { + let wallet_path = match data_path { Some(p) => p, - None => node_path.clone(), + 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 = grin_path.clone(); + let mut config_path = wallet_path.clone(); config_path.push(WALLET_CONFIG_FILE_NAME); - // Return defaults if file doesn't exist + // 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 - dbg!( - "This is the node_path before asking update_paths {}", - &node_path - ); - default_config.update_paths(&grin_path, &node_path); - dbg!( - "This path, after this line write to config file before default will do so: {}", - &default_config - .members - .as_mut() - .unwrap() - .wallet - .node_api_secret_path - ); + // Update paths relative to current dir, assumes node secret is in user home + default_config.update_paths(&wallet_path, &node_path); // Write config file, otherwise defaults will be writen - dbg!( - "This path, after this line write to config file before default will do so: {}", - &default_config - .members - .as_mut() - .unwrap() - .wallet - .node_api_secret_path - ); default_config.write_to_file( &default_config .config_file_path @@ -204,29 +222,12 @@ pub fn initial_setup_wallet( None, None, ); - - // After writing the updated config, load it as strig - let mut path = default_config.config_file_path.clone(); - path.pop(); - dbg!( - "Here we load from the cofig we just wrote {}", - &config_path.clone().exists() - ); - ( - path.unwrap(), - GlobalWalletConfig::new(config_path.to_str().unwrap())?, - ) - - // Before it would retuur - // (grin_path, default_config) + (wallet_path, default_config) } + true => { let mut path = config_path.clone(); path.pop(); - dbg!( - "This means the file exists already.... arm on in match in connfig init {}", - &config_path.clone().exists() - ); ( path, GlobalWalletConfig::new(config_path.to_str().unwrap())?, @@ -234,13 +235,9 @@ pub fn initial_setup_wallet( } } }; + 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)?; - println!( - "{:#?}", - &config.members.clone().unwrap().wallet.node_api_secret_path - ); - // println!("CONFIG APPEARS TO BE OK"); Ok(config) } diff --git a/controller/src/command.rs b/controller/src/command.rs index 938eb8291..113d2a49e 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -86,7 +86,8 @@ where let mut w_lock = owner_api.wallet_inst.lock(); let p = w_lock.lc_provider()?; - p.create_config(&chain_type, WALLET_CONFIG_FILE_NAME, None, None, None)?; + // Config and updating of config was moved to main, avoid overwriting with default value + //p.create_config(&chain_type, WALLET_CONFIG_FILE_NAME, None, None, None)?; p.create_wallet( None, args.recovery_phrase, diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index eb696f2ab..b42ffb1f0 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -133,7 +133,8 @@ where // just leave as is if file exists but there's no data dir if config_file_name.exists() { - return Ok(()); + panic!("Crash and burn!!!!!"); + //return Ok(()); } // let mut abs_path_node = std::env::current_dir()?; From c0216a152a2e5e79b36596ba60a6cf5b651c8893 Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Fri, 14 Feb 2025 16:14:20 +0100 Subject: [PATCH 05/10] Tested wallet config creation fix for Issues: #728 #3394 Pull 3420, partial fix for 3002 --- config/src/comments.rs | 2 +- config/src/config.rs | 34 +++++++++++++++++----------------- controller/src/command.rs | 13 +++++++------ impls/src/node_clients/http.rs | 4 ++-- src/bin/grin-wallet.rs | 5 +---- 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/config/src/comments.rs b/config/src/comments.rs index 4c6d9cde8..4ae9f1b73 100644 --- a/config/src/comments.rs +++ b/config/src/comments.rs @@ -74,7 +74,7 @@ fn comments() -> HashMap { .to_string(), ); retval.insert( - "node_api_secret_path".to_string().replace("/", "\\"), + "node_api_secret_path".to_string(), " #location of the node api secret for basic auth on the Grin API " diff --git a/config/src/config.rs b/config/src/config.rs index ba8c961ff..6eb5b2044 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -34,6 +34,7 @@ use crate::util::logger::LoggingConfig; /// Wallet configuration file name pub const WALLET_CONFIG_FILE_NAME: &str = "grin-wallet.toml"; const WALLET_LOG_FILE_NAME: &str = "grin-wallet.log"; +/// .grin folder, usually in home/.grin pub const GRIN_HOME: &str = ".grin"; /// Wallet data directory pub const GRIN_WALLET_DIR: &str = "wallet_data"; @@ -211,22 +212,26 @@ pub fn initial_setup_wallet( // Update paths relative to current dir, assumes node secret is in user home 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, - ); + 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) } true => { - let mut path = config_path.clone(); + let mut path = wallet_path.clone(); path.pop(); ( path, @@ -335,13 +340,8 @@ impl GlobalWalletConfig { Some(secret_path.to_str().unwrap().to_owned()); let mut node_secret_path = node_home.clone(); node_secret_path.push(API_SECRET_FILE_NAME); - dbg!("Voor update, path to node to set to:{}", &node_secret_path); self.members.as_mut().unwrap().wallet.node_api_secret_path = Some(node_secret_path.to_str().unwrap().to_owned()); - dbg!( - "Na update, path to node:{}", - &self.members.as_mut().unwrap().wallet.node_api_secret_path - ); let mut log_path = wallet_home.clone(); log_path.push(WALLET_LOG_FILE_NAME); self.members diff --git a/controller/src/command.rs b/controller/src/command.rs index 113d2a49e..0f3e1de8a 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, WALLET_CONFIG_FILE_NAME}; -use crate::core::{core, global}; +use crate::config::{TorConfig, WalletConfig}; +use crate::core::core; use crate::error::Error; use crate::impls::PathToSlatepack; use crate::impls::SlateGetter as _; @@ -82,12 +82,13 @@ 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 and updating of config was moved to main, avoid overwriting with default value - //p.create_config(&chain_type, WALLET_CONFIG_FILE_NAME, None, None, None)?; + // 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_wallet( None, args.recovery_phrase, diff --git a/impls/src/node_clients/http.rs b/impls/src/node_clients/http.rs index cd293f95e..6251d3847 100644 --- a/impls/src/node_clients/http.rs +++ b/impls/src/node_clients/http.rs @@ -128,8 +128,8 @@ impl NodeClient for HTTPNodeClient { verified: Some(false), }); } else { - error!("Unable to contact Node to get version info: {}", e); - warn!("Warning: You might need to correct 'node_api_secret_path' in your 'grin-wallet.toml' file"); + error!("Unable to contact Node to get version info: {}, check your node is running", e); + warn!("Warning: a) Node is offline, or b) 'node_api_secret_path' in 'grin-wallet.toml' is set incorrectly"); return None; } } diff --git a/src/bin/grin-wallet.rs b/src/bin/grin-wallet.rs index 097440795..69e892354 100644 --- a/src/bin/grin-wallet.rs +++ b/src/bin/grin-wallet.rs @@ -87,7 +87,7 @@ fn real_main() -> i32 { let res = args.value_of("top_level_dir"); match res { Some(d) => { - current_dir = Some(PathBuf::from(d)); + current_dir = Some(PathBuf::from(d.replace("/", "\\"))); } None => { warn!("Argument --top_level_dir needs a value. Defaulting to current directory") @@ -138,10 +138,8 @@ fn real_main() -> i32 { "Using wallet configuration file at {}", config.config_file_path.as_ref().unwrap().to_str().unwrap() ); - println!("here3"); log_build_info(); - dbg!(&config.members.as_mut().unwrap().wallet.node_api_secret_path); global::init_global_chain_type( config .members @@ -156,7 +154,6 @@ fn real_main() -> i32 { global::init_global_accept_fee_base(config.members.as_ref().unwrap().wallet.accept_fee_base()); let wallet_config = config.clone().members.unwrap().wallet; - dbg!(&config.members.as_mut().unwrap().wallet.node_api_secret_path); let node_client = HTTPNodeClient::new(&wallet_config.check_node_api_http_addr, None).unwrap(); cmd::wallet_command(&args, config, node_client) } From 0c5bdde4e861bc5ae528dedf25cd7684a28081a8 Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Tue, 18 Feb 2025 13:44:09 +0100 Subject: [PATCH 06/10] Removed some debugging code --- impls/Cargo.toml | 1 - impls/src/lifecycle/default.rs | 13 +++++-------- 2 files changed, 5 insertions(+), 9 deletions(-) diff --git a/impls/Cargo.toml b/impls/Cargo.toml index 4dd380f41..7689d6179 100644 --- a/impls/Cargo.toml +++ b/impls/Cargo.toml @@ -19,7 +19,6 @@ serde_derive = "1" serde_json = "1" log = "0.4" ring = "0.16" -dirs = "2.0" uuid = { version = "0.8", features = ["serde", "v4"] } chrono = { version = "0.4.11", features = ["serde"] } lazy_static = "1" diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index b42ffb1f0..d0cc60636 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -13,6 +13,7 @@ // limitations under the License. //! Default wallet lifecycle provider + use crate::config::{ config, GlobalWalletConfig, GlobalWalletConfigMembers, TorConfig, WalletConfig, GRIN_WALLET_DIR, }; @@ -133,17 +134,13 @@ where // just leave as is if file exists but there's no data dir if config_file_name.exists() { - panic!("Crash and burn!!!!!"); - //return Ok(()); + return Ok(()); } - // let mut abs_path_node = std::env::current_dir()?; - // let mut abs_path_wallet = std::env::current_dir()?; - // abs_path_wallet.push(self.data_dir.clone()); - // abs_path_node.push(self.data_dir.clone()); - // default_config.update_paths(&abs_path_wallet,&abs_path_node); - // println!("here, updating and writing config in default"); + let mut abs_path = std::env::current_dir()?; + abs_path.push(self.data_dir.clone()); + default_config.update_paths(&abs_path); let res = default_config.write_to_file(config_file_name.to_str().unwrap(), false, None, None); if let Err(e) = res { From 7b1144e4ecc72fda12729753a406e69ec00c3bdf Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Sat, 1 Mar 2025 13:28:41 +0100 Subject: [PATCH 07/10] Fixed passing config to create_config function, some more test needed before pulling to main --- Cargo.lock | 1 - config/src/config.rs | 79 ++++++++++++++++++++-------------- controller/src/command.rs | 18 +++++--- impls/src/lifecycle/default.rs | 20 ++++++--- 4 files changed, 70 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 103411eee..9b2c95122 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 6eb5b2044..9855eb7d0 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 0f3e1de8a..4fa781ef7 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 d0cc60636..759d34d18 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 { From b45b6033822a3fe16e313d7b30da995016488376 Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Sat, 1 Mar 2025 13:52:04 +0100 Subject: [PATCH 08/10] All test succeed: a) normal, b) top-dir, c) here, d) with default config template in working dir --- impls/src/lifecycle/default.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index 759d34d18..45435dadd 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -137,8 +137,8 @@ where if config_file_name.exists() { return Ok(()); } - - let mut abs_path_node = get_node_path?; + // default settings are updated if no config was provided, no support for top_dir/here + let mut abs_path_node = std::env::current_dir()?; 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()); From fe86c7a7e5ac32de98be0038958a9075bba2cf7c Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Sat, 1 Mar 2025 14:23:39 +0100 Subject: [PATCH 09/10] Cleaning up --- config/src/config.rs | 21 ++------------------- controller/src/command.rs | 3 +-- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/config/src/config.rs b/config/src/config.rs index 9855eb7d0..9666ccbb8 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -178,7 +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 +/// Use config file in a) current dir as template, b) in top path, or c) .grin home pub fn initial_setup_wallet( chain_type: &global::ChainTypes, data_path: Option, @@ -224,25 +224,8 @@ pub fn initial_setup_wallet( default_config.config_file_path = Some(config_path); // 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); - // }); (wallet_path, default_config) } - true => { let mut path = wallet_path.clone(); path.pop(); @@ -253,7 +236,7 @@ pub fn initial_setup_wallet( } } }; - + // 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), API_SECRET_FILE_NAME)?; Ok(config) diff --git a/controller/src/command.rs b/controller/src/command.rs index 4fa781ef7..1d1c0823e 100644 --- a/controller/src/command.rs +++ b/controller/src/command.rs @@ -70,6 +70,7 @@ pub struct InitArgs { pub restore: bool, } +/// Write config (default if None), initiate the wallet pub fn init( owner_api: &mut Owner, _g_args: &GlobalArgs, @@ -81,9 +82,7 @@ where C: NodeClient + 'static, 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()?; p.create_config( From c5b8c3413c2ffad00a019c48cbfeb1ad0d19770b Mon Sep 17 00:00:00 2001 From: Anynomouss Date: Mon, 3 Mar 2025 20:58:27 +0100 Subject: [PATCH 10/10] Fixed a single test for config that needed adjusting --- tests/common/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index de71fc981..569fd330f 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -165,7 +165,7 @@ pub fn config_command_wallet( .to_owned(), ))?; } - default_config.update_paths(¤t_dir); + default_config.update_paths(¤t_dir, ¤t_dir); default_config .write_to_file(config_file_name.to_str().unwrap(), false, None, None) .unwrap_or_else(|e| {