aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicholas Rodrigues Lordello <[email protected]>2024-07-11 09:04:48 +0200
committerNicholas Rodrigues Lordello <[email protected]>2024-07-11 09:06:42 +0200
commita71e61dea52c261c73d32df4e521ff0a0760c088 (patch)
treed629ff9f2fb062655556696d447f2fbfe753de77
parenta3612d21d164de14f4aeb4442ef747eb8fdfa8eb (diff)
Added multi-core support for vanity address mining
-rw-r--r--Cargo.lock17
-rw-r--r--Cargo.toml1
-rw-r--r--src/cmd.rs2
-rw-r--r--src/cmd/new.rs55
-rw-r--r--src/main.rs2
5 files changed, 66 insertions, 11 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 761f8db..d6dbbd1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -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"
diff --git a/Cargo.toml b/Cargo.toml
index 7c0eaab..c99c21e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -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"
diff --git a/src/cmd.rs b/src/cmd.rs
index 31add32..0be06ab 100644
--- a/src/cmd.rs
+++ b/src/cmd.rs
@@ -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);
}