Răsfoiți Sursa

vanityaddr: Add searches for TokenId

parazyd 3 ani în urmă
părinte
comite
2c5a1eb84b
4 a modificat fișierele cu 83 adăugiri și 32 ștergeri
  1. 1 1
      Makefile
  2. 1 1
      bin/vanityaddr/Cargo.toml
  3. 74 24
      bin/vanityaddr/src/main.rs
  4. 7 6
      doc/src/misc/vanityaddr.md

+ 1 - 1
Makefile

@@ -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 = \

+ 1 - 1
bin/vanityaddr/Cargo.toml

@@ -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"

+ 74 - 24
bin/vanityaddr/src/main.rs

@@ -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 { public, secret }
+    }
+
+    pub fn starts_with(&self, prefix: &str, case_sensitive: bool) -> bool {
+        if case_sensitive {
+            self.public.to_string().starts_with(prefix)
+        } else {
+            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 { secret: kp.secret, address: format!("{}", kp.public) }
+        Self { token_id, secret }
     }
 
     pub fn starts_with(&self, prefix: &str, case_sensitive: bool) -> bool {
         if case_sensitive {
-            self.address.starts_with(prefix)
+            self.token_id.to_string().starts_with(prefix)
         } else {
-            self.address.to_lowercase().starts_with(prefix.to_lowercase().as_str())
+            self.token_id.to_string().to_lowercase().starts_with(prefix.to_lowercase().as_str())
         }
     }
 
@@ -108,24 +144,38 @@ fn main() {
 
     // Fire off the threadpool
     rayon_pool.spawn(move || {
-        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");
-
-        // The above will keep running until it finds a match or until the
-        // program terminates. Only if a match is found shall the following
-        // code be executed and the program exit successfully:
-        let attempts = progress.position();
-        progress.finish_and_clear();
-
-        println!(
-            "{{\"address\":\"{}\",\"attempts\":{},\"secret\":\"{:?}\"}}",
-            addr.address,
-            attempts,
-            addr.secret.inner()
-        );
+        if args.token_id {
+            let tid = rayon::iter::repeat(DrkToken::new)
+                .inspect(|_| progress.inc(1))
+                .map(|create| create())
+                .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
+            // code be executed and the program exit successfully:
+            let attempts = progress.position();
+            progress.finish_and_clear();
+
+            println!(
+                "{{\"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);
     });

+ 7 - 6
doc/src/misc/vanityaddr.md

@@ -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"}
 ```