vanityaddr: Add searches for TokenId

This commit is contained in:
parazyd
2022-12-12 15:10:44 +01:00
parent ce71169a8a
commit 2c5a1eb84b
4 changed files with 81 additions and 30 deletions

View File

@@ -10,7 +10,7 @@ CARGO = cargo
#RUSTFLAGS = -C target-cpu=native
# Binaries to be built
BINS = drk darkfid tau taud ircd dnetview darkwikid darkwiki faucetd
BINS = drk darkfid tau taud ircd dnetview darkwikid darkwiki faucetd vanityaddr
# Common dependencies which should force the binaries to be rebuilt
BINDEPS = \

View File

@@ -2,7 +2,7 @@
name = "vanityaddr"
version = "0.3.0"
homepage = "https://dark.fi"
description = "Vanity address generation tool for DarkFi keypairs."
description = "Vanity address generation tool for DarkFi keypairs and token IDs"
authors = ["Dyne.org foundation <foundation@dyne.org>"]
repository = "https://github.com/darkrenaissance/darkfi"
license = "AGPL-3.0-only"

View File

@@ -19,7 +19,7 @@
use std::{process::exit, sync::mpsc::channel};
use clap::Parser;
use darkfi_sdk::crypto::{Keypair, SecretKey};
use darkfi_sdk::crypto::{PublicKey, SecretKey, TokenId};
use indicatif::{ProgressBar, ProgressStyle};
use rand::rngs::OsRng;
use rayon::prelude::*;
@@ -37,28 +37,64 @@ struct Args {
#[clap(short)]
case_sensitive: bool,
/// Search for TokenId instead of an address
#[clap(long)]
token_id: bool,
/// Number of threads to use (defaults to number of available CPUs)
#[clap(short)]
threads: Option<usize>,
}
struct DrkAddr {
pub address: String,
pub public: PublicKey,
pub secret: SecretKey,
}
impl DrkAddr {
pub fn new() -> Self {
let kp = Keypair::random(&mut OsRng);
let secret = SecretKey::random(&mut OsRng);
let public = PublicKey::from_secret(secret);
Self { secret: kp.secret, address: format!("{}", kp.public) }
Self { public, secret }
}
pub fn starts_with(&self, prefix: &str, case_sensitive: bool) -> bool {
if case_sensitive {
self.address.starts_with(prefix)
self.public.to_string().starts_with(prefix)
} else {
self.address.to_lowercase().starts_with(prefix.to_lowercase().as_str())
self.public.to_string().to_lowercase().starts_with(prefix.to_lowercase().as_str())
}
}
pub fn starts_with_any(&self, prefixes: &[String], case_sensitive: bool) -> bool {
for prefix in prefixes {
if self.starts_with(prefix, case_sensitive) {
return true
}
}
false
}
}
struct DrkToken {
pub token_id: TokenId,
pub secret: SecretKey,
}
impl DrkToken {
pub fn new() -> Self {
let secret = SecretKey::random(&mut OsRng);
let token_id = TokenId::derive(secret);
Self { token_id, secret }
}
pub fn starts_with(&self, prefix: &str, case_sensitive: bool) -> bool {
if case_sensitive {
self.token_id.to_string().starts_with(prefix)
} else {
self.token_id.to_string().to_lowercase().starts_with(prefix.to_lowercase().as_str())
}
}
@@ -108,11 +144,12 @@ fn main() {
// Fire off the threadpool
rayon_pool.spawn(move || {
let addr = rayon::iter::repeat(DrkAddr::new)
if args.token_id {
let tid = rayon::iter::repeat(DrkToken::new)
.inspect(|_| progress.inc(1))
.map(|create| create())
.find_any(|address| address.starts_with_any(&args.prefix, args.case_sensitive))
.expect("Failed to find an address match");
.find_any(|token_id| token_id.starts_with_any(&args.prefix, args.case_sensitive))
.expect("Failed to find a token ID match");
// The above will keep running until it finds a match or until the
// program terminates. Only if a match is found shall the following
@@ -121,11 +158,24 @@ fn main() {
progress.finish_and_clear();
println!(
"{{\"address\":\"{}\",\"attempts\":{},\"secret\":\"{:?}\"}}",
addr.address,
attempts,
addr.secret.inner()
"{{\"token_id\":\"{}\",\"attempts\":{},\"secret\":\"{}\"}}",
tid.token_id, attempts, tid.secret,
);
} else {
let addr = rayon::iter::repeat(DrkAddr::new)
.inspect(|_| progress.inc(1))
.map(|create| create())
.find_any(|address| address.starts_with_any(&args.prefix, args.case_sensitive))
.expect("Failed to find an address match");
let attempts = progress.position();
progress.finish_and_clear();
println!(
"{{\"address\":\"{}\",\"attempts\":{},\"secret\":\"{}\"}}",
addr.public, attempts, addr.secret,
);
}
exit(0);
});

View File

@@ -1,15 +1,15 @@
vanityaddr
==========
A tool for Vanity address generation for DarkFi keypairs. Given some
prefix, the tool will bruteforce secret keys to find one which, when
derived into an address, starts with a given prefix.
A tool for Vanity address generation for DarkFi keypairs and token IDs.
Given some prefix, the tool will bruteforce secret keys to find one
which, when derived, starts with a given prefix.
## Usage
```
vanityaddr 0.3.0
Vanity address generation tool for DarkFi keypairs.
Vanity address generation tool for DarkFi keypairs and token IDs.
USAGE:
vanityaddr [OPTIONS] <PREFIX>
@@ -19,6 +19,7 @@ ARGS:
OPTIONS:
-c Should the search be case-sensitive
--token-id Search for TokenID instead of an address
-h, --help Print help information
-t <THREADS> Number of threads to use (defaults to number of available CPUs)
-V, --version Print version information
@@ -27,7 +28,7 @@ OPTIONS:
We can use the tool in our command line:
```
% vanityaddr 1Foo
% vanityaddr drk
[00:00:05] 53370 attempts
```
@@ -36,5 +37,5 @@ we will get JSON output containing an address, secret key, and the
number of attempts it took to find the secret key.
```
{"address":"1FoomByzBBQywKaeBB5XPkAm5eCboh8K4CBhBe9uKbJm3kEiCS","attempts":78418,"secret":"0x16545da4a401adcd035ef51c8040acf5f4f1c66c0dd290bb5ec9e95991ae3615"}
{"address":"DrkZcAiZPQoQUrdii9CUCQC2SNcUrSYEYW4wTj6Nhtp1","attempts":78418,"secret":"BL9zmxqFhCHHU42CPY1G4hj1ahUYh61F54rPBBwLVLVv"}
```