Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit 8f4db26

Browse files
committed
Make --file optional for generate-node-key (#7043)
This pr makes the `--file` argument optional to `generate-node-key`. If the argument is not given, the secret node key will be printed to `stdout`. The public node key will always be printed to `stderr`.
1 parent 6deea30 commit 8f4db26

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

bin/utils/subkey/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ use sp_core::crypto::Ss58Codec;
3131
about = "Utility for generating and restoring with Substrate keys",
3232
)]
3333
pub enum Subkey {
34-
/// Generate a random node libp2p key, save it to file and print its peer ID
34+
/// Generate a random node libp2p key, save it to file or print it to stdout
35+
/// and print its peer ID to stderr.
3536
GenerateNodeKey(GenerateNodeKeyCmd),
3637

3738
/// Generate a random account

client/cli/src/commands/generate_node_key.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,31 @@ use libp2p::identity::{ed25519 as libp2p_ed25519, PublicKey};
2626
#[derive(Debug, StructOpt)]
2727
#[structopt(
2828
name = "generate-node-key",
29-
about = "Generate a random node libp2p key, save it to file and print its peer ID"
29+
about = "Generate a random node libp2p key, save it to \
30+
file or print it to stdout and print its peer ID to stderr"
3031
)]
3132
pub struct GenerateNodeKeyCmd {
3233
/// Name of file to save secret key to.
34+
///
35+
/// If not given, the secret key is printed to stdout.
3336
#[structopt(long)]
34-
file: PathBuf,
37+
file: Option<PathBuf>,
3538
}
3639

3740
impl GenerateNodeKeyCmd {
3841
/// Run the command
3942
pub fn run(&self) -> Result<(), Error> {
40-
let file = &self.file;
41-
4243
let keypair = libp2p_ed25519::Keypair::generate();
4344
let secret = keypair.secret();
4445
let peer_id = PublicKey::Ed25519(keypair.public()).into_peer_id();
46+
let secret_hex = hex::encode(secret.as_ref());
4547

46-
fs::write(file, hex::encode(secret.as_ref()))?;
48+
match &self.file {
49+
Some(file) => fs::write(file, secret_hex)?,
50+
None => print!("{}", secret_hex),
51+
}
4752

48-
println!("{}", peer_id);
53+
eprintln!("{}", peer_id);
4954

5055
Ok(())
5156
}

client/cli/src/commands/key.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ use super::{
2828
generate_node_key::GenerateNodeKeyCmd,
2929
};
3030

31-
/// key utilities for the cli.
31+
/// Key utilities for the cli.
3232
#[derive(Debug, StructOpt)]
3333
pub enum KeySubcommand {
34-
/// Generate a random node libp2p key, save it to file and print its peer ID
34+
/// Generate a random node libp2p key, save it to file or print it to stdout
35+
/// and print its peer ID to stderr.
3536
GenerateNodeKey(GenerateNodeKeyCmd),
3637

3738
/// Generate a random account

0 commit comments

Comments
 (0)