Fabricio Demattê e38eb09f5d
misc/keygen: Implement cli tool to handle key material (#2453)
- Load keys from file
- Generate new keys (with optional prefix)
- Replaces peer-id-generator

Co-authored-by: Fabricio Dematte <fabriciodematte7p@gmail.com>
Co-authored-by: Max Inden <mail@max-inden.de>
2022-03-11 12:44:05 +01:00

42 lines
1.0 KiB
Rust

use serde::{Deserialize, Serialize};
use std::error::Error;
use std::path::Path;
use libp2p_core::identity::Keypair;
use libp2p_core::PeerId;
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Config {
pub identity: Identity,
}
impl Config {
pub fn from_file(path: &Path) -> Result<Self, Box<dyn Error>> {
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
}
pub fn from_key_material(peer_id: PeerId, keypair: &Keypair) -> Result<Self, Box<dyn Error>> {
let priv_key = base64::encode(keypair.to_protobuf_encoding()?);
let peer_id = peer_id.to_base58();
Ok(Self {
identity: Identity { peer_id, priv_key },
})
}
}
#[derive(Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Identity {
#[serde(rename = "PeerID")]
pub peer_id: String,
pub priv_key: String,
}
impl zeroize::Zeroize for Config {
fn zeroize(&mut self) {
self.identity.peer_id.zeroize();
self.identity.priv_key.zeroize();
}
}