Key generation, saving and loading works.

This commit is contained in:
Dave Hrycyszyn
2024-06-06 18:52:39 +01:00
parent 6d6c544dd5
commit cda6dd2901
9 changed files with 67 additions and 58 deletions

27
side-node/src/keys.rs Normal file
View File

@@ -0,0 +1,27 @@
use std::{
fs::{self, File},
io::Write,
path::PathBuf,
};
use bft_json_crdt::keypair::{make_keypair, Ed25519KeyPair};
use fastcrypto::traits::EncodeDecodeBase64;
/// Writes a new Ed25519 keypair to the file at key_path.
pub(crate) fn write(key_path: PathBuf) -> Result<(), std::io::Error> {
let keys = make_keypair();
let mut file = File::create(key_path)?;
let out = keys.encode_base64();
file.write(out.as_bytes())?;
Ok(())
}
pub(crate) fn load_from_file(side_dir: PathBuf) -> Ed25519KeyPair {
let key_path = crate::utils::side_paths(side_dir.clone()).0;
let data = fs::read_to_string(key_path).expect("couldn't read key file");
println!("data: {:?}", data);
Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file")
}