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 14 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ FLAGS:
OPTIONS:
-c, --count <count> Generates a specified number of wallets
-d, --derivation <"path"> Generates an HD wallet for a specified derivation path (in quotes) [possible values: ethereum, keepkey, ledger-legacy, ledger-live, trezor, "<custom path>"]
-i, --indices <indices> Generates an HD wallet with a specified indices
-l, --language <language> Generates an HD wallet with a specified language [possible values: chinese_simplified, chinese_traditional, english, french, italian, japanese, korean, spanish]
-p, --password <password> Generates an HD wallet with a specified password
-w, --word-count <word count> Generates an HD wallet with a specified word count [possible values: 12, 15, 18, 21, 24]
Expand Down
119 changes: 86 additions & 33 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 @@ -317,6 +318,7 @@ pub struct EthereumOptions {
extended_private_key: Option<String>,
extended_public_key: Option<String>,
index: u32,
indices: u32,
language: String,
mnemonic: Option<String>,
password: Option<String>,
Expand Down Expand Up @@ -345,6 +347,7 @@ impl Default for EthereumOptions {
extended_private_key: None,
extended_public_key: None,
index: 0,
indices: 1,
language: "english".into(),
mnemonic: None,
password: None,
Expand Down Expand Up @@ -374,6 +377,7 @@ impl EthereumOptions {
"extended public" => self.extended_public(arguments.value_of(option)),
"json" => self.json(arguments.is_present(option)),
"index" => self.index(clap::value_t!(arguments.value_of(*option), u32).ok()),
"indices" => self.indices(clap::value_t!(arguments.value_of(*option), u32).ok()),
"language" => self.language(arguments.value_of(option)),
"mnemonic" => self.mnemonic(arguments.value_of(option)),
"network" => self.network(arguments.value_of(option)),
Expand Down Expand Up @@ -451,6 +455,14 @@ impl EthereumOptions {
}
}

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

/// Sets `json` to the specified boolean value, overriding its previous state.
fn json(&mut self, argument: bool) {
self.json = argument;
Expand Down Expand Up @@ -534,14 +546,14 @@ 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
"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" => Some(format!("m/44'/60'/0'/0/{}", self.index)),
howardwu marked this conversation as resolved.
Show resolved Hide resolved
"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,
},
}
Expand Down Expand Up @@ -574,7 +586,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", "index", "indices", "language", "password", "word count"],
);
}
("import", Some(arguments)) => {
options.subcommand = Some("import".into());
Expand All @@ -593,6 +608,7 @@ impl CLI for EthereumCLI {
"extended private",
"extended public",
"index",
"indices",
"mnemonic",
"password",
],
Expand All @@ -613,22 +629,28 @@ 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),
&path,
) {
Some("hd") => (0..options.count)
.flat_map(|_| {
let mut rng = StdRng::from_entropy();
let mnemonic = EthereumMnemonic::<N, W>::new_with_count(&mut rng, 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 hd_wallet_from_mnemonic = move |i| {
opt.index(Some(i));
let path = opt.to_derivation_path(true).unwrap();

match EthereumWallet::from_mnemonic::<N, W>(&format!("{}", mnemonic), password, &path) {
Ok(wallet) => vec![wallet],
_ => vec![],
}
})
.collect()
}
};
if options.index > 0 {
(options.index..options.index + 1).flat_map(hd_wallet_from_mnemonic)
} else {
(0..options.indices).flat_map(hd_wallet_from_mnemonic)
}
})
.collect(),
Some("import") => {
if let Some(private_key) = options.private {
vec![EthereumWallet::from_private_key(&private_key)?]
Expand All @@ -641,6 +663,19 @@ impl CLI for EthereumCLI {
}
}
Some("import-hd") => {
let begin;
let end;
if options.index > 0 {
begin = options.index;
end = options.index + 1;
} else if options.indices > 1 {
begin = 0;
end = options.indices;
} else {
begin = 0;
end = 1;
}
let mut ops = options.clone();
if let Some(mnemonic) = options.mnemonic.clone() {
fn process_mnemonic<EN: EthereumNetwork, EW: EthereumWordlist>(
mnemonic: &String,
Expand All @@ -652,24 +687,42 @@ impl CLI for EthereumCLI {
&options.to_derivation_path(true).unwrap(),
)
}
vec![process_mnemonic::<N, ChineseSimplified>(&mnemonic, &options)
.or(process_mnemonic::<N, ChineseTraditional>(&mnemonic, &options))
.or(process_mnemonic::<N, English>(&mnemonic, &options))
.or(process_mnemonic::<N, French>(&mnemonic, &options))
.or(process_mnemonic::<N, Italian>(&mnemonic, &options))
.or(process_mnemonic::<N, Japanese>(&mnemonic, &options))
.or(process_mnemonic::<N, Korean>(&mnemonic, &options))
.or(process_mnemonic::<N, Spanish>(&mnemonic, &options))?]
(begin..end)
.map(|i| {
ops.index(Some(i));
process_mnemonic::<N, ChineseSimplified>(&mnemonic, &ops)
.or(process_mnemonic::<N, ChineseTraditional>(&mnemonic, &ops))
.or(process_mnemonic::<N, English>(&mnemonic, &ops))
.or(process_mnemonic::<N, French>(&mnemonic, &ops))
.or(process_mnemonic::<N, Italian>(&mnemonic, &ops))
.or(process_mnemonic::<N, Japanese>(&mnemonic, &ops))
.or(process_mnemonic::<N, Korean>(&mnemonic, &ops))
.or(process_mnemonic::<N, Spanish>(&mnemonic, &ops))
.unwrap()
})
.collect()
} else if let Some(extended_private_key) = options.extended_private_key.clone() {
vec![EthereumWallet::from_extended_private_key::<N>(
&extended_private_key,
&options.to_derivation_path(false),
)?]
(begin..end)
.map(|i| {
ops.index(Some(i));
EthereumWallet::from_extended_private_key::<N>(
&extended_private_key,
&ops.to_derivation_path(false),
)
.unwrap()
})
.collect()
} else if let Some(extended_public_key) = options.extended_public_key.clone() {
vec![EthereumWallet::from_extended_public_key::<N>(
&extended_public_key,
&options.to_derivation_path(false),
)?]
(begin..end)
.map(|i| {
ops.index(Some(i));
EthereumWallet::from_extended_public_key::<N>(
&extended_public_key,
&ops.to_derivation_path(false),
)
.unwrap()
})
.collect()
} else {
vec![]
}
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 @@ -205,6 +205,12 @@ pub const DIVERSIFIER_HD_ZCASH: OptionType = (
&[],
&[],
);
pub const INDICES: OptionType = (
"[indices] -s --indices=[indices] 'Generates an HD wallet with a specified indices'",
&["index"],
&[],
&[],
);
pub const LANGUAGE_HD: OptionType = (
"[language] -l --language=[language] 'Generates an HD wallet with a specified language'",
&[],
Expand Down
3 changes: 3 additions & 0 deletions wagyu/cli/parameters/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ pub const HD_ETHEREUM: SubCommandType = (
&[
option::COUNT,
option::DERIVATION_ETHEREUM,
option::INDEX,
option::INDICES,
option::LANGUAGE_HD,
option::PASSWORD_HD,
option::WORD_COUNT,
Expand Down Expand Up @@ -156,6 +158,7 @@ pub const IMPORT_HD_ETHEREUM: SubCommandType = (
option::EXTENDED_PUBLIC,
option::EXTENDED_PRIVATE,
option::INDEX,
option::INDICES,
option::MNEMONIC,
option::PASSWORD_IMPORT_HD,
],
Expand Down