2019-09-12 18:37:29 -07:00
|
|
|
use bip39::{Language, Mnemonic, MnemonicType, Seed};
|
2019-10-30 20:47:42 -07:00
|
|
|
use bs58;
|
2019-06-07 17:54:54 -07:00
|
|
|
use clap::{
|
2019-11-13 20:10:38 -08:00
|
|
|
crate_description, crate_name, values_t_or_exit, App, AppSettings, Arg, ArgMatches, SubCommand,
|
2019-06-07 17:54:54 -07:00
|
|
|
};
|
2019-11-03 19:41:26 -08:00
|
|
|
use num_cpus;
|
2019-11-25 20:33:15 -08:00
|
|
|
use solana_clap_utils::keypair::{keypair_from_seed_phrase, SKIP_SEED_PHRASE_VALIDATION_ARG};
|
2019-11-03 19:41:26 -08:00
|
|
|
use solana_sdk::{
|
2019-11-06 11:18:25 -08:00
|
|
|
pubkey::write_pubkey_file,
|
2019-11-03 19:41:26 -08:00
|
|
|
signature::{
|
|
|
|
keypair_from_seed, read_keypair, read_keypair_file, write_keypair, write_keypair_file,
|
|
|
|
Keypair, KeypairUtil,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
use std::{
|
|
|
|
collections::HashSet,
|
|
|
|
error,
|
|
|
|
path::Path,
|
|
|
|
process::exit,
|
|
|
|
sync::{
|
|
|
|
atomic::{AtomicU64, Ordering},
|
|
|
|
Arc,
|
|
|
|
},
|
|
|
|
thread,
|
|
|
|
time::Instant,
|
2019-10-10 16:01:03 -07:00
|
|
|
};
|
2019-06-07 17:54:54 -07:00
|
|
|
|
2019-09-12 18:37:29 -07:00
|
|
|
const NO_PASSPHRASE: &str = "";
|
|
|
|
|
2019-06-07 17:54:54 -07:00
|
|
|
fn check_for_overwrite(outfile: &str, matches: &ArgMatches) {
|
|
|
|
let force = matches.is_present("force");
|
|
|
|
if !force && Path::new(outfile).exists() {
|
|
|
|
eprintln!("Refusing to overwrite {} without --force flag", outfile);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2018-07-12 10:59:40 -07:00
|
|
|
|
2019-10-10 16:01:03 -07:00
|
|
|
fn output_keypair(
|
|
|
|
keypair: &Keypair,
|
|
|
|
outfile: &str,
|
|
|
|
source: &str,
|
|
|
|
) -> Result<(), Box<dyn error::Error>> {
|
|
|
|
if outfile == "-" {
|
|
|
|
let mut stdout = std::io::stdout();
|
|
|
|
write_keypair(&keypair, &mut stdout)?;
|
|
|
|
} else {
|
|
|
|
write_keypair_file(&keypair, outfile)?;
|
|
|
|
eprintln!("Wrote {} keypair to {}", source, outfile);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-12-08 21:44:20 -08:00
|
|
|
fn main() -> Result<(), Box<dyn error::Error>> {
|
2019-03-13 20:54:30 -07:00
|
|
|
let matches = App::new(crate_name!())
|
|
|
|
.about(crate_description!())
|
2019-11-13 20:10:38 -08:00
|
|
|
.version(solana_clap_utils::version!())
|
2019-06-07 17:54:54 -07:00
|
|
|
.setting(AppSettings::SubcommandRequiredElseHelp)
|
2019-03-19 14:19:50 -07:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("new")
|
2019-11-25 20:33:15 -08:00
|
|
|
.about("Generate new keypair file from a passphrase and random seed phrase")
|
2019-06-07 17:54:54 -07:00
|
|
|
.setting(AppSettings::DisableVersion)
|
2019-03-19 14:19:50 -07:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("outfile")
|
|
|
|
.short("o")
|
|
|
|
.long("outfile")
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Path to generated file"),
|
2019-06-07 17:54:54 -07:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("force")
|
|
|
|
.short("f")
|
|
|
|
.long("force")
|
|
|
|
.help("Overwrite the output file if it exists"),
|
2019-09-12 18:37:29 -07:00
|
|
|
)
|
2019-11-25 20:33:15 -08:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("no_passphrase")
|
|
|
|
.long("no-passphrase")
|
|
|
|
.help("Do not prompt for a passphrase"),
|
|
|
|
)
|
2019-09-12 18:37:29 -07:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("silent")
|
|
|
|
.short("s")
|
|
|
|
.long("silent")
|
2019-11-25 20:33:15 -08:00
|
|
|
.help("Do not display seed phrase. Useful when piping output to other programs that prompt for user input, like gpg"),
|
2019-10-30 20:47:42 -07:00
|
|
|
)
|
2019-11-03 19:41:26 -08:00
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("grind")
|
|
|
|
.about("Grind for vanity keypairs")
|
|
|
|
.setting(AppSettings::DisableVersion)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("ignore_case")
|
|
|
|
.long("ignore-case")
|
|
|
|
.help("Perform case insensitive matches"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("includes")
|
|
|
|
.long("includes")
|
|
|
|
.value_name("BASE58")
|
|
|
|
.takes_value(true)
|
|
|
|
.multiple(true)
|
|
|
|
.validator(|value| {
|
|
|
|
bs58::decode(&value).into_vec()
|
|
|
|
.map(|_| ())
|
|
|
|
.map_err(|err| format!("{}: {:?}", value, err))
|
|
|
|
})
|
|
|
|
.help("Save keypair if its public key includes this string\n(may be specified multiple times)"),
|
|
|
|
)
|
2019-10-30 20:47:42 -07:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("starts_with")
|
|
|
|
.long("starts-with")
|
|
|
|
.value_name("BASE58 PREFIX")
|
|
|
|
.takes_value(true)
|
2019-11-03 19:41:26 -08:00
|
|
|
.multiple(true)
|
2019-10-30 20:47:42 -07:00
|
|
|
.validator(|value| {
|
2019-11-03 19:41:26 -08:00
|
|
|
bs58::decode(&value).into_vec()
|
2019-10-30 20:47:42 -07:00
|
|
|
.map(|_| ())
|
2019-11-03 19:41:26 -08:00
|
|
|
.map_err(|err| format!("{}: {:?}", value, err))
|
2019-10-30 20:47:42 -07:00
|
|
|
})
|
2019-11-03 19:41:26 -08:00
|
|
|
.help("Save keypair if its public key starts with this prefix\n(may be specified multiple times)"),
|
2019-03-19 14:19:50 -07:00
|
|
|
),
|
|
|
|
)
|
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("pubkey")
|
2019-04-23 19:24:42 -07:00
|
|
|
.about("Display the pubkey from a keypair file")
|
2019-06-07 17:54:54 -07:00
|
|
|
.setting(AppSettings::DisableVersion)
|
2019-03-19 14:19:50 -07:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("infile")
|
|
|
|
.index(1)
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Path to keypair file"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("outfile")
|
|
|
|
.short("o")
|
|
|
|
.long("outfile")
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Path to generated file"),
|
2019-06-07 17:54:54 -07:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("force")
|
|
|
|
.short("f")
|
|
|
|
.long("force")
|
|
|
|
.help("Overwrite the output file if it exists"),
|
2019-03-19 14:19:50 -07:00
|
|
|
),
|
|
|
|
)
|
2019-09-12 18:37:29 -07:00
|
|
|
.subcommand(
|
|
|
|
SubCommand::with_name("recover")
|
2019-11-25 20:33:15 -08:00
|
|
|
.about("Recover keypair from seed phrase and passphrase")
|
2019-09-12 18:37:29 -07:00
|
|
|
.setting(AppSettings::DisableVersion)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("outfile")
|
|
|
|
.short("o")
|
|
|
|
.long("outfile")
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Path to generated file"),
|
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("force")
|
|
|
|
.short("f")
|
|
|
|
.long("force")
|
|
|
|
.help("Overwrite the output file if it exists"),
|
2019-11-25 20:33:15 -08:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name(SKIP_SEED_PHRASE_VALIDATION_ARG.name)
|
|
|
|
.long(SKIP_SEED_PHRASE_VALIDATION_ARG.long)
|
|
|
|
.help(SKIP_SEED_PHRASE_VALIDATION_ARG.help),
|
2019-09-12 18:37:29 -07:00
|
|
|
),
|
2019-11-25 20:33:15 -08:00
|
|
|
|
2019-09-12 18:37:29 -07:00
|
|
|
)
|
2018-12-07 19:01:28 -08:00
|
|
|
.get_matches();
|
2018-07-12 15:29:49 -07:00
|
|
|
|
2019-03-19 14:19:50 -07:00
|
|
|
match matches.subcommand() {
|
2019-06-07 17:54:54 -07:00
|
|
|
("pubkey", Some(matches)) => {
|
2019-03-19 14:19:50 -07:00
|
|
|
let mut path = dirs::home_dir().expect("home directory");
|
2019-06-07 17:54:54 -07:00
|
|
|
let infile = if matches.is_present("infile") {
|
|
|
|
matches.value_of("infile").unwrap()
|
2019-03-19 14:19:50 -07:00
|
|
|
} else {
|
|
|
|
path.extend(&[".config", "solana", "id.json"]);
|
|
|
|
path.to_str().unwrap()
|
|
|
|
};
|
2019-10-10 16:01:03 -07:00
|
|
|
let keypair = if infile == "-" {
|
|
|
|
let mut stdin = std::io::stdin();
|
|
|
|
read_keypair(&mut stdin)?
|
|
|
|
} else {
|
|
|
|
read_keypair_file(infile)?
|
|
|
|
};
|
2019-03-19 14:19:50 -07:00
|
|
|
|
2019-06-07 17:54:54 -07:00
|
|
|
if matches.is_present("outfile") {
|
|
|
|
let outfile = matches.value_of("outfile").unwrap();
|
|
|
|
check_for_overwrite(&outfile, &matches);
|
2019-11-06 11:18:25 -08:00
|
|
|
write_pubkey_file(outfile, keypair.pubkey())?;
|
2019-03-19 14:19:50 -07:00
|
|
|
} else {
|
|
|
|
println!("{}", keypair.pubkey());
|
|
|
|
}
|
|
|
|
}
|
2019-06-07 17:54:54 -07:00
|
|
|
("new", Some(matches)) => {
|
2019-03-19 14:19:50 -07:00
|
|
|
let mut path = dirs::home_dir().expect("home directory");
|
2019-06-07 17:54:54 -07:00
|
|
|
let outfile = if matches.is_present("outfile") {
|
|
|
|
matches.value_of("outfile").unwrap()
|
2019-03-19 14:19:50 -07:00
|
|
|
} else {
|
|
|
|
path.extend(&[".config", "solana", "id.json"]);
|
|
|
|
path.to_str().unwrap()
|
|
|
|
};
|
|
|
|
|
2019-06-07 17:54:54 -07:00
|
|
|
if outfile != "-" {
|
|
|
|
check_for_overwrite(&outfile, &matches);
|
|
|
|
}
|
2019-09-12 18:37:29 -07:00
|
|
|
|
2019-11-03 19:41:26 -08:00
|
|
|
let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English);
|
2019-11-25 20:33:15 -08:00
|
|
|
let passphrase = if matches.is_present("no_passphrase") {
|
|
|
|
NO_PASSPHRASE.to_string()
|
|
|
|
} else {
|
|
|
|
eprintln!("Generating a new keypair");
|
|
|
|
rpassword::prompt_password_stderr(
|
|
|
|
"For added security, enter a passphrase (empty for no passphrase):",
|
|
|
|
)?
|
|
|
|
};
|
|
|
|
let seed = Seed::new(&mnemonic, &passphrase);
|
2019-11-03 19:41:26 -08:00
|
|
|
let keypair = keypair_from_seed(seed.as_bytes())?;
|
2019-09-12 18:37:29 -07:00
|
|
|
|
2019-10-10 16:01:03 -07:00
|
|
|
output_keypair(&keypair, &outfile, "new")?;
|
2019-09-12 18:37:29 -07:00
|
|
|
|
|
|
|
let silent = matches.is_present("silent");
|
|
|
|
if !silent {
|
2019-10-30 20:47:42 -07:00
|
|
|
let phrase: &str = mnemonic.phrase();
|
2019-09-12 18:37:29 -07:00
|
|
|
let divider = String::from_utf8(vec![b'='; phrase.len()]).unwrap();
|
2019-10-10 16:01:03 -07:00
|
|
|
eprintln!(
|
2019-11-25 20:33:15 -08:00
|
|
|
"{}\npubkey: {}\n{}\nSave this seed phrase to recover your new keypair:\n{}\n{}",
|
2019-11-03 19:41:26 -08:00
|
|
|
÷r, keypair.pubkey(), ÷r, phrase, ÷r
|
2019-09-12 18:37:29 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
("recover", Some(matches)) => {
|
|
|
|
let mut path = dirs::home_dir().expect("home directory");
|
|
|
|
let outfile = if matches.is_present("outfile") {
|
|
|
|
matches.value_of("outfile").unwrap()
|
|
|
|
} else {
|
|
|
|
path.extend(&[".config", "solana", "id.json"]);
|
|
|
|
path.to_str().unwrap()
|
|
|
|
};
|
|
|
|
|
|
|
|
if outfile != "-" {
|
|
|
|
check_for_overwrite(&outfile, &matches);
|
|
|
|
}
|
|
|
|
|
2019-11-25 20:33:15 -08:00
|
|
|
let skip_validation = matches.is_present(SKIP_SEED_PHRASE_VALIDATION_ARG.name);
|
|
|
|
let keypair = keypair_from_seed_phrase("recover", skip_validation)?;
|
2019-10-10 16:01:03 -07:00
|
|
|
output_keypair(&keypair, &outfile, "recovered")?;
|
2019-03-19 14:19:50 -07:00
|
|
|
}
|
2019-11-03 19:41:26 -08:00
|
|
|
("grind", Some(matches)) => {
|
2019-11-12 13:24:37 -08:00
|
|
|
let ignore_case = matches.is_present("ignore_case");
|
2019-11-03 19:41:26 -08:00
|
|
|
let includes = if matches.is_present("includes") {
|
|
|
|
values_t_or_exit!(matches, "includes", String)
|
|
|
|
.into_iter()
|
2019-11-12 13:24:37 -08:00
|
|
|
.map(|s| if ignore_case { s.to_lowercase() } else { s })
|
2019-11-03 19:41:26 -08:00
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
HashSet::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
let starts_with = if matches.is_present("starts_with") {
|
|
|
|
values_t_or_exit!(matches, "starts_with", String)
|
|
|
|
.into_iter()
|
2019-11-12 13:24:37 -08:00
|
|
|
.map(|s| if ignore_case { s.to_lowercase() } else { s })
|
2019-11-03 19:41:26 -08:00
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
HashSet::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
if includes.is_empty() && starts_with.is_empty() {
|
|
|
|
eprintln!(
|
|
|
|
"Error: No keypair search criteria provided (--includes or --starts-with)"
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let attempts = Arc::new(AtomicU64::new(1));
|
|
|
|
let found = Arc::new(AtomicU64::new(0));
|
|
|
|
let start = Instant::now();
|
|
|
|
|
|
|
|
println!(
|
|
|
|
"Searching with {} threads for a pubkey containing {:?} or starting with {:?}",
|
|
|
|
num_cpus::get(),
|
|
|
|
includes,
|
|
|
|
starts_with
|
|
|
|
);
|
|
|
|
|
|
|
|
let _threads = (0..num_cpus::get())
|
|
|
|
.map(|_| {
|
|
|
|
let attempts = attempts.clone();
|
|
|
|
let found = found.clone();
|
|
|
|
let includes = includes.clone();
|
|
|
|
let starts_with = starts_with.clone();
|
|
|
|
|
|
|
|
thread::spawn(move || loop {
|
|
|
|
let attempts = attempts.fetch_add(1, Ordering::Relaxed);
|
|
|
|
if attempts % 5_000_000 == 0 {
|
|
|
|
println!(
|
|
|
|
"Searched {} keypairs in {}s. {} matches found",
|
|
|
|
attempts,
|
|
|
|
start.elapsed().as_secs(),
|
|
|
|
found.load(Ordering::Relaxed),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let keypair = Keypair::new();
|
|
|
|
let mut pubkey = bs58::encode(keypair.pubkey()).into_string();
|
|
|
|
|
|
|
|
if ignore_case {
|
|
|
|
pubkey = pubkey.to_lowercase();
|
|
|
|
}
|
|
|
|
|
|
|
|
if starts_with.iter().any(|s| pubkey.starts_with(s))
|
|
|
|
|| includes.iter().any(|s| pubkey.contains(s))
|
|
|
|
{
|
|
|
|
let found = found.fetch_add(1, Ordering::Relaxed);
|
|
|
|
output_keypair(
|
|
|
|
&keypair,
|
|
|
|
&format!("{}.json", keypair.pubkey()),
|
|
|
|
&format!("{}", found),
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
thread::park();
|
|
|
|
}
|
2019-06-07 17:54:54 -07:00
|
|
|
_ => unreachable!(),
|
2018-07-12 15:29:49 -07:00
|
|
|
}
|
2019-03-19 14:19:50 -07:00
|
|
|
|
2018-07-12 10:59:40 -07:00
|
|
|
Ok(())
|
|
|
|
}
|