From 3303185f6348de259a5138d8b5565f6d3a2ba3b5 Mon Sep 17 00:00:00 2001 From: willyrgf Date: Fri, 2 Sep 2022 11:50:03 -0300 Subject: [PATCH] fix(console_wallet): use cli.non_interactive instead of propmt to show seed words --- .../tari_console_wallet/src/init/mod.rs | 32 +++++++++++-------- applications/tari_console_wallet/src/main.rs | 25 ++++++++++----- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/applications/tari_console_wallet/src/init/mod.rs b/applications/tari_console_wallet/src/init/mod.rs index b9f1db92da..97ce4105f4 100644 --- a/applications/tari_console_wallet/src/init/mod.rs +++ b/applications/tari_console_wallet/src/init/mod.rs @@ -118,8 +118,9 @@ pub async fn change_password( config: &ApplicationConfig, arg_password: Option, shutdown_signal: ShutdownSignal, + non_interactive_mode: bool, ) -> Result<(), ExitError> { - let mut wallet = init_wallet(config, arg_password, None, None, shutdown_signal).await?; + let mut wallet = init_wallet(config, arg_password, None, None, shutdown_signal, non_interactive_mode).await?; let passphrase = prompt_password("New wallet password: ")?; let confirmed = prompt_password("Confirm new password: ")?; @@ -248,6 +249,7 @@ pub async fn init_wallet( seed_words_file_name: Option, recovery_seed: Option, shutdown_signal: ShutdownSignal, + non_interactive_mode: bool, ) -> Result { fs::create_dir_all( &config @@ -359,27 +361,29 @@ pub async fn init_wallet( debug!(target: LOG_TARGET, "Wallet is not encrypted."); // create using --password arg if supplied and skip seed words confirmation - let (passphrase, interactive) = if let Some(password) = arg_password { - debug!(target: LOG_TARGET, "Setting password from command line argument."); - - (password, false) - } else { - debug!(target: LOG_TARGET, "Prompting for password."); - let password = prompt_password("Create wallet password: ")?; - let confirmed = prompt_password("Confirm wallet password: ")?; + let passphrase = match arg_password { + Some(password) => { + debug!(target: LOG_TARGET, "Setting password from command line argument."); + password + }, + None => { + debug!(target: LOG_TARGET, "Prompting for password."); + let password = prompt_password("Create wallet password: ")?; + let confirmed = prompt_password("Confirm wallet password: ")?; - if password != confirmed { - return Err(ExitError::new(ExitCode::InputError, "Passwords don't match!")); - } + if password != confirmed { + return Err(ExitError::new(ExitCode::InputError, "Passwords don't match!")); + } - (password, true) + password + }, }; wallet.apply_encryption(passphrase).await?; debug!(target: LOG_TARGET, "Wallet encrypted."); - if interactive && recovery_seed.is_none() { + if !non_interactive_mode && recovery_seed.is_none() { match confirm_seed_words(&mut wallet) { Ok(()) => { print!("\x1Bc"); // Clear the screen diff --git a/applications/tari_console_wallet/src/main.rs b/applications/tari_console_wallet/src/main.rs index 9eb5479dd8..e8f0b4060c 100644 --- a/applications/tari_console_wallet/src/main.rs +++ b/applications/tari_console_wallet/src/main.rs @@ -47,6 +47,7 @@ use tari_key_manager::cipher_seed::CipherSeed; #[cfg(all(unix, feature = "libtor"))] use tari_libtor::tor::Tor; use tari_shutdown::Shutdown; +use tari_utilities::SafePassword; use tracing_subscriber::{layer::SubscriberExt, Registry}; use wallet_modes::{command_mode, grpc_mode, recovery_mode, script_mode, tui_mode, WalletMode}; @@ -92,8 +93,7 @@ fn main() { fn main_inner() -> Result<(), ExitError> { let cli = Cli::parse(); - let config_path = cli.common.config_path(); - let cfg = load_configuration(config_path.as_path(), true, &cli)?; + let cfg = load_configuration(cli.common.config_path().as_path(), true, &cli)?; initialize_logging( &cli.common.log_config_path("wallet"), include_str!("../log4rs_sample.yml"), @@ -118,11 +118,7 @@ fn main_inner() -> Result<(), ExitError> { consts::APP_VERSION ); - let password = cli - .password - .as_ref() - .or(config.wallet.password.as_ref()) - .map(|s| s.to_owned()); + let password = get_password(&config, &cli); if password.is_none() { tari_splash_screen("Console Wallet"); @@ -141,7 +137,12 @@ fn main_inner() -> Result<(), ExitError> { if cli.change_password { info!(target: LOG_TARGET, "Change password requested."); - return runtime.block_on(change_password(&config, password, shutdown_signal)); + return runtime.block_on(change_password( + &config, + password, + shutdown_signal, + cli.non_interactive_mode, + )); } // Run our own Tor instance, if configured @@ -164,6 +165,7 @@ fn main_inner() -> Result<(), ExitError> { seed_words_file_name, recovery_seed, shutdown_signal, + cli.non_interactive_mode, ))?; // Check if there is an in progress recovery in the wallet's database @@ -219,6 +221,13 @@ fn main_inner() -> Result<(), ExitError> { result } +fn get_password(config: &ApplicationConfig, cli: &Cli) -> Option { + cli.password + .as_ref() + .or(config.wallet.password.as_ref()) + .map(|s| s.to_owned()) +} + fn get_recovery_seed(boot_mode: WalletBoot, cli: &Cli) -> Result, ExitError> { if matches!(boot_mode, WalletBoot::Recovery) { let seed = if cli.seed_words.is_some() {