main.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. use std::{process::exit, sync::mpsc::channel};
  2. use clap::Parser;
  3. use indicatif::{ProgressBar, ProgressStyle};
  4. use rand::rngs::OsRng;
  5. use rayon::prelude::*;
  6. use serde_json::json;
  7. use darkfi::crypto::{
  8. address::Address,
  9. keypair::{Keypair, SecretKey},
  10. };
  11. #[derive(Parser)]
  12. #[clap(name = "vanityaddr", about, version)]
  13. #[clap(arg_required_else_help(true))]
  14. struct Args {
  15. /// Prefixes to search (must start with 1)
  16. prefix: Vec<String>,
  17. /// Should the search be case-sensitive
  18. #[clap(short)]
  19. case_sensitive: bool,
  20. /// Number of threads to use (defaults to number of available CPUs)
  21. #[clap(short, parse(try_from_str))]
  22. threads: Option<usize>,
  23. }
  24. struct DrkAddr {
  25. pub address: String,
  26. pub secret: SecretKey,
  27. }
  28. impl DrkAddr {
  29. pub fn new() -> Self {
  30. let kp = Keypair::random(&mut OsRng);
  31. let addr = Address::from(kp.public);
  32. Self { secret: kp.secret, address: format!("{}", addr) }
  33. }
  34. pub fn starts_with(&self, prefix: &str, case_sensitive: bool) -> bool {
  35. if case_sensitive {
  36. self.address.starts_with(prefix)
  37. } else {
  38. self.address.to_lowercase().starts_with(prefix.to_lowercase().as_str())
  39. }
  40. }
  41. pub fn starts_with_any(&self, prefixes: &[String], case_sensitive: bool) -> bool {
  42. for prefix in prefixes {
  43. if self.starts_with(prefix, case_sensitive) {
  44. return true
  45. }
  46. }
  47. false
  48. }
  49. }
  50. fn main() {
  51. let args = Args::parse();
  52. if args.prefix.is_empty() {
  53. eprintln!("Error: No prefix given to search.");
  54. exit(1);
  55. }
  56. for (idx, prefix) in args.prefix.iter().enumerate() {
  57. if !prefix.starts_with('1') {
  58. eprintln!("Error: Address prefix at index {} must start with \"1\".", idx);
  59. exit(1);
  60. }
  61. }
  62. // Check if prefixes are valid base58
  63. for (idx, prefix) in args.prefix.iter().enumerate() {
  64. match bs58::decode(prefix).into_vec() {
  65. Ok(_) => {}
  66. Err(e) => {
  67. eprintln!("Error: Invalid base58 for prefix {}: {}", idx, e);
  68. exit(1);
  69. }
  70. };
  71. }
  72. // Threadpool
  73. let num_threads = if args.threads.is_some() { args.threads.unwrap() } else { num_cpus::get() };
  74. let rayon_pool = rayon::ThreadPoolBuilder::new().num_threads(num_threads).build().unwrap();
  75. // Handle SIGINT
  76. let (tx, rx) = channel();
  77. ctrlc::set_handler(move || tx.send(()).expect("Could not send signal on channel"))
  78. .expect("Error setting SIGINT handler");
  79. // Something fancy
  80. let progress = ProgressBar::new_spinner();
  81. progress.set_style(ProgressStyle::default_bar().template("[{elapsed_precise}] {pos} attempts"));
  82. progress.set_draw_rate(10);
  83. // Fire off the threadpool
  84. rayon_pool.spawn(move || {
  85. let addr = rayon::iter::repeat(DrkAddr::new)
  86. .inspect(|_| progress.inc(1))
  87. .map(|create| create())
  88. .find_any(|address| address.starts_with_any(&args.prefix, args.case_sensitive))
  89. .expect("Failed to find an address match");
  90. let attempts = progress.position();
  91. progress.finish_and_clear();
  92. let result = json!({
  93. "address": addr.address,
  94. "secret": format!("{:?}", addr.secret.0),
  95. "attempts": attempts,
  96. });
  97. println!("{}", result);
  98. exit(0);
  99. });
  100. // This now blocks and lets our threadpool execute in the background.
  101. rx.recv().expect("Could not receive from channel");
  102. eprintln!("\rCaught SIGINT, exiting...");
  103. exit(127);
  104. }