From 0466f9bdf03daa17d4e8aa080201dab90e0267fd Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Tue, 28 Nov 2023 18:54:30 +0800 Subject: [PATCH] feat: support safely input private key --- Cargo.lock | 22 ++++++++++++++++++++++ Cargo.toml | 3 +++ errors/src/errors/cli/cli_errors.rs | 7 +++++++ leo/cli/commands/account.rs | 8 ++++---- 4 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4704d76bfa..942d1de70f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1442,6 +1442,7 @@ dependencies = [ "rand_chacha", "rand_core", "reqwest", + "rpassword", "rusty-hook", "self_update 0.39.0", "serde", @@ -2194,6 +2195,27 @@ dependencies = [ "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]] name = "rustc-demangle" version = "0.1.23" diff --git a/Cargo.toml b/Cargo.toml index 1b72c1fa2d..4975e925ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/errors/src/errors/cli/cli_errors.rs b/errors/src/errors/cli/cli_errors.rs index ca13d0ead5..27d885ebc7 100644 --- a/errors/src/errors/cli/cli_errors.rs +++ b/errors/src/errors/cli/cli_errors.rs @@ -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, + } ); diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index eea0ed6883..d3aa753a91 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -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; @@ -35,8 +35,6 @@ pub enum Account { }, /// Derive an Aleo account from a private key. Import { - /// Private key plaintext - private_key: PrivateKey, /// Write the private key to the .env file. #[clap(short = 'w', long)] write: bool, @@ -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)?;