No longer serialize as JSON-encoded pkcs8

That's supposed to be an ASCII format, but we're not making use
of it. We can switch back to that some day, but if we do, it shouldn't
be JSON-encoded.
This commit is contained in:
Greg Fitzgerald 2018-11-09 16:19:22 -07:00
parent 7c610b216b
commit fea86b2955
6 changed files with 33 additions and 32 deletions

View File

@ -9,7 +9,7 @@ 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::signature::read_pkcs8;
use solana::signature::read_keypair;
use std::io;
use std::net::SocketAddr;
@ -65,11 +65,11 @@ fn main() {
path.extend(&[".config", "solana", "id.json"]);
path.to_str().unwrap()
};
let pkcs8 = read_pkcs8(id_path).expect("client keypair");
let keypair = read_keypair(id_path).expect("client keypair");
// 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);
let config = Config::new(&bind_addr, keypair.to_bytes().to_vec());
let stdout = io::stdout();
serde_json::to_writer(stdout, &config).expect("serialize");
}

View File

@ -60,8 +60,8 @@ fn main() -> Result<(), Box<error::Error>> {
// Parse the input mint configuration
let num_tokens = value_t_or_exit!(matches, "num_tokens", u64);
let file = File::open(Path::new(&matches.value_of("mint").unwrap())).unwrap();
let pkcs8: Vec<u8> = serde_json::from_reader(&file)?;
let mint = Mint::new_with_pkcs8(num_tokens, pkcs8, leader_keypair.pubkey(), 1);
let keypair_bytes: Vec<u8> = serde_json::from_reader(&file)?;
let mint = Mint::new_with_keypair_bytes(num_tokens, keypair_bytes, leader_keypair.pubkey(), 1);
// Write the ledger entries
let ledger_path = matches.value_of("ledger").unwrap();

View File

@ -107,18 +107,22 @@ pub struct Fullnode {
/// Fullnode configuration to be stored in file
pub struct Config {
pub node_info: NodeInfo,
pkcs8: Vec<u8>,
keypair_bytes: Vec<u8>,
}
impl Config {
pub fn new(bind_addr: &SocketAddr, pkcs8: Vec<u8>) -> Self {
let keypair = Keypair::from_bytes(&pkcs8).expect("from_pkcs8 in fullnode::Config new");
pub fn new(bind_addr: &SocketAddr, keypair_bytes: Vec<u8>) -> Self {
let keypair =
Keypair::from_bytes(&keypair_bytes).expect("from_bytes in fullnode::Config new");
let pubkey = keypair.pubkey();
let node_info = NodeInfo::new_with_pubkey_socketaddr(pubkey, bind_addr);
Config { node_info, pkcs8 }
Config {
node_info,
keypair_bytes,
}
}
pub fn keypair(&self) -> Keypair {
Keypair::from_bytes(&self.pkcs8).expect("from_pkcs8 in fullnode::Config keypair")
Keypair::from_bytes(&self.keypair_bytes).expect("from_bytes in fullnode::Config keypair")
}
}

View File

@ -9,7 +9,7 @@ use transaction::Transaction;
#[derive(Serialize, Deserialize, Debug)]
pub struct Mint {
pub pkcs8: Vec<u8>,
pub keypair_bytes: Vec<u8>,
pubkey: Pubkey,
pub tokens: u64,
pub bootstrap_leader_id: Pubkey,
@ -17,16 +17,16 @@ pub struct Mint {
}
impl Mint {
pub fn new_with_pkcs8(
pub fn new_with_keypair_bytes(
tokens: u64,
pkcs8: Vec<u8>,
keypair_bytes: Vec<u8>,
bootstrap_leader_id: Pubkey,
bootstrap_leader_tokens: u64,
) -> Self {
let keypair = Keypair::from_bytes(&pkcs8).expect("from_pkcs8 in mint pub fn new");
let keypair = Keypair::from_bytes(&keypair_bytes).expect("from_bytes in mint pub fn new");
let pubkey = keypair.pubkey();
Mint {
pkcs8,
keypair_bytes,
pubkey,
tokens,
bootstrap_leader_id,
@ -39,8 +39,8 @@ impl Mint {
bootstrap_leader: Pubkey,
bootstrap_leader_tokens: u64,
) -> Self {
let pkcs8 = Keypair::new().to_bytes().to_vec();
Self::new_with_pkcs8(tokens, pkcs8, bootstrap_leader, bootstrap_leader_tokens)
let bytes = Keypair::new().to_bytes().to_vec();
Self::new_with_keypair_bytes(tokens, bytes, bootstrap_leader, bootstrap_leader_tokens)
}
pub fn new(tokens: u64) -> Self {
@ -48,7 +48,7 @@ impl Mint {
}
pub fn seed(&self) -> Hash {
hash(&self.pkcs8)
hash(&self.keypair_bytes)
}
pub fn last_id(&self) -> Hash {
@ -56,7 +56,7 @@ impl Mint {
}
pub fn keypair(&self) -> Keypair {
Keypair::from_bytes(&self.pkcs8).expect("from_pkcs8 in mint pub fn keypair")
Keypair::from_bytes(&self.keypair_bytes).expect("from_bytes in mint pub fn keypair")
}
pub fn pubkey(&self) -> Pubkey {

View File

@ -93,15 +93,10 @@ impl GenKeys {
}
}
pub fn read_pkcs8(path: &str) -> Result<Vec<u8>, Box<error::Error>> {
let file = File::open(path.to_string())?;
let pkcs8: Vec<u8> = serde_json::from_reader(file)?;
Ok(pkcs8)
}
pub fn read_keypair(path: &str) -> Result<Keypair, Box<error::Error>> {
let pkcs8 = read_pkcs8(path)?;
let keypair = Keypair::from_bytes(&pkcs8).unwrap();
let file = File::open(path.to_string())?;
let bytes: Vec<u8> = serde_json::from_reader(file)?;
let keypair = Keypair::from_bytes(&bytes).unwrap();
Ok(keypair)
}

View File

@ -691,8 +691,8 @@ pub fn request_airdrop(
}
pub fn gen_keypair_file(outfile: String) -> Result<String, Box<error::Error>> {
let pkcs8_bytes = Keypair::new().to_bytes();
let serialized = serde_json::to_string(&pkcs8_bytes.to_vec())?;
let keypair_bytes = Keypair::new().to_bytes();
let serialized = serde_json::to_string(&keypair_bytes.to_vec())?;
if outfile != "-" {
if let Some(outdir) = Path::new(&outfile).parent() {
@ -794,7 +794,7 @@ mod tests {
use leader_scheduler::LeaderScheduler;
use ledger::create_tmp_genesis;
use serde_json::Value;
use signature::{read_keypair, read_pkcs8, Keypair, KeypairUtil};
use signature::{read_keypair, Keypair, KeypairUtil};
use std::fs::remove_dir_all;
use std::sync::mpsc::channel;
use std::sync::{Arc, RwLock};
@ -1229,8 +1229,10 @@ mod tests {
let serialized_keypair = gen_keypair_file(outfile.to_string()).unwrap();
let keypair_vec: Vec<u8> = serde_json::from_str(&serialized_keypair).unwrap();
assert!(Path::new(&outfile).exists());
assert_eq!(keypair_vec, read_pkcs8(&outfile).unwrap());
assert!(read_keypair(&outfile).is_ok());
assert_eq!(
read_keypair(&outfile).unwrap().to_bytes().to_vec(),
keypair_vec
);
assert_eq!(
read_keypair(&outfile).unwrap().pubkey().as_ref().len(),
mem::size_of::<Pubkey>()