Add tty password input (#2503)

* Add tty password input

* Move password from core/service to core/cli

* Fix test build error

* Password should be entered only once if it's for decoding

* Update Cargo.lock from rebuild
This commit is contained in:
郭光华
2019-05-16 00:11:59 +08:00
committed by Gavin Wood
parent c4e3970d9f
commit b399ea0b31
7 changed files with 31 additions and 2 deletions
+1
View File
@@ -33,6 +33,7 @@ substrate-telemetry = { path = "../../core/telemetry" }
keyring = { package = "substrate-keyring", path = "../keyring" }
names = "0.11.0"
structopt = "0.2"
rpassword = "3.0"
[dev-dependencies]
tempdir = "0.3"
+8
View File
@@ -365,6 +365,11 @@ fn fill_network_configuration(
Ok(())
}
fn input_keystore_password() -> Result<String, String> {
rpassword::read_password_from_tty(Some("Keystore password: "))
.map_err(|e| format!("{:?}", e))
}
fn create_run_node_config<F, S>(
cli: RunCmd, spec_factory: S, impl_name: &'static str, version: &VersionInfo
) -> error::Result<FactoryFullConfiguration<F>>
@@ -374,6 +379,9 @@ where
{
let spec = load_spec(&cli.shared_params, spec_factory)?;
let mut config = service::Configuration::default_with_spec(spec.clone());
if cli.interactive_password {
config.password = input_keystore_password()?
}
config.impl_name = impl_name;
config.impl_commit = version.commit;
+4
View File
@@ -395,6 +395,10 @@ pub struct RunCmd {
/// Enable authoring even when offline.
#[structopt(long = "force-authoring")]
pub force_authoring: bool,
/// Interactive password for validator key.
#[structopt(short = "i")]
pub interactive_password: bool,
}
/// Stores all required Cli values for a keyring test account.
+3
View File
@@ -78,6 +78,8 @@ pub struct Configuration<C, G: Serialize + DeserializeOwned + BuildStorage> {
pub force_authoring: bool,
/// Disable GRANDPA when running in validator mode
pub disable_grandpa: bool,
/// Node keystore's password
pub password: String,
}
impl<C: Default, G: Serialize + DeserializeOwned + BuildStorage> Configuration<C, G> {
@@ -108,6 +110,7 @@ impl<C: Default, G: Serialize + DeserializeOwned + BuildStorage> Configuration<C
offchain_worker: Default::default(),
force_authoring: false,
disable_grandpa: false,
password: "".to_string(),
};
configuration.network.boot_nodes = configuration.chain_spec.boot_nodes().to_vec();
+2 -2
View File
@@ -142,7 +142,7 @@ impl<Components: components::Components> Service<Components> {
let public_key = match keystore.contents()?.get(0) {
Some(public_key) => public_key.clone(),
None => {
let key = keystore.generate("")?;
let key = keystore.generate(&config.password)?;
let public_key = key.public();
info!("Generated a new keypair: {:?}", public_key);
@@ -382,7 +382,7 @@ impl<Components: components::Components> Service<Components> {
if self.config.roles != Roles::AUTHORITY { return None }
let keystore = &self.keystore;
if let Ok(Some(Ok(key))) = keystore.contents().map(|keys| keys.get(0)
.map(|k| keystore.load(k, "")))
.map(|k| keystore.load(k, &self.config.password)))
{
Some(key)
} else {
+1
View File
@@ -127,6 +127,7 @@ fn node_config<F: ServiceFactory> (
offchain_worker: false,
force_authoring: false,
disable_grandpa: false,
password: "".to_string(),
}
}