Starting to write config file
This commit is contained in:
@@ -19,6 +19,7 @@ sha256 = "1.5.0"
|
||||
tokio = { version = "1.37.0", features = ["full"] }
|
||||
websockets = "0.3.0"
|
||||
pem = "3.0.4"
|
||||
toml = "0.8.14"
|
||||
|
||||
[features]
|
||||
default = ["bft", "logging-list", "logging-json"]
|
||||
|
||||
21
side-node/src/init/config.rs
Normal file
21
side-node/src/init/config.rs
Normal file
@@ -0,0 +1,21 @@
|
||||
use serde::Serialize;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use toml::to_string;
|
||||
|
||||
/// Our node config. For now, it just has a name, but it'll expand as we add more features.
|
||||
#[derive(Serialize)]
|
||||
pub(crate) struct SideNodeConfig {
|
||||
pub(crate) name: String,
|
||||
}
|
||||
|
||||
pub(crate) fn write_config_to_file(
|
||||
config: &SideNodeConfig,
|
||||
file_path: &PathBuf,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let toml_string = to_string(config)?;
|
||||
let mut file = File::create(file_path)?;
|
||||
file.write_all(toml_string.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
21
side-node/src/init/keys.rs
Normal file
21
side-node/src/init/keys.rs
Normal 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(())
|
||||
}
|
||||
@@ -1,26 +1,21 @@
|
||||
use std::{
|
||||
fs::File,
|
||||
io::Write,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use pem::Pem;
|
||||
use config::SideNodeConfig;
|
||||
|
||||
use bft_json_crdt::keypair::{make_keypair, KeyPair};
|
||||
pub(crate) mod config;
|
||||
mod keys;
|
||||
|
||||
const KEY_FILE: &str = "keys.pem";
|
||||
const CONFIG_FILE: &str = "config.toml";
|
||||
|
||||
pub(crate) fn init(home: PathBuf) -> Result<(), std::io::Error> {
|
||||
pub(crate) fn init(home: PathBuf, config: SideNodeConfig) -> Result<(), std::io::Error> {
|
||||
ensure_side_directory_exists(&home)?;
|
||||
let (key_path, config_path) = side_paths(home.clone());
|
||||
|
||||
let (key_path, config_path) = side_path(home.clone());
|
||||
|
||||
let pem = create_pem();
|
||||
write_pem(key_path, pem)?;
|
||||
|
||||
let mut file = File::create(config_path)?;
|
||||
let pem = keys::setup();
|
||||
keys::write_pem(key_path, pem)?;
|
||||
|
||||
config::write_config_to_file(&config, &config_path).expect("unable to write config file");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -34,25 +29,8 @@ fn ensure_side_directory_exists(side_dir: &PathBuf) -> Result<(), std::io::Error
|
||||
std::fs::create_dir_all(side_dir)
|
||||
}
|
||||
|
||||
/// Writes a PEM-encoded string to a file at key_path.
|
||||
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(())
|
||||
}
|
||||
|
||||
/// 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 side_path(prefix: PathBuf) -> (PathBuf, PathBuf) {
|
||||
fn side_paths(prefix: PathBuf) -> (PathBuf, PathBuf) {
|
||||
let mut key_path = prefix.clone();
|
||||
key_path.push(KEY_FILE);
|
||||
|
||||
@@ -64,10 +42,16 @@ fn side_path(prefix: PathBuf) -> (PathBuf, PathBuf) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::fs;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn default_side_node_config() -> SideNodeConfig {
|
||||
SideNodeConfig {
|
||||
name: "alice".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn creates_side_node_directory() {
|
||||
let mut test_home = PathBuf::new();
|
||||
@@ -78,7 +62,7 @@ mod tests {
|
||||
|
||||
test_home.push(side_dir);
|
||||
let node_dir = Path::new(&test_home).parent().unwrap().to_str().unwrap();
|
||||
let _ = init(test_home.clone());
|
||||
let _ = init(test_home.clone(), default_side_node_config());
|
||||
assert!(std::path::Path::new(node_dir).exists());
|
||||
}
|
||||
|
||||
@@ -89,7 +73,7 @@ mod tests {
|
||||
let side_dir = file_path.clone();
|
||||
file_path.push(KEY_FILE);
|
||||
|
||||
let _ = init(side_dir.clone());
|
||||
let _ = init(side_dir.clone(), default_side_node_config());
|
||||
assert!(file_path.exists());
|
||||
|
||||
// check that the pem is readable
|
||||
@@ -104,7 +88,7 @@ mod tests {
|
||||
let side_dir = file_path.clone();
|
||||
file_path.push(CONFIG_FILE);
|
||||
|
||||
let _ = init(side_dir.clone());
|
||||
let _ = init(side_dir.clone(), default_side_node_config());
|
||||
assert!(file_path.exists());
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,11 @@ async fn main() {
|
||||
|
||||
match &args.command {
|
||||
Some(Commands::Init {}) => {
|
||||
let _ = init::init(home());
|
||||
let config = init::config::SideNodeConfig {
|
||||
name: "alice".to_string(),
|
||||
};
|
||||
|
||||
let _ = init::init(home(), config);
|
||||
}
|
||||
Some(Commands::Run {}) => {
|
||||
let (mut bft_crdt, keys) = list_transaction_crdt::new();
|
||||
|
||||
Reference in New Issue
Block a user