diff --git a/sdk/src/signature.rs b/sdk/src/signature.rs index d00cc05f62..c9a8c08973 100644 --- a/sdk/src/signature.rs +++ b/sdk/src/signature.rs @@ -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> { let keypair = Ed25519KeyPair::from_pkcs8(Input::from(&pkcs8))?; Ok(keypair) } + +pub fn gen_pkcs8() -> Result, Box> { + 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> { +pub fn gen_keypair_file(outfile: String) -> Result> { + 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) +} diff --git a/src/bin/fullnode-config.rs b/src/bin/fullnode-config.rs index fecf2d9a16..a74989b040 100644 --- a/src/bin/fullnode-config.rs +++ b/src/bin/fullnode-config.rs @@ -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"); } diff --git a/src/bin/keygen.rs b/src/bin/keygen.rs index 604e935cc0..5231a22e03 100644 --- a/src/bin/keygen.rs +++ b/src/bin/keygen.rs @@ -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> { diff --git a/src/bin/wallet.rs b/src/bin/wallet.rs index 1fba4712de..d122688839 100644 --- a/src/bin/wallet.rs +++ b/src/bin/wallet.rs @@ -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; diff --git a/src/mint.rs b/src/mint.rs index 83c2d60f9f..794632722f 100644 --- a/src/mint.rs +++ b/src/mint.rs @@ -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) } diff --git a/src/wallet.rs b/src/wallet.rs index fe72c4eeff..95d9c07628 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -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 { }) } -pub fn gen_keypair_file(outfile: String) -> Result> { - 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> { 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;