Use solana-cli config keypair in solana-keygen (#8074)

* Use solana-cli config keypair in solana-keygen

* s/infile/keypair for consistency across modules and more generality across access methods

* Move config into separate crate
This commit is contained in:
Tyera Eulberg
2020-01-31 19:27:37 -07:00
committed by GitHub
parent 408ef8b2cb
commit fab8ef379f
10 changed files with 86 additions and 30 deletions

View File

@ -15,6 +15,7 @@ dirs = "2.0.2"
num_cpus = "1.12.0"
rpassword = "4.0"
solana-clap-utils = { path = "../clap-utils", version = "0.24.0" }
solana-cli-config = { path = "../cli-config", version = "0.24.0" }
solana-sdk = { path = "../sdk", version = "0.24.0" }
tiny-bip39 = "0.7.0"

View File

@ -8,6 +8,7 @@ use num_cpus;
use solana_clap_utils::keypair::{
keypair_from_seed_phrase, prompt_passphrase, ASK_KEYWORD, SKIP_SEED_PHRASE_VALIDATION_ARG,
};
use solana_cli_config::config::{Config, CONFIG_FILE};
use solana_sdk::{
pubkey::write_pubkey_file,
signature::{
@ -44,23 +45,28 @@ fn check_for_overwrite(outfile: &str, matches: &ArgMatches) {
}
}
fn get_keypair_from_matches(matches: &ArgMatches) -> Result<Keypair, Box<dyn error::Error>> {
fn get_keypair_from_matches(
matches: &ArgMatches,
config: Config,
) -> Result<Keypair, Box<dyn error::Error>> {
let mut path = dirs::home_dir().expect("home directory");
let infile = if matches.is_present("infile") {
matches.value_of("infile").unwrap()
let keypair = if matches.is_present("keypair") {
matches.value_of("keypair").unwrap()
} else if config.keypair_path != "" {
&config.keypair_path
} else {
path.extend(&[".config", "solana", "id.json"]);
path.to_str().unwrap()
};
if infile == "-" {
if keypair == "-" {
let mut stdin = std::io::stdin();
read_keypair(&mut stdin)
} else if infile == ASK_KEYWORD {
} else if keypair == ASK_KEYWORD {
let skip_validation = matches.is_present(SKIP_SEED_PHRASE_VALIDATION_ARG.name);
keypair_from_seed_phrase("pubkey recovery", skip_validation, false)
} else {
read_keypair_file(infile)
read_keypair_file(keypair)
}
}
@ -193,23 +199,38 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.about(crate_description!())
.version(solana_clap_utils::version!())
.setting(AppSettings::SubcommandRequiredElseHelp)
.arg({
let arg = Arg::with_name("config_file")
.short("C")
.long("config")
.value_name("PATH")
.takes_value(true)
.global(true)
.help("Configuration file to use");
if let Some(ref config_file) = *CONFIG_FILE {
arg.default_value(&config_file)
} else {
arg
}
})
.subcommand(
SubCommand::with_name("verify")
.about("Verify a keypair can sign and verify a message.")
.arg(
Arg::with_name("infile")
Arg::with_name("pubkey")
.index(1)
.value_name("BASE58_PUBKEY")
.takes_value(true)
.required(true)
.help("Public key"),
)
.arg(
Arg::with_name("keypair")
.index(2)
.value_name("PATH")
.takes_value(true)
.help("Path to keypair file"),
)
.arg(
Arg::with_name("pubkey")
.index(2)
.value_name("BASE58_PUBKEY")
.takes_value(true)
.help("Public key"),
)
)
.subcommand(
SubCommand::with_name("new")
@ -301,7 +322,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
.about("Display the pubkey from a keypair file")
.setting(AppSettings::DisableVersion)
.arg(
Arg::with_name("infile")
Arg::with_name("keypair")
.index(1)
.value_name("PATH")
.takes_value(true)
@ -353,10 +374,15 @@ fn main() -> Result<(), Box<dyn error::Error>> {
)
.get_matches();
let config = if let Some(config_file) = matches.value_of("config_file") {
Config::load(config_file).unwrap_or_default()
} else {
Config::default()
};
match matches.subcommand() {
("pubkey", Some(matches)) => {
let keypair = get_keypair_from_matches(matches)?;
let keypair = get_keypair_from_matches(matches, config)?;
if matches.is_present("outfile") {
let outfile = matches.value_of("outfile").unwrap();
@ -533,7 +559,7 @@ fn main() -> Result<(), Box<dyn error::Error>> {
}
}
("verify", Some(matches)) => {
let keypair = get_keypair_from_matches(matches)?;
let keypair = get_keypair_from_matches(matches, config)?;
let test_data = b"test";
let signature = Signature::new(&keypair.sign(test_data).to_bytes());
let pubkey_bs58 = matches.value_of("pubkey").unwrap();