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 1/7] 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<CurrentNetwork>, /// 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)?; From 0c1948908c6506502e6f9ff5faafa0206dab0db4 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 17 Jan 2024 15:30:23 +0800 Subject: [PATCH 2/7] refactor: use the --discreet flag for the password prompt --- leo/cli/commands/account.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index 482547c65d..d31f092d6e 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -40,6 +40,8 @@ pub enum Account { }, /// Derive an Aleo account from a private key. Import { + /// Private key plaintext + private_key: Option<PrivateKey<CurrentNetwork>>, /// Write the private key to the .env file. #[clap(short = 'w', long)] write: bool, @@ -83,15 +85,20 @@ impl Command for Account { write_to_env_file(private_key, &ctx)?; } } - Account::Import { write, discreet } => { - 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)?; + 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(); + FromStr::from_str(&private_key_input).map_err(CliError::failed_to_execute_account)? + } + false => private_key.expect("PRIVATE_KEY shouldn't be empty when --discreet is false"), + }; // 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)?; } } } From ba6b9c4e411b0c6d7eb5f762fe00eb8b0d801d0d Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 17 Jan 2024 15:30:48 +0800 Subject: [PATCH 3/7] fix: add `CliError::failed_to_execute_account` error --- Cargo.lock | 2 +- Cargo.toml | 2 +- compiler/ast/Cargo.toml | 2 +- compiler/compiler/Cargo.toml | 2 +- compiler/parser/Cargo.toml | 2 +- compiler/passes/Cargo.toml | 2 +- errors/Cargo.toml | 2 +- leo/package/Cargo.toml | 2 +- tests/test-framework/Cargo.toml | 2 +- utils/retriever/Cargo.toml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8d437d323b..25889a9f27 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1475,7 +1475,7 @@ dependencies = [ [[package]] name = "leo-errors" -version = "1.10.0" +version = "1.10.1" dependencies = [ "anyhow", "backtrace", diff --git a/Cargo.toml b/Cargo.toml index 84aff9adc8..9a51f04aa5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,7 +72,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "./errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.leo-package] path = "./leo/package" diff --git a/compiler/ast/Cargo.toml b/compiler/ast/Cargo.toml index 4947bcc935..57f6f252a7 100644 --- a/compiler/ast/Cargo.toml +++ b/compiler/ast/Cargo.toml @@ -23,7 +23,7 @@ workspace = true [dependencies.leo-errors] path = "../../errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.leo-span] path = "../span" diff --git a/compiler/compiler/Cargo.toml b/compiler/compiler/Cargo.toml index fa8d357d69..246a8a21d9 100644 --- a/compiler/compiler/Cargo.toml +++ b/compiler/compiler/Cargo.toml @@ -24,7 +24,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.leo-passes] path = "../passes" diff --git a/compiler/parser/Cargo.toml b/compiler/parser/Cargo.toml index 44ea479e6b..abecd0fff4 100644 --- a/compiler/parser/Cargo.toml +++ b/compiler/parser/Cargo.toml @@ -24,7 +24,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.leo-span] path = "../span" diff --git a/compiler/passes/Cargo.toml b/compiler/passes/Cargo.toml index 7ed741b106..55bf1ca051 100644 --- a/compiler/passes/Cargo.toml +++ b/compiler/passes/Cargo.toml @@ -30,7 +30,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.leo-parser] path = "../parser" diff --git a/errors/Cargo.toml b/errors/Cargo.toml index c752209ab3..cbed3b02e0 100644 --- a/errors/Cargo.toml +++ b/errors/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leo-errors" -version = "1.10.0" +version = "1.10.1" authors = [ "The Aleo Team <hello@aleo.org>" ] description = "Errors for the Leo programming language" homepage = "https://aleo.org" diff --git a/leo/package/Cargo.toml b/leo/package/Cargo.toml index d62c03f861..f2f5770be8 100644 --- a/leo/package/Cargo.toml +++ b/leo/package/Cargo.toml @@ -27,7 +27,7 @@ workspace = true [dependencies.leo-errors] path = "../../errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.indexmap] version = "1.9" diff --git a/tests/test-framework/Cargo.toml b/tests/test-framework/Cargo.toml index 51a67cd713..d2804eb752 100644 --- a/tests/test-framework/Cargo.toml +++ b/tests/test-framework/Cargo.toml @@ -24,7 +24,7 @@ harness = false [dependencies.leo-errors] path = "../../errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.backtrace] version = "0.3.68" diff --git a/utils/retriever/Cargo.toml b/utils/retriever/Cargo.toml index 2b9aebfce1..1c915b4fca 100644 --- a/utils/retriever/Cargo.toml +++ b/utils/retriever/Cargo.toml @@ -14,7 +14,7 @@ version = "1.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.0" +version = "=1.10.1" [dependencies.indexmap] version = "1.9" From a0e38b9f5e4ffe5afdc5da7f418b0bc34ed7dbad Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 17 Jan 2024 16:15:13 +0800 Subject: [PATCH 4/7] refactor: error display --- errors/src/errors/cli/cli_errors.rs | 7 +++++++ leo/cli/commands/account.rs | 12 ++++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/errors/src/errors/cli/cli_errors.rs b/errors/src/errors/cli/cli_errors.rs index 27d885ebc7..f1c23ab558 100644 --- a/errors/src/errors/cli/cli_errors.rs +++ b/errors/src/errors/cli/cli_errors.rs @@ -179,6 +179,13 @@ 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), diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index d31f092d6e..103aeabff4 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -89,9 +89,17 @@ impl Command for Account { let priv_key = match discreet { true => { let private_key_input = rpassword::prompt_password("Please enter your private key: ").unwrap(); - FromStr::from_str(&private_key_input).map_err(CliError::failed_to_execute_account)? + FromStr::from_str(&private_key_input).map_err(CliError::failed_to_parse_private_key)? } - false => private_key.expect("PRIVATE_KEY shouldn't be empty when --discreet is false"), + 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(priv_key, discreet)?; From 570dc936c8ec1aebd901feca4b96921556dee845 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 17 Jan 2024 16:15:56 +0800 Subject: [PATCH 5/7] lint --- leo/cli/commands/account.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index 103aeabff4..52a6372a16 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -97,7 +97,7 @@ impl Command for Account { return Err(CliError::failed_to_execute_account( "PRIVATE_KEY shouldn't be empty when --discreet is false", ) - .into()) + .into()); } }, }; From b92f6cec8ce808338b30fc8d990bcc75380909e6 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 24 Jan 2024 10:38:34 +0800 Subject: [PATCH 6/7] Revert "fix: add `CliError::failed_to_execute_account` error" This reverts commit ba6b9c4e411b0c6d7eb5f762fe00eb8b0d801d0d. --- Cargo.lock | 2 +- Cargo.toml | 2 +- compiler/ast/Cargo.toml | 2 +- compiler/compiler/Cargo.toml | 2 +- compiler/parser/Cargo.toml | 2 +- compiler/passes/Cargo.toml | 2 +- errors/Cargo.toml | 2 +- leo/package/Cargo.toml | 2 +- tests/test-framework/Cargo.toml | 2 +- utils/retriever/Cargo.toml | 2 +- 10 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 25889a9f27..8d437d323b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1475,7 +1475,7 @@ dependencies = [ [[package]] name = "leo-errors" -version = "1.10.1" +version = "1.10.0" dependencies = [ "anyhow", "backtrace", diff --git a/Cargo.toml b/Cargo.toml index 9a51f04aa5..84aff9adc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -72,7 +72,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "./errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.leo-package] path = "./leo/package" diff --git a/compiler/ast/Cargo.toml b/compiler/ast/Cargo.toml index 57f6f252a7..4947bcc935 100644 --- a/compiler/ast/Cargo.toml +++ b/compiler/ast/Cargo.toml @@ -23,7 +23,7 @@ workspace = true [dependencies.leo-errors] path = "../../errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.leo-span] path = "../span" diff --git a/compiler/compiler/Cargo.toml b/compiler/compiler/Cargo.toml index 246a8a21d9..fa8d357d69 100644 --- a/compiler/compiler/Cargo.toml +++ b/compiler/compiler/Cargo.toml @@ -24,7 +24,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.leo-passes] path = "../passes" diff --git a/compiler/parser/Cargo.toml b/compiler/parser/Cargo.toml index abecd0fff4..44ea479e6b 100644 --- a/compiler/parser/Cargo.toml +++ b/compiler/parser/Cargo.toml @@ -24,7 +24,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.leo-span] path = "../span" diff --git a/compiler/passes/Cargo.toml b/compiler/passes/Cargo.toml index 55bf1ca051..7ed741b106 100644 --- a/compiler/passes/Cargo.toml +++ b/compiler/passes/Cargo.toml @@ -30,7 +30,7 @@ version = "=1.10.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.leo-parser] path = "../parser" diff --git a/errors/Cargo.toml b/errors/Cargo.toml index cbed3b02e0..c752209ab3 100644 --- a/errors/Cargo.toml +++ b/errors/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "leo-errors" -version = "1.10.1" +version = "1.10.0" authors = [ "The Aleo Team <hello@aleo.org>" ] description = "Errors for the Leo programming language" homepage = "https://aleo.org" diff --git a/leo/package/Cargo.toml b/leo/package/Cargo.toml index f2f5770be8..d62c03f861 100644 --- a/leo/package/Cargo.toml +++ b/leo/package/Cargo.toml @@ -27,7 +27,7 @@ workspace = true [dependencies.leo-errors] path = "../../errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.indexmap] version = "1.9" diff --git a/tests/test-framework/Cargo.toml b/tests/test-framework/Cargo.toml index d2804eb752..51a67cd713 100644 --- a/tests/test-framework/Cargo.toml +++ b/tests/test-framework/Cargo.toml @@ -24,7 +24,7 @@ harness = false [dependencies.leo-errors] path = "../../errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.backtrace] version = "0.3.68" diff --git a/utils/retriever/Cargo.toml b/utils/retriever/Cargo.toml index 1c915b4fca..2b9aebfce1 100644 --- a/utils/retriever/Cargo.toml +++ b/utils/retriever/Cargo.toml @@ -14,7 +14,7 @@ version = "1.0" [dependencies.leo-errors] path = "../../errors" -version = "=1.10.1" +version = "=1.10.0" [dependencies.indexmap] version = "1.9" From 9297c442d34867a50c32f7757c5949e8635469aa Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Mon, 12 Feb 2024 15:54:39 +0800 Subject: [PATCH 7/7] fix: ci run --- errors/src/errors/cli/cli_errors.rs | 1 + leo/cli/commands/account.rs | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/errors/src/errors/cli/cli_errors.rs b/errors/src/errors/cli/cli_errors.rs index 3aa16e2490..27923bb65e 100644 --- a/errors/src/errors/cli/cli_errors.rs +++ b/errors/src/errors/cli/cli_errors.rs @@ -207,6 +207,7 @@ create_messages!( args: (error: impl Display), msg: format!("Failed to execute the `account` command.\nSnarkVM Error: {error}"), help: None, + } @backtraced failed_to_read_environment_private_key { diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index 2346062540..471e21bf98 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -20,7 +20,7 @@ use leo_package::root::Env; use snarkvm::{ cli::dotenv_private_key, console::program::{Signature, ToFields, Value}, - prelude::{Address, FromStr, PrivateKey, ViewKey}, + prelude::{Address, PrivateKey, ViewKey}, }; use crossterm::ExecutableCommand; @@ -131,7 +131,8 @@ impl Command for Account { let priv_key = match discreet { true => { let private_key_input = rpassword::prompt_password("Please enter your private key: ").unwrap(); - FromStr::from_str(&private_key_input).map_err(CliError::failed_to_parse_private_key)? + 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,