Add pubkey read/write tools
Co-authored-by: Tyera Eulberg <tyera@solana.com> Co-authored-by: Tristan Debrunner <tristan@solana.com>
This commit is contained in:
committed by
Tyera Eulberg
parent
7a81f327ce
commit
37494c67d0
@ -19,4 +19,3 @@ solana-sdk = { path = "../sdk", version = "0.13.0" }
|
||||
[[bin]]
|
||||
name = "solana-keygen"
|
||||
path = "src/keygen.rs"
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
use clap::{crate_description, crate_name, crate_version, App, Arg};
|
||||
use solana_sdk::signature::gen_keypair_file;
|
||||
use clap::{crate_description, crate_name, crate_version, App, Arg, SubCommand};
|
||||
use solana_sdk::pubkey::write_pubkey;
|
||||
use solana_sdk::signature::{gen_keypair_file, read_keypair, KeypairUtil};
|
||||
use std::error;
|
||||
|
||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
||||
@ -14,19 +15,78 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
||||
.takes_value(true)
|
||||
.help("Path to generated file"),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("new")
|
||||
.about("Generate new keypair file")
|
||||
.arg(
|
||||
Arg::with_name("outfile")
|
||||
.short("o")
|
||||
.long("outfile")
|
||||
.value_name("PATH")
|
||||
.takes_value(true)
|
||||
.help("Path to generated file"),
|
||||
),
|
||||
)
|
||||
.subcommand(
|
||||
SubCommand::with_name("pubkey")
|
||||
.about("Generate a pubkey from keypair file")
|
||||
.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"),
|
||||
),
|
||||
)
|
||||
.get_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()
|
||||
};
|
||||
match matches.subcommand() {
|
||||
("pubkey", Some(pubkey_matches)) => {
|
||||
let mut path = dirs::home_dir().expect("home directory");
|
||||
let infile = if pubkey_matches.is_present("infile") {
|
||||
pubkey_matches.value_of("infile").unwrap()
|
||||
} else {
|
||||
path.extend(&[".config", "solana", "id.json"]);
|
||||
path.to_str().unwrap()
|
||||
};
|
||||
let keypair = read_keypair(infile)?;
|
||||
|
||||
let serialized_keypair = gen_keypair_file(outfile.to_string())?;
|
||||
if outfile == "-" {
|
||||
println!("{}", serialized_keypair);
|
||||
if pubkey_matches.is_present("outfile") {
|
||||
let outfile = pubkey_matches.value_of("outfile").unwrap();
|
||||
write_pubkey(outfile, keypair.pubkey())?;
|
||||
} else {
|
||||
println!("{}", keypair.pubkey());
|
||||
}
|
||||
}
|
||||
match_tuple => {
|
||||
let working_matches = if let (_, Some(new_matches)) = match_tuple {
|
||||
new_matches
|
||||
} else {
|
||||
&matches
|
||||
};
|
||||
|
||||
let mut path = dirs::home_dir().expect("home directory");
|
||||
let outfile = if working_matches.is_present("outfile") {
|
||||
working_matches.value_of("outfile").unwrap()
|
||||
} else {
|
||||
path.extend(&[".config", "solana", "id.json"]);
|
||||
path.to_str().unwrap()
|
||||
};
|
||||
|
||||
let serialized_keypair = gen_keypair_file(outfile.to_string())?;
|
||||
if outfile == "-" {
|
||||
println!("{}", serialized_keypair);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user