diff options
| -rw-r--r-- | Cargo.lock | 17 | ||||
| -rw-r--r-- | Cargo.toml | 1 | ||||
| -rw-r--r-- | src/cmd.rs | 2 | ||||
| -rw-r--r-- | src/cmd/new.rs | 55 | ||||
| -rw-r--r-- | src/main.rs | 2 |
5 files changed, 66 insertions, 11 deletions
@@ -308,6 +308,7 @@ dependencies = [ "hmac", "k256", "maplit", + "num_cpus", "pbkdf2", "serde", "serde_json", @@ -322,6 +323,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" [[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] name = "hex" version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -390,6 +397,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" [[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] name = "once_cell" version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -18,6 +18,7 @@ ethnum = { version = "1", features = ["serde"] } hex = "0.4" hmac = { version = "0.12", features = ["std"] } k256 = "0.13" +num_cpus = "1" pbkdf2 = { version = "0.12", default-features = false } serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -18,7 +18,7 @@ use std::{ }; /// Shared account options. -#[derive(Debug, Parser)] +#[derive(Clone, Debug, Parser)] struct AccountOptions { /// The BIP-0039 mnemonic phrase for seeding the HD wallet. #[clap(short, long, env, hide_env_values = true)] diff --git a/src/cmd/new.rs b/src/cmd/new.rs index 29d7c88..825e19f 100644 --- a/src/cmd/new.rs +++ b/src/cmd/new.rs @@ -9,6 +9,8 @@ use hdwallet::mnemonic::{Language, Mnemonic}; use std::{ fmt::{self, Display, Formatter}, str::FromStr, + sync::mpsc, + thread, }; #[derive(Debug, Parser)] @@ -40,6 +42,11 @@ pub struct Options { /// with the "--vanity-3account-index" option. #[clap(long, conflicts_with = "vanity_account_index")] vanity_hd_path: Option<String>, + + /// The number of parallel threads to use when searching for a vanity + /// address. Defaults to the number of CPUs. + #[clap(short = 'j', long, default_value_t = num_cpus::get())] + vanity_threads: usize, } #[derive(Clone, Debug)] @@ -106,19 +113,47 @@ impl FromStr for Prefix { } pub fn run(options: Options) -> Result<()> { - let random_mnemonic = || Mnemonic::random(options.language, options.length); + let random_mnemonic = { + let language = options.language; + let length = options.length; + move || Mnemonic::random(language, length) + }; + let mnemonic = if let Some(prefix) = options.vanity_prefix { - let mut account = AccountOptions { - mnemonic: random_mnemonic()?, - password: options.vanity_password, - account_index: options.vanity_account_index, - hd_path: options.vanity_hd_path, + let vanity = { + let prefix = prefix.clone(); + let mut account = AccountOptions { + mnemonic: random_mnemonic()?, + password: options.vanity_password, + account_index: options.vanity_account_index, + hd_path: options.vanity_hd_path, + }; + move || -> Result<Mnemonic> { + while !prefix.matches(account.private_key()?.address()) { + account.mnemonic = random_mnemonic()?; + } + Ok(account.mnemonic) + } }; - while !prefix.matches(account.private_key()?.address()) { - account.mnemonic = random_mnemonic()?; - } - account.mnemonic + if options.vanity_threads > 0 { + let (sender, receiver) = mpsc::channel(); + let _threads = (0..options.vanity_threads) + .map(|_| { + thread::spawn({ + let vanity = vanity.clone(); + let result = sender.clone(); + move || { + let _ = result.send(vanity()); + } + }) + }) + .collect::<Vec<_>>(); + + receiver.recv().unwrap()? + } else { + vanity()? + } } else { random_mnemonic()? }; diff --git a/src/main.rs b/src/main.rs index 3f91b7a..6a87bef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,4 +40,6 @@ fn main() { } process::exit(-1); } + + process::exit(0); } |
