2018-08-06 20:51:12 -07:00
|
|
|
#[macro_use]
|
2018-07-12 15:29:49 -07:00
|
|
|
extern crate clap;
|
2018-07-12 17:16:30 -07:00
|
|
|
extern crate dirs;
|
2018-07-12 10:59:40 -07:00
|
|
|
extern crate ring;
|
|
|
|
extern crate serde_json;
|
|
|
|
|
2018-07-12 15:29:49 -07:00
|
|
|
use clap::{App, Arg};
|
2018-07-12 10:59:40 -07:00
|
|
|
use ring::rand::SystemRandom;
|
|
|
|
use ring::signature::Ed25519KeyPair;
|
|
|
|
use std::error;
|
2018-07-12 15:29:49 -07:00
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::Write;
|
|
|
|
use std::path::Path;
|
2018-07-12 10:59:40 -07:00
|
|
|
|
|
|
|
fn main() -> Result<(), Box<error::Error>> {
|
2018-07-12 16:29:10 -07:00
|
|
|
let matches = App::new("solana-keygen")
|
2018-08-06 20:51:12 -07:00
|
|
|
.version(crate_version!())
|
2018-07-12 15:29:49 -07:00
|
|
|
.arg(
|
|
|
|
Arg::with_name("outfile")
|
|
|
|
.short("o")
|
|
|
|
.long("outfile")
|
|
|
|
.value_name("PATH")
|
|
|
|
.takes_value(true)
|
2018-07-12 16:29:10 -07:00
|
|
|
.help("path to generated file"),
|
2018-07-12 15:29:49 -07:00
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
2018-07-12 10:59:40 -07:00
|
|
|
let rnd = SystemRandom::new();
|
|
|
|
let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rnd)?;
|
|
|
|
let serialized = serde_json::to_string(&pkcs8_bytes.to_vec())?;
|
2018-07-12 15:29:49 -07:00
|
|
|
|
2018-07-12 17:16:30 -07:00
|
|
|
let mut path = dirs::home_dir().expect("home directory");
|
2018-07-12 16:04:42 -07:00
|
|
|
let outfile = if matches.is_present("outfile") {
|
|
|
|
matches.value_of("outfile").unwrap()
|
|
|
|
} else {
|
|
|
|
path.extend(&[".config", "solana", "id.json"]);
|
|
|
|
path.to_str().unwrap()
|
|
|
|
};
|
2018-07-12 15:29:49 -07:00
|
|
|
|
|
|
|
if outfile == "-" {
|
|
|
|
println!("{}", serialized);
|
|
|
|
} else {
|
|
|
|
if let Some(outdir) = Path::new(outfile).parent() {
|
|
|
|
fs::create_dir_all(outdir)?;
|
|
|
|
}
|
|
|
|
let mut f = File::create(outfile)?;
|
|
|
|
f.write_all(&serialized.into_bytes())?;
|
|
|
|
}
|
|
|
|
|
2018-07-12 10:59:40 -07:00
|
|
|
Ok(())
|
|
|
|
}
|