Relocate all keypair generation into one location: sdk/src/signature.rs

This commit is contained in:
Michael Vines 2018-12-12 13:13:18 -08:00
parent 4f48f1a850
commit cafeef33c3
6 changed files with 37 additions and 41 deletions

View File

@ -9,7 +9,9 @@ use ring::{rand, signature};
use serde_json;
use std::error;
use std::fmt;
use std::fs::File;
use std::fs::{self, File};
use std::io::Write;
use std::path::Path;
use untrusted::Input;
pub type Keypair = Ed25519KeyPair;
@ -94,3 +96,23 @@ pub fn read_keypair(path: &str) -> Result<Keypair, Box<error::Error>> {
let keypair = Ed25519KeyPair::from_pkcs8(Input::from(&pkcs8))?;
Ok(keypair)
}
pub fn gen_pkcs8() -> Result<Vec<u8>, Box<error::Error>> {
let rnd = rand::SystemRandom::new();
let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rnd)?;
Ok(pkcs8_bytes.to_vec())
}
//pub fn gen_keypair_file(outfile: String) -> Result<String, Box<dyn error::Error>> {
pub fn gen_keypair_file(outfile: String) -> Result<String, Box<error::Error>> {
let serialized = serde_json::to_string(&gen_pkcs8()?)?;
if outfile != "-" {
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(serialized)
}

View File

@ -5,13 +5,11 @@ use dirs;
use serde_json;
use clap::{App, Arg};
use ring::rand::SystemRandom;
use ring::signature::Ed25519KeyPair;
use solana::cluster_info::FULLNODE_PORT_RANGE;
use solana::fullnode::Config;
use solana::logger;
use solana::netutil::{get_ip_addr, get_public_ip_addr, parse_port_or_addr};
use solana_sdk::signature::read_pkcs8;
use solana_sdk::signature::{gen_pkcs8, read_pkcs8};
use std::io;
use std::net::SocketAddr;
@ -73,12 +71,11 @@ fn main() {
};
let pkcs8 = read_pkcs8(id_path).expect("client keypair");
let rnd = SystemRandom::new();
let vote_account_pkcs8 = Ed25519KeyPair::generate_pkcs8(&rnd).unwrap();
let vote_account_pkcs8 = gen_pkcs8().unwrap();
// we need all the receiving sockets to be bound within the expected
// port range that we open on aws
let config = Config::new(&bind_addr, pkcs8, vote_account_pkcs8.to_vec());
let config = Config::new(&bind_addr, pkcs8, vote_account_pkcs8);
let stdout = io::stdout();
serde_json::to_writer(stdout, &config).expect("serialize");
}

View File

@ -3,7 +3,7 @@ extern crate clap;
use dirs;
use clap::{App, Arg};
use solana::wallet::gen_keypair_file;
use solana_sdk::signature::gen_keypair_file;
use std::error;
fn main() -> Result<(), Box<dyn error::Error>> {

View File

@ -6,8 +6,8 @@ extern crate solana;
use clap::{App, Arg, ArgMatches, SubCommand};
use solana::logger;
use solana::wallet::{gen_keypair_file, parse_command, process_command, WalletConfig, WalletError};
use solana_sdk::signature::{read_keypair, KeypairUtil};
use solana::wallet::{parse_command, process_command, WalletConfig, WalletError};
use solana_sdk::signature::{gen_keypair_file, read_keypair, KeypairUtil};
use std::error;
use std::net::SocketAddr;

View File

@ -1,10 +1,9 @@
//! The `mint` module is a library for generating the chain's genesis block.
use crate::entry::Entry;
use ring::rand::SystemRandom;
use solana_sdk::hash::{hash, Hash};
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil};
use solana_sdk::signature::{gen_pkcs8, Keypair, KeypairUtil};
use solana_sdk::system_transaction::SystemTransaction;
use solana_sdk::transaction::Transaction;
use untrusted::Input;
@ -42,18 +41,12 @@ impl Mint {
bootstrap_leader: Pubkey,
bootstrap_leader_tokens: u64,
) -> Self {
let rnd = SystemRandom::new();
let pkcs8 = Keypair::generate_pkcs8(&rnd)
.expect("generate_pkcs8 in mint pub fn new")
.to_vec();
let pkcs8 = gen_pkcs8().expect("generate_pkcs8 in mint pub fn new");
Self::new_with_pkcs8(tokens, pkcs8, bootstrap_leader, bootstrap_leader_tokens)
}
pub fn new(tokens: u64) -> Self {
let rnd = SystemRandom::new();
let pkcs8 = Keypair::generate_pkcs8(&rnd)
.expect("generate_pkcs8 in mint pub fn new")
.to_vec();
let pkcs8 = gen_pkcs8().expect("generate_pkcs8 in mint pub fn new");
Self::new_with_pkcs8(tokens, pkcs8, Pubkey::default(), 0)
}

View File

@ -6,8 +6,6 @@ use bincode::serialize;
use bs58;
use chrono::prelude::*;
use clap::ArgMatches;
use ring::rand::SystemRandom;
use ring::signature::Ed25519KeyPair;
use serde_json;
use solana_drone::drone::{request_airdrop_transaction, DRONE_PORT};
use solana_sdk::bpf_loader;
@ -19,10 +17,9 @@ use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::{Keypair, KeypairUtil, Signature};
use solana_sdk::system_transaction::SystemTransaction;
use solana_sdk::transaction::Transaction;
use std::fs::{self, File};
use std::io::{Read, Write};
use std::fs::File;
use std::io::Read;
use std::net::{Ipv4Addr, SocketAddr};
use std::path::Path;
use std::str::FromStr;
use std::thread::sleep;
use std::time::Duration;
@ -662,21 +659,6 @@ pub fn read_leader(path: &str) -> Result<Config, WalletError> {
})
}
pub fn gen_keypair_file(outfile: String) -> Result<String, Box<dyn error::Error>> {
let rnd = SystemRandom::new();
let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rnd)?;
let serialized = serde_json::to_string(&pkcs8_bytes.to_vec())?;
if outfile != "-" {
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(serialized)
}
fn get_last_id(rpc_client: &RpcClient) -> Result<Hash, Box<dyn error::Error>> {
let result = RpcRequest::GetLastId.make_rpc_request(rpc_client, 1, None)?;
if result.as_str().is_none() {
@ -833,8 +815,10 @@ mod tests {
use clap::{App, Arg, SubCommand};
use serde_json::Value;
use solana_drone::drone::run_local_drone;
use solana_sdk::signature::{read_keypair, read_pkcs8, Keypair, KeypairUtil};
use solana_sdk::signature::{gen_keypair_file, read_keypair, read_pkcs8, Keypair, KeypairUtil};
use std::fs;
use std::fs::remove_dir_all;
use std::path::Path;
use std::sync::mpsc::channel;
use std::sync::{Arc, RwLock};
use std::thread::sleep;