Key generation, saving and loading works.
This commit is contained in:
12
Cargo.lock
generated
12
Cargo.lock
generated
@@ -1554,16 +1554,6 @@ version = "1.0.15"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "pem"
|
|
||||||
version = "3.0.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8e459365e590736a54c3fa561947c84837534b8e9af6fc5bf781307e82658fae"
|
|
||||||
dependencies = [
|
|
||||||
"base64 0.22.1",
|
|
||||||
"serde",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pem-rfc7468"
|
name = "pem-rfc7468"
|
||||||
version = "0.6.0"
|
version = "0.6.0"
|
||||||
@@ -2213,7 +2203,7 @@ dependencies = [
|
|||||||
"bft-json-crdt",
|
"bft-json-crdt",
|
||||||
"clap",
|
"clap",
|
||||||
"dirs",
|
"dirs",
|
||||||
"pem",
|
"fastcrypto",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_with 3.8.1",
|
"serde_with 3.8.1",
|
||||||
|
|||||||
@@ -18,8 +18,8 @@ serde_with = "3.8.1"
|
|||||||
sha256 = "1.5.0"
|
sha256 = "1.5.0"
|
||||||
tokio = { version = "1.37.0", features = ["full"] }
|
tokio = { version = "1.37.0", features = ["full"] }
|
||||||
websockets = "0.3.0"
|
websockets = "0.3.0"
|
||||||
pem = "3.0.4"
|
|
||||||
toml = "0.8.14"
|
toml = "0.8.14"
|
||||||
|
fastcrypto = "0.1.8"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["bft", "logging-list", "logging-json"]
|
default = ["bft", "logging-list", "logging-json"]
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ pub(crate) struct Args {
|
|||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
pub(crate) enum Commands {
|
pub(crate) enum Commands {
|
||||||
/// runs the Side Node
|
/// runs the Side Node
|
||||||
Run {},
|
Run { name: String },
|
||||||
|
|
||||||
/// initializes the Side Node with a new keypair
|
/// initializes the Side Node with a new keypair
|
||||||
Init { name: String },
|
Init { name: String },
|
||||||
|
|||||||
@@ -1,20 +0,0 @@
|
|||||||
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(key_path: PathBuf, pem: String) -> Result<(), std::io::Error> {
|
|
||||||
let mut file = File::create(key_path)?;
|
|
||||||
file.write(pem.to_string().as_bytes())?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
@@ -2,19 +2,16 @@ use std::path::PathBuf;
|
|||||||
|
|
||||||
use config::SideNodeConfig;
|
use config::SideNodeConfig;
|
||||||
|
|
||||||
pub(crate) mod config;
|
use crate::{keys, utils};
|
||||||
mod keys;
|
|
||||||
|
|
||||||
const KEY_FILE: &str = "keys.pem";
|
pub(crate) mod config;
|
||||||
const CONFIG_FILE: &str = "config.toml";
|
|
||||||
|
|
||||||
pub(crate) fn init(home: PathBuf, config: SideNodeConfig) -> Result<(), std::io::Error> {
|
pub(crate) fn init(home: PathBuf, config: SideNodeConfig) -> Result<(), std::io::Error> {
|
||||||
ensure_side_directory_exists(&home)?;
|
ensure_side_directory_exists(&home)?;
|
||||||
let (key_path, config_path) = side_paths(home.clone());
|
let (key_path, config_path) = utils::side_paths(home.clone());
|
||||||
|
|
||||||
println!("Writing key to: {:?}", key_path);
|
println!("Writing key to: {:?}", key_path);
|
||||||
let pem = keys::setup();
|
keys::write(key_path)?;
|
||||||
keys::write(key_path, pem)?;
|
|
||||||
|
|
||||||
println!("Writing config to: {:?}", config_path);
|
println!("Writing config to: {:?}", config_path);
|
||||||
config::write(&config, &config_path).expect("unable to write config file");
|
config::write(&config, &config_path).expect("unable to write config file");
|
||||||
@@ -28,21 +25,13 @@ fn ensure_side_directory_exists(side_dir: &PathBuf) -> Result<(), std::io::Error
|
|||||||
if side_dir.exists() {
|
if side_dir.exists() {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
println!("Creating side config directory: {:?}", side_dir);
|
println!(
|
||||||
|
"Config directory doesn't exist, creating at: {:?}",
|
||||||
|
side_dir
|
||||||
|
);
|
||||||
std::fs::create_dir_all(side_dir)
|
std::fs::create_dir_all(side_dir)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the path to the key file for this host OS.
|
|
||||||
fn side_paths(prefix: PathBuf) -> (PathBuf, PathBuf) {
|
|
||||||
let mut key_path = prefix.clone();
|
|
||||||
key_path.push(KEY_FILE);
|
|
||||||
|
|
||||||
let mut config_path = prefix.clone();
|
|
||||||
config_path.push(CONFIG_FILE);
|
|
||||||
|
|
||||||
(key_path, config_path)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::{fs, path::Path};
|
use std::{fs, path::Path};
|
||||||
@@ -74,7 +63,7 @@ mod tests {
|
|||||||
let mut file_path = PathBuf::new();
|
let mut file_path = PathBuf::new();
|
||||||
file_path.push("/tmp/side");
|
file_path.push("/tmp/side");
|
||||||
let side_dir = file_path.clone();
|
let side_dir = file_path.clone();
|
||||||
file_path.push(KEY_FILE);
|
file_path.push(utils::KEY_FILE);
|
||||||
|
|
||||||
let _ = init(side_dir.clone(), default_side_node_config());
|
let _ = init(side_dir.clone(), default_side_node_config());
|
||||||
assert!(file_path.exists());
|
assert!(file_path.exists());
|
||||||
@@ -89,7 +78,7 @@ mod tests {
|
|||||||
let mut file_path = PathBuf::new();
|
let mut file_path = PathBuf::new();
|
||||||
file_path.push("/tmp/side");
|
file_path.push("/tmp/side");
|
||||||
let side_dir = file_path.clone();
|
let side_dir = file_path.clone();
|
||||||
file_path.push(CONFIG_FILE);
|
file_path.push(utils::CONFIG_FILE);
|
||||||
|
|
||||||
let _ = init(side_dir.clone(), default_side_node_config());
|
let _ = init(side_dir.clone(), default_side_node_config());
|
||||||
assert!(file_path.exists());
|
assert!(file_path.exists());
|
||||||
|
|||||||
27
side-node/src/keys.rs
Normal file
27
side-node/src/keys.rs
Normal 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")
|
||||||
|
}
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use bft_crdt_derive::add_crdt_fields;
|
use bft_crdt_derive::add_crdt_fields;
|
||||||
|
|
||||||
use bft_json_crdt::{
|
use bft_json_crdt::{
|
||||||
@@ -10,6 +12,8 @@ use serde::{Deserialize, Serialize};
|
|||||||
use serde_json::{json, Value};
|
use serde_json::{json, Value};
|
||||||
use websockets::WebSocket;
|
use websockets::WebSocket;
|
||||||
|
|
||||||
|
use crate::keys;
|
||||||
|
|
||||||
#[add_crdt_fields]
|
#[add_crdt_fields]
|
||||||
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
#[derive(Clone, CrdtNode, Serialize, Deserialize)]
|
||||||
pub(crate) struct CrdtList {
|
pub(crate) struct CrdtList {
|
||||||
@@ -25,8 +29,9 @@ pub(crate) struct Transaction {
|
|||||||
amount: f64,
|
amount: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn new() -> (BaseCrdt<CrdtList>, Ed25519KeyPair) {
|
pub(crate) fn new(side_dir: PathBuf) -> (BaseCrdt<CrdtList>, Ed25519KeyPair) {
|
||||||
let keys = make_keypair();
|
let keys = keys::load_from_file(side_dir);
|
||||||
|
// let keys = make_keypair();
|
||||||
let bft_crdt = BaseCrdt::<CrdtList>::new(&keys);
|
let bft_crdt = BaseCrdt::<CrdtList>::new(&keys);
|
||||||
println!("Author is {}", keys.public().to_string());
|
println!("Author is {}", keys.public().to_string());
|
||||||
(bft_crdt, keys)
|
(bft_crdt, keys)
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ use cli::{parse_args, Commands};
|
|||||||
|
|
||||||
pub(crate) mod cli;
|
pub(crate) mod cli;
|
||||||
pub(crate) mod init;
|
pub(crate) mod init;
|
||||||
|
pub(crate) mod keys;
|
||||||
pub(crate) mod list_transaction_crdt;
|
pub(crate) mod list_transaction_crdt;
|
||||||
|
pub(crate) mod utils;
|
||||||
pub(crate) mod websocket;
|
pub(crate) mod websocket;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
@@ -17,8 +19,9 @@ async fn main() {
|
|||||||
|
|
||||||
let _ = init::init(home(name), config);
|
let _ = init::init(home(name), config);
|
||||||
}
|
}
|
||||||
Some(Commands::Run {}) => {
|
Some(Commands::Run { name }) => {
|
||||||
let (mut bft_crdt, keys) = list_transaction_crdt::new();
|
let side_dir = home(name);
|
||||||
|
let (mut bft_crdt, keys) = list_transaction_crdt::new(side_dir);
|
||||||
websocket::start(keys, &mut bft_crdt).await.unwrap();
|
websocket::start(keys, &mut bft_crdt).await.unwrap();
|
||||||
}
|
}
|
||||||
None => println!("No command provided. Exiting. See --help for more information."),
|
None => println!("No command provided. Exiting. See --help for more information."),
|
||||||
|
|||||||
15
side-node/src/utils.rs
Normal file
15
side-node/src/utils.rs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
pub(crate) const KEY_FILE: &str = "keys.pem";
|
||||||
|
pub(crate) const CONFIG_FILE: &str = "config.toml";
|
||||||
|
|
||||||
|
/// Returns the path to the key file for this host OS.
|
||||||
|
pub(crate) fn side_paths(prefix: PathBuf) -> (PathBuf, PathBuf) {
|
||||||
|
let mut key_path = prefix.clone();
|
||||||
|
key_path.push(KEY_FILE);
|
||||||
|
|
||||||
|
let mut config_path = prefix.clone();
|
||||||
|
config_path.push(CONFIG_FILE);
|
||||||
|
|
||||||
|
(key_path, config_path)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user