-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improvements for keygen cli and crates
Import improvements from FuelLabs/sway#5153. Once this PR is merged and a new release is created sway will reference this crate instead of having its own copy.
- Loading branch information
Showing
7 changed files
with
248 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,114 @@ | ||
//! A simple keygen cli utility tool for configuring fuel-core | ||
|
||
use atty::Stream; | ||
use clap::Parser; | ||
use fuel_core_keygen::keygen; | ||
use fuel_core_keygen::{ | ||
new_key, | ||
parse_secret, | ||
KeyType, | ||
}; | ||
use std::io::{ | ||
stdin, | ||
stdout, | ||
Read, | ||
Write, | ||
}; | ||
use termion::screen::IntoAlternateScreen; | ||
|
||
/// Parse a secret key to view the associated public key | ||
#[derive(Debug, clap::Args)] | ||
#[clap(author, version, about)] | ||
pub struct ParseSecret { | ||
/// A private key in hex format | ||
secret: String, | ||
/// Print the JSON in pretty format | ||
#[clap(long = "pretty", short = 'p')] | ||
pub pretty: bool, | ||
/// Key type to generate. It can either be `block-production` or `peering`. | ||
#[clap( | ||
long = "key-type", | ||
short = 'k', | ||
value_enum, | ||
default_value = <KeyType as std::convert::Into<&'static str>>::into(KeyType::BlockProduction), | ||
)] | ||
pub key_type: KeyType, | ||
} | ||
|
||
/// Generate a random new secret & public key in the format expected by fuel-core | ||
#[derive(Debug, clap::Args)] | ||
#[clap(author, version, about)] | ||
pub struct NewKey { | ||
/// Print the JSON in pretty format | ||
#[clap(long = "pretty", short = 'p')] | ||
pub pretty: bool, | ||
/// Key type to generate. It can either be `block-production` or `peering`. | ||
#[clap( | ||
long = "key-type", | ||
short = 'k', | ||
value_enum, | ||
default_value = <KeyType as std::convert::Into<&'static str>>::into(KeyType::BlockProduction), | ||
)] | ||
pub key_type: KeyType, | ||
} | ||
|
||
/// Key management utilities for configuring fuel-core | ||
#[derive(Debug, Parser)] | ||
pub(crate) enum Command { | ||
New(keygen::NewKey), | ||
Parse(keygen::ParseSecret), | ||
New(NewKey), | ||
Parse(ParseSecret), | ||
} | ||
|
||
impl Command { | ||
pub(crate) fn exec(&self) -> anyhow::Result<()> { | ||
pub(crate) fn exec(&self) -> anyhow::Result<(serde_json::Value, bool)> { | ||
match self { | ||
Command::New(cmd) => cmd.exec(), | ||
Command::Parse(cmd) => cmd.exec(), | ||
Command::New(cmd) => { | ||
Ok((serde_json::to_value(new_key(cmd.key_type)?)?, cmd.pretty)) | ||
} | ||
Command::Parse(cmd) => Ok(( | ||
serde_json::to_value(parse_secret(cmd.key_type, &cmd.secret)?)?, | ||
cmd.pretty, | ||
)), | ||
} | ||
} | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let cmd = Command::parse(); | ||
cmd.exec() | ||
let (result, is_pretty) = cmd.exec()?; | ||
print_value(result, is_pretty) | ||
} | ||
|
||
fn wait_for_keypress() { | ||
let mut single_key = [0u8]; | ||
stdin().read_exact(&mut single_key).unwrap(); | ||
} | ||
|
||
fn display_string_discreetly( | ||
discreet_string: &str, | ||
continue_message: &str, | ||
) -> anyhow::Result<()> { | ||
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(()) | ||
} | ||
|
||
fn print_value(output: serde_json::Value, pretty: bool) -> anyhow::Result<()> { | ||
let output = if pretty { | ||
serde_json::to_string_pretty(&output) | ||
} else { | ||
serde_json::to_string(&output) | ||
} | ||
.map_err(anyhow::Error::msg); | ||
|
||
let _ = display_string_discreetly( | ||
&output?, | ||
"### Do not share or lose this private key! Press any key to complete. ###", | ||
); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.