feat: support safely input private key

This commit is contained in:
Halimao 2023-11-28 18:54:30 +08:00
parent 6126189e59
commit 0466f9bdf0
4 changed files with 36 additions and 4 deletions

22
Cargo.lock generated
View File

@ -1442,6 +1442,7 @@ dependencies = [
"rand_chacha", "rand_chacha",
"rand_core", "rand_core",
"reqwest", "reqwest",
"rpassword",
"rusty-hook", "rusty-hook",
"self_update 0.39.0", "self_update 0.39.0",
"serde", "serde",
@ -2194,6 +2195,27 @@ dependencies = [
"windows-sys 0.48.0", "windows-sys 0.48.0",
] ]
[[package]]
name = "rpassword"
version = "7.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80472be3c897911d0137b2d2b9055faf6eeac5b14e324073d83bc17b191d7e3f"
dependencies = [
"libc",
"rtoolbox",
"windows-sys 0.48.0",
]
[[package]]
name = "rtoolbox"
version = "0.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c247d24e63230cdb56463ae328478bd5eac8b8faa8c69461a77e8e323afac90e"
dependencies = [
"libc",
"windows-sys 0.48.0",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.23" version = "0.1.23"

View File

@ -157,6 +157,9 @@ features = [ "fmt" ]
[dependencies.zip] [dependencies.zip]
version = "^0.6" version = "^0.6"
[dependencies.rpassword]
version = "7.3.1"
[target."cfg(windows)".dependencies.ansi_term] [target."cfg(windows)".dependencies.ansi_term]
version = "0.12.1" version = "0.12.1"

View File

@ -178,4 +178,11 @@ create_messages!(
msg: format!("Failed to write file.\nIO Error: {error}"), msg: format!("Failed to write file.\nIO Error: {error}"),
help: None, help: None,
} }
@backtraced
failed_to_execute_account {
args: (error: impl Display),
msg: format!("Failed to execute the `account` command.\nSnarkVM Error: {error}"),
help: None,
}
); );

View File

@ -16,7 +16,7 @@
use super::*; use super::*;
use leo_package::root::Env; use leo_package::root::Env;
use snarkvm::prelude::{Address, PrivateKey, ViewKey}; use snarkvm::prelude::{Address, FromStr, PrivateKey, ViewKey};
use rand::SeedableRng; use rand::SeedableRng;
use rand_chacha::ChaChaRng; use rand_chacha::ChaChaRng;
@ -35,8 +35,6 @@ pub enum Account {
}, },
/// Derive an Aleo account from a private key. /// Derive an Aleo account from a private key.
Import { Import {
/// Private key plaintext
private_key: PrivateKey<CurrentNetwork>,
/// Write the private key to the .env file. /// Write the private key to the .env file.
#[clap(short = 'w', long)] #[clap(short = 'w', long)]
write: bool, write: bool,
@ -77,7 +75,9 @@ impl Command for Account {
write_to_env_file(private_key, &ctx)?; 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. // Derive the view key and address and print to stdout.
print_keys(private_key)?; print_keys(private_key)?;