From b34df420f881943ef8bf5aa5bf9ed8c32cd0be66 Mon Sep 17 00:00:00 2001
From: Hayden Bakkum <53467950+hbakkum-dotstar@users.noreply.github.com>
Date: Wed, 12 Feb 2020 09:17:08 +1300
Subject: [PATCH] Add a sub command to generate a node key file (#4884)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* Add a sub command to generate a node key file in the format required by a substrate node

* Update lock file

* Apply suggestions from code review

Co-Authored-By: Bastian Köcher <bkchr@users.noreply.github.com>

* Updates as per code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
---
 substrate/Cargo.lock                   |  1 +
 substrate/bin/utils/subkey/Cargo.toml  |  1 +
 substrate/bin/utils/subkey/src/main.rs | 15 +++++++++++++++
 3 files changed, 17 insertions(+)

diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock
index 7564a72a01e..0f214fe87aa 100644
--- a/substrate/Cargo.lock
+++ b/substrate/Cargo.lock
@@ -7445,6 +7445,7 @@ dependencies = [
  "hyper 0.12.35",
  "itertools",
  "jsonrpc-core-client",
+ "libp2p",
  "node-primitives",
  "node-runtime",
  "pallet-balances",
diff --git a/substrate/bin/utils/subkey/Cargo.toml b/substrate/bin/utils/subkey/Cargo.toml
index d352cf28fa8..e29ee9a6c5b 100644
--- a/substrate/bin/utils/subkey/Cargo.toml
+++ b/substrate/bin/utils/subkey/Cargo.toml
@@ -28,6 +28,7 @@ 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"
 serde_json = "1.0"
 
 [features]
diff --git a/substrate/bin/utils/subkey/src/main.rs b/substrate/bin/utils/subkey/src/main.rs
index 52bacea3841..4d7e2206422 100644
--- a/substrate/bin/utils/subkey/src/main.rs
+++ b/substrate/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 serde_json::json;
@@ -248,6 +249,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, \
@@ -408,6 +412,17 @@ where
 			let mnemonic = generate_mnemonic(matches)?;
 			C::print_from_uri(mnemonic.phrase(), password, maybe_network, output);
 		}
+		("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();
+
+			fs::write(file, secret.as_ref())?;
+
+			println!("{}", peer_id);
+		}
 		("inspect", Some(matches)) => {
 			C::print_from_uri(&get_uri("uri", &matches)?, password, maybe_network, output);
 		}
-- 
GitLab