From d549a0749830b036513d959b08b677fb3be70153 Mon Sep 17 00:00:00 2001 From: dommyrock Date: Fri, 12 Jul 2024 19:04:11 +0200 Subject: [PATCH 1/2] core/identity/cli/identity.rs cleanup --- core/identity/src/cli/identity.rs | 60 ++++++++++++++----------------- 1 file changed, 26 insertions(+), 34 deletions(-) diff --git a/core/identity/src/cli/identity.rs b/core/identity/src/cli/identity.rs index 1a0ae872ff..375e7476c4 100644 --- a/core/identity/src/cli/identity.rs +++ b/core/identity/src/cli/identity.rs @@ -322,41 +322,29 @@ impl IdentityCommand { log::warn!("Using private key directly is not recommended. Use keystore instead. Your key could leak in command history, check and clean logs.") } - let from_private_key_slice: Option<[u8; 32]> = if let Some(from_private_key) = - from_private_key - { - let v = hex::decode(from_private_key).map_err(|e| { - anyhow::anyhow!( - "Private key has to be plain hex string without 0x prefix - {e}" - ) + let from_private_key_slice = match from_private_key { + Some(from_private_key) => hex::decode(from_private_key) + .map_err(|e| anyhow::anyhow!("Private key has to be plain hex string without 0x prefix - {e}")) + .and_then(|v| v[0..32].try_into().map_err(|e| anyhow::anyhow!("Ethereum key has to be 32 bytes long. Provide hex string of length 64 - {e}"))), + None => Err(anyhow::anyhow!("No private key provided")), + }.ok(); + + let key_file = from_keystore + .as_ref() + .map(|p| std::fs::read_to_string(p).context("unable to read input path")) + .unwrap_or_else(|| { + let password = if *no_password { + Protected::from("") + } else { + let password = read_password("Password: ")?; + let password2 = read_password("Confirm password: ")?; + if password.as_ref() != password2.as_ref() { + anyhow::bail!("Password and confirmation do not match.") + } + password + }; + crate::id_key::generate_new_keyfile(password, from_private_key_slice) })?; - let slice = v[0..32].try_into().map_err(|e| - anyhow::anyhow!("Ethereum key has to be 32 bytes long. Provide hex string of length 64 - {e}") - )?; - Some(slice) - } else { - None - }; - - let key_file = if let Some(keystore) = from_keystore { - std::fs::read_to_string(keystore)? - } else { - let password = if *no_password { - Protected::from("") - } else if let Some(password) = password { - Protected::from(password.as_str()) - } else { - let password: Protected = - rpassword::read_password_from_tty(Some("Password: "))?.into(); - let password2: Protected = - rpassword::read_password_from_tty(Some("Confirm password: "))?.into(); - if password.as_ref() != password2.as_ref() { - anyhow::bail!("Password and confirmation do not match.") - } - password - }; - crate::id_key::generate_new_keyfile(password, from_private_key_slice)? - }; let id = bus::service(identity::BUS_ID) .send(identity::CreateGenerated { @@ -446,3 +434,7 @@ impl IdentityCommand { } } } + +fn read_password(prompt: &str) -> Result { + Ok(rpassword::read_password_from_tty(Some(prompt))?.into()) +} From 52ee75993c50d9fa84fe2a9891485ffaeb9bea5c Mon Sep 17 00:00:00 2001 From: dommyrock Date: Sat, 21 Sep 2024 21:45:11 +0200 Subject: [PATCH 2/2] re add --pasword flag for core/identity/cli/identity.rs --- core/identity/src/cli/identity.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/identity/src/cli/identity.rs b/core/identity/src/cli/identity.rs index 48bb7ec5ac..a1e3585ad3 100644 --- a/core/identity/src/cli/identity.rs +++ b/core/identity/src/cli/identity.rs @@ -351,6 +351,8 @@ impl IdentityCommand { .unwrap_or_else(|| { let password = if *no_password { Protected::from("") + } else if let Some(password) = password { + Protected::from(password.as_str()) } else { let password = read_password("Password: ")?; let password2 = read_password("Confirm password: ")?;