Starting to write config file

This commit is contained in:
Dave Hrycyszyn
2024-06-06 15:54:33 +01:00
parent dc3d0ad83a
commit 1e1f452cff
6 changed files with 117 additions and 39 deletions

View File

@@ -0,0 +1,21 @@
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(())
}