Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: hide inputed private key when import an account #10062

Merged
merged 10 commits into from
Feb 29, 2024
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ version = "^0.6"
[dependencies.crossterm]
version = "0.27.0"

[dependencies.rpassword]
version = "7.3.1"

[target."cfg(windows)".dependencies.ansi_term]
version = "0.12.1"

Expand Down
14 changes: 14 additions & 0 deletions errors/src/errors/cli/cli_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,20 @@ create_messages!(
help: None,
}

@backtraced
failed_to_parse_private_key {
args: (error: impl Display),
msg: format!("Failed to parse private key.\nSnarkVM Error: {error}"),
help: None,
}

@backtraced
failed_to_execute_account {
args: (error: impl Display),
msg: format!("Failed to execute the `account` command.\nSnarkVM Error: {error}"),
help: None,
}

@backtraced
failed_to_read_environment_private_key {
args: (error: impl Display),
Expand Down
22 changes: 19 additions & 3 deletions leo/cli/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub enum Account {
/// Derive an Aleo account from a private key.
Import {
/// Private key plaintext
private_key: PrivateKey<CurrentNetwork>,
private_key: Option<PrivateKey<CurrentNetwork>>,
/// Write the private key to the .env file.
#[clap(short = 'w', long)]
write: bool,
Expand Down Expand Up @@ -128,12 +128,28 @@ impl Command for Account {
}
}
Account::Import { private_key, write, discreet } => {
let priv_key = match discreet {
true => {
let private_key_input = rpassword::prompt_password("Please enter your private key: ").unwrap();
snarkvm::prelude::FromStr::from_str(&private_key_input)
.map_err(CliError::failed_to_parse_private_key)?
}
false => match private_key {
Some(private_key) => private_key,
None => {
return Err(CliError::failed_to_execute_account(
"PRIVATE_KEY shouldn't be empty when --discreet is false",
)
.into());
}
},
};
// Derive the view key and address and print to stdout.
print_keys(private_key, discreet)?;
print_keys(priv_key, discreet)?;

// Save key data to .env file.
if write {
write_to_env_file(private_key, &ctx)?;
write_to_env_file(priv_key, &ctx)?;
}
}
Self::Sign { message, seed, raw, private_key, private_key_file } => {
Expand Down
Loading