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

Reuse password option prompts again on a wrong password #4380

Merged
merged 6 commits into from
Aug 13, 2024
Merged
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
50 changes: 36 additions & 14 deletions account_manager/src/validator/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,13 @@ pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), Strin
let password_opt = loop {
if let Some(password) = previous_password.clone() {
eprintln!("Reuse previous password.");
break Some(password);
if check_password_on_keystore(&keystore, &password)? {
break Some(password);
} else {
eprintln!("Reused password incorrect. Retry!");
previous_password = None;
continue;
}
}
eprintln!();
eprintln!("{}", PASSWORD_PROMPT);
Expand All @@ -201,20 +207,12 @@ pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), Strin
}
};

match keystore.decrypt_keypair(password.as_ref()) {
Ok(_) => {
eprintln!("Password is correct.");
eprintln!();
sleep(Duration::from_secs(1)); // Provides nicer UX.
if reuse_password {
previous_password = Some(password.clone());
}
break Some(password);
}
Err(eth2_keystore::Error::InvalidPassword) => {
eprintln!("Invalid password");
// Check if the password unlocks the keystore
if check_password_on_keystore(&keystore, &password)? {
if reuse_password {
previous_password = Some(password.clone());
}
Err(e) => return Err(format!("Error whilst decrypting keypair: {:?}", e)),
break Some(password);
}
};

Expand Down Expand Up @@ -317,3 +315,27 @@ pub fn cli_run(matches: &ArgMatches, validator_dir: PathBuf) -> Result<(), Strin

Ok(())
}

/// Checks if the given password unlocks the keystore.
///
/// Returns `Ok(true)` if password unlocks the keystore successfully.
/// Returns `Ok(false` if password is incorrect.
/// Otherwise, returns the keystore error.
fn check_password_on_keystore(
keystore: &Keystore,
password: &ZeroizeString,
) -> Result<bool, String> {
match keystore.decrypt_keypair(password.as_ref()) {
Ok(_) => {
eprintln!("Password is correct.");
eprintln!();
sleep(Duration::from_secs(1)); // Provides nicer UX.
Ok(true)
}
Err(eth2_keystore::Error::InvalidPassword) => {
eprintln!("Invalid password");
Ok(false)
}
Err(e) => Err(format!("Error whilst decrypting keypair: {:?}", e)),
}
}