Skip to content

Commit

Permalink
Added get get_node_path function, seperating wallet dir and node_dir …
Browse files Browse the repository at this point in the history
…-compiling but some warnings
  • Loading branch information
Anynomouss committed Feb 13, 2025
1 parent 0573393 commit 80586cc
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 63 deletions.
2 changes: 1 addition & 1 deletion config/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fn comments() -> HashMap<String, String> {
.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
"
Expand Down
117 changes: 57 additions & 60 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

//! Configuration file management
use dirs;
use rand::distributions::{Alphanumeric, Distribution};
use rand::thread_rng;
use std::env;
Expand Down Expand Up @@ -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<PathBuf, ConfigError> {
Expand All @@ -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<PathBuf>,
chain_type: &global::ChainTypes,
) -> Result<PathBuf, ConfigError> {
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<PathBuf> {
let p = env::current_dir();
let mut c = match p {
Expand Down Expand Up @@ -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);
Expand All @@ -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
Expand All @@ -204,43 +222,22 @@ 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())?,
)
}
}
};

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)
}

Expand Down
3 changes: 2 additions & 1 deletion controller/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion impls/src/lifecycle/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()?;
Expand Down

0 comments on commit 80586cc

Please sign in to comment.