Skip to content

Commit

Permalink
feat: support safely input private key
Browse files Browse the repository at this point in the history
  • Loading branch information
Halimao committed Nov 28, 2023
1 parent 6126189 commit 0466f9b
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
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 @@ -157,6 +157,9 @@ features = [ "fmt" ]
[dependencies.zip]
version = "^0.6"

[dependencies.rpassword]
version = "7.3.1"

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

Expand Down
7 changes: 7 additions & 0 deletions errors/src/errors/cli/cli_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,11 @@ create_messages!(
msg: format!("Failed to write file.\nIO 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,
}
);
8 changes: 4 additions & 4 deletions leo/cli/commands/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use super::*;
use leo_package::root::Env;
use snarkvm::prelude::{Address, PrivateKey, ViewKey};
use snarkvm::prelude::{Address, FromStr, PrivateKey, ViewKey};

use rand::SeedableRng;
use rand_chacha::ChaChaRng;
Expand All @@ -35,8 +35,6 @@ pub enum Account {
},
/// Derive an Aleo account from a private key.
Import {
/// Private key plaintext
private_key: PrivateKey<CurrentNetwork>,
/// Write the private key to the .env file.
#[clap(short = 'w', long)]
write: bool,
Expand Down Expand Up @@ -77,7 +75,9 @@ impl Command for Account {
write_to_env_file(private_key, &ctx)?;
}
}
Account::Import { private_key, write } => {
Account::Import { write } => {
let private_key_input = rpassword::prompt_password("Please enter your private key: ").unwrap();
let private_key = FromStr::from_str(&private_key_input).map_err(CliError::failed_to_execute_account)?;
// Derive the view key and address and print to stdout.
print_keys(private_key)?;

Expand Down

0 comments on commit 0466f9b

Please sign in to comment.