Skip to content

Commit

Permalink
Merge pull request #13 from kanari-network/dev
Browse files Browse the repository at this point in the history
Refactor wallet selection and listing; update to handle encrypted wal…
  • Loading branch information
jamesatomc authored Jan 28, 2025
2 parents e3ab4d2 + 1b23984 commit d7a0e68
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 20 deletions.
60 changes: 43 additions & 17 deletions crates/command/src/keytool_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,56 @@ pub fn handle_keytool_command() -> Option<String> {
println!("{}", "No wallets found!".red());
return None;
}

println!("\nAvailable wallets:");
for (i, (wallet, _)) in wallets.iter().enumerate() {
println!("{}. {}", i + 1, wallet.trim_end_matches(".toml"));
for (i, (wallet, is_selected)) in wallets.iter().enumerate() {
let wallet_name = wallet.trim_end_matches(".enc");
if *is_selected {
println!("{}. {} {}", i + 1, wallet_name, "(current)".green());
} else {
println!("{}. {}", i + 1, wallet_name);
}
}
println!("\nEnter wallet number to select:");

println!("\nEnter wallet number to select (or press Enter to cancel):");
let mut input = String::new();
let _ = io::stdin().read_line(&mut input);

if let Ok(index) = input.trim().parse::<usize>() {
if index > 0 && index <= wallets.len() {
let selected = wallets[index - 1].0.trim_end_matches(".toml");
if let Err(e) = set_selected_wallet(selected) {
println!("Error setting wallet: {}", e);
} else {
println!("Selected wallet: {}", selected.green());
match io::stdin().read_line(&mut input) {
Ok(_) => {
if input.trim().is_empty() {
return None;
}

match input.trim().parse::<usize>() {
Ok(index) if index > 0 && index <= wallets.len() => {
let selected = wallets[index - 1].0.trim_end_matches(".enc");
match set_selected_wallet(selected) {
Ok(_) => {
println!("{}", format!("Selected wallet: {}", selected).green());
Some(selected.to_string())
},
Err(e) => {
println!("{}", format!("Error setting wallet: {}", e).red());
None
}
}
},
_ => {
println!("{}", format!("Invalid selection. Please enter a number between 1 and {}", wallets.len()).red());
None
}
}
},
Err(e) => {
println!("{}", format!("Error reading input: {}", e).red());
None
}
}
},
Err(e) => println!("Error listing wallets: {}", e),
Err(e) => {
println!("{}", format!("Error listing wallets: {}", e).red());
None
}
}
None
},

"wallet" => {
Expand Down Expand Up @@ -247,7 +273,7 @@ pub fn handle_keytool_command() -> Option<String> {
println!("------------------");
for (wallet_name, is_selected) in wallets {
let status_symbol = if is_selected { "✓ " } else { " " };
let wallet_display = wallet_name.trim_end_matches(".json");
let wallet_display = wallet_name.trim_end_matches(".enc");
if is_selected {
println!("{}{}", status_symbol, wallet_display.green().bold());
} else {
Expand Down
19 changes: 16 additions & 3 deletions crates/core/wallet/key/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ pub fn list_wallet_files() -> Result<Vec<(String, bool)>, std::io::Error> {
let kari_dir = get_kari_dir();
let wallet_dir = kari_dir.join("wallets");

// Create wallet directory if it doesn't exist
if !wallet_dir.exists() {
fs::create_dir_all(&wallet_dir)?;
}

// Get currently selected wallet
let selected = get_selected_wallet().unwrap_or_default();

Expand All @@ -197,12 +202,20 @@ pub fn list_wallet_files() -> Result<Vec<(String, bool)>, std::io::Error> {
let path = entry.path();
if path.is_file() {
if let Some(filename) = path.file_name().and_then(|s| s.to_str()) {
// Check if this wallet is selected
let is_selected = filename.trim_end_matches(".toml") == selected;
wallets.push((filename.to_string(), is_selected));
// Only include .enc files
if filename.ends_with(".enc") {
// Check if this wallet is selected
let wallet_name = filename.trim_end_matches(".enc");
let is_selected = wallet_name == selected;
wallets.push((filename.to_string(), is_selected));
}
}
}
}

// Sort wallets alphabetically
wallets.sort_by(|a, b| a.0.cmp(&b.0));

Ok(wallets)
}

Expand Down

0 comments on commit d7a0e68

Please sign in to comment.