improving documentation and error handling

This commit is contained in:
scilio
2022-04-27 15:11:20 -04:00
parent 115e0d2022
commit 8cf41e4730
4 changed files with 47 additions and 11 deletions
+19 -10
View File
@@ -14,22 +14,31 @@ use std::path::PathBuf;
// The decrypted server config to be passed around and used by the rest of the mwixnet code
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct ServerConfig {
/// private key used by the server to decrypt onion packets
pub key: SecretKey,
/// interval (in seconds) to wait before each mixing round
pub interval_s: u32,
/// socket address the server listener should bind to
pub addr: SocketAddr,
/// foreign api address of the grin node
pub grin_node_url: SocketAddr,
/// owner api address of the grin wallet
pub wallet_owner_url: SocketAddr,
}
/// Encrypted server key, for storing on disk and decrypting with provided password
/// Encrypted server key, for storing on disk and decrypting with a password.
/// Includes a salt used by key derivation and a nonce used when sealing the encrypted data.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
struct EncryptedServerKey {
encrypted_key: String,
pub salt: String,
pub nonce: String,
salt: String,
nonce: String,
}
impl EncryptedServerKey {
/// Generates a random salt for pbkdf2 key derivation and a random nonce for aead sealing.
/// Then derives an encryption key from the password and salt. Finally, it encrypts and seals
/// the server key with chacha20-poly1305 using the derived key and random nonce.
pub fn from_secret_key(
server_key: &SecretKey,
password: &ZeroingString,
@@ -104,13 +113,13 @@ impl EncryptedServerKey {
/// The config attributes saved to disk
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
struct RawConfig {
pub encrypted_key: String,
pub salt: String,
pub nonce: String,
pub interval_s: u32,
pub addr: SocketAddr,
pub grin_node_url: SocketAddr,
pub wallet_owner_url: SocketAddr,
encrypted_key: String,
salt: String,
nonce: String,
interval_s: u32,
addr: SocketAddr,
grin_node_url: SocketAddr,
wallet_owner_url: SocketAddr,
}
/// Writes the server config to the config_path given, encrypting the server_key first.
+12 -1
View File
@@ -6,6 +6,7 @@ use wallet::HttpWallet;
use clap::App;
use grin_util::{StopState, ZeroingString};
use std::env;
use std::io::{self, Write};
use std::path::PathBuf;
use std::sync::Arc;
@@ -26,7 +27,16 @@ mod wallet;
const DEFAULT_INTERVAL: u32 = 12 * 60 * 60;
fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
fn main() {
if let Err(e) = real_main() {
io::stderr().write_all(format!("mwixnet server exited with error:\n{}\n", e).as_bytes()).unwrap();
std::process::exit(1);
}
std::process::exit(0);
}
fn real_main() -> std::result::Result<(), Box<dyn std::error::Error>> {
let yml = load_yaml!("../mwixnet.yml");
let args = App::from_yaml(yml).get_matches();
@@ -61,6 +71,7 @@ fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
};
config::write_config(&config_path, &server_config, &password)?;
println!("Config file written to {:?}. Please back this file up in a safe place.", config_path);
return Ok(());
}