-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(
cast wallet list
) issue #6958: Include HW wallets in cast wall…
…et ls (#7123) * issue #6958: Include HW wallets in cast wallet ls * Changes after review: - use annotations for builder defaults - handle Local signer in available_senders, return Ledger addresses for legacy derivation, add doc - fix condition to list files in keystore dir - simplify creation of keystore default directory * Changes after review: use list_signers macro * Changes after review: - remove help_headings - remove match and use ? as dir already exists - remove async from list_local_senders fn - move Ok(senders) at the bottom of available_senders fn - list_senders doesn't need match as available_senders cannot fail - make max_senders arg for ls command , default 3 * Nit * Remove list_senders fn, move logic in macro * Nit macro
- Loading branch information
Showing
7 changed files
with
266 additions
and
38 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
use clap::Parser; | ||
use eyre::Result; | ||
|
||
use foundry_common::{fs, types::ToAlloy}; | ||
use foundry_config::Config; | ||
use foundry_wallets::multi_wallet::MultiWalletOptsBuilder; | ||
|
||
/// CLI arguments for `cast wallet list`. | ||
#[derive(Clone, Debug, Parser)] | ||
pub struct ListArgs { | ||
/// List all the accounts in the keystore directory. | ||
/// Default keystore directory is used if no path provided. | ||
#[clap(long, default_missing_value = "", num_args(0..=1))] | ||
dir: Option<String>, | ||
|
||
/// List accounts from a Ledger hardware wallet. | ||
#[clap(long, short, group = "hw-wallets")] | ||
ledger: bool, | ||
|
||
/// List accounts from a Trezor hardware wallet. | ||
#[clap(long, short, group = "hw-wallets")] | ||
trezor: bool, | ||
|
||
/// List accounts from AWS KMS. | ||
#[clap(long)] | ||
aws: bool, | ||
|
||
/// List all configured accounts. | ||
#[clap(long, group = "hw-wallets")] | ||
all: bool, | ||
|
||
/// Max number of addresses to display from hardware wallets. | ||
#[clap(long, short, default_value = "3", requires = "hw-wallets")] | ||
max_senders: Option<usize>, | ||
} | ||
|
||
impl ListArgs { | ||
pub async fn run(self) -> Result<()> { | ||
// list local accounts as files in keystore dir, no need to unlock / provide password | ||
if self.dir.is_some() || self.all || (!self.ledger && !self.trezor && !self.aws) { | ||
let _ = self.list_local_senders(); | ||
} | ||
|
||
// Create options for multi wallet - ledger, trezor and AWS | ||
let list_opts = MultiWalletOptsBuilder::default() | ||
.ledger(self.ledger || self.all) | ||
.mnemonic_indexes(Some(vec![0])) | ||
.trezor(self.trezor || self.all) | ||
.aws(self.aws || self.all) | ||
.interactives(0) | ||
.build() | ||
.expect("build multi wallet"); | ||
|
||
// macro to print senders for a list of signers | ||
macro_rules! list_senders { | ||
($signers:expr, $label:literal) => { | ||
match $signers.await { | ||
Ok(signers) => { | ||
for signer in signers.unwrap_or_default().iter() { | ||
signer | ||
.available_senders(self.max_senders.unwrap()) | ||
.await? | ||
.iter() | ||
.for_each(|sender| println!("{} ({})", sender.to_alloy(), $label)); | ||
} | ||
} | ||
Err(e) => { | ||
if !self.all { | ||
println!("{}", e) | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
list_senders!(list_opts.ledgers(), "Ledger"); | ||
list_senders!(list_opts.trezors(), "Trezor"); | ||
list_senders!(list_opts.aws_signers(), "AWS"); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn list_local_senders(&self) -> Result<()> { | ||
let keystore_path = self.dir.clone().unwrap_or_default(); | ||
let keystore_dir = if keystore_path.is_empty() { | ||
// Create the keystore default directory if it doesn't exist | ||
let default_dir = Config::foundry_keystores_dir().unwrap(); | ||
fs::create_dir_all(&default_dir)?; | ||
default_dir | ||
} else { | ||
dunce::canonicalize(keystore_path)? | ||
}; | ||
|
||
// list files within keystore dir | ||
std::fs::read_dir(keystore_dir)?.flatten().for_each(|entry| { | ||
let path = entry.path(); | ||
if path.is_file() && path.extension().is_none() { | ||
if let Some(file_name) = path.file_name() { | ||
if let Some(name) = file_name.to_str() { | ||
println!("{} (Local)", name); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.