Skip to content
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

Add --address-count options. Generate multiple addresses for the same wallet #207

Merged
merged 16 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 38 additions & 13 deletions wagyu/cli/ethereum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl EthereumWallet {
})
}

#[allow(dead_code)]
howardwu marked this conversation as resolved.
Show resolved Hide resolved
pub fn new_hd<N: EthereumNetwork, W: EthereumWordlist, R: Rng>(
rng: &mut R,
word_count: u8,
Expand Down Expand Up @@ -313,6 +314,7 @@ pub struct EthereumOptions {
json: bool,
subcommand: Option<String>,
// HD and Import HD subcommands
address_count: u8,
derivation: String,
extended_private_key: Option<String>,
extended_public_key: Option<String>,
Expand Down Expand Up @@ -341,6 +343,7 @@ impl Default for EthereumOptions {
json: false,
subcommand: None,
// HD and Import HD subcommands
address_count: 1,
derivation: "ethereum".into(),
extended_private_key: None,
extended_public_key: None,
Expand All @@ -367,6 +370,7 @@ impl EthereumOptions {
fn parse(&mut self, arguments: &ArgMatches, options: &[&str]) {
options.iter().for_each(|option| match *option {
"address" => self.address(arguments.value_of(option)),
"address count" => self.address_count(clap::value_t!(arguments.value_of(*option), u8).ok()),
"count" => self.count(clap::value_t!(arguments.value_of(*option), usize).ok()),
"createrawtransaction" => self.create_raw_transaction(arguments.value_of(option)),
"derivation" => self.derivation(arguments.value_of(option)),
Expand Down Expand Up @@ -394,6 +398,14 @@ impl EthereumOptions {
}
}

/// Sets `address_count` to the specified address count, overriding its previous state.
/// If the specified argument is `None`, then no change occurs.
fn address_count(&mut self, argument: Option<u8>) {
if let Some(address_count) = argument {
self.address_count = address_count;
}
}

/// Sets `count` to the specified count, overriding its previous state.
fn count(&mut self, argument: Option<usize>) {
if let Some(count) = argument {
Expand Down Expand Up @@ -534,14 +546,16 @@ impl EthereumOptions {
/// If `default` is enabled, then return the default path if no derivation was provided.
fn to_derivation_path(&self, default: bool) -> Option<String> {
match self.derivation.as_str() {
"ethereum" => Some(format!("m/44'/60'/0'/{}", self.index)),
"ethereum" => Some(format!("m/44'/60'/0'/0/{}", self.index)),
howardwu marked this conversation as resolved.
Show resolved Hide resolved
"ethereum-legacy" => Some(format!("m/44'/60'/0'/{}", self.index)),
"keepkey" => Some(format!("m/44'/60'/{}'/0", self.index)),
"ledger-legacy" => Some(format!("m/44'/60'/0'/{}", self.index)),
"ledger-live" => Some(format!("m/44'/60'/{}'/0/0", self.index)),
"trezor" => Some(format!("m/44'/60'/0'/{}", self.index)),
"trezor-bip44" => Some(format!("m/44'/60'/0'/0/{}", self.index)),
"custom" => self.path.clone(),
_ => match default {
true => Some(format!("m/44'/60'/0'/{}", self.index)),
true => Some(format!("m/44'/60'/0'/0/{}", self.index)),
false => None,
},
howardwu marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -574,7 +588,10 @@ impl CLI for EthereumCLI {
("hd", Some(arguments)) => {
options.subcommand = Some("hd".into());
options.parse(arguments, &["count", "json"]);
options.parse(arguments, &["derivation", "language", "password", "word count"]);
options.parse(
arguments,
&["derivation", "language", "password", "word count", "address count"],
);
}
("import", Some(arguments)) => {
options.subcommand = Some("import".into());
Expand Down Expand Up @@ -613,22 +630,30 @@ impl CLI for EthereumCLI {
fn print(options: Self::Options) -> Result<(), CLIError> {
fn output<N: EthereumNetwork, W: EthereumWordlist>(options: EthereumOptions) -> Result<(), CLIError> {
let wallets = match options.subcommand.as_ref().map(String::as_str) {
Some("hd") => {
let path = options.to_derivation_path(true).unwrap();
(0..options.count)
.flat_map(|_| {
match EthereumWallet::new_hd::<N, W, _>(
&mut StdRng::from_entropy(),
options.word_count,
options.password.as_ref().map(String::as_str),
Some("hd") => (0..options.count)
.flat_map(|_| {
let mut rnd = StdRng::from_entropy();
let mnemonic = EthereumMnemonic::<N, W>::new_with_count(&mut rnd, options.word_count).unwrap();
howardwu marked this conversation as resolved.
Show resolved Hide resolved
let password = options.password.as_ref().map(String::as_str);
let mut opt = options.clone();
let mut i = 0;

(0..options.address_count).flat_map(move |_| {
howardwu marked this conversation as resolved.
Show resolved Hide resolved
let path = opt.to_derivation_path(true).unwrap();
i += 1;
opt.index(Some(i));

match EthereumWallet::from_mnemonic::<N, W>(
&format!("{}", mnemonic),
password,
&path,
) {
Ok(wallet) => vec![wallet],
_ => vec![],
}
})
.collect()
}
})
.collect(),
Some("import") => {
if let Some(private_key) = options.private {
vec![EthereumWallet::from_private_key(&private_key)?]
Expand Down
6 changes: 6 additions & 0 deletions wagyu/cli/parameters/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ pub const SUBADDRESS_IMPORT_MONERO: OptionType = (

// HD

pub const ADDRESS_COUNT: OptionType = (
"[address count] -a --address-count=[address count] 'Generates an HD wallet with a specified address count'",
howardwu marked this conversation as resolved.
Show resolved Hide resolved
&[],
&[],
&[],
);
pub const DERIVATION_BITCOIN: OptionType = (
"[derivation] -d --derivation=[\"path\"] 'Generates an HD wallet for a specified derivation path (in quotes) [possible values: bip32, bip44, bip49, \"<custom path>\"]'",
&[],
Expand Down
1 change: 1 addition & 0 deletions wagyu/cli/parameters/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub const HD_ETHEREUM: SubCommandType = (
"hd",
"Generates an HD wallet (include -h for more options)",
&[
option::ADDRESS_COUNT,
option::COUNT,
option::DERIVATION_ETHEREUM,
option::LANGUAGE_HD,
Expand Down