Skip to content

Commit

Permalink
Treat --key as private key of pkcs8 (paritytech#7)
Browse files Browse the repository at this point in the history
* Treat --key as private key of pkcs8

* Fix mismatched formatter
  • Loading branch information
liuchengxu committed Feb 22, 2019
1 parent 5bf3d03 commit fe056d3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions core/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
34 changes: 34 additions & 0 deletions core/service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -71,6 +72,7 @@ pub struct Service<Components: components::Components> {
pub network: Option<Arc<components::NetworkService<Components::Factory>>>,
pub transaction_pool: Arc<TransactionPool<Components::TransactionPoolApi>>,
keystore: Keystore,
private_key: Option<String>,
exit: ::exit_future::Exit,
signal: Option<Signal>,
/// Configuration of this Service
Expand Down Expand Up @@ -123,6 +125,25 @@ impl<Components: components::Components> Service<Components> {
}
};

let private_key = config.keys.get(0).map(|x| x.to_owned());
let public_key = match private_key {
Some(ref private_key) => {
let pkcs8_bytes: Vec<u8> = hex::decode(&private_key[2..]).unwrap();
if let Ok(pair) = Pair::from_pkcs8(&pkcs8_bytes) {
pair.public()
} else {
panic!("Fail to generate pair from pkcs8 bytes")
}
},
None => {
let key = keystore.generate("")?;
let public_key = key.public();
info!("Generated a new keypair: {:?}", public_key);

public_key
}
};

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()?;
Expand Down Expand Up @@ -299,6 +320,7 @@ impl<Components: components::Components> Service<Components> {
transaction_pool,
signal: Some(signal),
keystore,
private_key,
config,
exit,
//_rpc: Box::new(rpc),
Expand All @@ -309,6 +331,17 @@ impl<Components: components::Components> Service<Components> {
/// give the authority key, if we are an authority and have a key
pub fn authority_key(&self) -> Option<primitives::ed25519::Pair> {
if self.config.roles != Roles::AUTHORITY { return None }
if let Some(ref private_key) = self.private_key {
let pkcs8_bytes: Vec<u8> = 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, "")))
Expand All @@ -317,6 +350,7 @@ impl<Components: components::Components> Service<Components> {
} else {
None
}
*/
}

pub fn telemetry(&self) -> Option<Arc<tel::Telemetry>> {
Expand Down

0 comments on commit fe056d3

Please sign in to comment.