Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 5 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
6 changes: 3 additions & 3 deletions bin/utils/subkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use sc_cli::{
version
)]
pub enum Subkey {
/// Generate a random node libp2p key, save it to file or print it to stdout
/// and print its peer ID to stderr.
/// Generate a random node key, write it to a file or stdout and write the
/// corresponding peer-id to stderr
GenerateNodeKey(GenerateNodeKeyCmd),

/// Generate a random account
Expand All @@ -40,7 +40,7 @@ pub enum Subkey {
/// Gets a public key and a SS58 address from the provided Secret URI
Inspect(InspectKeyCmd),

/// Print the peer ID corresponding to the node key in the given file
/// Load a node key from a file or stdin and print the corresponding peer-id
InspectNodeKey(InspectNodeKeyCmd),

/// Sign a message, with a given (secret) key.
Expand Down
32 changes: 24 additions & 8 deletions client/cli/src/commands/generate_node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,53 @@
use crate::Error;
use clap::Parser;
use libp2p::identity::{ed25519 as libp2p_ed25519, PublicKey};
use std::{fs, path::PathBuf};
use std::{
fs,
io::{self, Write},
path::PathBuf,
};

/// The `generate-node-key` command
#[derive(Debug, Parser)]
#[clap(
name = "generate-node-key",
about = "Generate a random node libp2p key, save it to \
file or print it to stdout and print its peer ID to stderr"
about = "Generate a random node key, write it to a file or stdout \
and write the corresponding 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.
#[clap(long)]
file: Option<PathBuf>,

/// The output is in raw binary format.
///
/// If not given, the output is written as an hex encoded string.
#[clap(long)]
bin: bool,
}

impl GenerateNodeKeyCmd {
/// Run the command
pub fn run(&self) -> Result<(), Error> {
let keypair = libp2p_ed25519::Keypair::generate();

let secret = keypair.secret();
let peer_id = PublicKey::Ed25519(keypair.public()).to_peer_id();
let secret_hex = hex::encode(secret.as_ref());

let file_data = if self.bin {
secret.as_ref().to_owned()
} else {
hex::encode(secret.as_ref()).into_bytes()
};

match &self.file {
Some(file) => fs::write(file, secret_hex)?,
None => print!("{}", secret_hex),
Some(file) => fs::write(file, file_data)?,
None => io::stdout().lock().write_all(&file_data)?,
}

eprintln!("{}", peer_id);
let peer_id = PublicKey::Ed25519(keypair.public()).to_peer_id();
eprintln!("Peer-ID: {}", peer_id);

Ok(())
}
Expand Down
52 changes: 40 additions & 12 deletions client/cli/src/commands/inspect_node_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,67 @@

//! Implementation of the `inspect-node-key` subcommand
use crate::{Error, NetworkSchemeFlag};
use crate::Error;
use clap::Parser;
use libp2p::identity::{ed25519, PublicKey};
use std::{fs, path::PathBuf};
use std::{
fs,
io::{self, Read},
path::PathBuf,
};

/// The `inspect-node-key` command
#[derive(Debug, Parser)]
#[clap(
name = "inspect-node-key",
about = "Print the peer ID corresponding to the node key in the given file."
about = "Load a node key from a file or stdin and print the corresponding peer-id."
)]
pub struct InspectNodeKeyCmd {
/// Name of file to read the secret key from.
///
/// If not given, the secret key is read from stdin (up to EOF).
#[clap(long)]
file: PathBuf,
file: Option<PathBuf>,

#[allow(missing_docs)]
#[clap(flatten)]
pub network_scheme: NetworkSchemeFlag,
/// The input is in raw binary format.
///
/// If not given, the input is read as an hex encoded string.
#[clap(long)]
bin: bool,
}

impl InspectNodeKeyCmd {
/// runs the command
pub fn run(&self) -> Result<(), Error> {
let mut file_content =
hex::decode(fs::read(&self.file)?).map_err(|_| "failed to decode secret as hex")?;
let mut file_data = match &self.file {
Some(file) => fs::read(&file)?,
None => {
let mut buf = Vec::with_capacity(64);
io::stdin().lock().read_to_end(&mut buf)?;
buf
},
};

if !self.bin {
// With hex input, give to the user a bit of tolerance about whitespaces
let is_not_ascii_whitespace = |c: &u8| !c.is_ascii_whitespace();
let beg = file_data.iter().position(is_not_ascii_whitespace).unwrap_or_default();
let end = file_data
.iter()
.rposition(is_not_ascii_whitespace)
.map(|off| off + 1)
.unwrap_or_default();
file_data =
hex::decode(&file_data[beg..end]).map_err(|_| "failed to decode secret as hex")?;
}

let secret =
ed25519::SecretKey::from_bytes(&mut file_content).map_err(|_| "Bad node key file")?;
ed25519::SecretKey::from_bytes(&mut file_data).map_err(|_| "Bad node key file")?;

let keypair = ed25519::Keypair::from(secret);
let peer_id = PublicKey::Ed25519(keypair.public()).to_peer_id();

println!("{}", peer_id);
let peer_id = PublicKey::Ed25519(keypair.public()).to_peer_id();
println!("Peer-ID: {}", peer_id);

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions client/cli/src/commands/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use crate::{Error, SubstrateCli};
/// Key utilities for the cli.
#[derive(Debug, clap::Subcommand)]
pub enum KeySubcommand {
/// Generate a random node libp2p key, save it to file or print it to stdout
/// and print its peer ID to stderr.
/// Generate a random node key, write it to a file or stdout and write the
/// corresponding peer-id to stderr
GenerateNodeKey(GenerateNodeKeyCmd),

/// Generate a random account
Expand All @@ -36,7 +36,7 @@ pub enum KeySubcommand {
/// Gets a public key and a SS58 address from the provided Secret URI
Inspect(InspectKeyCmd),

/// Print the peer ID corresponding to the node key in the given file
/// Load a node key from a file or stdin and print the corresponding peer-id
InspectNodeKey(InspectNodeKeyCmd),

/// Insert a key to the keystore of a node.
Expand Down