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

Make --file optional for generate-node-key #7043

Merged
merged 1 commit into from
Sep 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bin/utils/subkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ use sp_core::crypto::Ss58Codec;
about = "Utility for generating and restoring with Substrate keys",
)]
pub enum Subkey {
/// Generate a random node libp2p key, save it to file and print its peer ID
/// Generate a random node libp2p key, save it to file or print it to stdout
/// and print its peer ID to stderr.
GenerateNodeKey(GenerateNodeKeyCmd),

/// Generate a random account
Expand Down
17 changes: 11 additions & 6 deletions client/cli/src/commands/generate_node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,31 @@ use libp2p::identity::{ed25519 as libp2p_ed25519, PublicKey};
#[derive(Debug, StructOpt)]
#[structopt(
name = "generate-node-key",
about = "Generate a random node libp2p key, save it to file and print its peer ID"
about = "Generate a random node libp2p key, save it to \
file or print it to stdout and print its peer ID to stderr"
)]
pub struct GenerateNodeKeyCmd {
/// Name of file to save secret key to.
///
/// If not given, the secret key is printed to stdout.
#[structopt(long)]
file: PathBuf,
file: Option<PathBuf>,
}

impl GenerateNodeKeyCmd {
/// Run the command
pub fn run(&self) -> Result<(), Error> {
let file = &self.file;

let keypair = libp2p_ed25519::Keypair::generate();
let secret = keypair.secret();
let peer_id = PublicKey::Ed25519(keypair.public()).into_peer_id();
let secret_hex = hex::encode(secret.as_ref());

fs::write(file, hex::encode(secret.as_ref()))?;
match &self.file {
Some(file) => fs::write(file, secret_hex)?,
None => print!("{}", secret_hex),
}

println!("{}", peer_id);
eprintln!("{}", peer_id);

Ok(())
}
Expand Down
5 changes: 3 additions & 2 deletions client/cli/src/commands/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ use super::{
generate_node_key::GenerateNodeKeyCmd,
};

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

/// Generate a random account
Expand Down