foundry_wallets/
raw_wallet.rs

1use crate::{utils, PendingSigner, WalletSigner};
2use clap::Parser;
3use eyre::Result;
4use serde::Serialize;
5
6/// A wrapper for the raw data options for `Wallet`, extracted to also be used standalone.
7/// The raw wallet options can either be:
8/// 1. Private Key (cleartext in CLI)
9/// 2. Private Key (interactively via secure prompt)
10/// 3. Mnemonic (via file path)
11#[derive(Clone, Debug, Default, Serialize, Parser)]
12#[command(next_help_heading = "Wallet options - raw", about = None, long_about = None)]
13pub struct RawWalletOpts {
14    /// Open an interactive prompt to enter your private key.
15    #[arg(long, short)]
16    pub interactive: bool,
17
18    /// Use the provided private key.
19    #[arg(long, value_name = "RAW_PRIVATE_KEY")]
20    pub private_key: Option<String>,
21
22    /// Use the mnemonic phrase of mnemonic file at the specified path.
23    #[arg(long, alias = "mnemonic-path")]
24    pub mnemonic: Option<String>,
25
26    /// Use a BIP39 passphrase for the mnemonic.
27    #[arg(long, value_name = "PASSPHRASE")]
28    pub mnemonic_passphrase: Option<String>,
29
30    /// The wallet derivation path.
31    ///
32    /// Works with both --mnemonic-path and hardware wallets.
33    #[arg(long = "mnemonic-derivation-path", alias = "hd-path", value_name = "PATH")]
34    pub hd_path: Option<String>,
35
36    /// Use the private key from the given mnemonic index.
37    ///
38    /// Used with --mnemonic-path.
39    #[arg(long, conflicts_with = "hd_path", default_value_t = 0, value_name = "INDEX")]
40    pub mnemonic_index: u32,
41}
42
43impl RawWalletOpts {
44    /// Returns signer configured by provided parameters.
45    pub fn signer(&self) -> Result<Option<WalletSigner>> {
46        if self.interactive {
47            return Ok(Some(PendingSigner::Interactive.unlock()?));
48        }
49        if let Some(private_key) = &self.private_key {
50            return Ok(Some(utils::create_private_key_signer(private_key)?));
51        }
52        if let Some(mnemonic) = &self.mnemonic {
53            return Ok(Some(utils::create_mnemonic_signer(
54                mnemonic,
55                self.mnemonic_passphrase.as_deref(),
56                self.hd_path.as_deref(),
57                self.mnemonic_index,
58            )?));
59        }
60        Ok(None)
61    }
62}