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

Feat/cli: added mnemonic gen from console only #983

Merged
merged 5 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
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
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) => {
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -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
Loading