Explorar o código

vanityaddr: Add searches for TokenId

parazyd %!s(int64=3) %!d(string=hai) anos
pai
achega
2c5a1eb84b
Modificáronse 4 ficheiros con 83 adicións e 32 borrados
  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
 #RUSTFLAGS = -C target-cpu=native
 
 
 # Binaries to be built
 # 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
 # Common dependencies which should force the binaries to be rebuilt
 BINDEPS = \
 BINDEPS = \

+ 1 - 1
bin/vanityaddr/Cargo.toml

@@ -2,7 +2,7 @@
 name = "vanityaddr"
 name = "vanityaddr"
 version = "0.3.0"
 version = "0.3.0"
 homepage = "https://dark.fi"
 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>"]
 authors = ["Dyne.org foundation <foundation@dyne.org>"]
 repository = "https://github.com/darkrenaissance/darkfi"
 repository = "https://github.com/darkrenaissance/darkfi"
 license = "AGPL-3.0-only"
 license = "AGPL-3.0-only"

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

@@ -19,7 +19,7 @@
 use std::{process::exit, sync::mpsc::channel};
 use std::{process::exit, sync::mpsc::channel};
 
 
 use clap::Parser;
 use clap::Parser;
-use darkfi_sdk::crypto::{Keypair, SecretKey};
+use darkfi_sdk::crypto::{PublicKey, SecretKey, TokenId};
 use indicatif::{ProgressBar, ProgressStyle};
 use indicatif::{ProgressBar, ProgressStyle};
 use rand::rngs::OsRng;
 use rand::rngs::OsRng;
 use rayon::prelude::*;
 use rayon::prelude::*;
@@ -37,28 +37,64 @@ struct Args {
     #[clap(short)]
     #[clap(short)]
     case_sensitive: bool,
     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)
     /// Number of threads to use (defaults to number of available CPUs)
     #[clap(short)]
     #[clap(short)]
     threads: Option<usize>,
     threads: Option<usize>,
 }
 }
 
 
 struct DrkAddr {
 struct DrkAddr {
-    pub address: String,
+    pub public: PublicKey,
     pub secret: SecretKey,
     pub secret: SecretKey,
 }
 }
 
 
 impl DrkAddr {
 impl DrkAddr {
     pub fn new() -> Self {
     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 {
     pub fn starts_with(&self, prefix: &str, case_sensitive: bool) -> bool {
         if case_sensitive {
         if case_sensitive {
-            self.address.starts_with(prefix)
+            self.token_id.to_string().starts_with(prefix)
         } else {
         } 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
     // Fire off the threadpool
     rayon_pool.spawn(move || {
     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);
         exit(0);
     });
     });

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

@@ -1,15 +1,15 @@
 vanityaddr
 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
 ## Usage
 
 
 ```
 ```
 vanityaddr 0.3.0
 vanityaddr 0.3.0
-Vanity address generation tool for DarkFi keypairs.
+Vanity address generation tool for DarkFi keypairs and token IDs.
 
 
 USAGE:
 USAGE:
     vanityaddr [OPTIONS] <PREFIX>
     vanityaddr [OPTIONS] <PREFIX>
@@ -19,6 +19,7 @@ ARGS:
 
 
 OPTIONS:
 OPTIONS:
     -c                  Should the search be case-sensitive
     -c                  Should the search be case-sensitive
+    --token-id          Search for TokenID instead of an address
     -h, --help          Print help information
     -h, --help          Print help information
     -t <THREADS>        Number of threads to use (defaults to number of available CPUs)
     -t <THREADS>        Number of threads to use (defaults to number of available CPUs)
     -V, --version       Print version information
     -V, --version       Print version information
@@ -27,7 +28,7 @@ OPTIONS:
 We can use the tool in our command line:
 We can use the tool in our command line:
 
 
 ```
 ```
-% vanityaddr 1Foo
+% vanityaddr drk
 [00:00:05] 53370 attempts
 [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.
 number of attempts it took to find the secret key.
 
 
 ```
 ```
-{"address":"1FoomByzBBQywKaeBB5XPkAm5eCboh8K4CBhBe9uKbJm3kEiCS","attempts":78418,"secret":"0x16545da4a401adcd035ef51c8040acf5f4f1c66c0dd290bb5ec9e95991ae3615"}
+{"address":"DrkZcAiZPQoQUrdii9CUCQC2SNcUrSYEYW4wTj6Nhtp1","attempts":78418,"secret":"BL9zmxqFhCHHU42CPY1G4hj1ahUYh61F54rPBBwLVLVv"}
 ```
 ```