2024-06-06 15:54:33 +01:00
|
|
|
use std::path::PathBuf;
|
2024-06-06 14:39:50 +01:00
|
|
|
|
2024-06-06 15:54:33 +01:00
|
|
|
use config::SideNodeConfig;
|
2024-06-06 14:39:50 +01:00
|
|
|
|
2024-06-06 18:52:39 +01:00
|
|
|
use crate::{keys, utils};
|
2024-06-06 14:39:50 +01:00
|
|
|
|
2024-06-06 18:52:39 +01:00
|
|
|
pub(crate) mod config;
|
2024-06-06 13:54:15 +01:00
|
|
|
|
2024-06-06 15:54:33 +01:00
|
|
|
pub(crate) fn init(home: PathBuf, config: SideNodeConfig) -> Result<(), std::io::Error> {
|
2024-06-06 15:30:47 +01:00
|
|
|
ensure_side_directory_exists(&home)?;
|
2024-06-06 18:52:39 +01:00
|
|
|
let (key_path, config_path) = utils::side_paths(home.clone());
|
2024-06-06 14:39:50 +01:00
|
|
|
|
2024-06-06 16:03:36 +01:00
|
|
|
println!("Writing key to: {:?}", key_path);
|
2024-06-06 18:52:39 +01:00
|
|
|
keys::write(key_path)?;
|
2024-06-06 13:54:15 +01:00
|
|
|
|
2024-06-06 16:03:36 +01:00
|
|
|
println!("Writing config to: {:?}", config_path);
|
2024-06-07 18:22:07 +01:00
|
|
|
config::write_toml(&config, &config_path).expect("unable to write config file");
|
2024-06-06 16:03:36 +01:00
|
|
|
|
2024-06-06 14:39:50 +01:00
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 15:29:22 +01:00
|
|
|
/// Ensures that the directory at side_dir exists, so we have a place
|
|
|
|
|
/// to store our key file and config file.
|
2024-06-06 15:30:47 +01:00
|
|
|
fn ensure_side_directory_exists(side_dir: &PathBuf) -> Result<(), std::io::Error> {
|
2024-06-06 15:29:22 +01:00
|
|
|
if side_dir.exists() {
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
2024-06-06 18:52:39 +01:00
|
|
|
println!(
|
|
|
|
|
"Config directory doesn't exist, creating at: {:?}",
|
|
|
|
|
side_dir
|
|
|
|
|
);
|
2024-06-06 15:29:22 +01:00
|
|
|
std::fs::create_dir_all(side_dir)
|
2024-06-06 14:39:50 +01:00
|
|
|
}
|
2024-06-06 13:54:15 +01:00
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
2024-06-06 15:54:33 +01:00
|
|
|
use std::{fs, path::Path};
|
2024-06-06 15:29:22 +01:00
|
|
|
|
2024-06-06 18:55:48 +01:00
|
|
|
use fastcrypto::{
|
|
|
|
|
ed25519::Ed25519KeyPair,
|
|
|
|
|
traits::{EncodeDecodeBase64, KeyPair, ToFromBytes},
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-06 14:39:50 +01:00
|
|
|
use super::*;
|
2024-06-06 13:54:15 +01:00
|
|
|
|
2024-06-06 15:54:33 +01:00
|
|
|
fn default_side_node_config() -> SideNodeConfig {
|
|
|
|
|
SideNodeConfig {
|
|
|
|
|
name: "alice".to_string(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-06 13:54:15 +01:00
|
|
|
#[test]
|
|
|
|
|
fn creates_side_node_directory() {
|
2024-06-06 15:29:22 +01:00
|
|
|
let mut test_home = PathBuf::new();
|
|
|
|
|
let side_dir = "/tmp/side";
|
|
|
|
|
|
|
|
|
|
// clean up any previous test runs
|
|
|
|
|
fs::remove_dir_all(side_dir).expect("couldn't remove side directory during test");
|
|
|
|
|
|
|
|
|
|
test_home.push(side_dir);
|
2024-06-06 14:48:14 +01:00
|
|
|
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
|
2024-06-06 15:54:33 +01:00
|
|
|
let _ = init(test_home.clone(), default_side_node_config());
|
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 15:29:22 +01:00
|
|
|
let mut file_path = PathBuf::new();
|
|
|
|
|
file_path.push("/tmp/side");
|
|
|
|
|
let side_dir = file_path.clone();
|
2024-06-06 18:52:39 +01:00
|
|
|
file_path.push(utils::KEY_FILE);
|
2024-06-06 15:29:22 +01:00
|
|
|
|
2024-06-06 15:54:33 +01:00
|
|
|
let _ = init(side_dir.clone(), default_side_node_config());
|
2024-06-06 15:29:22 +01:00
|
|
|
assert!(file_path.exists());
|
|
|
|
|
|
|
|
|
|
// check that the pem is readable
|
2024-06-06 18:55:48 +01:00
|
|
|
let data = fs::read_to_string(file_path).expect("couldn't read key file");
|
|
|
|
|
let keys = Ed25519KeyPair::decode_base64(&data).expect("couldn't load keypair from file");
|
|
|
|
|
assert_eq!(keys.public().as_bytes().len(), 32);
|
2024-06-06 14:39:50 +01:00
|
|
|
}
|
2024-06-06 13:54:15 +01:00
|
|
|
|
2024-06-06 15:29:22 +01:00
|
|
|
#[test]
|
|
|
|
|
fn creates_config_file() {
|
|
|
|
|
let mut file_path = PathBuf::new();
|
|
|
|
|
file_path.push("/tmp/side");
|
|
|
|
|
let side_dir = file_path.clone();
|
2024-06-06 18:52:39 +01:00
|
|
|
file_path.push(utils::CONFIG_FILE);
|
2024-06-06 15:29:22 +01:00
|
|
|
|
2024-06-06 15:54:33 +01:00
|
|
|
let _ = init(side_dir.clone(), default_side_node_config());
|
2024-06-06 15:29:22 +01:00
|
|
|
assert!(file_path.exists());
|
|
|
|
|
}
|
2024-05-29 16:47:35 +01:00
|
|
|
}
|