Skip to content

Commit

Permalink
added mnemonic gen from console only
Browse files Browse the repository at this point in the history
  • Loading branch information
Brord van Wierst committed Aug 1, 2023
1 parent 8e38220 commit 6ef9bb3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 22 deletions.
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 @@ -61,7 +61,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..2, 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 @@ -178,9 +185,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
46 changes: 32 additions & 14 deletions cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,36 +121,54 @@ 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 file_path = output_file_name
.clone()
.unwrap_or(DEFAULT_MNEMONIC_FILE_PATH.to_string());

let selected_choice = match (output_file_name, output_stdout) {
(None, None) => {
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) | (None, Some(false)) => 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}'.");
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 @@ -54,8 +54,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 6ef9bb3

Please sign in to comment.