From df72ffdcf4c9a4425ba208309c6fe773cd1ede53 Mon Sep 17 00:00:00 2001 From: Hayden Bakkum Date: Tue, 11 Feb 2020 20:10:59 +1300 Subject: [PATCH 1/4] Add a sub command to generate a node key file in the format required by a substrate node --- bin/utils/subkey/Cargo.toml | 2 ++ bin/utils/subkey/src/main.rs | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/bin/utils/subkey/Cargo.toml b/bin/utils/subkey/Cargo.toml index 9b7db6699ffe2..a267d3846c6c2 100644 --- a/bin/utils/subkey/Cargo.toml +++ b/bin/utils/subkey/Cargo.toml @@ -28,6 +28,8 @@ derive_more = { version = "0.99.2" } sc-rpc = { version = "2.0.0", path = "../../../client/rpc" } jsonrpc-core-client = { version = "14.0.3", features = ["http"] } hyper = "0.12.35" +libp2p = "0.15.0" + [features] bench = [] diff --git a/bin/utils/subkey/src/main.rs b/bin/utils/subkey/src/main.rs index c2fd7e28c2548..505622cd007cd 100644 --- a/bin/utils/subkey/src/main.rs +++ b/bin/utils/subkey/src/main.rs @@ -23,6 +23,7 @@ use clap::{App, ArgMatches, SubCommand}; use codec::{Decode, Encode}; use hex_literal::hex; use itertools::Itertools; +use libp2p::identity::{ed25519 as libp2p_ed25519, PublicKey}; use node_primitives::{Balance, Hash, Index, AccountId, Signature}; use node_runtime::{BalancesCall, Call, Runtime, SignedPayload, UncheckedExtrinsic, VERSION}; use sp_core::{ @@ -31,7 +32,7 @@ use sp_core::{ }; use sp_runtime::{traits::{IdentifyAccount, Verify}, generic::Era}; use std::{ - convert::{TryInto, TryFrom}, io::{stdin, Read}, str::FromStr, path::PathBuf, fs, fmt, + convert::{TryInto, TryFrom}, io::{stdin, Read, Write}, str::FromStr, path::PathBuf, fs, fmt, }; mod rpc; @@ -181,6 +182,9 @@ fn get_app<'a, 'b>(usage: &'a str) -> App<'a, 'b> { 'The number of words in the phrase to generate. One of 12 \ (default), 15, 18, 21 and 24.' "), + SubCommand::with_name("generate-node-key") + .about("Generate a random node libp2p key, save it to file and print its peer ID") + .args_from_usage("[file] 'Name of file to save secret key to'"), SubCommand::with_name("inspect") .about("Gets a public key and a SS58 address from the provided Secret URI") .args_from_usage("[uri] 'A Key URI to be inspected. May be a secret seed, \ @@ -334,6 +338,22 @@ where let mnemonic = generate_mnemonic(matches)?; C::print_from_uri(mnemonic.phrase(), password, maybe_network); } + ("generate-node-key", Some(matches)) => { + let file = matches.value_of("file").ok_or(Error::Static("Output file name is required"))?; + + let keypair = libp2p_ed25519::Keypair::generate(); + let secret = keypair.secret(); + let peer_id = PublicKey::Ed25519(keypair.public()).into_peer_id(); + + let mut file = fs::OpenOptions::new() + .create_new(true) + .write(true) + .open(file)?; + + file.write_all(secret.as_ref())?; + + println!("{}", peer_id); + } ("inspect", Some(matches)) => { C::print_from_uri(&get_uri("uri", &matches)?, password, maybe_network); } From 6b72350f42e9faeaad8433525f697695406fd473 Mon Sep 17 00:00:00 2001 From: Hayden Bakkum Date: Tue, 11 Feb 2020 20:43:46 +1300 Subject: [PATCH 2/4] Update lock file --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index 76d3d004c5a9c..ca20745187ccd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7445,6 +7445,7 @@ dependencies = [ "hyper 0.12.35", "itertools", "jsonrpc-core-client", + "libp2p", "node-primitives", "node-runtime", "pallet-balances", From 6bdba8dc3cc77c8fa9a0f4f0b5d0979ec11a953d Mon Sep 17 00:00:00 2001 From: Hayden Bakkum <53467950+hbakkum-dotstar@users.noreply.github.com> Date: Tue, 11 Feb 2020 22:14:30 +1300 Subject: [PATCH 3/4] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Bastian Köcher --- bin/utils/subkey/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/utils/subkey/Cargo.toml b/bin/utils/subkey/Cargo.toml index a267d3846c6c2..2ef07e2a5278a 100644 --- a/bin/utils/subkey/Cargo.toml +++ b/bin/utils/subkey/Cargo.toml @@ -30,6 +30,5 @@ jsonrpc-core-client = { version = "14.0.3", features = ["http"] } hyper = "0.12.35" libp2p = "0.15.0" - [features] bench = [] From 6be4f752bf9f29dfc7e3375ecdad84946b4be34f Mon Sep 17 00:00:00 2001 From: Hayden Bakkum Date: Tue, 11 Feb 2020 22:32:09 +1300 Subject: [PATCH 4/4] Updates as per code review --- bin/utils/subkey/src/main.rs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/bin/utils/subkey/src/main.rs b/bin/utils/subkey/src/main.rs index 505622cd007cd..d02b573dd6b7f 100644 --- a/bin/utils/subkey/src/main.rs +++ b/bin/utils/subkey/src/main.rs @@ -32,7 +32,7 @@ use sp_core::{ }; use sp_runtime::{traits::{IdentifyAccount, Verify}, generic::Era}; use std::{ - convert::{TryInto, TryFrom}, io::{stdin, Read, Write}, str::FromStr, path::PathBuf, fs, fmt, + convert::{TryInto, TryFrom}, io::{stdin, Read}, str::FromStr, path::PathBuf, fs, fmt, }; mod rpc; @@ -345,12 +345,7 @@ where let secret = keypair.secret(); let peer_id = PublicKey::Ed25519(keypair.public()).into_peer_id(); - let mut file = fs::OpenOptions::new() - .create_new(true) - .write(true) - .open(file)?; - - file.write_all(secret.as_ref())?; + fs::write(file, secret.as_ref())?; println!("{}", peer_id); }