Skip to content

Commit

Permalink
Feat/cli: added mnemonic gen from console only (#983)
Browse files Browse the repository at this point in the history
* added mnemonic gen from console only

* made range more readable

* changelog

* undecisive person options moved to give a second try

---------

Co-authored-by: Alexandcoats <[email protected]>
  • Loading branch information
Brord van Wierst and Alexandcoats authored Aug 4, 2023
1 parent 862cdfd commit b8a06c4
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 22 deletions.
4 changes: 4 additions & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## 1.1.0 - 2023-MM-DD

### Changed

- `WalletCommand::Mnemonic` now takes 2 optional arguments to avoid user interaction;

### Added

- `WalletCommand::Accounts` variant to list all available accounts in a wallet;
Expand Down
3 changes: 2 additions & 1 deletion cli/src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ pub enum AccountCommand {
token_id: String,
/// Amount to send, e.g. 1000000.
amount: String,
/// Whether to gift the storage deposit for the output or not, e.g. ` true`.
/// Whether to gift the storage deposit for the output or not, e.g. `true`.
#[arg(value_parser = clap::builder::BoolishValueParser::new())]
gift_storage_deposit: Option<bool>,
},
/// Send an NFT.
Expand Down
16 changes: 11 additions & 5 deletions cli/src/command/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::path::Path;

use clap::{Args, Parser, Subcommand};
use clap::{builder::BoolishValueParser, Args, Parser, Subcommand};
use iota_sdk::{
client::{
constants::SHIMMER_COIN_TYPE,
Expand Down Expand Up @@ -63,7 +63,14 @@ pub enum WalletCommand {
path: Option<String>,
},
/// Generate a random mnemonic.
Mnemonic,
Mnemonic {
// Output the mnemonic to the specified file.
#[arg(long)]
output_file_name: Option<String>,
// Output the mnemonic to the stdout.
#[arg(long, num_args = 0..=1, default_missing_value = Some("true"), value_parser = BoolishValueParser::new())]
output_stdout: Option<bool>,
},
/// Create a new account.
NewAccount {
/// Account alias, next available account index if not provided.
Expand Down Expand Up @@ -194,9 +201,8 @@ pub async fn migrate_stronghold_snapshot_v2_to_v3_command(path: Option<String>)
Ok(())
}

pub async fn mnemonic_command() -> Result<(), Error> {
generate_mnemonic().await?;

pub async fn mnemonic_command(output_file_name: Option<String>, output_stdout: Option<bool>) -> Result<(), Error> {
generate_mnemonic(output_file_name, output_stdout).await?;
Ok(())
}

Expand Down
45 changes: 31 additions & 14 deletions cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,53 @@ pub async fn enter_or_generate_mnemonic() -> Result<Mnemonic, Error> {
.interact_on(&Term::stderr())?;

let mnemnonic = match selected_choice {
0 => generate_mnemonic().await?,
0 => generate_mnemonic(None, None).await?,
1 => enter_mnemonic()?,
_ => unreachable!(),
};

Ok(mnemnonic)
}

pub async fn generate_mnemonic() -> Result<Mnemonic, Error> {
pub async fn generate_mnemonic(
output_file_name: Option<String>,
output_stdout: Option<bool>,
) -> Result<Mnemonic, Error> {
let mnemonic = iota_sdk::client::generate_mnemonic()?;
println_log_info!("Mnemonic has been generated.");
let choices = [
"Write it to the console only",
"Write it to a file only",
"Write it to the console and a file",
];

let selected_choice = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select how to proceed with it")
.items(&choices)
.default(0)
.interact_on(&Term::stderr())?;
let selected_choice = match (&output_file_name, &output_stdout) {
// Undecided, we give the user a choice
(None, None) | (None, Some(false)) => {
let choices = [
"Write it to the console only",
"Write it to a file only",
"Write it to the console and a file",
];

Select::with_theme(&ColorfulTheme::default())
.with_prompt("Select how to proceed with it")
.items(&choices)
.default(0)
.interact_on(&Term::stderr())?
}
// Only console
(None, Some(true)) => 0,
// Only file
(Some(_), Some(false)) | (Some(_), None) => 1,
// File and console
(Some(_), Some(true)) => 2,
};

if [0, 2].contains(&selected_choice) {
println!("YOUR MNEMONIC:");
println!("{}", mnemonic.as_ref());
}
if [1, 2].contains(&selected_choice) {
write_mnemonic_to_file(DEFAULT_MNEMONIC_FILE_PATH, &mnemonic).await?;
println_log_info!("Mnemonic has been written to '{DEFAULT_MNEMONIC_FILE_PATH}'.");
let file_path = output_file_name.unwrap_or(DEFAULT_MNEMONIC_FILE_PATH.to_string());

write_mnemonic_to_file(&file_path, &mnemonic).await?;
println_log_info!("Mnemonic has been written to '{file_path}'.");
}

println_log_info!("IMPORTANT:");
Expand Down
7 changes: 5 additions & 2 deletions cli/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ pub async fn new_wallet(cli: WalletCli) -> Result<(Option<Wallet>, Option<String
let wallet = sync_command(storage_path, snapshot_path).await?;
(Some(wallet), None)
}
WalletCommand::Mnemonic => {
mnemonic_command().await?;
WalletCommand::Mnemonic {
output_file_name,
output_stdout,
} => {
mnemonic_command(output_file_name, output_stdout).await?;
return Ok((None, None));
}
WalletCommand::NodeInfo => {
Expand Down

0 comments on commit b8a06c4

Please sign in to comment.