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]]
|
[[bin]]
|
||||||
name = "solana-keygen"
|
name = "solana-keygen"
|
||||||
path = "src/keygen.rs"
|
path = "src/keygen.rs"
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
use clap::{crate_description, crate_name, crate_version, App, Arg};
|
use clap::{crate_description, crate_name, crate_version, App, Arg, SubCommand};
|
||||||
use solana_sdk::signature::gen_keypair_file;
|
use solana_sdk::pubkey::write_pubkey;
|
||||||
|
use solana_sdk::signature::{gen_keypair_file, read_keypair, KeypairUtil};
|
||||||
use std::error;
|
use std::error;
|
||||||
|
|
||||||
fn main() -> Result<(), Box<dyn error::Error>> {
|
fn main() -> Result<(), Box<dyn error::Error>> {
|
||||||
@ -14,19 +15,78 @@ fn main() -> Result<(), Box<dyn error::Error>> {
|
|||||||
.takes_value(true)
|
.takes_value(true)
|
||||||
.help("Path to generated file"),
|
.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();
|
.get_matches();
|
||||||
|
|
||||||
let mut path = dirs::home_dir().expect("home directory");
|
match matches.subcommand() {
|
||||||
let outfile = if matches.is_present("outfile") {
|
("pubkey", Some(pubkey_matches)) => {
|
||||||
matches.value_of("outfile").unwrap()
|
let mut path = dirs::home_dir().expect("home directory");
|
||||||
} else {
|
let infile = if pubkey_matches.is_present("infile") {
|
||||||
path.extend(&[".config", "solana", "id.json"]);
|
pubkey_matches.value_of("infile").unwrap()
|
||||||
path.to_str().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 pubkey_matches.is_present("outfile") {
|
||||||
if outfile == "-" {
|
let outfile = pubkey_matches.value_of("outfile").unwrap();
|
||||||
println!("{}", serialized_keypair);
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,11 @@
|
|||||||
use bs58;
|
|
||||||
use generic_array::typenum::U32;
|
use generic_array::typenum::U32;
|
||||||
use generic_array::GenericArray;
|
use generic_array::GenericArray;
|
||||||
|
use std::error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::fs::{self, File};
|
||||||
|
use std::io::Write;
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use std::path::Path;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
@ -15,6 +18,14 @@ pub enum ParsePubkeyError {
|
|||||||
Invalid,
|
Invalid,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for ParsePubkeyError {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
write!(f, "ParsePubkeyError: {:?}", self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl error::Error for ParsePubkeyError {}
|
||||||
|
|
||||||
impl FromStr for Pubkey {
|
impl FromStr for Pubkey {
|
||||||
type Err = ParsePubkeyError;
|
type Err = ParsePubkeyError;
|
||||||
|
|
||||||
@ -54,11 +65,30 @@ impl fmt::Display for Pubkey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn write_pubkey(outfile: &str, pubkey: Pubkey) -> Result<(), Box<error::Error>> {
|
||||||
|
let printable = format!("{}", pubkey);
|
||||||
|
let serialized = serde_json::to_string(&printable)?;
|
||||||
|
|
||||||
|
if let Some(outdir) = Path::new(&outfile).parent() {
|
||||||
|
fs::create_dir_all(outdir)?;
|
||||||
|
}
|
||||||
|
let mut f = File::create(outfile)?;
|
||||||
|
f.write_all(&serialized.clone().into_bytes())?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_pubkey(infile: &str) -> Result<Pubkey, Box<error::Error>> {
|
||||||
|
let f = File::open(infile.to_string())?;
|
||||||
|
let printable: String = serde_json::from_reader(f)?;
|
||||||
|
Ok(Pubkey::from_str(&printable)?)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::signature::{Keypair, KeypairUtil};
|
use crate::signature::{Keypair, KeypairUtil};
|
||||||
use bs58;
|
use std::fs::remove_file;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pubkey_fromstr() {
|
fn pubkey_fromstr() {
|
||||||
@ -93,4 +123,14 @@ mod tests {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_read_write_pubkey() -> Result<(), Box<error::Error>> {
|
||||||
|
let filename = "test_pubkey.json";
|
||||||
|
let pubkey = Keypair::new().pubkey();
|
||||||
|
write_pubkey(filename, pubkey)?;
|
||||||
|
let read = read_pubkey(filename)?;
|
||||||
|
assert_eq!(read, pubkey);
|
||||||
|
remove_file(filename)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user