Skip to content

Commit

Permalink
feat(cast): support raw mnemonics and rename --mnemonic-path to `--…
Browse files Browse the repository at this point in the history
…mnemonic` (foundry-rs#3071)
  • Loading branch information
not-poma authored Sep 4, 2022
1 parent c390df8 commit 2eceb70
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
25 changes: 13 additions & 12 deletions cli/src/opts/multi_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,13 @@ pub struct MultiWallet {
pub private_key: Option<String>,

#[clap(
long = "mnemonic-paths",
long = "mnemonics",
alias = "mnemonic-paths",
help_heading = "WALLET OPTIONS - RAW",
help = "Use the mnemonic files at the specified paths.",
help = "Use the mnemonic phrases or mnemonic files at the specified paths.",
value_name = "PATHS"
)]
pub mnemonic_paths: Option<Vec<String>>,
pub mnemonics: Option<Vec<String>>,

#[clap(
long = "mnemonic-passphrases",
Expand Down Expand Up @@ -299,30 +300,30 @@ impl MultiWallet {
}

pub fn mnemonics(&self) -> Result<Option<Vec<LocalWallet>>> {
if let Some(mnemonic_paths) = self.mnemonic_paths.as_ref() {
if let Some(ref mnemonics) = self.mnemonics {
let mut wallets = vec![];
let hd_paths: Vec<_> = if let Some(ref hd_paths) = self.hd_paths {
hd_paths.iter().map(String::as_str).map(Some).collect()
hd_paths.iter().map(Some).collect()
} else {
repeat(None).take(mnemonic_paths.len()).collect()
repeat(None).take(mnemonics.len()).collect()
};
let mnemonic_passphrases: Vec<_> =
if let Some(ref mnemonic_passphrases) = self.mnemonic_passphrases {
mnemonic_passphrases.iter().map(String::as_str).map(Some).collect()
mnemonic_passphrases.iter().map(Some).collect()
} else {
repeat(None).take(mnemonic_paths.len()).collect()
repeat(None).take(mnemonics.len()).collect()
};
let mnemonic_indexes: Vec<_> = if let Some(ref mnemonic_indexes) = self.mnemonic_indexes
{
mnemonic_indexes.to_vec()
} else {
repeat(0).take(mnemonic_paths.len()).collect()
repeat(0).take(mnemonics.len()).collect()
};
for (path, mnemonic_passphrase, hd_path, mnemonic_index) in
izip!(mnemonic_paths, mnemonic_passphrases, hd_paths, mnemonic_indexes)
for (mnemonic, mnemonic_passphrase, hd_path, mnemonic_index) in
izip!(mnemonics, mnemonic_passphrases, hd_paths, mnemonic_indexes)
{
wallets.push(self.get_from_mnemonic(
path,
mnemonic,
mnemonic_passphrase,
hd_path,
mnemonic_index,
Expand Down
40 changes: 24 additions & 16 deletions cli/src/opts/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{str::FromStr, sync::Arc};
use std::{path::Path, str::FromStr, sync::Arc};

use clap::Parser;
use ethers::{
Expand Down Expand Up @@ -81,12 +81,13 @@ pub struct Wallet {
pub private_key: Option<String>,

#[clap(
long = "mnemonic-path",
long = "mnemonic",
alias = "mnemonic-path",
help_heading = "WALLET OPTIONS - RAW",
help = "Use the mnemonic file at the specified path.",
help = "Use the mnemonic phrase of mnemonic file at the specified path.",
value_name = "PATH"
)]
pub mnemonic_path: Option<String>,
pub mnemonic: Option<String>,

#[clap(
long = "mnemonic-passphrase",
Expand Down Expand Up @@ -178,11 +179,11 @@ impl Wallet {
}

pub fn mnemonic(&self) -> Result<Option<LocalWallet>> {
Ok(if let Some(ref path) = self.mnemonic_path {
Ok(if let Some(ref mnemonic) = self.mnemonic {
Some(self.get_from_mnemonic(
path,
self.mnemonic_passphrase.as_deref(),
self.hd_path.as_deref(),
mnemonic,
self.mnemonic_passphrase.as_ref(),
self.hd_path.as_ref(),
self.mnemonic_index,
)?)
} else {
Expand All @@ -209,17 +210,24 @@ pub trait WalletTrait {

fn get_from_mnemonic(
&self,
path: &str,
passphrase: Option<&str>,
derivation_path: Option<&str>,
mnemonic: &String,
passphrase: Option<&String>,
derivation_path: Option<&String>,
index: u32,
) -> Result<LocalWallet> {
let mnemonic = fs::read_to_string(path)?.replace('\n', "");
let mnemonic = if Path::new(mnemonic).is_file() {
fs::read_to_string(mnemonic)?.replace('\n', "")
} else {
mnemonic.to_owned()
};
let builder = MnemonicBuilder::<English>::default().phrase(mnemonic.as_str());
let builder =
if let Some(passphrase) = passphrase { builder.password(passphrase) } else { builder };
let builder = if let Some(passphrase) = passphrase {
builder.password(passphrase.as_str())
} else {
builder
};
let builder = if let Some(hd_path) = derivation_path {
builder.derivation_path(hd_path)?
builder.derivation_path(hd_path.as_str())?
} else {
builder.index(index)?
};
Expand Down Expand Up @@ -255,7 +263,7 @@ mod tests {
private_key: Some("123".to_string()),
keystore_path: None,
keystore_password: None,
mnemonic_path: None,
mnemonic: None,
mnemonic_passphrase: None,
ledger: false,
trezor: false,
Expand Down

0 comments on commit 2eceb70

Please sign in to comment.