Files
bft-crdt-experiment/side-node/src/init/keys.rs

22 lines
760 B
Rust
Raw Normal View History

2024-06-06 15:54:33 +01:00
use std::{fs::File, io::Write, path::PathBuf};
use bft_json_crdt::keypair::{make_keypair, KeyPair};
use pem::Pem;
/// Makes an Ed25519 keypair and returns the public and private keys as a PEM-encoded string.
pub(crate) fn setup() -> String {
let keys = make_keypair();
let public_pem = Pem::new("PUBLIC", keys.public().to_string());
let private_pem = Pem::new("PRIVATE", keys.private().to_string());
pem::encode_many(&[public_pem, private_pem])
}
/// Writes a PEM-encoded string to a file at key_path.
pub(crate) fn write_pem(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> {
println!("Writing key to: {:?}", key_path);
let mut file = File::create(key_path)?;
file.write(pem.to_string().as_bytes())?;
Ok(())
}