Skip to content

Commit

Permalink
Use printer for printing prompt when reading password from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
elizabethengelman committed Jan 14, 2025
1 parent 5b763f2 commit 8e234bc
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
20 changes: 11 additions & 9 deletions cmd/soroban-cli/src/commands/keys/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sep5::SeedPhrase;
use crate::{
commands::global,
config::{address::KeyName, locator, secret::{self, Secret}},
print::Print, signer::secure_store::{self, SecureStore},
print::{self, Print}, signer::secure_store::{self, SecureStore},
};

#[derive(thiserror::Error, Debug)]
Expand Down Expand Up @@ -43,30 +43,32 @@ pub struct Cmd {
impl Cmd {
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let print = Print::new(global_args.quiet);
let secret = self.read_secret()?;
let secret = self.read_secret(&print)?;
let path = self.config_locator.write_identity(&self.name, &secret)?;
print.checkln(format!("Key saved with alias {:?} in {path:?}", self.name));
Ok(())
}

fn read_secret(&self) -> Result<Secret, Error> {
fn read_secret(&self, print: &Print) -> Result<Secret, Error> {
if let Ok(secret_key) = std::env::var("SOROBAN_SECRET_KEY") {
return Ok(Secret::SecretKey { secret_key })
}
} else if self.secrets.secure_store {
let prompt = "Type a 12 or 24 word seed phrase:";
let secret_key = read_password(print, prompt)?;

println!("Type a secret key or 12/24 word seed phrase:");
let secret_key = read_password()?;
if self.secrets.secure_store {
let seed_phrase: SeedPhrase = secret_key.parse()?;
let print = &Print::new(false);

Ok(SecureStore::save_secret(print, &self.name, seed_phrase)?)
} else {
let prompt = "Type a secret key or 12/24 word seed phrase:";
let secret_key = read_password(print, prompt)?;
Ok(secret_key.parse()?)
}
}
}

fn read_password() -> Result<String, Error> {
fn read_password(print: &Print, prompt: &str) -> Result<String, Error> {
print.arrowln(prompt);
std::io::stdout().flush().map_err(|_| Error::PasswordRead)?;
rpassword::read_password().map_err(|_| Error::PasswordRead)
}
1 change: 1 addition & 0 deletions cmd/soroban-cli/src/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,4 @@ create_print_functions!(save, saveln, "💾");
create_print_functions!(search, searchln, "🔎");
create_print_functions!(warn, warnln, "⚠️");
create_print_functions!(exclaim, exclaimln, "❗️");
create_print_functions!(arrow, arrowln, "➡️");

0 comments on commit 8e234bc

Please sign in to comment.