Add mnenomic keypair generation and recovery to cli (#5889)

* Add mnenomic keypair generation and recovery to cli

* Use password input to retrieve mnemonic phrase

* Direct users without keypair file to use solana-keygen
This commit is contained in:
Justin Starry
2019-09-12 18:37:29 -07:00
committed by GitHub
parent 92a5979558
commit 8f5a1535af
6 changed files with 150 additions and 8 deletions

View File

@@ -15,7 +15,9 @@ cuda = []
[dependencies]
clap = "2.33"
dirs = "2.0.2"
rpassword = "4.0"
solana-sdk = { path = "../sdk", version = "0.19.0-pre0" }
tiny-bip39 = "0.6.2"
[[bin]]
name = "solana-keygen"

View File

@@ -1,12 +1,15 @@
use bip39::{Language, Mnemonic, MnemonicType, Seed};
use clap::{
crate_description, crate_name, crate_version, App, AppSettings, Arg, ArgMatches, SubCommand,
};
use solana_sdk::pubkey::write_pubkey;
use solana_sdk::signature::{gen_keypair_file, read_keypair, KeypairUtil};
use solana_sdk::signature::{keypair_from_seed, read_keypair, write_keypair, KeypairUtil};
use std::error;
use std::path::Path;
use std::process::exit;
const NO_PASSPHRASE: &str = "";
fn check_for_overwrite(outfile: &str, matches: &ArgMatches) {
let force = matches.is_present("force");
if !force && Path::new(outfile).exists() {
@@ -37,6 +40,12 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.short("f")
.long("force")
.help("Overwrite the output file if it exists"),
)
.arg(
Arg::with_name("silent")
.short("s")
.long("silent")
.help("Do not display mnemonic phrase"),
),
)
.subcommand(
@@ -65,6 +74,25 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.help("Overwrite the output file if it exists"),
),
)
.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"),
),
)
.get_matches();
match matches.subcommand() {
@@ -98,11 +126,51 @@ fn main() -> Result<(), Box<dyn error::Error>> {
if outfile != "-" {
check_for_overwrite(&outfile, &matches);
}
let serialized_keypair = gen_keypair_file(outfile)?;
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 {}", outfile);
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{}",
&divider, phrase, &divider
);
}
}
("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)?;
if outfile == "-" {
println!("{}", serialized_keypair);
} else {
println!("Wrote recovered keypair to {}", outfile);
}
}
_ => unreachable!(),