Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core/identity/cli/identity.rs cleanup #3270

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions core/identity/src/cli/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,41 +338,31 @@ 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 if let Some(password) = password {
Protected::from(password.as_str())
} 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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You lost this case:

$ yagna id create --from-private-key ".." --password ".." alias

without --password means that you will be asked for a password unless the --no-password flag is passed.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, my mistake. Thanks for the head up!

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 = gsb
.local()
Expand Down Expand Up @@ -481,3 +471,7 @@ impl IdentityCommand {
}
}
}

fn read_password(prompt: &str) -> Result<Protected, anyhow::Error> {
Ok(rpassword::read_password_from_tty(Some(prompt))?.into())
}