From 0ce35ceff22ab2375a5e48b007b92566208b3a42 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Thu, 7 Jul 2022 20:19:12 +0200 Subject: [PATCH 1/3] Change password command --- src/account_manager.rs | 6 ++++-- src/command/account_manager.rs | 20 +++++++++++++++++--- src/helper.rs | 15 ++++++--------- 3 files changed, 27 insertions(+), 14 deletions(-) diff --git a/src/account_manager.rs b/src/account_manager.rs index 637cf5b..96f1186 100644 --- a/src/account_manager.rs +++ b/src/account_manager.rs @@ -10,7 +10,8 @@ use iota_wallet::{ use crate::{ command::account_manager::{ - init_command, new_command, set_node_command, sync_command, AccountManagerCli, AccountManagerCommand, + change_password_command, init_command, new_command, set_node_command, sync_command, AccountManagerCli, + AccountManagerCommand, }, error::Error, helper::get_password, @@ -23,7 +24,7 @@ pub async fn new_account_manager(cli: AccountManagerCli) -> Result<(AccountManag ); let stronghold_path = std::path::Path::new("./stardust-cli-wallet.stronghold"); - let password = get_password(stronghold_path)?; + let password = get_password("Stronghold password", !stronghold_path.exists())?; let secret_manager = SecretManager::Stronghold( StrongholdSecretManager::builder() .password(&password) @@ -42,6 +43,7 @@ pub async fn new_account_manager(cli: AccountManagerCli) -> Result<(AccountManag let mut account = None; match command { + AccountManagerCommand::ChangePassword => change_password_command(&account_manager).await?, // PANIC: this will never happen because of the if/else. AccountManagerCommand::Init(_) => unreachable!(), AccountManagerCommand::New { alias } => account = Some(new_command(&account_manager, alias).await?), diff --git a/src/command/account_manager.rs b/src/command/account_manager.rs index 569f731..2185fc8 100644 --- a/src/command/account_manager.rs +++ b/src/command/account_manager.rs @@ -11,7 +11,7 @@ use iota_wallet::{ }; use log::LevelFilter; -use crate::error::Error; +use crate::{error::Error, helper::get_password}; #[derive(Debug, Clone, Parser)] #[clap(version, long_about = None)] @@ -26,12 +26,17 @@ pub struct AccountManagerCli { #[derive(Debug, Clone, Subcommand)] pub enum AccountManagerCommand { + ChangePassword, /// Parameters for the init command. Init(InitParameters), /// Create a new account with an optional alias. - New { alias: Option }, + New { + alias: Option, + }, /// Set the node to use. - SetNode { url: String }, + SetNode { + url: String, + }, /// Sync all accounts. Sync, } @@ -46,6 +51,15 @@ pub struct InitParameters { pub coin_type: Option, } +pub async fn change_password_command(manager: &AccountManager) -> Result<(), Error> { + let current = get_password("Stronghold current password", false)?; + let new = get_password("Stronghold new password", false)?; + + manager.change_stronghold_password(¤t, &new).await?; + + Ok(()) +} + pub async fn init_command( secret_manager: SecretManager, storage_path: String, diff --git a/src/helper.rs b/src/helper.rs index cc3602a..8fa320f 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -1,24 +1,21 @@ // Copyright 2020-2022 IOTA Stiftung // SPDX-License-Identifier: Apache-2.0 -use std::path::Path; - use dialoguer::{console::Term, theme::ColorfulTheme, Password, Select}; use iota_wallet::account_manager::AccountManager; use crate::error::Error; -pub fn get_password(path: &Path) -> Result { - let mut prompt = Password::new(); +pub fn get_password(prompt: &str, confirmation: bool) -> Result { + let mut password = Password::new(); - prompt.with_prompt("Stronghold password"); + password.with_prompt(prompt); - // Check if the stronghold exists already - if !path.exists() { - prompt.with_confirmation("Confirm password", "Password mismatch"); + if confirmation { + password.with_confirmation("Confirm password", "Password mismatch"); } - Ok(prompt.interact()?) + Ok(password.interact()?) } pub async fn pick_account(manager: &AccountManager) -> Result, Error> { From 0975f7904f3097c53c880daa22c615964d9812c6 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Fri, 8 Jul 2022 09:40:35 +0200 Subject: [PATCH 2/3] Add confirmation --- src/command/account_manager.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/command/account_manager.rs b/src/command/account_manager.rs index 2185fc8..e8f00cf 100644 --- a/src/command/account_manager.rs +++ b/src/command/account_manager.rs @@ -53,7 +53,7 @@ pub struct InitParameters { pub async fn change_password_command(manager: &AccountManager) -> Result<(), Error> { let current = get_password("Stronghold current password", false)?; - let new = get_password("Stronghold new password", false)?; + let new = get_password("Stronghold new password", true)?; manager.change_stronghold_password(¤t, &new).await?; From 762d2515fa4efcde2681379c4619c06b92d98c27 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Fri, 8 Jul 2022 09:59:06 +0200 Subject: [PATCH 3/3] Reuse previous password --- src/account_manager.rs | 2 +- src/command/account_manager.rs | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/account_manager.rs b/src/account_manager.rs index 96f1186..fe5ce33 100644 --- a/src/account_manager.rs +++ b/src/account_manager.rs @@ -43,7 +43,7 @@ pub async fn new_account_manager(cli: AccountManagerCli) -> Result<(AccountManag let mut account = None; match command { - AccountManagerCommand::ChangePassword => change_password_command(&account_manager).await?, + AccountManagerCommand::ChangePassword => change_password_command(&account_manager, &password).await?, // PANIC: this will never happen because of the if/else. AccountManagerCommand::Init(_) => unreachable!(), AccountManagerCommand::New { alias } => account = Some(new_command(&account_manager, alias).await?), diff --git a/src/command/account_manager.rs b/src/command/account_manager.rs index e8f00cf..3ad67cb 100644 --- a/src/command/account_manager.rs +++ b/src/command/account_manager.rs @@ -51,8 +51,7 @@ pub struct InitParameters { pub coin_type: Option, } -pub async fn change_password_command(manager: &AccountManager) -> Result<(), Error> { - let current = get_password("Stronghold current password", false)?; +pub async fn change_password_command(manager: &AccountManager, current: &str) -> Result<(), Error> { let new = get_password("Stronghold new password", true)?; manager.change_stronghold_password(¤t, &new).await?;