Skip to content

Commit

Permalink
Print secret message to tty
Browse files Browse the repository at this point in the history
If stdout is not a tty (i.e it is a pipe to another process or it writes
to another file) a normal message will be printed.
  • Loading branch information
cr-fuel committed Oct 19, 2023
1 parent f809ead commit de80a12
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/keygen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description = "Create to create command line utilities for fuel-core key managem

[dependencies]
anyhow = { workspace = true }
atty = "0.2.14"
clap = { workspace = true, features = ["derive", "env"] }
fuel-core-types = { workspace = true, features = ["serde", "random"] }
libp2p-identity = { version = "0.2.4", features = ["secp256k1", "peerid"] }
Expand Down
21 changes: 14 additions & 7 deletions crates/keygen/src/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::{
BLOCK_PRODUCTION,
P2P,
};
use atty::Stream;
use clap::ValueEnum;
use fuel_core_types::{
fuel_crypto::{
Expand All @@ -21,12 +22,15 @@ use libp2p_identity::{
use serde_json::json;
use std::{
io::{
stdin,
stdout,
Read,
Write,
},
ops::Deref,
str::FromStr,
};
use termion::screen::IntoAlternateScreen;

/// Generate a random new secret & public key in the format expected by fuel-core
#[derive(Debug, clap::Args)]
Expand Down Expand Up @@ -132,19 +136,22 @@ impl ParseSecret {

fn wait_for_keypress() {
let mut single_key = [0u8];
std::io::stdin().read_exact(&mut single_key).unwrap();
stdin().read_exact(&mut single_key).unwrap();
}

fn display_string_discreetly(
discreet_string: &str,
continue_message: &str,
) -> anyhow::Result<()> {
use termion::screen::IntoAlternateScreen;
let mut screen = std::io::stdout().into_alternate_screen()?;
writeln!(screen, "{discreet_string}")?;
screen.flush()?;
println!("{continue_message}");
wait_for_keypress();
if atty::is(Stream::Stdout) {
let mut screen = stdout().into_alternate_screen()?;
writeln!(screen, "{discreet_string}")?;
screen.flush()?;
println!("{continue_message}");
wait_for_keypress();
} else {
println!("{discreet_string}");
}
Ok(())
}

Expand Down

0 comments on commit de80a12

Please sign in to comment.