2024-06-06 14:39:50 +01:00
|
|
|
use std::{
|
|
|
|
|
fs::File,
|
|
|
|
|
io::Write,
|
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use pem::Pem;
|
|
|
|
|
|
|
|
|
|
use bft_json_crdt::keypair::{make_keypair, KeyPair};
|
|
|
|
|
|
2024-06-06 14:48:14 +01:00
|
|
|
const KEY_FILE: &str = "side/node/keys.pem";
|
2024-06-06 13:54:15 +01:00
|
|
|
|
2024-06-06 14:48:14 +01:00
|
|
|
pub(crate) fn init(home: String) -> Result<(), std::io::Error> {
|
|
|
|
|
println!("Initializing Side Node at {home}");
|
2024-06-06 14:39:50 +01:00
|
|
|
let key_path = key_path(home);
|
|
|
|
|
|
|
|
|
|
ensure_directory_exists(&key_path)?;
|
|
|
|
|
|
|
|
|
|
let pem = create_pem();
|
|
|
|
|
write(key_path, pem)?;
|
2024-06-06 13:54:15 +01:00
|
|
|
|
2024-06-06 14:39:50 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn ensure_directory_exists(key_path: &PathBuf) -> Result<(), std::io::Error> {
|
|
|
|
|
Ok(if let Some(parent_dir) = key_path.parent() {
|
2024-06-06 13:54:15 +01:00
|
|
|
println!("Creating parent directory: {:?}", parent_dir);
|
|
|
|
|
std::fs::create_dir_all(parent_dir)?;
|
2024-06-06 14:39:50 +01:00
|
|
|
})
|
|
|
|
|
}
|
2024-06-06 13:54:15 +01:00
|
|
|
|
2024-06-06 14:39:50 +01:00
|
|
|
/// Writes a PEM-encoded string to a file at key_path.
|
|
|
|
|
fn write(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> {
|
|
|
|
|
let mut file = File::create(key_path)?;
|
|
|
|
|
file.write(pem.to_string().as_bytes())?;
|
2024-06-06 13:54:15 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:39:50 +01:00
|
|
|
/// Makes an Ed25519 keypair and returns the public and private keys as a PEM-encoded string.
|
|
|
|
|
fn create_pem() -> 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])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the path to the key file for this host OS.
|
|
|
|
|
fn key_path(home: String) -> PathBuf {
|
|
|
|
|
let key_path = format!("{home}/.{KEY_FILE}");
|
|
|
|
|
let path = Path::new(&key_path).to_owned();
|
|
|
|
|
path
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 13:54:15 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2024-06-06 14:39:50 +01:00
|
|
|
use super::*;
|
2024-06-06 13:54:15 +01:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn creates_side_node_directory() {
|
2024-06-06 14:48:14 +01:00
|
|
|
let test_home = format!("/tmp/{KEY_FILE}");
|
|
|
|
|
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
|
|
|
|
|
let _ = init(test_home.clone());
|
2024-06-06 13:54:15 +01:00
|
|
|
assert!(std::path::Path::new(node_dir).exists());
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 14:39:50 +01:00
|
|
|
#[test]
|
|
|
|
|
fn creates_key_file() {
|
2024-06-06 14:48:14 +01:00
|
|
|
let test_home = format!("/tmp/{KEY_FILE}");
|
|
|
|
|
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
|
|
|
|
|
let _ = init(test_home.clone());
|
|
|
|
|
assert!(std::path::Path::new(node_dir).exists());
|
2024-06-06 14:39:50 +01:00
|
|
|
}
|
2024-06-06 13:54:15 +01:00
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
|
// fn creates_config_file() {}
|
2024-05-29 16:47:35 +01:00
|
|
|
}
|