-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(cast wallet list
) issue #6958: Include HW wallets in cast wallet ls
#7123
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e8a8bf5
issue #6958: Include HW wallets in cast wallet ls
grandizzy 981b966
Changes after review:
grandizzy c4ce461
Changes after review: use list_signers macro
grandizzy 38fc356
Changes after review:
grandizzy 4ecb726
Nit
grandizzy 6474ca3
Remove list_senders fn, move logic in macro
grandizzy a9cd6ad
Nit macro
grandizzy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm personally okay with this
any objections @DaniPopes ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's fine