use crate::{
DEFAULT_CACHE_ACCOUNTS,
account::derive_and_cache_addresses,
utils::{
display_string_discreetly, ensure_no_wallet_exists, request_new_password,
write_wallet_from_mnemonic_and_password,
},
};
use clap::Args;
use fuels::accounts::signers::private_key::generate_mnemonic_phrase;
use std::io::stdin;
#[derive(Debug, Args)]
pub struct New {
#[clap(short, long)]
pub force: bool,
#[clap(short, long)]
pub cache_accounts: Option<usize>,
}
pub async fn new_wallet_cli(ctx: &crate::CliContext, new: New) -> anyhow::Result<()> {
ensure_no_wallet_exists(&ctx.wallet_path, new.force, stdin().lock())?;
let password = request_new_password();
let mnemonic = generate_mnemonic_phrase(&mut rand::thread_rng(), 24)?;
write_wallet_from_mnemonic_and_password(&ctx.wallet_path, &mnemonic, &password)?;
derive_and_cache_addresses(
ctx,
&mnemonic,
0..new.cache_accounts.unwrap_or(DEFAULT_CACHE_ACCOUNTS),
)
.await?;
let mnemonic_string = format!("Wallet mnemonic phrase: {mnemonic}\n");
display_string_discreetly(
&mnemonic_string,
"### Do not share or lose this mnemonic phrase! Press any key to complete. ###",
)?;
Ok(())
}