diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2024-07-11 09:04:48 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2024-07-11 09:06:42 +0200 |
| commit | a71e61dea52c261c73d32df4e521ff0a0760c088 (patch) | |
| tree | d629ff9f2fb062655556696d447f2fbfe753de77 /src | |
| parent | a3612d21d164de14f4aeb4442ef747eb8fdfa8eb (diff) | |
Added multi-core support for vanity address mining
Diffstat (limited to 'src')
| -rw-r--r-- | src/cmd.rs | 2 | ||||
| -rw-r--r-- | src/cmd/new.rs | 55 | ||||
| -rw-r--r-- | src/main.rs | 2 |
3 files changed, 48 insertions, 11 deletions
@@ -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); } |
