From 6244ecd939203b9346743a381ba78da3b29b1f7a Mon Sep 17 00:00:00 2001 From: Liu-Cheng Xu Date: Fri, 22 Feb 2019 17:37:31 +0800 Subject: [PATCH] Treat --key as private key of pkcs8 (#7) * Treat --key as private key of pkcs8 * Fix mismatched formatter --- Cargo.lock | 1 + core/service/Cargo.toml | 1 + core/service/src/lib.rs | 17 +++++++++++++++++ 3 files changed, 19 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index efff803e83126..2b60fb8be3c79 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4020,6 +4020,7 @@ dependencies = [ "error-chain 0.12.0 (registry+https://github.com/rust-lang/crates.io-index)", "exit-future 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.25 (registry+https://github.com/rust-lang/crates.io-index)", + "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "parity-codec 3.0.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/core/service/Cargo.toml b/core/service/Cargo.toml index 5350cb3554493..b711aa173ff0c 100644 --- a/core/service/Cargo.toml +++ b/core/service/Cargo.toml @@ -12,6 +12,7 @@ lazy_static = "1.0" log = "0.4" slog = "^2" tokio = "0.1.7" +hex = "0.3" exit-future = "0.1" serde = "1.0" serde_json = "1.0" diff --git a/core/service/src/lib.rs b/core/service/src/lib.rs index d2b1e92520e13..26dc6a1acdd6e 100644 --- a/core/service/src/lib.rs +++ b/core/service/src/lib.rs @@ -41,6 +41,7 @@ use exit_future::Signal; pub use tokio::runtime::TaskExecutor; use substrate_executor::NativeExecutor; use parity_codec::{Encode, Decode}; +use primitives::ed25519::Pair; use tel::telemetry; pub use self::error::{ErrorKind, Error}; @@ -71,6 +72,7 @@ pub struct Service { pub network: Option>>, pub transaction_pool: Arc>, keystore: Keystore, + private_key: Option, exit: ::exit_future::Exit, signal: Option, /// Configuration of this Service @@ -123,6 +125,8 @@ impl Service { } }; + let private_key = config.keys.get(0).map(|x| x.to_owned()); + let (client, on_demand) = Components::build_client(&config, executor)?; let import_queue = Arc::new(Components::build_import_queue(&mut config, client.clone())?); let best_header = client.best_block_header()?; @@ -299,6 +303,7 @@ impl Service { transaction_pool, signal: Some(signal), keystore, + private_key, config, exit, //_rpc: Box::new(rpc), @@ -309,6 +314,17 @@ impl Service { /// give the authority key, if we are an authority and have a key pub fn authority_key(&self) -> Option { if self.config.roles != Roles::AUTHORITY { return None } + if let Some(ref private_key) = self.private_key { + let pkcs8_bytes: Vec = hex::decode(&private_key[2..]).unwrap(); + if let Ok(pair) = Pair::from_pkcs8(&pkcs8_bytes) { + Some(pair) + } else { + panic!("Fail to generate pair from pkcs8 bytes") + } + } else { + panic!("AUTHORITY must provide private key") + } + /* let keystore = &self.keystore; if let Ok(Some(Ok(key))) = keystore.contents().map(|keys| keys.get(0) .map(|k| keystore.load(k, ""))) @@ -317,6 +333,7 @@ impl Service { } else { None } + */ } pub fn telemetry(&self) -> Option> {