forc_wallet/
new.rs

1use crate::{
2    DEFAULT_CACHE_ACCOUNTS,
3    account::derive_and_cache_addresses,
4    utils::{
5        display_string_discreetly, ensure_no_wallet_exists, request_new_password,
6        write_wallet_from_mnemonic_and_password,
7    },
8};
9use clap::Args;
10use fuels::accounts::signers::private_key::generate_mnemonic_phrase;
11use std::io::stdin;
12
13#[derive(Debug, Args)]
14pub struct New {
15    /// Forces wallet creation, removing any existing wallet file
16    #[clap(short, long)]
17    pub force: bool,
18
19    /// How many accounts to cache by default (Default 10)
20    #[clap(short, long)]
21    pub cache_accounts: Option<usize>,
22}
23
24pub async fn new_wallet_cli(ctx: &crate::CliContext, new: New) -> anyhow::Result<()> {
25    ensure_no_wallet_exists(&ctx.wallet_path, new.force, stdin().lock())?;
26    let password = request_new_password();
27    // Generate a random mnemonic phrase.
28    let mnemonic = generate_mnemonic_phrase(&mut rand::thread_rng(), 24)?;
29    write_wallet_from_mnemonic_and_password(&ctx.wallet_path, &mnemonic, &password)?;
30
31    derive_and_cache_addresses(
32        ctx,
33        &mnemonic,
34        0..new.cache_accounts.unwrap_or(DEFAULT_CACHE_ACCOUNTS),
35    )
36    .await?;
37
38    let mnemonic_string = format!("Wallet mnemonic phrase: {mnemonic}\n");
39    display_string_discreetly(
40        &mnemonic_string,
41        "### Do not share or lose this mnemonic phrase! Press any key to complete. ###",
42    )?;
43    Ok(())
44}