2019-09-12 18:37:29 -07:00
|
|
|
use bip39::{Language, Mnemonic, MnemonicType, Seed};
|
2019-06-07 17:54:54 -07:00
|
|
|
use clap::{
|
|
|
|
crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgMatches, SubCommand,
|
|
|
|
};
|
2019-03-19 14:19:50 -07:00
|
|
|
use solana_sdk::pubkey::write_pubkey;
|
2019-09-12 18:37:29 -07:00
|
|
|
use solana_sdk::signature::{keypair_from_seed, read_keypair, write_keypair, KeypairUtil};
|
2018-07-12 10:59:40 -07:00
|
|
|
use std::error;
|
2019-06-07 17:54:54 -07:00
|
|
|
use std::path::Path;
|
|
|
|
use std::process::exit;
|
|
|
|
|
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
|
|
|
|
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!())
|
2018-08-06 20:51:12 -07:00
|
|
|
.version(crate_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")
|
|
|
|
.about("Generate new 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("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
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("silent")
|
|
|
|
.short("s")
|
|
|
|
.long("silent")
|
|
|
|
.help("Do not display mnemonic phrase"),
|
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")
|
|
|
|
.about("Recover keypair from mnemonic phrase")
|
|
|
|
.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"),
|
|
|
|
),
|
|
|
|
)
|
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()
|
|
|
|
};
|
|
|
|
let keypair = read_keypair(infile)?;
|
|
|
|
|
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-03-19 14:19:50 -07:00
|
|
|
write_pubkey(outfile, keypair.pubkey())?;
|
|
|
|
} 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
|
|
|
|
|
|
|
let mnemonic = Mnemonic::new(MnemonicType::Words12, Language::English);
|
|
|
|
let phrase: &str = mnemonic.phrase();
|
|
|
|
let seed = Seed::new(&mnemonic, NO_PASSPHRASE);
|
|
|
|
let keypair = keypair_from_seed(seed.as_bytes())?;
|
|
|
|
|
|
|
|
let serialized_keypair = write_keypair(&keypair, outfile)?;
|
|
|
|
if outfile == "-" {
|
|
|
|
println!("{}", serialized_keypair);
|
|
|
|
} else {
|
|
|
|
println!("Wrote new keypair to {}", outfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
let silent = matches.is_present("silent");
|
|
|
|
if !silent {
|
|
|
|
let divider = String::from_utf8(vec![b'='; phrase.len()]).unwrap();
|
|
|
|
println!(
|
|
|
|
"{}\nSave this mnemonic phrase to recover your new keypair:\n{}\n{}",
|
|
|
|
÷r, phrase, ÷r
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
("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);
|
|
|
|
}
|
|
|
|
|
|
|
|
let phrase = rpassword::prompt_password_stdout("Mnemonic recovery phrase: ").unwrap();
|
|
|
|
let mnemonic = Mnemonic::from_phrase(phrase.trim(), Language::English)?;
|
|
|
|
let seed = Seed::new(&mnemonic, NO_PASSPHRASE);
|
|
|
|
let keypair = keypair_from_seed(seed.as_bytes())?;
|
|
|
|
|
|
|
|
let serialized_keypair = write_keypair(&keypair, outfile)?;
|
2019-03-19 14:19:50 -07:00
|
|
|
if outfile == "-" {
|
|
|
|
println!("{}", serialized_keypair);
|
2019-06-07 17:54:54 -07:00
|
|
|
} else {
|
2019-09-12 18:37:29 -07:00
|
|
|
println!("Wrote recovered keypair to {}", outfile);
|
2019-03-19 14:19:50 -07:00
|
|
|
}
|
|
|
|
}
|
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(())
|
|
|
|
}
|