From b59d91db06278338bbef3f7569994ff50fe8c31d Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Tue, 28 Nov 2023 17:00:20 +0800 Subject: [PATCH 1/7] feat: support display private key discreetly --- Cargo.lock | 38 ++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 +++ leo/cli/commands/account.rs | 27 ++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4704d76bfa..9a0a28264e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1448,6 +1448,7 @@ dependencies = [ "serde_json", "snarkvm", "sys-info", + "termion", "test_dir", "toml 0.8.8", "tracing", @@ -1549,6 +1550,17 @@ dependencies = [ "redox_syscall", ] +[[package]] +name = "libredox" +version = "0.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3af92c55d7d839293953fcd0fda5ecfe93297cfde6ffbdec13b41d99c0ba6607" +dependencies = [ + "bitflags 2.4.1", + "libc", + "redox_syscall", +] + [[package]] name = "libz-sys" version = "1.1.12" @@ -1757,6 +1769,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" +[[package]] +name = "numtoa" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" + [[package]] name = "object" version = "0.32.1" @@ -2101,6 +2119,12 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "redox_termios" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20145670ba436b55d91fc92d25e71160fbfbdd57831631c8d7d36377a476f1cb" + [[package]] name = "redox_users" version = "0.4.4" @@ -2108,7 +2132,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" dependencies = [ "getrandom", - "libredox", + "libredox 0.0.1", "thiserror", ] @@ -3522,6 +3546,18 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "termion" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4648c7def6f2043b2568617b9f9b75eae88ca185dbc1f1fda30e95a85d49d7d" +dependencies = [ + "libc", + "libredox 0.0.2", + "numtoa", + "redox_termios", +] + [[package]] name = "termtree" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index 1b72c1fa2d..c594e2bbd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -157,6 +157,9 @@ features = [ "fmt" ] [dependencies.zip] version = "^0.6" +[dependencies.termion] +version = "2.0.3" + [target."cfg(windows)".dependencies.ansi_term] version = "0.12.1" diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index eea0ed6883..0e196c0d6f 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -20,6 +20,7 @@ use snarkvm::prelude::{Address, PrivateKey, ViewKey}; use rand::SeedableRng; use rand_chacha::ChaChaRng; +use std::io::{Read, Write}; /// Commands to manage Aleo accounts. #[derive(Parser, Debug)] @@ -98,9 +99,12 @@ fn print_keys(private_key: PrivateKey) -> Result<()> { let view_key = ViewKey::try_from(&private_key)?; let address = Address::::try_from(&view_key)?; + display_string_discreetly( + &private_key.to_string(), + "### Do not share or lose this private key! Press any key to complete. ###", + )?; println!( - "\n {:>12} {private_key}\n {:>12} {view_key}\n {:>12} {address}\n", - "Private Key".cyan().bold(), + "\n {:>12} {view_key}\n {:>12} {address}\n", "View Key".cyan().bold(), "Address".cyan().bold(), ); @@ -115,3 +119,22 @@ fn write_to_env_file(private_key: PrivateKey, ctx: &Context) -> tracing::info!("✅ Private Key written to {}", program_dir.join(".env").display()); Ok(()) } + +/// Print the string to an alternate screen, so that the string won't been printed to the terminal. +fn display_string_discreetly( + discreet_string: &str, + continue_message: &str, +) -> Result<()> { + use termion::screen::IntoAlternateScreen; + let mut screen = std::io::stdout().into_alternate_screen().unwrap(); + writeln!(screen, "{discreet_string}").unwrap(); + screen.flush().unwrap(); + println!("\n{continue_message}"); + wait_for_keypress(); + Ok(()) +} + +fn wait_for_keypress() { + let mut single_key = [0u8]; + std::io::stdin().read_exact(&mut single_key).unwrap(); +} \ No newline at end of file From 48765574068c8c7f24acf8b858bb636821923526 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Tue, 28 Nov 2023 18:58:40 +0800 Subject: [PATCH 2/7] fix fmt type --- leo/cli/commands/account.rs | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index 0e196c0d6f..61bc77e730 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -100,14 +100,10 @@ fn print_keys(private_key: PrivateKey) -> Result<()> { let address = Address::::try_from(&view_key)?; display_string_discreetly( - &private_key.to_string(), + &private_key.to_string(), "### Do not share or lose this private key! Press any key to complete. ###", )?; - println!( - "\n {:>12} {view_key}\n {:>12} {address}\n", - "View Key".cyan().bold(), - "Address".cyan().bold(), - ); + println!("\n {:>12} {view_key}\n {:>12} {address}\n", "View Key".cyan().bold(), "Address".cyan().bold(),); Ok(()) } @@ -121,10 +117,7 @@ fn write_to_env_file(private_key: PrivateKey, ctx: &Context) -> } /// Print the string to an alternate screen, so that the string won't been printed to the terminal. -fn display_string_discreetly( - discreet_string: &str, - continue_message: &str, -) -> Result<()> { +fn display_string_discreetly(discreet_string: &str, continue_message: &str) -> Result<()> { use termion::screen::IntoAlternateScreen; let mut screen = std::io::stdout().into_alternate_screen().unwrap(); writeln!(screen, "{discreet_string}").unwrap(); @@ -137,4 +130,4 @@ fn display_string_discreetly( fn wait_for_keypress() { let mut single_key = [0u8]; std::io::stdin().read_exact(&mut single_key).unwrap(); -} \ No newline at end of file +} From eb44550bc279343dd1be00fbbd7f667fc75b4057 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 29 Nov 2023 10:23:32 +0800 Subject: [PATCH 3/7] fix: change to use crossterm to fix ci test in windows error --- Cargo.lock | 95 ++++++++++++++++++++++--------------- Cargo.toml | 4 +- leo/cli/commands/account.rs | 18 ++++--- 3 files changed, 72 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9a0a28264e..c87c5a9010 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -661,6 +661,31 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossterm" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" +dependencies = [ + "bitflags 2.4.1", + "crossterm_winapi", + "libc", + "mio", + "parking_lot", + "signal-hook", + "signal-hook-mio", + "winapi", +] + +[[package]] +name = "crossterm_winapi" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acdd7c62a3665c7f6830a51635d9ac9b23ed385797f70a83bb8bafe9c572ab2b" +dependencies = [ + "winapi", +] + [[package]] name = "crunchy" version = "0.2.2" @@ -1428,6 +1453,7 @@ dependencies = [ "color-backtrace", "colored", "console", + "crossterm", "dirs 5.0.1", "dotenvy", "indexmap 1.9.3", @@ -1448,7 +1474,6 @@ dependencies = [ "serde_json", "snarkvm", "sys-info", - "termion", "test_dir", "toml 0.8.8", "tracing", @@ -1550,17 +1575,6 @@ dependencies = [ "redox_syscall", ] -[[package]] -name = "libredox" -version = "0.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3af92c55d7d839293953fcd0fda5ecfe93297cfde6ffbdec13b41d99c0ba6607" -dependencies = [ - "bitflags 2.4.1", - "libc", - "redox_syscall", -] - [[package]] name = "libz-sys" version = "1.1.12" @@ -1654,6 +1668,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3dce281c5e46beae905d4de1870d8b1509a9142b62eedf18b443b011ca8343d0" dependencies = [ "libc", + "log", "wasi", "windows-sys 0.48.0", ] @@ -1769,12 +1784,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b246a0e5f20af87141b25c173cd1b609bd7779a4617d6ec582abaf90870f3" -[[package]] -name = "numtoa" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" - [[package]] name = "object" version = "0.32.1" @@ -2119,12 +2128,6 @@ dependencies = [ "bitflags 1.3.2", ] -[[package]] -name = "redox_termios" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "20145670ba436b55d91fc92d25e71160fbfbdd57831631c8d7d36377a476f1cb" - [[package]] name = "redox_users" version = "0.4.4" @@ -2132,7 +2135,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a18479200779601e498ada4e8c1e1f50e3ee19deb0259c25825a98b5603b2cb4" dependencies = [ "getrandom", - "libredox 0.0.1", + "libredox", "thiserror", ] @@ -2502,6 +2505,36 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "signal-hook" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" +dependencies = [ + "libc", + "signal-hook-registry", +] + +[[package]] +name = "signal-hook-mio" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" +dependencies = [ + "libc", + "mio", + "signal-hook", +] + +[[package]] +name = "signal-hook-registry" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8229b473baa5980ac72ef434c4415e70c4b5e71b423043adb4ba059f89c99a1" +dependencies = [ + "libc", +] + [[package]] name = "signature" version = "2.2.0" @@ -3546,18 +3579,6 @@ dependencies = [ "winapi-util", ] -[[package]] -name = "termion" -version = "2.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4648c7def6f2043b2568617b9f9b75eae88ca185dbc1f1fda30e95a85d49d7d" -dependencies = [ - "libc", - "libredox 0.0.2", - "numtoa", - "redox_termios", -] - [[package]] name = "termtree" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index c594e2bbd7..e90ff3b5bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -157,8 +157,8 @@ features = [ "fmt" ] [dependencies.zip] version = "^0.6" -[dependencies.termion] -version = "2.0.3" +[dependencies.crossterm] +version = "0.27.0" [target."cfg(windows)".dependencies.ansi_term] version = "0.12.1" diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index 61bc77e730..349f7b4f8e 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -15,12 +15,13 @@ // along with the Leo library. If not, see . use super::*; +use crossterm::ExecutableCommand; use leo_package::root::Env; use snarkvm::prelude::{Address, PrivateKey, ViewKey}; use rand::SeedableRng; use rand_chacha::ChaChaRng; -use std::io::{Read, Write}; +use std::io::{self, Read, Write}; /// Commands to manage Aleo accounts. #[derive(Parser, Debug)] @@ -118,12 +119,17 @@ fn write_to_env_file(private_key: PrivateKey, ctx: &Context) -> /// Print the string to an alternate screen, so that the string won't been printed to the terminal. fn display_string_discreetly(discreet_string: &str, continue_message: &str) -> Result<()> { - use termion::screen::IntoAlternateScreen; - let mut screen = std::io::stdout().into_alternate_screen().unwrap(); - writeln!(screen, "{discreet_string}").unwrap(); - screen.flush().unwrap(); - println!("\n{continue_message}"); + use crossterm::{ + style::Print, + terminal::{EnterAlternateScreen, LeaveAlternateScreen}, + }; + let mut stdout = io::stdout(); + stdout.execute(EnterAlternateScreen).unwrap(); + // print msg on the alternate screen + stdout.execute(Print(format!("{discreet_string}\n{continue_message}"))).unwrap(); + stdout.flush().unwrap(); wait_for_keypress(); + stdout.execute(LeaveAlternateScreen).unwrap(); Ok(()) } From 0ef8654fc884c3bd7d4ca6c37f2b98feea66fd64 Mon Sep 17 00:00:00 2001 From: Halimao <1065621723@qq.com> Date: Wed, 27 Dec 2023 21:15:05 +0800 Subject: [PATCH 4/7] refactor: add --discreetly flag make display discreet optional --- leo/cli/commands/account.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/leo/cli/commands/account.rs b/leo/cli/commands/account.rs index 349f7b4f8e..c2f79d5513 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -34,6 +34,9 @@ pub enum Account { /// Write the private key to the .env file. #[clap(short = 'w', long)] write: bool, + /// Print sensitive information(such as private key) discreetly to an alternate screen + #[clap(long)] + discreet: bool, }, /// Derive an Aleo account from a private key. Import { @@ -42,6 +45,9 @@ pub enum Account { /// Write the private key to the .env file. #[clap(short = 'w', long)] write: bool, + /// Print sensitive information(such as private key) discreetly to an alternate screen + #[clap(long)] + discreet: bool, }, } @@ -61,7 +67,7 @@ impl Command for Account { Self: Sized, { match self { - Account::New { seed, write } => { + Account::New { seed, write, discreet } => { // Sample a new Aleo account. let private_key = match seed { // Recover the field element deterministically. @@ -72,16 +78,16 @@ impl Command for Account { .map_err(CliError::failed_to_parse_seed)?; // Derive the view key and address and print to stdout. - print_keys(private_key)?; + print_keys(private_key, discreet)?; // Save key data to .env file. if write { write_to_env_file(private_key, &ctx)?; } } - Account::Import { private_key, write } => { + Account::Import { private_key, write, discreet } => { // Derive the view key and address and print to stdout. - print_keys(private_key)?; + print_keys(private_key, discreet)?; // Save key data to .env file. if write { @@ -96,10 +102,19 @@ impl Command for Account { // Helper functions // Print keys as a formatted string without log level. -fn print_keys(private_key: PrivateKey) -> Result<()> { +fn print_keys(private_key: PrivateKey, discreet: bool) -> Result<()> { let view_key = ViewKey::try_from(&private_key)?; let address = Address::::try_from(&view_key)?; + if !discreet { + println!( + "\n {:>12} {private_key}\n {:>12} {view_key}\n {:>12} {address}\n", + "Private Key".cyan().bold(), + "View Key".cyan().bold(), + "Address".cyan().bold(), + ); + return Ok(()); + } display_string_discreetly( &private_key.to_string(), "### Do not share or lose this private key! Press any key to complete. ###", From 36be9adb9cc8b3221d1a2d1b63e85bf84f4eb16e Mon Sep 17 00:00:00 2001 From: d0cd <23022326+d0cd@users.noreply.github.com> Date: Mon, 15 Jan 2024 19:48:34 -0800 Subject: [PATCH 5/7] Update leo/cli/commands/account.rs Signed-off-by: d0cd <23022326+d0cd@users.noreply.github.com> --- 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 c2f79d5513..dfbae72bbb 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -15,10 +15,10 @@ // along with the Leo library. If not, see . use super::*; -use crossterm::ExecutableCommand; use leo_package::root::Env; use snarkvm::prelude::{Address, PrivateKey, ViewKey}; +use crossterm::ExecutableCommand; use rand::SeedableRng; use rand_chacha::ChaChaRng; use std::io::{self, Read, Write}; From 91daac9b0cd55fc6cab71395f6b21f688a7f92ab Mon Sep 17 00:00:00 2001 From: d0cd <23022326+d0cd@users.noreply.github.com> Date: Mon, 15 Jan 2024 19:48:41 -0800 Subject: [PATCH 6/7] Update leo/cli/commands/account.rs Signed-off-by: d0cd <23022326+d0cd@users.noreply.github.com> --- 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 dfbae72bbb..8e36555731 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -34,7 +34,7 @@ pub enum Account { /// Write the private key to the .env file. #[clap(short = 'w', long)] write: bool, - /// Print sensitive information(such as private key) discreetly to an alternate screen + /// Print sensitive information (such as private key) discreetly to an alternate screen #[clap(long)] discreet: bool, }, From f354221e3ae387197f492017d524686ca5d5fbbf Mon Sep 17 00:00:00 2001 From: d0cd <23022326+d0cd@users.noreply.github.com> Date: Mon, 15 Jan 2024 19:48:52 -0800 Subject: [PATCH 7/7] Update leo/cli/commands/account.rs Signed-off-by: d0cd <23022326+d0cd@users.noreply.github.com> --- 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 8e36555731..0666bbe2fe 100644 --- a/leo/cli/commands/account.rs +++ b/leo/cli/commands/account.rs @@ -45,7 +45,7 @@ pub enum Account { /// Write the private key to the .env file. #[clap(short = 'w', long)] write: bool, - /// Print sensitive information(such as private key) discreetly to an alternate screen + /// Print sensitive information (such as private key) discreetly to an alternate screen #[clap(long)] discreet: bool, },