From 6a3316897308fc7670c166f9dbd878c49e6f9a9c Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 28 Jan 2020 12:46:53 +0000 Subject: [PATCH 01/39] validator: adding the abci crate --- Cargo.lock | 76 +++++++++++++++++++++++++++++++++++++++++++- validator/Cargo.toml | 1 + 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 14b3f014ca..cf783a09b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,5 +1,22 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. +[[package]] +name = "abci" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1cfb90d266d6ca61cea4d155dd5033c9516495f4f74f82faf1ca5c4feeb82577" +dependencies = [ + "byteorder", + "bytes 0.4.12", + "env_logger 0.7.1", + "futures 0.3.1", + "integer-encoding", + "log", + "protobuf", + "protobuf-codegen-pure", + "tokio 0.1.22", +] + [[package]] name = "addressing" version = "0.1.0" @@ -597,6 +614,19 @@ dependencies = [ "termcolor", ] +[[package]] +name = "env_logger" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + [[package]] name = "error-chain" version = "0.12.1" @@ -1020,6 +1050,12 @@ dependencies = [ "bytes 0.4.12", ] +[[package]] +name = "integer-encoding" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aec89c15e2cfa0f0eae8ca60e03cb10b30d25ea2c0ad7d6be60a95e32729994" + [[package]] name = "iovec" version = "0.1.4" @@ -1419,6 +1455,7 @@ dependencies = [ name = "nym-validator" version = "0.4.0-rc.1" dependencies = [ + "abci", "built", "clap", "crypto", @@ -1582,7 +1619,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "717ee476b1690853d222af4634056d830b5197ffd747726a9a1eee6da9f49074" dependencies = [ "chrono", - "env_logger", + "env_logger 0.6.2", "log", ] @@ -1612,6 +1649,31 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "protobuf" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6563a657a014b771e7f69f06447d88d8fbb5a215ffc4cab724afb3acedcc7701" + +[[package]] +name = "protobuf-codegen" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f1bbc6db30d5d3e730b6e2326e9a64a75ca9c80d6427d6f054dc8cacc79d225" +dependencies = [ + "protobuf", +] + +[[package]] +name = "protobuf-codegen-pure" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5473ffa23d2ea3b9046764f1a22149791967aad946b6cbd99601e720afc4d0" +dependencies = [ + "protobuf", + "protobuf-codegen", +] + [[package]] name = "provider-client" version = "0.1.0" @@ -2240,6 +2302,7 @@ dependencies = [ "futures 0.1.29", "mio", "num_cpus", + "tokio-codec", "tokio-current-thread", "tokio-executor", "tokio-io", @@ -2284,6 +2347,17 @@ dependencies = [ "futures 0.1.29", ] +[[package]] +name = "tokio-codec" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c501eceaf96f0e1793cf26beb63da3d11c738c4a943fdf3746d81d64684c39f" +dependencies = [ + "bytes 0.4.12", + "futures 0.1.29", + "tokio-io", +] + [[package]] name = "tokio-current-thread" version = "0.1.6" diff --git a/validator/Cargo.toml b/validator/Cargo.toml index 5bf0cf583a..49c827ec8b 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +abci = "0.6.4" clap = "2.33.0" # Read notes https://crates.io/crates/dotenv - tl;dr: don't use in production, set environmental variables properly. dotenv = "0.15.0" From ee8da3a019e57b8a07e08579dfb91857087da095 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 28 Jan 2020 12:47:16 +0000 Subject: [PATCH 02/39] validator: starting a README so docs will be easier to write when we switch from Go --- validator/README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 validator/README.md diff --git a/validator/README.md b/validator/README.md new file mode 100644 index 0000000000..3f6dcb7b2a --- /dev/null +++ b/validator/README.md @@ -0,0 +1,18 @@ +Nym Validator +============= + +The Nym Validator has several jobs: + +* use Tendermint (v0.33.0) to maintain a total global ordering of incoming transactions +* track quality of service for mixnet nodes (mixmining) +* generate Coconut credentials and ensure they're not double spent +* maintain a decentralized directory of all Nym nodes that have staked into the system + +Some of these functions may be moved away to their own node types in the future, for example to increase scalability or performance. At the moment, we'd like to keep deployments simple, so they're all in the validator node. + +Running the validator +--------------------- + +1. Download and install [Tendermint 0.33.0](https://github.com/tendermint/tendermint/releases/tag/v0.33.0) +2. `tendermint init` +3. `tendermint run` \ No newline at end of file From 74bf2ed9f43d100f7d63d99554e3529755d524d2 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 28 Jan 2020 13:03:24 +0000 Subject: [PATCH 03/39] validator: using exact Tendermint version specified by rust-abci crate docs --- validator/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validator/README.md b/validator/README.md index 3f6dcb7b2a..e3a5722acf 100644 --- a/validator/README.md +++ b/validator/README.md @@ -13,6 +13,6 @@ Some of these functions may be moved away to their own node types in the future, Running the validator --------------------- -1. Download and install [Tendermint 0.33.0](https://github.com/tendermint/tendermint/releases/tag/v0.33.0) +1. Download and install [Tendermint 0.32.7](https://github.com/tendermint/tendermint/releases/tag/v0.32.7) 2. `tendermint init` -3. `tendermint run` \ No newline at end of file +3. `tendermint node` \ No newline at end of file From dd2e4f153a60b06a62948c3a844c6c4beba90fdf Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 28 Jan 2020 13:28:11 +0000 Subject: [PATCH 04/39] validator: adding byteorder crate --- Cargo.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.lock b/Cargo.lock index cf783a09b9..97d346cbc9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1457,6 +1457,7 @@ version = "0.4.0-rc.1" dependencies = [ "abci", "built", + "byteorder", "clap", "crypto", "dotenv", From 58f839ecd8caddd0436b92938d01664cba941d4e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 28 Jan 2020 13:28:20 +0000 Subject: [PATCH 05/39] ibid --- validator/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/validator/Cargo.toml b/validator/Cargo.toml index 49c827ec8b..d59ebdbf06 100644 --- a/validator/Cargo.toml +++ b/validator/Cargo.toml @@ -9,6 +9,7 @@ edition = "2018" [dependencies] abci = "0.6.4" +byteorder = "1.3.2" clap = "2.33.0" # Read notes https://crates.io/crates/dotenv - tl;dr: don't use in production, set environmental variables properly. dotenv = "0.15.0" From 742c428a251ffc442bfad52fb4fa44d6c6e8c0d1 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 28 Jan 2020 13:28:42 +0000 Subject: [PATCH 06/39] validator: sample tendermint counter app is breathing. --- validator/src/main.rs | 8 ++++ validator/src/tendermint_abci/mod.rs | 65 ++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 validator/src/tendermint_abci/mod.rs diff --git a/validator/src/main.rs b/validator/src/main.rs index 39d0d375f3..3e9a27705c 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -5,6 +5,7 @@ use log::{error, trace}; use std::process; use toml; +mod tendermint_abci; mod validator; fn main() { @@ -26,6 +27,7 @@ fn main() { .required(true), ), ) + .subcommand(SubCommand::with_name("tm").about("Starts the Tendermint test app")) .get_matches(); if let Err(e) = execute(arg_matches) { @@ -42,6 +44,11 @@ fn run(matches: &ArgMatches) { validator.start() } +fn tm(_matches: &ArgMatches) { + println!("Starting Tendermint app."); + abci::run_local(tendermint_abci::CounterApp::new()); +} + fn parse_config(matches: &ArgMatches) -> Config { let config_file_path = matches.value_of("config").unwrap(); // since this is happening at the very startup, it's fine to panic if file doesn't exist @@ -57,6 +64,7 @@ pub mod built_info { fn execute(matches: ArgMatches) -> Result<(), String> { match matches.subcommand() { ("run", Some(m)) => Ok(run(m)), + ("tm", Some(m)) => Ok(tm(m)), _ => Err(usage()), } } diff --git a/validator/src/tendermint_abci/mod.rs b/validator/src/tendermint_abci/mod.rs new file mode 100644 index 0000000000..69f859bd9e --- /dev/null +++ b/validator/src/tendermint_abci/mod.rs @@ -0,0 +1,65 @@ +use abci::*; +use byteorder::{BigEndian, ByteOrder}; + +// Simple counter application. Its only state is a u64 count +// We use BigEndian to serialize the data across transactions calls +pub struct CounterApp { + count: u64, +} + +impl CounterApp { + pub fn new() -> CounterApp { + CounterApp { count: 0 } + } +} + +// Convert incoming tx data to the proper BigEndian size. txs.len() > 8 will return 0 +fn convert_tx(tx: &[u8]) -> u64 { + if tx.len() < 8 { + let pad = 8 - tx.len(); + let mut x = vec![0; pad]; + x.extend_from_slice(tx); + return BigEndian::read_u64(x.as_slice()); + } + BigEndian::read_u64(tx) +} + +impl abci::Application for CounterApp { + // Validate transactions. Rule: Transactions must be incremental: 1,2,3,4... + fn check_tx(&mut self, req: &RequestCheckTx) -> ResponseCheckTx { + // Get the Tx [u8] and convert to u64 + let c = convert_tx(req.get_tx()); + let mut resp = ResponseCheckTx::new(); + + // Validation logic + if c != self.count + 1 { + resp.set_code(1); + resp.set_log(String::from("Count must be incremental!")); + return resp; + } + + // Update state to keep state correct for next check_tx call + self.count = c; + resp + } + + fn deliver_tx(&mut self, req: &RequestDeliverTx) -> ResponseDeliverTx { + // Get the Tx [u8] + let c = convert_tx(req.get_tx()); + // Update state + self.count = c; + // Return default code 0 == bueno + ResponseDeliverTx::new() + } + + fn commit(&mut self, _req: &RequestCommit) -> ResponseCommit { + // Create the response + let mut resp = ResponseCommit::new(); + // Convert count to bits + let mut buf = [0; 8]; + BigEndian::write_u64(&mut buf, self.count); + // Set data so last state is included in the block + resp.set_data(buf.to_vec()); + resp + } +} From b20555c622537c85d240d663b55ad026c9ff1f4b Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Tue, 28 Jan 2020 13:31:13 +0000 Subject: [PATCH 07/39] validator: slight comment change --- validator/src/validator/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/src/validator/mod.rs b/validator/src/validator/mod.rs index 6ed56fa42b..98c51880e6 100644 --- a/validator/src/validator/mod.rs +++ b/validator/src/validator/mod.rs @@ -42,6 +42,6 @@ impl Validator Date: Wed, 29 Jan 2020 08:20:06 +0000 Subject: [PATCH 08/39] validator: using info logging --- validator/src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validator/src/main.rs b/validator/src/main.rs index 3e9a27705c..5e3701d0f2 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -1,7 +1,7 @@ use crate::validator::config::Config; use crate::validator::Validator; use clap::{App, Arg, ArgMatches, SubCommand}; -use log::{error, trace}; +use log::{error, info, trace}; use std::process; use toml; @@ -45,7 +45,7 @@ fn run(matches: &ArgMatches) { } fn tm(_matches: &ArgMatches) { - println!("Starting Tendermint app."); + info!("Starting Tendermint app."); abci::run_local(tendermint_abci::CounterApp::new()); } From 482deb718c9f8312bec11b6101dcfc785cd4255e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 08:45:51 +0000 Subject: [PATCH 09/39] validator: more docs --- validator/README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/validator/README.md b/validator/README.md index e3a5722acf..1c6467e22d 100644 --- a/validator/README.md +++ b/validator/README.md @@ -10,9 +10,11 @@ The Nym Validator has several jobs: Some of these functions may be moved away to their own node types in the future, for example to increase scalability or performance. At the moment, we'd like to keep deployments simple, so they're all in the validator node. -Running the validator ---------------------- +Running the validator on your local machine +------------------------------------------- 1. Download and install [Tendermint 0.32.7](https://github.com/tendermint/tendermint/releases/tag/v0.32.7) -2. `tendermint init` -3. `tendermint node` \ No newline at end of file +2. `tendermint init` sets up Tendermint for use +3. `tendermint node` runs Tendermint +4. `cargo run -- run` builds the Nym Validator and runs it + From 79f9372ebbef736e4f695df6367861e45ddc1b02 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 08:46:09 +0000 Subject: [PATCH 10/39] validator: banging structure around a bit --- validator/src/main.rs | 5 +++-- validator/src/network/mod.rs | 1 + validator/src/{ => network}/tendermint_abci/mod.rs | 0 validator/src/validator/config/mod.rs | 7 ------- validator/src/validator/mod.rs | 9 +++++++-- 5 files changed, 11 insertions(+), 11 deletions(-) create mode 100644 validator/src/network/mod.rs rename validator/src/{ => network}/tendermint_abci/mod.rs (100%) delete mode 100644 validator/src/validator/config/mod.rs diff --git a/validator/src/main.rs b/validator/src/main.rs index 5e3701d0f2..d964d8991a 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -1,11 +1,12 @@ -use crate::validator::config::Config; +use crate::validator::Config; use crate::validator::Validator; use clap::{App, Arg, ArgMatches, SubCommand}; use log::{error, info, trace}; +use network::tendermint_abci; use std::process; use toml; -mod tendermint_abci; +mod network; mod validator; fn main() { diff --git a/validator/src/network/mod.rs b/validator/src/network/mod.rs new file mode 100644 index 0000000000..30db3a456d --- /dev/null +++ b/validator/src/network/mod.rs @@ -0,0 +1 @@ +pub mod tendermint_abci; diff --git a/validator/src/tendermint_abci/mod.rs b/validator/src/network/tendermint_abci/mod.rs similarity index 100% rename from validator/src/tendermint_abci/mod.rs rename to validator/src/network/tendermint_abci/mod.rs diff --git a/validator/src/validator/config/mod.rs b/validator/src/validator/config/mod.rs deleted file mode 100644 index 4ef70b9c7e..0000000000 --- a/validator/src/validator/config/mod.rs +++ /dev/null @@ -1,7 +0,0 @@ -use serde_derive::Deserialize; - -#[derive(Deserialize, Debug)] -pub struct Config { - #[serde(rename(deserialize = "healthcheck"))] - pub health_check: healthcheck::config::HealthCheck, -} diff --git a/validator/src/validator/mod.rs b/validator/src/validator/mod.rs index 98c51880e6..d9c220318f 100644 --- a/validator/src/validator/mod.rs +++ b/validator/src/validator/mod.rs @@ -1,4 +1,3 @@ -use crate::validator::config::Config; use crypto::identity::{ DummyMixIdentityKeyPair, DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey, MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey, @@ -7,7 +6,13 @@ use healthcheck::HealthChecker; use log::debug; use tokio::runtime::Runtime; -pub mod config; +use serde_derive::Deserialize; + +#[derive(Deserialize, Debug)] +pub struct Config { + #[serde(rename(deserialize = "healthcheck"))] + pub health_check: healthcheck::config::HealthCheck, +} // allow for a generic validator pub struct Validator From 4ab9b8414d050f046b16309a6fe3aa2da787ab19 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 08:49:13 +0000 Subject: [PATCH 11/39] Removing the validator folder, having two seems overly nested --- validator/src/{validator/mod.rs => validator.rs} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename validator/src/{validator/mod.rs => validator.rs} (100%) diff --git a/validator/src/validator/mod.rs b/validator/src/validator.rs similarity index 100% rename from validator/src/validator/mod.rs rename to validator/src/validator.rs From 80f92254e00e1fc404d8c98cea3ccfe1924348d9 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 09:25:34 +0000 Subject: [PATCH 12/39] Ignoring validator config file inside repo --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 74a82c4e13..0e2a851081 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ target .env /.vscode/settings.json +sample-configs/validator-config.toml \ No newline at end of file From ae7c1467aea22db2fb863d495f40b8a2f413256b Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 09:26:08 +0000 Subject: [PATCH 13/39] validator: simplifying structure of networking interface files --- validator/src/network/ethereum.rs | 0 validator/src/network/mod.rs | 3 +++ .../mod.rs => tendermint_abci.rs} | 20 +++++++------------ 3 files changed, 10 insertions(+), 13 deletions(-) create mode 100644 validator/src/network/ethereum.rs rename validator/src/network/{tendermint_abci/mod.rs => tendermint_abci.rs} (84%) diff --git a/validator/src/network/ethereum.rs b/validator/src/network/ethereum.rs new file mode 100644 index 0000000000..e69de29bb2 diff --git a/validator/src/network/mod.rs b/validator/src/network/mod.rs index 30db3a456d..cc58e96b21 100644 --- a/validator/src/network/mod.rs +++ b/validator/src/network/mod.rs @@ -1 +1,4 @@ +//! The `network` module provides interfaces to external systems via network +//! connectivity. +//! pub mod tendermint_abci; diff --git a/validator/src/network/tendermint_abci/mod.rs b/validator/src/network/tendermint_abci.rs similarity index 84% rename from validator/src/network/tendermint_abci/mod.rs rename to validator/src/network/tendermint_abci.rs index 69f859bd9e..90236fbe9a 100644 --- a/validator/src/network/tendermint_abci/mod.rs +++ b/validator/src/network/tendermint_abci.rs @@ -1,17 +1,9 @@ +use crate::validator::Validator; use abci::*; use byteorder::{BigEndian, ByteOrder}; - -// Simple counter application. Its only state is a u64 count -// We use BigEndian to serialize the data across transactions calls -pub struct CounterApp { - count: u64, -} - -impl CounterApp { - pub fn new() -> CounterApp { - CounterApp { count: 0 } - } -} +use crypto::identity::{ + DummyMixIdentityKeyPair, DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey, +}; // Convert incoming tx data to the proper BigEndian size. txs.len() > 8 will return 0 fn convert_tx(tx: &[u8]) -> u64 { @@ -24,7 +16,9 @@ fn convert_tx(tx: &[u8]) -> u64 { BigEndian::read_u64(tx) } -impl abci::Application for CounterApp { +impl abci::Application + for Validator +{ // Validate transactions. Rule: Transactions must be incremental: 1,2,3,4... fn check_tx(&mut self, req: &RequestCheckTx) -> ResponseCheckTx { // Get the Tx [u8] and convert to u64 From 17a513d5fb39fb3bf4b6a103a0ac3ead4472ee04 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 09:31:01 +0000 Subject: [PATCH 14/39] validator: Tendermint ABCI works with Validator struct. --- validator/src/main.rs | 20 ++++++++++++++++---- validator/src/validator.rs | 8 +++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/validator/src/main.rs b/validator/src/main.rs index d964d8991a..832173d6d8 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -2,7 +2,6 @@ use crate::validator::Config; use crate::validator::Validator; use clap::{App, Arg, ArgMatches, SubCommand}; use log::{error, info, trace}; -use network::tendermint_abci; use std::process; use toml; @@ -28,7 +27,17 @@ fn main() { .required(true), ), ) - .subcommand(SubCommand::with_name("tm").about("Starts the Tendermint test app")) + .subcommand( + SubCommand::with_name("tm") + .about("Starts the Tendermint test app") + .arg( + Arg::with_name("config") + .long("config") + .help("Location of the validator configuration file") + .takes_value(true) + .required(true), + ), + ) .get_matches(); if let Err(e) = execute(arg_matches) { @@ -45,9 +54,12 @@ fn run(matches: &ArgMatches) { validator.start() } -fn tm(_matches: &ArgMatches) { +fn tm(matches: &ArgMatches) { + let config = parse_config(matches); + trace!("read config: {:?}", config); info!("Starting Tendermint app."); - abci::run_local(tendermint_abci::CounterApp::new()); + let validator = Validator::new(config); + abci::run_local(validator); } fn parse_config(matches: &ArgMatches) -> Config { diff --git a/validator/src/validator.rs b/validator/src/validator.rs index d9c220318f..1725def40b 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -21,9 +21,10 @@ where Priv: MixnetIdentityPrivateKey, Pub: MixnetIdentityPublicKey, { + pub count: u64, + heath_check: HealthChecker, #[allow(dead_code)] identity_keypair: IDPair, - heath_check: HealthChecker, } // but for time being, since it's a dummy one, have it use dummy keys @@ -34,8 +35,9 @@ impl Validator Date: Wed, 29 Jan 2020 09:33:38 +0000 Subject: [PATCH 15/39] validator: removed separate "tm" command --- validator/src/main.rs | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/validator/src/main.rs b/validator/src/main.rs index 832173d6d8..db7b56a87c 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -27,17 +27,6 @@ fn main() { .required(true), ), ) - .subcommand( - SubCommand::with_name("tm") - .about("Starts the Tendermint test app") - .arg( - Arg::with_name("config") - .long("config") - .help("Location of the validator configuration file") - .takes_value(true) - .required(true), - ), - ) .get_matches(); if let Err(e) = execute(arg_matches) { @@ -54,14 +43,6 @@ fn run(matches: &ArgMatches) { validator.start() } -fn tm(matches: &ArgMatches) { - let config = parse_config(matches); - trace!("read config: {:?}", config); - info!("Starting Tendermint app."); - let validator = Validator::new(config); - abci::run_local(validator); -} - fn parse_config(matches: &ArgMatches) -> Config { let config_file_path = matches.value_of("config").unwrap(); // since this is happening at the very startup, it's fine to panic if file doesn't exist @@ -77,7 +58,6 @@ pub mod built_info { fn execute(matches: ArgMatches) -> Result<(), String> { match matches.subcommand() { ("run", Some(m)) => Ok(run(m)), - ("tm", Some(m)) => Ok(tm(m)), _ => Err(usage()), } } From 657e09288714a44322ae022c88a3cdb9a6d27562 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 10:47:46 +0000 Subject: [PATCH 16/39] mixnode: minor re-ordering of start() operations --- mixnode/src/node/mod.rs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/mixnode/src/node/mod.rs b/mixnode/src/node/mod.rs index 0432dec413..502ff77843 100644 --- a/mixnode/src/node/mod.rs +++ b/mixnode/src/node/mod.rs @@ -242,17 +242,20 @@ impl MixNode { } pub fn start(&self, config: node::Config) -> Result<(), Box> { - // Create the runtime, probably later move it to MixNode itself? - let mut rt = Runtime::new()?; - - let (received_tx, received_rx) = mpsc::channel(1024); - let (sent_tx, sent_rx) = mpsc::channel(1024); - + // Set up config and public key for this node let directory_cfg = directory_client::Config { base_url: self.directory_server.clone(), }; let pub_key_str = bs58::encode(&self.public_key.to_bytes().to_vec()).into_string(); + // Set up channels + let (received_tx, received_rx) = mpsc::channel(1024); + let (sent_tx, sent_rx) = mpsc::channel(1024); + + // Create the runtime, probably later move it to MixNode itself? + let mut rt = Runtime::new()?; + + // Spawn Tokio tasks as necessary for node functionality rt.spawn({ let presence_notifier = presence::Notifier::new(&config); presence_notifier.run() From 81ed98d3ec13f72e3af628245dfeda16e09a8528 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 11:33:23 +0000 Subject: [PATCH 17/39] validator: pulling all log levels into main --- validator/src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/src/main.rs b/validator/src/main.rs index db7b56a87c..335c3b0845 100644 --- a/validator/src/main.rs +++ b/validator/src/main.rs @@ -1,7 +1,7 @@ use crate::validator::Config; use crate::validator::Validator; use clap::{App, Arg, ArgMatches, SubCommand}; -use log::{error, info, trace}; +use log::*; use std::process; use toml; From ef64871f78aee96e8f7fcf5fe4d75b70e20e0e2a Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 11:33:47 +0000 Subject: [PATCH 18/39] validator: adding an async start() method to abci network interface --- validator/src/network/tendermint_abci.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/validator/src/network/tendermint_abci.rs b/validator/src/network/tendermint_abci.rs index 90236fbe9a..deea902fa6 100644 --- a/validator/src/network/tendermint_abci.rs +++ b/validator/src/network/tendermint_abci.rs @@ -57,3 +57,8 @@ impl abci::Application resp } } +pub async fn start( + app: Validator, +) { + abci::run_local(app); +} From 7a68499c1179b79a2f93df667afff5b3adbc1eda Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 11:34:18 +0000 Subject: [PATCH 19/39] validator: easier log level switches --- validator/src/validator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 1725def40b..dcbc11b470 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -3,7 +3,7 @@ use crypto::identity::{ MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey, }; use healthcheck::HealthChecker; -use log::debug; +use log::*; use tokio::runtime::Runtime; use serde_derive::Deserialize; From 62f4c8bebde88b4fc822236b011907565feabc6e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 11:34:49 +0000 Subject: [PATCH 20/39] validator: import the abci interface for use by the runner --- validator/src/validator.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index dcbc11b470..f75281d027 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -1,3 +1,4 @@ +use crate::network::tendermint_abci; use crypto::identity::{ DummyMixIdentityKeyPair, DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey, MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey, From 4c4ec081229adbc2b8102862811ebca4a4a4cb16 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 11:35:39 +0000 Subject: [PATCH 21/39] validator: fixing health_check typo in Validator struct --- validator/src/validator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index f75281d027..1f94185e92 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -23,7 +23,7 @@ where Pub: MixnetIdentityPublicKey, { pub count: u64, - heath_check: HealthChecker, + health_check: HealthChecker, #[allow(dead_code)] identity_keypair: IDPair, } From d9613043ecf30136736df54bb6b2e1a6b67fe92b Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 12:28:29 +0000 Subject: [PATCH 22/39] validator: comment on networks --- validator/src/network/tendermint_abci.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/src/network/tendermint_abci.rs b/validator/src/network/tendermint_abci.rs index deea902fa6..68358e08c2 100644 --- a/validator/src/network/tendermint_abci.rs +++ b/validator/src/network/tendermint_abci.rs @@ -5,7 +5,7 @@ use crypto::identity::{ DummyMixIdentityKeyPair, DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey, }; -// Convert incoming tx data to the proper BigEndian size. txs.len() > 8 will return 0 +// Convert incoming tx network data to the proper BigEndian size. txs.len() > 8 will return 0 fn convert_tx(tx: &[u8]) -> u64 { if tx.len() < 8 { let pad = 8 - tx.len(); From 8c9d68041c417281c9df96b5a262a85cd00a0c80 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 12:49:51 +0000 Subject: [PATCH 23/39] validator: renamed tendermint integration --- validator/src/network/mod.rs | 2 +- .../{tendermint_abci.rs => tendermint.rs} | 24 +++++++++++-------- 2 files changed, 15 insertions(+), 11 deletions(-) rename validator/src/network/{tendermint_abci.rs => tendermint.rs} (80%) diff --git a/validator/src/network/mod.rs b/validator/src/network/mod.rs index cc58e96b21..6ad3fa6cb5 100644 --- a/validator/src/network/mod.rs +++ b/validator/src/network/mod.rs @@ -1,4 +1,4 @@ //! The `network` module provides interfaces to external systems via network //! connectivity. //! -pub mod tendermint_abci; +pub mod tendermint; diff --git a/validator/src/network/tendermint_abci.rs b/validator/src/network/tendermint.rs similarity index 80% rename from validator/src/network/tendermint_abci.rs rename to validator/src/network/tendermint.rs index 68358e08c2..c493dbb407 100644 --- a/validator/src/network/tendermint_abci.rs +++ b/validator/src/network/tendermint.rs @@ -1,9 +1,6 @@ -use crate::validator::Validator; use abci::*; use byteorder::{BigEndian, ByteOrder}; -use crypto::identity::{ - DummyMixIdentityKeyPair, DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey, -}; +use serde_derive::Deserialize; // Convert incoming tx network data to the proper BigEndian size. txs.len() > 8 will return 0 fn convert_tx(tx: &[u8]) -> u64 { @@ -16,9 +13,18 @@ fn convert_tx(tx: &[u8]) -> u64 { BigEndian::read_u64(tx) } -impl abci::Application - for Validator -{ +#[derive(Debug, Deserialize)] +pub struct Abci { + count: u64, +} + +impl Abci { + pub fn new() -> Abci { + Abci { count: 0 } + } +} + +impl abci::Application for Abci { // Validate transactions. Rule: Transactions must be incremental: 1,2,3,4... fn check_tx(&mut self, req: &RequestCheckTx) -> ResponseCheckTx { // Get the Tx [u8] and convert to u64 @@ -57,8 +63,6 @@ impl abci::Application resp } } -pub async fn start( - app: Validator, -) { +pub async fn start(app: Abci) { abci::run_local(app); } From f02b37f5c932d8e8166ef0ed308db56c8efa7a20 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 12:50:09 +0000 Subject: [PATCH 24/39] validator: pulling tendermint ABCI out into its own type. --- validator/src/validator.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 1f94185e92..f2c668336a 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -1,4 +1,4 @@ -use crate::network::tendermint_abci; +use crate::network::tendermint; use crypto::identity::{ DummyMixIdentityKeyPair, DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey, MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey, @@ -22,7 +22,7 @@ where Priv: MixnetIdentityPrivateKey, Pub: MixnetIdentityPublicKey, { - pub count: u64, + tendermint_abci: tendermint::Abci, health_check: HealthChecker, #[allow(dead_code)] identity_keypair: IDPair, @@ -36,18 +36,20 @@ impl Validator Date: Wed, 29 Jan 2020 12:54:30 +0000 Subject: [PATCH 25/39] validator: changing tendermint start to run() --- validator/src/network/tendermint.rs | 7 ++++--- validator/src/validator.rs | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/validator/src/network/tendermint.rs b/validator/src/network/tendermint.rs index c493dbb407..b2960a6f40 100644 --- a/validator/src/network/tendermint.rs +++ b/validator/src/network/tendermint.rs @@ -22,6 +22,10 @@ impl Abci { pub fn new() -> Abci { Abci { count: 0 } } + + pub async fn run(self) { + abci::run_local(self); + } } impl abci::Application for Abci { @@ -63,6 +67,3 @@ impl abci::Application for Abci { resp } } -pub async fn start(app: Abci) { - abci::run_local(app); -} diff --git a/validator/src/validator.rs b/validator/src/validator.rs index f2c668336a..ec6c1b4aa3 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -46,7 +46,7 @@ impl Validator Date: Wed, 29 Jan 2020 12:55:04 +0000 Subject: [PATCH 26/39] ibid --- validator/src/validator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index ec6c1b4aa3..1b27c4344b 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -46,7 +46,7 @@ impl Validator Date: Wed, 29 Jan 2020 12:58:40 +0000 Subject: [PATCH 27/39] validator: removing serde stuff in abci --- validator/src/network/tendermint.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/validator/src/network/tendermint.rs b/validator/src/network/tendermint.rs index b2960a6f40..4a581774e6 100644 --- a/validator/src/network/tendermint.rs +++ b/validator/src/network/tendermint.rs @@ -1,6 +1,5 @@ use abci::*; use byteorder::{BigEndian, ByteOrder}; -use serde_derive::Deserialize; // Convert incoming tx network data to the proper BigEndian size. txs.len() > 8 will return 0 fn convert_tx(tx: &[u8]) -> u64 { @@ -13,7 +12,6 @@ fn convert_tx(tx: &[u8]) -> u64 { BigEndian::read_u64(tx) } -#[derive(Debug, Deserialize)] pub struct Abci { count: u64, } From 536b84c55cb808ea5ce901a1b951da6b94859de9 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 12:59:02 +0000 Subject: [PATCH 28/39] validator: removing logging calls --- validator/src/validator.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 1b27c4344b..4bf44d82ad 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -31,8 +31,6 @@ where // but for time being, since it's a dummy one, have it use dummy keys impl Validator { pub fn new(config: Config) -> Self { - debug!("validator new"); - let dummy_keypair = DummyMixIdentityKeyPair::new(); Validator { @@ -43,7 +41,6 @@ impl Validator Date: Wed, 29 Jan 2020 13:01:00 +0000 Subject: [PATCH 29/39] validator: removing logging for now --- validator/src/validator.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 4bf44d82ad..99d462317d 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -4,7 +4,6 @@ use crypto::identity::{ MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey, }; use healthcheck::HealthChecker; -use log::*; use tokio::runtime::Runtime; use serde_derive::Deserialize; From aa251c245039ca04acc8927752a6e31c8b322824 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Wed, 29 Jan 2020 13:02:48 +0000 Subject: [PATCH 30/39] validator: multi-threading tendermint so that health check also works --- validator/src/validator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 99d462317d..343b8f54cd 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -45,7 +45,7 @@ impl Validator Date: Wed, 29 Jan 2020 13:05:16 +0000 Subject: [PATCH 31/39] validator: updating run instructions --- validator/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/validator/README.md b/validator/README.md index 1c6467e22d..c8bbad5042 100644 --- a/validator/README.md +++ b/validator/README.md @@ -15,6 +15,7 @@ Running the validator on your local machine 1. Download and install [Tendermint 0.32.7](https://github.com/tendermint/tendermint/releases/tag/v0.32.7) 2. `tendermint init` sets up Tendermint for use -3. `tendermint node` runs Tendermint -4. `cargo run -- run` builds the Nym Validator and runs it +3. `tendermint node` runs Tendermint. You'll get errors until you run the Nym validator, this is normal :). +4. `cp sample-configs/validator-config.toml.sample sample-configs/validator-config.toml` +5. `cargo run -- run --config ../sample-configs/validator-config.toml` builds the Nym Validator and runs it From a73585e13aeddfac4c419db8eabee9dda0fef556 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Wed, 29 Jan 2020 15:30:20 +0000 Subject: [PATCH 32/39] Simplified the use of generics on identity keypair by using output types --- common/crypto/src/identity/mod.rs | 20 ++++++++--------- common/healthcheck/src/lib.rs | 27 ++++------------------- common/healthcheck/src/path_check.rs | 11 +++------ common/healthcheck/src/result.rs | 8 +++---- common/pemstore/src/pemstore.rs | 19 ++++++---------- nym-client/src/client/mod.rs | 22 +++++------------- nym-client/src/client/topology_control.rs | 16 +++++--------- 7 files changed, 39 insertions(+), 84 deletions(-) diff --git a/common/crypto/src/identity/mod.rs b/common/crypto/src/identity/mod.rs index cf39000e5d..3c920f84dc 100644 --- a/common/crypto/src/identity/mod.rs +++ b/common/crypto/src/identity/mod.rs @@ -6,14 +6,13 @@ use bs58; use curve25519_dalek::scalar::Scalar; use sphinx::route::DestinationAddressBytes; -pub trait MixnetIdentityKeyPair: Clone -where - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, -{ +pub trait MixnetIdentityKeyPair: Clone { + type PublicKeyMaterial: MixnetIdentityPublicKey; + type PrivateKeyMaterial: MixnetIdentityPrivateKey; + fn new() -> Self; - fn private_key(&self) -> &Priv; - fn public_key(&self) -> &Pub; + fn private_key(&self) -> &Self::PrivateKeyMaterial; + fn public_key(&self) -> &Self::PublicKeyMaterial; fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self; // TODO: signing related methods @@ -56,9 +55,10 @@ pub struct DummyMixIdentityKeyPair { pub public_key: DummyMixIdentityPublicKey, } -impl MixnetIdentityKeyPair - for DummyMixIdentityKeyPair -{ +impl MixnetIdentityKeyPair for DummyMixIdentityKeyPair { + type PublicKeyMaterial = DummyMixIdentityPublicKey; + type PrivateKeyMaterial = DummyMixIdentityPrivateKey; + fn new() -> Self { let keypair = encryption::x25519::KeyPair::new(); DummyMixIdentityKeyPair { diff --git a/common/healthcheck/src/lib.rs b/common/healthcheck/src/lib.rs index 7cd97692db..5554b2bb97 100644 --- a/common/healthcheck/src/lib.rs +++ b/common/healthcheck/src/lib.rs @@ -1,10 +1,7 @@ use crate::result::HealthCheckResult; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; -use directory_client::requests::presence_topology_get::PresenceTopologyGetRequester; -use directory_client::DirectoryClient; -use log::{debug, error, info, trace}; +use crypto::identity::MixnetIdentityKeyPair; +use log::trace; use std::fmt::{Error, Formatter}; -use std::marker::PhantomData; use std::time::Duration; use topology::{NymTopology, NymTopologyError}; @@ -36,26 +33,13 @@ impl From for HealthCheckerError { } } -pub struct HealthChecker -where - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, -{ +pub struct HealthChecker { num_test_packets: usize, resolution_timeout: Duration, identity_keypair: IDPair, - - _phantom_private: PhantomData, - _phantom_public: PhantomData, } -impl HealthChecker -where - IDPair: crypto::identity::MixnetIdentityKeyPair, - Priv: crypto::identity::MixnetIdentityPrivateKey, - Pub: crypto::identity::MixnetIdentityPublicKey, -{ +impl HealthChecker { pub fn new( resolution_timeout_f64: f64, num_test_packets: usize, @@ -65,9 +49,6 @@ where resolution_timeout: Duration::from_secs_f64(resolution_timeout_f64), num_test_packets, identity_keypair, - - _phantom_private: PhantomData, - _phantom_public: PhantomData, } } diff --git a/common/healthcheck/src/path_check.rs b/common/healthcheck/src/path_check.rs index b1d2617ad8..a345e82d99 100644 --- a/common/healthcheck/src/path_check.rs +++ b/common/healthcheck/src/path_check.rs @@ -1,4 +1,4 @@ -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; use itertools::Itertools; use log::{debug, error, info, trace, warn}; use mix_client::MixClient; @@ -27,16 +27,11 @@ pub(crate) struct PathChecker { } impl PathChecker { - pub(crate) async fn new( + pub(crate) async fn new( providers: Vec, identity_keys: &IDPair, check_id: [u8; 16], - ) -> Self - where - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, - { + ) -> Self { let mut provider_clients = HashMap::new(); let address = identity_keys.public_key().derive_address(); diff --git a/common/healthcheck/src/result.rs b/common/healthcheck/src/result.rs index 9eda0885f5..2dd199ffde 100644 --- a/common/healthcheck/src/result.rs +++ b/common/healthcheck/src/result.rs @@ -1,6 +1,6 @@ use crate::path_check::{PathChecker, PathStatus}; use crate::score::NodeScore; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::MixnetIdentityKeyPair; use log::{debug, error, info, warn}; use rand_os::rand_core::RngCore; use sphinx::route::NodeAddressBytes; @@ -102,7 +102,7 @@ impl HealthCheckResult { id } - pub async fn calculate( + pub async fn calculate( topology: &T, iterations: usize, resolution_timeout: Duration, @@ -110,9 +110,7 @@ impl HealthCheckResult { ) -> Self where T: NymTopology, - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, + IDPair: MixnetIdentityKeyPair, { // currently healthchecker supports only up to 255 iterations - if we somehow // find we need more, it's relatively easy change diff --git a/common/pemstore/src/pemstore.rs b/common/pemstore/src/pemstore.rs index 9c7a914d02..84bc8cf60a 100644 --- a/common/pemstore/src/pemstore.rs +++ b/common/pemstore/src/pemstore.rs @@ -1,4 +1,6 @@ use crate::pathfinder::PathFinder; +use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::PemStorable; use pem::{encode, parse, Pem}; use std::fs::File; use std::io; @@ -25,12 +27,7 @@ impl PemStore { } } - pub fn read_identity(&self) -> io::Result - where - IDPair: crypto::identity::MixnetIdentityKeyPair, - Priv: crypto::identity::MixnetIdentityPrivateKey, - Pub: crypto::identity::MixnetIdentityPublicKey, - { + pub fn read_identity(&self) -> io::Result { let private_pem = self.read_pem_file(self.private_mix_key.clone())?; let public_pem = self.read_pem_file(self.public_mix_key.clone())?; @@ -62,12 +59,10 @@ impl PemStore { // This should be refactored and made more generic for when we have other kinds of // KeyPairs that we want to persist (e.g. validator keypairs, or keys for // signing vs encryption). However, for the moment, it does the job. - pub fn write_identity(&self, key_pair: IDPair) -> io::Result<()> - where - IDPair: crypto::identity::MixnetIdentityKeyPair, - Priv: crypto::identity::MixnetIdentityPrivateKey, - Pub: crypto::identity::MixnetIdentityPublicKey, - { + pub fn write_identity( + &self, + key_pair: IDPair, + ) -> io::Result<()> { std::fs::create_dir_all(self.config_dir.clone())?; let private_key = key_pair.private_key(); diff --git a/nym-client/src/client/mod.rs b/nym-client/src/client/mod.rs index 4d4e0da408..8bb090b95f 100644 --- a/nym-client/src/client/mod.rs +++ b/nym-client/src/client/mod.rs @@ -3,14 +3,13 @@ use crate::client::received_buffer::ReceivedMessagesBuffer; use crate::client::topology_control::TopologyInnerRef; use crate::sockets::tcp; use crate::sockets::ws; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; use directory_client::presence::Topology; use futures::channel::mpsc; use futures::join; use log::*; use sfw_provider_requests::AuthToken; use sphinx::route::Destination; -use std::marker::PhantomData; use std::net::SocketAddr; use tokio::runtime::Runtime; use topology::NymTopology; @@ -37,11 +36,9 @@ pub enum SocketType { None, } -pub struct NymClient +pub struct NymClient where - IDPair: MixnetIdentityKeyPair + Send + Sync, - Priv: MixnetIdentityPrivateKey + Send + Sync, - Pub: MixnetIdentityPublicKey + Send + Sync, + IDPair: MixnetIdentityKeyPair + Send + Sync, { keypair: IDPair, @@ -53,19 +50,14 @@ where directory: String, auth_token: Option, socket_type: SocketType, - - _phantom_private: PhantomData, - _phantom_public: PhantomData, } #[derive(Debug)] pub struct InputMessage(pub Destination, pub Vec); -impl NymClient +impl NymClient where - IDPair: MixnetIdentityKeyPair + Send + Sync, - Priv: MixnetIdentityPrivateKey + Send + Sync, - Pub: MixnetIdentityPublicKey + Send + Sync, + IDPair: 'static + MixnetIdentityKeyPair + Send + Sync, { pub fn new( keypair: IDPair, @@ -84,8 +76,6 @@ where directory, auth_token, socket_type, - _phantom_private: PhantomData, - _phantom_public: PhantomData, } } @@ -129,7 +119,7 @@ where // TODO: when we switch to our graph topology, we need to remember to change 'Topology' type let topology_controller = - rt.block_on(topology_control::TopologyControl::::new( + rt.block_on(topology_control::TopologyControl::::new( self.directory.clone(), TOPOLOGY_REFRESH_RATE, healthcheck_keys, diff --git a/nym-client/src/client/topology_control.rs b/nym-client/src/client/topology_control.rs index 9833f81166..5ec9422782 100644 --- a/nym-client/src/client/topology_control.rs +++ b/nym-client/src/client/topology_control.rs @@ -1,5 +1,5 @@ use crate::built_info; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::MixnetIdentityKeyPair; use healthcheck::HealthChecker; use log::{error, info, trace, warn}; use std::sync::Arc; @@ -12,16 +12,14 @@ const NODE_HEALTH_THRESHOLD: f64 = 0.0; // auxiliary type for ease of use pub type TopologyInnerRef = Arc>>; -pub(crate) struct TopologyControl +pub(crate) struct TopologyControl where T: NymTopology, - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, + IDPair: MixnetIdentityKeyPair, { directory_server: String, inner: Arc>>, - health_checker: HealthChecker, + health_checker: HealthChecker, refresh_rate: f64, } @@ -31,12 +29,10 @@ enum TopologyError { NoValidPathsError, } -impl TopologyControl +impl TopologyControl where T: NymTopology, - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, + IDPair: MixnetIdentityKeyPair, { pub(crate) async fn new( directory_server: String, From 2d99b2b93ac9872ed2425286397a9e6b6c845c39 Mon Sep 17 00:00:00 2001 From: Jedrzej Stuczynski Date: Thu, 30 Jan 2020 10:19:55 +0000 Subject: [PATCH 33/39] Fixed validator compilation error --- validator/src/validator/mod.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/validator/src/validator/mod.rs b/validator/src/validator/mod.rs index 26f031fab5..4cd4891255 100644 --- a/validator/src/validator/mod.rs +++ b/validator/src/validator/mod.rs @@ -1,8 +1,5 @@ use crate::validator::config::Config; -use crypto::identity::{ - DummyMixIdentityKeyPair, DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey, - MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey, -}; +use crypto::identity::{DummyMixIdentityKeyPair, MixnetIdentityKeyPair}; use directory_client::presence::Topology; use healthcheck::HealthChecker; use log::{debug, error, info}; @@ -13,20 +10,15 @@ use topology::NymTopology; pub mod config; // allow for a generic validator -pub struct Validator -where - IDPair: MixnetIdentityKeyPair, - Priv: MixnetIdentityPrivateKey, - Pub: MixnetIdentityPublicKey, -{ +pub struct Validator { config: Config, #[allow(dead_code)] identity_keypair: IDPair, - heath_check: HealthChecker, + heath_check: HealthChecker, } // but for time being, since it's a dummy one, have it use dummy keys -impl Validator { +impl Validator { pub fn new(config: Config) -> Self { debug!("validator new"); From d75ac203d2c370416afec19950dfb61c9b6f024a Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 30 Jan 2020 11:32:06 +0000 Subject: [PATCH 34/39] validator: simplified task spawning --- validator/src/network/tendermint.rs | 4 +- .../services/mixmining/health_check_runner.rs | 42 +++++++++---------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/validator/src/network/tendermint.rs b/validator/src/network/tendermint.rs index 2f3d2b924a..4a581774e6 100644 --- a/validator/src/network/tendermint.rs +++ b/validator/src/network/tendermint.rs @@ -21,8 +21,8 @@ impl Abci { Abci { count: 0 } } - pub fn run(self) { - tokio::spawn(async { abci::run_local(self) }) + pub async fn run(self) { + abci::run_local(self); } } diff --git a/validator/src/services/mixmining/health_check_runner.rs b/validator/src/services/mixmining/health_check_runner.rs index c2bd6c7b18..42a3821cea 100644 --- a/validator/src/services/mixmining/health_check_runner.rs +++ b/validator/src/services/mixmining/health_check_runner.rs @@ -24,27 +24,25 @@ impl HealthCheckRunner { } pub async fn run(self) { - tokio::spawn(async move { - let healthcheck_interval = Duration::from_secs_f64(self.interval); - debug!("healthcheck will run every {:?}", healthcheck_interval); - loop { - let full_topology = - directory_client::presence::Topology::new(self.directory_server.clone()); - let version_filtered_topology = full_topology.filter_node_versions( - crate::built_info::PKG_VERSION, - crate::built_info::PKG_VERSION, - crate::built_info::PKG_VERSION, - ); - match self - .health_checker - .do_check(&version_filtered_topology) - .await - { - Ok(health) => info!("current network health: \n{}", health), - Err(err) => error!("failed to perform healthcheck - {:?}", err), - }; - tokio::time::delay_for(healthcheck_interval).await; - } - }); + let healthcheck_interval = Duration::from_secs_f64(self.interval); + debug!("healthcheck will run every {:?}", healthcheck_interval); + loop { + let full_topology = + directory_client::presence::Topology::new(self.directory_server.clone()); + let version_filtered_topology = full_topology.filter_node_versions( + crate::built_info::PKG_VERSION, + crate::built_info::PKG_VERSION, + crate::built_info::PKG_VERSION, + ); + match self + .health_checker + .do_check(&version_filtered_topology) + .await + { + Ok(health) => info!("current network health: \n{}", health), + Err(err) => error!("failed to perform healthcheck - {:?}", err), + }; + tokio::time::delay_for(healthcheck_interval).await; + } } } From 46055fe8c494cdc997304a5690619a9662d4ab1a Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 30 Jan 2020 11:33:04 +0000 Subject: [PATCH 35/39] validator: removing unused config --- validator/src/validator.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 1ba280dae7..b4a3e2d0e0 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -14,7 +14,6 @@ pub struct Config { // allow for a generic validator pub struct Validator { - config: Config, #[allow(dead_code)] identity_keypair: IDPair, health_check_runner: health_check_runner::HealthCheckRunner, @@ -40,7 +39,6 @@ impl Validator { Validator { identity_keypair: dummy_keypair, health_check_runner, - config, tendermint_abci: tendermint::Abci::new(), } } From c426b09b07f40363674bc6bd6ea9ef07ff5e7e8e Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 30 Jan 2020 11:50:37 +0000 Subject: [PATCH 36/39] validator: placeholder comment for Ethereum integration --- validator/src/network/ethereum.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/validator/src/network/ethereum.rs b/validator/src/network/ethereum.rs index e69de29bb2..9aa85267fe 100644 --- a/validator/src/network/ethereum.rs +++ b/validator/src/network/ethereum.rs @@ -0,0 +1 @@ +// placeholder for Ethereum / ERC20 bridge integration From d3199778e4745424a00b9ef1902b320fe0c0d76d Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 30 Jan 2020 11:55:15 +0000 Subject: [PATCH 37/39] validator: starting removal of keypair generics --- .../src/services/mixmining/health_check_runner.rs | 12 ++++++------ validator/src/validator.rs | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/validator/src/services/mixmining/health_check_runner.rs b/validator/src/services/mixmining/health_check_runner.rs index 42a3821cea..b53162273f 100644 --- a/validator/src/services/mixmining/health_check_runner.rs +++ b/validator/src/services/mixmining/health_check_runner.rs @@ -1,21 +1,21 @@ -use crypto::identity::MixnetIdentityKeyPair; +use crypto::identity::DummyMixIdentityKeyPair; use healthcheck::HealthChecker; use log::*; use std::time::Duration; use topology::NymTopology; -pub struct HealthCheckRunner { +pub struct HealthCheckRunner { directory_server: String, - health_checker: HealthChecker, + health_checker: HealthChecker, interval: f64, } -impl HealthCheckRunner { +impl HealthCheckRunner { pub fn new( directory_server: String, interval: f64, - health_checker: HealthChecker, - ) -> HealthCheckRunner { + health_checker: HealthChecker, + ) -> HealthCheckRunner { HealthCheckRunner { directory_server, health_checker, diff --git a/validator/src/validator.rs b/validator/src/validator.rs index b4a3e2d0e0..6cdc6f6fc3 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -13,15 +13,15 @@ pub struct Config { } // allow for a generic validator -pub struct Validator { +pub struct Validator { #[allow(dead_code)] - identity_keypair: IDPair, - health_check_runner: health_check_runner::HealthCheckRunner, + identity_keypair: DummyMixIdentityKeyPair, + health_check_runner: health_check_runner::HealthCheckRunner, tendermint_abci: tendermint::Abci, } // but for time being, since it's a dummy one, have it use dummy keys -impl Validator { +impl Validator { pub fn new(config: Config) -> Self { let dummy_keypair = DummyMixIdentityKeyPair::new(); let hc = HealthChecker::new( From 6b879e88e77ffb0137192c99453533c74dfbc3a7 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 30 Jan 2020 12:19:23 +0000 Subject: [PATCH 38/39] nym: removing type parameters on keypairs --- common/crypto/src/identity/mod.rs | 125 ++++++------------ common/healthcheck/src/lib.rs | 10 +- common/healthcheck/src/path_check.rs | 6 +- common/healthcheck/src/result.rs | 12 +- common/pemstore/src/pemstore.rs | 12 +- nym-client/src/client/mod.rs | 29 ++-- nym-client/src/client/topology_control.rs | 18 +-- nym-client/src/commands/init.rs | 4 +- nym-client/src/commands/tcpsocket.rs | 4 +- nym-client/src/commands/websocket.rs | 4 +- sfw-provider/src/main.rs | 4 +- .../src/provider/client_handling/mod.rs | 14 +- sfw-provider/src/provider/mix_handling/mod.rs | 6 +- sfw-provider/src/provider/mod.rs | 14 +- sfw-provider/src/provider/presence.rs | 4 +- .../services/mixmining/health_check_runner.rs | 5 +- validator/src/validator.rs | 6 +- 17 files changed, 102 insertions(+), 175 deletions(-) diff --git a/common/crypto/src/identity/mod.rs b/common/crypto/src/identity/mod.rs index 3c920f84dc..a966ec4cb9 100644 --- a/common/crypto/src/identity/mod.rs +++ b/common/crypto/src/identity/mod.rs @@ -6,90 +6,44 @@ use bs58; use curve25519_dalek::scalar::Scalar; use sphinx::route::DestinationAddressBytes; -pub trait MixnetIdentityKeyPair: Clone { - type PublicKeyMaterial: MixnetIdentityPublicKey; - type PrivateKeyMaterial: MixnetIdentityPrivateKey; - - fn new() -> Self; - fn private_key(&self) -> &Self::PrivateKeyMaterial; - fn public_key(&self) -> &Self::PublicKeyMaterial; - fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self; - - // TODO: signing related methods -} - -pub trait MixnetIdentityPublicKey: - Sized - + PemStorable - + Clone - + for<'a> From<&'a ::PrivateKeyMaterial> -{ - // we need to couple public and private keys together - type PrivateKeyMaterial: MixnetIdentityPrivateKey; - - fn derive_address(&self) -> DestinationAddressBytes; - fn to_bytes(&self) -> Vec; - fn from_bytes(b: &[u8]) -> Self; -} - -pub trait MixnetIdentityPrivateKey: Sized + PemStorable + Clone { - // we need to couple public and private keys together - type PublicKeyMaterial: MixnetIdentityPublicKey; - - /// Returns the associated public key - fn public_key(&self) -> Self::PublicKeyMaterial { - self.into() - } - - fn to_bytes(&self) -> Vec; - fn from_bytes(b: &[u8]) -> Self; -} - -// same for validator - // for time being define a dummy identity using x25519 encryption keys (as we've done so far) // and replace it with proper keys, like ed25519 later on #[derive(Clone)] -pub struct DummyMixIdentityKeyPair { - pub private_key: DummyMixIdentityPrivateKey, - pub public_key: DummyMixIdentityPublicKey, +pub struct MixIdentityKeyPair { + pub private_key: MixIdentityPrivateKey, + pub public_key: MixIdentityPublicKey, } -impl MixnetIdentityKeyPair for DummyMixIdentityKeyPair { - type PublicKeyMaterial = DummyMixIdentityPublicKey; - type PrivateKeyMaterial = DummyMixIdentityPrivateKey; - - fn new() -> Self { +impl MixIdentityKeyPair { + pub fn new() -> Self { let keypair = encryption::x25519::KeyPair::new(); - DummyMixIdentityKeyPair { - private_key: DummyMixIdentityPrivateKey(keypair.private_key), - public_key: DummyMixIdentityPublicKey(keypair.public_key), + MixIdentityKeyPair { + private_key: MixIdentityPrivateKey(keypair.private_key), + public_key: MixIdentityPublicKey(keypair.public_key), } } - fn private_key(&self) -> &DummyMixIdentityPrivateKey { + pub fn private_key(&self) -> &MixIdentityPrivateKey { &self.private_key } - fn public_key(&self) -> &DummyMixIdentityPublicKey { + pub fn public_key(&self) -> &MixIdentityPublicKey { &self.public_key } - fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { - DummyMixIdentityKeyPair { - private_key: DummyMixIdentityPrivateKey::from_bytes(priv_bytes), - public_key: DummyMixIdentityPublicKey::from_bytes(pub_bytes), + pub fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { + MixIdentityKeyPair { + private_key: MixIdentityPrivateKey::from_bytes(priv_bytes), + public_key: MixIdentityPublicKey::from_bytes(pub_bytes), } } } #[derive(Debug, Clone, Eq, PartialEq)] -pub struct DummyMixIdentityPublicKey(encryption::x25519::PublicKey); +pub struct MixIdentityPublicKey(encryption::x25519::PublicKey); -impl MixnetIdentityPublicKey for DummyMixIdentityPublicKey { - type PrivateKeyMaterial = DummyMixIdentityPrivateKey; - - fn derive_address(&self) -> DestinationAddressBytes { +impl MixIdentityPublicKey { + pub fn derive_address(&self) -> DestinationAddressBytes { let mut temporary_address = [0u8; 32]; let public_key_bytes = self.to_bytes(); temporary_address.copy_from_slice(&public_key_bytes[..]); @@ -97,65 +51,60 @@ impl MixnetIdentityPublicKey for DummyMixIdentityPublicKey { temporary_address } - fn to_bytes(&self) -> Vec { + pub fn to_bytes(&self) -> Vec { self.0.to_bytes() } - fn from_bytes(b: &[u8]) -> Self { + pub fn from_bytes(b: &[u8]) -> Self { Self(encryption::x25519::PublicKey::from_bytes(b)) } + + pub fn to_base58_string(&self) -> String { + bs58::encode(&self.to_bytes()).into_string() + } + + pub fn from_base58_string(val: String) -> Self { + Self::from_bytes(&bs58::decode(&val).into_vec().unwrap()) + } } -impl PemStorable for DummyMixIdentityPublicKey { +impl PemStorable for MixIdentityPublicKey { fn pem_type(&self) -> String { format!("DUMMY KEY BASED ON {}", self.0.pem_type()) } } -impl DummyMixIdentityPublicKey { - pub fn to_base58_string(&self) -> String { - bs58::encode(&self.to_bytes()).into_string() - } - - #[allow(dead_code)] - fn from_base58_string(val: String) -> Self { - Self::from_bytes(&bs58::decode(&val).into_vec().unwrap()) - } -} - // COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed #[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub struct DummyMixIdentityPrivateKey(pub encryption::x25519::PrivateKey); +pub struct MixIdentityPrivateKey(pub encryption::x25519::PrivateKey); -impl<'a> From<&'a DummyMixIdentityPrivateKey> for DummyMixIdentityPublicKey { - fn from(pk: &'a DummyMixIdentityPrivateKey) -> Self { +impl<'a> From<&'a MixIdentityPrivateKey> for MixIdentityPublicKey { + fn from(pk: &'a MixIdentityPrivateKey) -> Self { let private_ref = &pk.0; let public: encryption::x25519::PublicKey = private_ref.into(); - DummyMixIdentityPublicKey(public) + MixIdentityPublicKey(public) } } -impl MixnetIdentityPrivateKey for DummyMixIdentityPrivateKey { - type PublicKeyMaterial = DummyMixIdentityPublicKey; - - fn to_bytes(&self) -> Vec { +impl MixIdentityPrivateKey { + pub fn to_bytes(&self) -> Vec { self.0.to_bytes() } - fn from_bytes(b: &[u8]) -> Self { + pub fn from_bytes(b: &[u8]) -> Self { Self(encryption::x25519::PrivateKey::from_bytes(b)) } } // TODO: this will be implemented differently by using the proper trait -impl DummyMixIdentityPrivateKey { +impl MixIdentityPrivateKey { pub fn as_scalar(self) -> Scalar { let encryption_key = self.0; encryption_key.0 } } -impl PemStorable for DummyMixIdentityPrivateKey { +impl PemStorable for MixIdentityPrivateKey { fn pem_type(&self) -> String { format!("DUMMY KEY BASED ON {}", self.0.pem_type()) } diff --git a/common/healthcheck/src/lib.rs b/common/healthcheck/src/lib.rs index 5554b2bb97..fc4be5850e 100644 --- a/common/healthcheck/src/lib.rs +++ b/common/healthcheck/src/lib.rs @@ -1,5 +1,5 @@ use crate::result::HealthCheckResult; -use crypto::identity::MixnetIdentityKeyPair; +use crypto::identity::MixIdentityKeyPair; use log::trace; use std::fmt::{Error, Formatter}; use std::time::Duration; @@ -33,17 +33,17 @@ impl From for HealthCheckerError { } } -pub struct HealthChecker { +pub struct HealthChecker { num_test_packets: usize, resolution_timeout: Duration, - identity_keypair: IDPair, + identity_keypair: MixIdentityKeyPair, } -impl HealthChecker { +impl HealthChecker { pub fn new( resolution_timeout_f64: f64, num_test_packets: usize, - identity_keypair: IDPair, + identity_keypair: MixIdentityKeyPair, ) -> Self { HealthChecker { resolution_timeout: Duration::from_secs_f64(resolution_timeout_f64), diff --git a/common/healthcheck/src/path_check.rs b/common/healthcheck/src/path_check.rs index a345e82d99..074df7da44 100644 --- a/common/healthcheck/src/path_check.rs +++ b/common/healthcheck/src/path_check.rs @@ -1,4 +1,4 @@ -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; +use crypto::identity::MixIdentityKeyPair; use itertools::Itertools; use log::{debug, error, info, trace, warn}; use mix_client::MixClient; @@ -27,9 +27,9 @@ pub(crate) struct PathChecker { } impl PathChecker { - pub(crate) async fn new( + pub(crate) async fn new( providers: Vec, - identity_keys: &IDPair, + identity_keys: &MixIdentityKeyPair, check_id: [u8; 16], ) -> Self { let mut provider_clients = HashMap::new(); diff --git a/common/healthcheck/src/result.rs b/common/healthcheck/src/result.rs index 2dd199ffde..677114a457 100644 --- a/common/healthcheck/src/result.rs +++ b/common/healthcheck/src/result.rs @@ -1,6 +1,6 @@ use crate::path_check::{PathChecker, PathStatus}; use crate::score::NodeScore; -use crypto::identity::MixnetIdentityKeyPair; +use crypto::identity::MixIdentityKeyPair; use log::{debug, error, info, warn}; use rand_os::rand_core::RngCore; use sphinx::route::NodeAddressBytes; @@ -102,16 +102,12 @@ impl HealthCheckResult { id } - pub async fn calculate( + pub async fn calculate( topology: &T, iterations: usize, resolution_timeout: Duration, - identity_keys: &IDPair, - ) -> Self - where - T: NymTopology, - IDPair: MixnetIdentityKeyPair, - { + identity_keys: &MixIdentityKeyPair, + ) -> Self { // currently healthchecker supports only up to 255 iterations - if we somehow // find we need more, it's relatively easy change assert!(iterations <= 255); diff --git a/common/pemstore/src/pemstore.rs b/common/pemstore/src/pemstore.rs index 84bc8cf60a..a8d7b4b239 100644 --- a/common/pemstore/src/pemstore.rs +++ b/common/pemstore/src/pemstore.rs @@ -1,5 +1,5 @@ use crate::pathfinder::PathFinder; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPrivateKey, MixnetIdentityPublicKey}; +use crypto::identity::MixIdentityKeyPair; use crypto::PemStorable; use pem::{encode, parse, Pem}; use std::fs::File; @@ -27,11 +27,12 @@ impl PemStore { } } - pub fn read_identity(&self) -> io::Result { + pub fn read_identity(&self) -> io::Result { let private_pem = self.read_pem_file(self.private_mix_key.clone())?; let public_pem = self.read_pem_file(self.public_mix_key.clone())?; - let key_pair = IDPair::from_bytes(&private_pem.contents, &public_pem.contents); + let key_pair = + MixIdentityKeyPair::from_bytes(&private_pem.contents, &public_pem.contents); if key_pair.private_key().pem_type() != private_pem.tag { return Err(io::Error::new( @@ -59,10 +60,7 @@ impl PemStore { // This should be refactored and made more generic for when we have other kinds of // KeyPairs that we want to persist (e.g. validator keypairs, or keys for // signing vs encryption). However, for the moment, it does the job. - pub fn write_identity( - &self, - key_pair: IDPair, - ) -> io::Result<()> { + pub fn write_identity(&self, key_pair: MixIdentityKeyPair) -> io::Result<()> { std::fs::create_dir_all(self.config_dir.clone())?; let private_key = key_pair.private_key(); diff --git a/nym-client/src/client/mod.rs b/nym-client/src/client/mod.rs index 8bb090b95f..af074e3b4f 100644 --- a/nym-client/src/client/mod.rs +++ b/nym-client/src/client/mod.rs @@ -3,7 +3,7 @@ use crate::client::received_buffer::ReceivedMessagesBuffer; use crate::client::topology_control::TopologyInnerRef; use crate::sockets::tcp; use crate::sockets::ws; -use crypto::identity::{MixnetIdentityKeyPair, MixnetIdentityPublicKey}; +use crypto::identity::MixIdentityKeyPair; use directory_client::presence::Topology; use futures::channel::mpsc; use futures::join; @@ -36,11 +36,8 @@ pub enum SocketType { None, } -pub struct NymClient -where - IDPair: MixnetIdentityKeyPair + Send + Sync, -{ - keypair: IDPair, +pub struct NymClient { + keypair: MixIdentityKeyPair, // to be used by "send" function or socket, etc pub input_tx: mpsc::UnboundedSender, @@ -55,12 +52,9 @@ where #[derive(Debug)] pub struct InputMessage(pub Destination, pub Vec); -impl NymClient -where - IDPair: 'static + MixnetIdentityKeyPair + Send + Sync, -{ +impl NymClient { pub fn new( - keypair: IDPair, + keypair: MixIdentityKeyPair, socket_listening_address: SocketAddr, directory: String, auth_token: Option, @@ -115,15 +109,14 @@ where let self_address = self.keypair.public_key().derive_address(); // generate same type of keys we have as our identity - let healthcheck_keys = IDPair::new(); + let healthcheck_keys = MixIdentityKeyPair::new(); // TODO: when we switch to our graph topology, we need to remember to change 'Topology' type - let topology_controller = - rt.block_on(topology_control::TopologyControl::::new( - self.directory.clone(), - TOPOLOGY_REFRESH_RATE, - healthcheck_keys, - )); + let topology_controller = rt.block_on(topology_control::TopologyControl::::new( + self.directory.clone(), + TOPOLOGY_REFRESH_RATE, + healthcheck_keys, + )); let provider_client_listener_address = rt.block_on(self.get_provider_socket_address(topology_controller.get_inner_ref())); diff --git a/nym-client/src/client/topology_control.rs b/nym-client/src/client/topology_control.rs index 5ec9422782..90c84679cc 100644 --- a/nym-client/src/client/topology_control.rs +++ b/nym-client/src/client/topology_control.rs @@ -1,5 +1,5 @@ use crate::built_info; -use crypto::identity::MixnetIdentityKeyPair; +use crypto::identity::MixIdentityKeyPair; use healthcheck::HealthChecker; use log::{error, info, trace, warn}; use std::sync::Arc; @@ -12,14 +12,10 @@ const NODE_HEALTH_THRESHOLD: f64 = 0.0; // auxiliary type for ease of use pub type TopologyInnerRef = Arc>>; -pub(crate) struct TopologyControl -where - T: NymTopology, - IDPair: MixnetIdentityKeyPair, -{ +pub(crate) struct TopologyControl { directory_server: String, inner: Arc>>, - health_checker: HealthChecker, + health_checker: HealthChecker, refresh_rate: f64, } @@ -29,15 +25,11 @@ enum TopologyError { NoValidPathsError, } -impl TopologyControl -where - T: NymTopology, - IDPair: MixnetIdentityKeyPair, -{ +impl TopologyControl { pub(crate) async fn new( directory_server: String, refresh_rate: f64, - identity_keypair: IDPair, + identity_keypair: MixIdentityKeyPair, ) -> Self { // this is a temporary solution as the healthcheck will eventually be moved to validators let health_checker = healthcheck::HealthChecker::new(5.0, 2, identity_keypair); diff --git a/nym-client/src/commands/init.rs b/nym-client/src/commands/init.rs index bc4403e6b9..1ee2385ded 100644 --- a/nym-client/src/commands/init.rs +++ b/nym-client/src/commands/init.rs @@ -1,6 +1,6 @@ use crate::config::persistance::pathfinder::ClientPathfinder; use clap::ArgMatches; -use crypto::identity::MixnetIdentityKeyPair; +use crypto::identity::MixIdentityKeyPair; use pemstore::pemstore::PemStore; pub fn execute(matches: &ArgMatches) { @@ -10,7 +10,7 @@ pub fn execute(matches: &ArgMatches) { let pathfinder = ClientPathfinder::new(id); println!("Writing keypairs to {:?}...", pathfinder.config_dir); - let mix_keys = crypto::identity::DummyMixIdentityKeyPair::new(); + let mix_keys = MixIdentityKeyPair::new(); let pem_store = PemStore::new(pathfinder); pem_store.write_identity(mix_keys).unwrap(); diff --git a/nym-client/src/commands/tcpsocket.rs b/nym-client/src/commands/tcpsocket.rs index 7a98a91681..5dce285c0b 100644 --- a/nym-client/src/commands/tcpsocket.rs +++ b/nym-client/src/commands/tcpsocket.rs @@ -1,7 +1,7 @@ use crate::client::{NymClient, SocketType}; use crate::config::persistance::pathfinder::ClientPathfinder; use clap::ArgMatches; -use crypto::identity::DummyMixIdentityKeyPair; +use crypto::identity::MixIdentityKeyPair; use pemstore::pemstore::PemStore; use std::net::ToSocketAddrs; @@ -27,7 +27,7 @@ pub fn execute(matches: &ArgMatches) { .expect("Failed to extract the socket address from the iterator"); // TODO: currently we know we are reading the 'DummyMixIdentityKeyPair', but how to properly assert the type? - let keypair: DummyMixIdentityKeyPair = PemStore::new(ClientPathfinder::new(id)) + let keypair: MixIdentityKeyPair = PemStore::new(ClientPathfinder::new(id)) .read_identity() .unwrap(); // TODO: reading auth_token from disk (if exists); diff --git a/nym-client/src/commands/websocket.rs b/nym-client/src/commands/websocket.rs index 3193c1ffde..bcb375761e 100644 --- a/nym-client/src/commands/websocket.rs +++ b/nym-client/src/commands/websocket.rs @@ -1,7 +1,7 @@ use crate::client::{NymClient, SocketType}; use crate::config::persistance::pathfinder::ClientPathfinder; use clap::ArgMatches; -use crypto::identity::DummyMixIdentityKeyPair; +use crypto::identity::MixIdentityKeyPair; use pemstore::pemstore::PemStore; use std::net::ToSocketAddrs; @@ -27,7 +27,7 @@ pub fn execute(matches: &ArgMatches) { .expect("Failed to extract the socket address from the iterator"); // TODO: currently we know we are reading the 'DummyMixIdentityKeyPair', but how to properly assert the type? - let keypair: DummyMixIdentityKeyPair = PemStore::new(ClientPathfinder::new(id)) + let keypair: MixIdentityKeyPair = PemStore::new(ClientPathfinder::new(id)) .read_identity() .unwrap(); diff --git a/sfw-provider/src/main.rs b/sfw-provider/src/main.rs index 6cd945a2c0..ec2dee141e 100644 --- a/sfw-provider/src/main.rs +++ b/sfw-provider/src/main.rs @@ -1,6 +1,6 @@ use crate::provider::ServiceProvider; use clap::{App, Arg, ArgMatches, SubCommand}; -use crypto::identity::MixnetIdentityKeyPair; +use crypto::identity::MixIdentityKeyPair; use log::error; use std::net::ToSocketAddrs; use std::path::PathBuf; @@ -121,7 +121,7 @@ fn new_config(matches: &ArgMatches) -> provider::Config { print_binding_warning(client_host); } - let key_pair = crypto::identity::DummyMixIdentityKeyPair::new(); // TODO: persist this so keypairs don't change every restart + let key_pair = crypto::identity::MixIdentityKeyPair::new(); // TODO: persist this so keypairs don't change every restart let store_dir = PathBuf::from( matches .value_of("storeDir") diff --git a/sfw-provider/src/provider/client_handling/mod.rs b/sfw-provider/src/provider/client_handling/mod.rs index e3b9dcf86e..67a2658ebe 100644 --- a/sfw-provider/src/provider/client_handling/mod.rs +++ b/sfw-provider/src/provider/client_handling/mod.rs @@ -1,6 +1,6 @@ use crate::provider::storage::{ClientStorage, StoreError}; use crate::provider::ClientLedger; -use crypto::identity::{DummyMixIdentityPrivateKey, MixnetIdentityPrivateKey}; +use crypto::identity::MixIdentityPrivateKey; use futures::lock::Mutex as FMutex; use hmac::{Hmac, Mac}; use log::*; @@ -54,14 +54,14 @@ impl From for ClientProcessingError { pub(crate) struct ClientProcessingData { store_dir: PathBuf, registered_clients_ledger: Arc>, - secret_key: DummyMixIdentityPrivateKey, + secret_key: MixIdentityPrivateKey, } impl ClientProcessingData { pub(crate) fn new( store_dir: PathBuf, registered_clients_ledger: Arc>, - secret_key: DummyMixIdentityPrivateKey, + secret_key: MixIdentityPrivateKey, ) -> Self { ClientProcessingData { store_dir, @@ -153,7 +153,7 @@ impl ClientRequestProcessor { std::fs::create_dir_all(full_store_dir) } - fn generate_new_auth_token(data: Vec, key: DummyMixIdentityPrivateKey) -> AuthToken { + fn generate_new_auth_token(data: Vec, key: MixIdentityPrivateKey) -> AuthToken { // also note that `new_varkey` doesn't even have an execution branch returning an error let mut auth_token_raw = HmacSha256::new_varkey(&key.to_bytes()) .expect("HMAC should be able take key of any size"); @@ -249,7 +249,7 @@ mod generating_new_auth_token { fn for_the_same_input_generates_the_same_auth_token() { let data1 = vec![1u8; 55]; let data2 = vec![1u8; 55]; - let key = DummyMixIdentityPrivateKey::from_bytes(&[1u8; 32]); + let key = MixIdentityPrivateKey::from_bytes(&[1u8; 32]); let token1 = ClientRequestProcessor::generate_new_auth_token(data1, key); let token2 = ClientRequestProcessor::generate_new_auth_token(data2, key); assert_eq!(token1, token2); @@ -259,14 +259,14 @@ mod generating_new_auth_token { fn for_different_inputs_generates_different_auth_tokens() { let data1 = vec![1u8; 55]; let data2 = vec![2u8; 55]; - let key = DummyMixIdentityPrivateKey::from_bytes(&[1u8; 32]); + let key = MixIdentityPrivateKey::from_bytes(&[1u8; 32]); let token1 = ClientRequestProcessor::generate_new_auth_token(data1, key); let token2 = ClientRequestProcessor::generate_new_auth_token(data2, key); assert_ne!(token1, token2); let data1 = vec![1u8; 50]; let data2 = vec![2u8; 55]; - let key = DummyMixIdentityPrivateKey::from_bytes(&[1u8; 32]); + let key = MixIdentityPrivateKey::from_bytes(&[1u8; 32]); let token1 = ClientRequestProcessor::generate_new_auth_token(data1, key); let token2 = ClientRequestProcessor::generate_new_auth_token(data2, key); assert_ne!(token1, token2); diff --git a/sfw-provider/src/provider/mix_handling/mod.rs b/sfw-provider/src/provider/mix_handling/mod.rs index 338943efd1..60ceaf700e 100644 --- a/sfw-provider/src/provider/mix_handling/mod.rs +++ b/sfw-provider/src/provider/mix_handling/mod.rs @@ -1,5 +1,5 @@ use crate::provider::storage::StoreData; -use crypto::identity::DummyMixIdentityPrivateKey; +use crypto::identity::MixIdentityPrivateKey; use log::{error, warn}; use sphinx::{ProcessedPacket, SphinxPacket}; use std::path::PathBuf; @@ -37,12 +37,12 @@ impl From for MixProcessingError { // ProcessingData defines all data required to correctly unwrap sphinx packets #[derive(Debug, Clone)] pub(crate) struct MixProcessingData { - secret_key: DummyMixIdentityPrivateKey, + secret_key: MixIdentityPrivateKey, pub(crate) store_dir: PathBuf, } impl MixProcessingData { - pub(crate) fn new(secret_key: DummyMixIdentityPrivateKey, store_dir: PathBuf) -> Self { + pub(crate) fn new(secret_key: MixIdentityPrivateKey, store_dir: PathBuf) -> Self { MixProcessingData { secret_key, store_dir, diff --git a/sfw-provider/src/provider/mod.rs b/sfw-provider/src/provider/mod.rs index 4e16e5a2a2..56085917aa 100644 --- a/sfw-provider/src/provider/mod.rs +++ b/sfw-provider/src/provider/mod.rs @@ -1,7 +1,7 @@ use crate::provider::client_handling::{ClientProcessingData, ClientRequestProcessor}; use crate::provider::mix_handling::{MixPacketProcessor, MixProcessingData}; use crate::provider::storage::ClientStorage; -use crypto::identity::{DummyMixIdentityPrivateKey, DummyMixIdentityPublicKey}; +use crypto::identity::{MixIdentityPrivateKey, MixIdentityPublicKey}; use directory_client::presence::providers::MixProviderClient; use futures::io::Error; use futures::lock::Mutex as FMutex; @@ -29,8 +29,8 @@ pub struct Config { pub client_socket_address: SocketAddr, pub directory_server: String, pub mix_socket_address: SocketAddr, - pub public_key: DummyMixIdentityPublicKey, - pub secret_key: DummyMixIdentityPrivateKey, + pub public_key: MixIdentityPublicKey, + pub secret_key: MixIdentityPrivateKey, pub store_dir: PathBuf, } @@ -102,8 +102,8 @@ pub struct ServiceProvider { directory_server: String, mix_network_address: SocketAddr, client_network_address: SocketAddr, - public_key: DummyMixIdentityPublicKey, - secret_key: DummyMixIdentityPrivateKey, + public_key: MixIdentityPublicKey, + secret_key: MixIdentityPrivateKey, store_dir: PathBuf, registered_clients_ledger: ClientLedger, } @@ -236,7 +236,7 @@ impl ServiceProvider { async fn start_mixnet_listening( address: SocketAddr, - secret_key: DummyMixIdentityPrivateKey, + secret_key: MixIdentityPrivateKey, store_dir: PathBuf, ) -> Result<(), ProviderError> { let mut listener = tokio::net::TcpListener::bind(address).await?; @@ -258,7 +258,7 @@ impl ServiceProvider { address: SocketAddr, store_dir: PathBuf, client_ledger: Arc>, - secret_key: DummyMixIdentityPrivateKey, + secret_key: MixIdentityPrivateKey, ) -> Result<(), ProviderError> { let mut listener = tokio::net::TcpListener::bind(address).await?; let processing_data = diff --git a/sfw-provider/src/provider/presence.rs b/sfw-provider/src/provider/presence.rs index adfcebaddc..77945cb096 100644 --- a/sfw-provider/src/provider/presence.rs +++ b/sfw-provider/src/provider/presence.rs @@ -1,5 +1,5 @@ use crate::provider::ClientLedger; -use crypto::identity::DummyMixIdentityPublicKey; +use crypto::identity::MixIdentityPublicKey; use directory_client::presence::providers::MixProviderPresence; use directory_client::requests::presence_providers_post::PresenceMixProviderPoster; use directory_client::DirectoryClient; @@ -22,7 +22,7 @@ impl Notifier { directory_server_address: String, client_listener: SocketAddr, mixnet_listener: SocketAddr, - pub_key: DummyMixIdentityPublicKey, + pub_key: MixIdentityPublicKey, client_ledger: Arc>, ) -> Notifier { let directory_config = directory_client::Config { diff --git a/validator/src/services/mixmining/health_check_runner.rs b/validator/src/services/mixmining/health_check_runner.rs index b53162273f..1200c6fe4f 100644 --- a/validator/src/services/mixmining/health_check_runner.rs +++ b/validator/src/services/mixmining/health_check_runner.rs @@ -1,4 +1,3 @@ -use crypto::identity::DummyMixIdentityKeyPair; use healthcheck::HealthChecker; use log::*; use std::time::Duration; @@ -6,7 +5,7 @@ use topology::NymTopology; pub struct HealthCheckRunner { directory_server: String, - health_checker: HealthChecker, + health_checker: HealthChecker, interval: f64, } @@ -14,7 +13,7 @@ impl HealthCheckRunner { pub fn new( directory_server: String, interval: f64, - health_checker: HealthChecker, + health_checker: HealthChecker, ) -> HealthCheckRunner { HealthCheckRunner { directory_server, diff --git a/validator/src/validator.rs b/validator/src/validator.rs index 6cdc6f6fc3..f793440bcc 100644 --- a/validator/src/validator.rs +++ b/validator/src/validator.rs @@ -1,6 +1,6 @@ use crate::network::tendermint; use crate::services::mixmining::health_check_runner; -use crypto::identity::{DummyMixIdentityKeyPair, MixnetIdentityKeyPair}; +use crypto::identity::MixIdentityKeyPair; use healthcheck::HealthChecker; use tokio::runtime::Runtime; @@ -15,7 +15,7 @@ pub struct Config { // allow for a generic validator pub struct Validator { #[allow(dead_code)] - identity_keypair: DummyMixIdentityKeyPair, + identity_keypair: MixIdentityKeyPair, health_check_runner: health_check_runner::HealthCheckRunner, tendermint_abci: tendermint::Abci, } @@ -23,7 +23,7 @@ pub struct Validator { // but for time being, since it's a dummy one, have it use dummy keys impl Validator { pub fn new(config: Config) -> Self { - let dummy_keypair = DummyMixIdentityKeyPair::new(); + let dummy_keypair = MixIdentityKeyPair::new(); let hc = HealthChecker::new( config.health_check.resolution_timeout, config.health_check.num_test_packets, From 9797b8da74072b044a3a193b93b1fa3a48403bc1 Mon Sep 17 00:00:00 2001 From: Dave Hrycyszyn Date: Thu, 30 Jan 2020 12:27:00 +0000 Subject: [PATCH 39/39] crypto: simplified keypairs a bit --- common/crypto/src/encryption/mod.rs | 111 ++++++++++++++++++------- common/crypto/src/encryption/x25519.rs | 99 ---------------------- common/crypto/src/identity/mod.rs | 15 ++-- common/pemstore/src/pemstore.rs | 5 +- sfw-provider/src/main.rs | 1 - 5 files changed, 90 insertions(+), 141 deletions(-) delete mode 100644 common/crypto/src/encryption/x25519.rs diff --git a/common/crypto/src/encryption/mod.rs b/common/crypto/src/encryption/mod.rs index b36ddf1af7..4a38cd89ed 100644 --- a/common/crypto/src/encryption/mod.rs +++ b/common/crypto/src/encryption/mod.rs @@ -1,39 +1,92 @@ use crate::PemStorable; +use curve25519_dalek::montgomery::MontgomeryPoint; +use curve25519_dalek::scalar::Scalar; -pub mod x25519; +// TODO: ensure this is a proper name for this considering we are not implementing entire DH here -pub trait MixnetEncryptionKeyPair -where - Priv: MixnetEncryptionPrivateKey, - Pub: MixnetEncryptionPublicKey, -{ - fn new() -> Self; - fn private_key(&self) -> &Priv; - fn public_key(&self) -> &Pub; - fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self; +const CURVE_GENERATOR: MontgomeryPoint = curve25519_dalek::constants::X25519_BASEPOINT; - // TODO: encryption related methods +pub struct KeyPair { + pub(crate) private_key: PrivateKey, + pub(crate) public_key: PublicKey, } -pub trait MixnetEncryptionPublicKey: - Sized + PemStorable + for<'a> From<&'a ::PrivateKeyMaterial> -{ - // we need to couple public and private keys together - type PrivateKeyMaterial: MixnetEncryptionPrivateKey; +impl KeyPair { + pub fn new() -> Self { + let mut rng = rand_os::OsRng::new().unwrap(); + let private_key_value = Scalar::random(&mut rng); + let public_key_value = CURVE_GENERATOR * private_key_value; - fn to_bytes(&self) -> Vec; - fn from_bytes(b: &[u8]) -> Self; -} - -pub trait MixnetEncryptionPrivateKey: Sized + PemStorable { - // we need to couple public and private keys together - type PublicKeyMaterial: MixnetEncryptionPublicKey; - - /// Returns the associated public key - fn public_key(&self) -> Self::PublicKeyMaterial { - self.into() + KeyPair { + private_key: PrivateKey(private_key_value), + public_key: PublicKey(public_key_value), + } } - fn to_bytes(&self) -> Vec; - fn from_bytes(b: &[u8]) -> Self; + pub fn private_key(&self) -> &PrivateKey { + &self.private_key + } + + pub fn public_key(&self) -> &PublicKey { + &self.public_key + } + + pub fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { + KeyPair { + private_key: PrivateKey::from_bytes(priv_bytes), + public_key: PublicKey::from_bytes(pub_bytes), + } + } +} + +// COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub struct PrivateKey(pub Scalar); + +impl PrivateKey { + pub fn to_bytes(&self) -> Vec { + self.0.to_bytes().to_vec() + } + + pub fn from_bytes(b: &[u8]) -> Self { + let mut bytes = [0; 32]; + bytes.copy_from_slice(&b[..]); + // due to trait restriction we have no choice but to panic if this fails + let key = Scalar::from_canonical_bytes(bytes).unwrap(); + Self(key) + } +} + +impl PemStorable for PrivateKey { + fn pem_type(&self) -> String { + String::from("X25519 PRIVATE KEY") + } +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct PublicKey(pub MontgomeryPoint); + +impl<'a> From<&'a PrivateKey> for PublicKey { + fn from(pk: &'a PrivateKey) -> Self { + PublicKey(CURVE_GENERATOR * pk.0) + } +} + +impl PublicKey { + pub fn to_bytes(&self) -> Vec { + self.0.to_bytes().to_vec() + } + + pub fn from_bytes(b: &[u8]) -> Self { + let mut bytes = [0; 32]; + bytes.copy_from_slice(&b[..]); + let key = MontgomeryPoint(bytes); + Self(key) + } +} + +impl PemStorable for PublicKey { + fn pem_type(&self) -> String { + String::from("X25519 PUBLIC KEY") + } } diff --git a/common/crypto/src/encryption/x25519.rs b/common/crypto/src/encryption/x25519.rs deleted file mode 100644 index 44d86783da..0000000000 --- a/common/crypto/src/encryption/x25519.rs +++ /dev/null @@ -1,99 +0,0 @@ -use crate::encryption::{ - MixnetEncryptionKeyPair, MixnetEncryptionPrivateKey, MixnetEncryptionPublicKey, -}; -use crate::PemStorable; -use curve25519_dalek::montgomery::MontgomeryPoint; -use curve25519_dalek::scalar::Scalar; - -// TODO: ensure this is a proper name for this considering we are not implementing entire DH here - -const CURVE_GENERATOR: MontgomeryPoint = curve25519_dalek::constants::X25519_BASEPOINT; - -pub struct KeyPair { - pub(crate) private_key: PrivateKey, - pub(crate) public_key: PublicKey, -} - -impl MixnetEncryptionKeyPair for KeyPair { - fn new() -> Self { - let mut rng = rand_os::OsRng::new().unwrap(); - let private_key_value = Scalar::random(&mut rng); - let public_key_value = CURVE_GENERATOR * private_key_value; - - KeyPair { - private_key: PrivateKey(private_key_value), - public_key: PublicKey(public_key_value), - } - } - - fn private_key(&self) -> &PrivateKey { - &self.private_key - } - - fn public_key(&self) -> &PublicKey { - &self.public_key - } - - fn from_bytes(priv_bytes: &[u8], pub_bytes: &[u8]) -> Self { - KeyPair { - private_key: PrivateKey::from_bytes(priv_bytes), - public_key: PublicKey::from_bytes(pub_bytes), - } - } -} - -// COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed -#[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub struct PrivateKey(pub Scalar); - -impl MixnetEncryptionPrivateKey for PrivateKey { - type PublicKeyMaterial = PublicKey; - - fn to_bytes(&self) -> Vec { - self.0.to_bytes().to_vec() - } - - fn from_bytes(b: &[u8]) -> Self { - let mut bytes = [0; 32]; - bytes.copy_from_slice(&b[..]); - // due to trait restriction we have no choice but to panic if this fails - let key = Scalar::from_canonical_bytes(bytes).unwrap(); - Self(key) - } -} - -impl PemStorable for PrivateKey { - fn pem_type(&self) -> String { - String::from("X25519 PRIVATE KEY") - } -} - -#[derive(Debug, Clone, Eq, PartialEq)] -pub struct PublicKey(pub MontgomeryPoint); - -impl<'a> From<&'a PrivateKey> for PublicKey { - fn from(pk: &'a PrivateKey) -> Self { - PublicKey(CURVE_GENERATOR * pk.0) - } -} - -impl MixnetEncryptionPublicKey for PublicKey { - type PrivateKeyMaterial = PrivateKey; - - fn to_bytes(&self) -> Vec { - self.0.to_bytes().to_vec() - } - - fn from_bytes(b: &[u8]) -> Self { - let mut bytes = [0; 32]; - bytes.copy_from_slice(&b[..]); - let key = MontgomeryPoint(bytes); - Self(key) - } -} - -impl PemStorable for PublicKey { - fn pem_type(&self) -> String { - String::from("X25519 PUBLIC KEY") - } -} diff --git a/common/crypto/src/identity/mod.rs b/common/crypto/src/identity/mod.rs index a966ec4cb9..270e25dcfb 100644 --- a/common/crypto/src/identity/mod.rs +++ b/common/crypto/src/identity/mod.rs @@ -1,6 +1,3 @@ -use crate::encryption::{ - MixnetEncryptionKeyPair, MixnetEncryptionPrivateKey, MixnetEncryptionPublicKey, -}; use crate::{encryption, PemStorable}; use bs58; use curve25519_dalek::scalar::Scalar; @@ -16,7 +13,7 @@ pub struct MixIdentityKeyPair { impl MixIdentityKeyPair { pub fn new() -> Self { - let keypair = encryption::x25519::KeyPair::new(); + let keypair = encryption::KeyPair::new(); MixIdentityKeyPair { private_key: MixIdentityPrivateKey(keypair.private_key), public_key: MixIdentityPublicKey(keypair.public_key), @@ -40,7 +37,7 @@ impl MixIdentityKeyPair { } #[derive(Debug, Clone, Eq, PartialEq)] -pub struct MixIdentityPublicKey(encryption::x25519::PublicKey); +pub struct MixIdentityPublicKey(encryption::PublicKey); impl MixIdentityPublicKey { pub fn derive_address(&self) -> DestinationAddressBytes { @@ -56,7 +53,7 @@ impl MixIdentityPublicKey { } pub fn from_bytes(b: &[u8]) -> Self { - Self(encryption::x25519::PublicKey::from_bytes(b)) + Self(encryption::PublicKey::from_bytes(b)) } pub fn to_base58_string(&self) -> String { @@ -76,12 +73,12 @@ impl PemStorable for MixIdentityPublicKey { // COPY IS DERIVED ONLY TEMPORARILY UNTIL https://github.com/nymtech/nym/issues/47 is fixed #[derive(Debug, Clone, Copy, Eq, PartialEq)] -pub struct MixIdentityPrivateKey(pub encryption::x25519::PrivateKey); +pub struct MixIdentityPrivateKey(pub encryption::PrivateKey); impl<'a> From<&'a MixIdentityPrivateKey> for MixIdentityPublicKey { fn from(pk: &'a MixIdentityPrivateKey) -> Self { let private_ref = &pk.0; - let public: encryption::x25519::PublicKey = private_ref.into(); + let public: encryption::PublicKey = private_ref.into(); MixIdentityPublicKey(public) } } @@ -92,7 +89,7 @@ impl MixIdentityPrivateKey { } pub fn from_bytes(b: &[u8]) -> Self { - Self(encryption::x25519::PrivateKey::from_bytes(b)) + Self(encryption::PrivateKey::from_bytes(b)) } } diff --git a/common/pemstore/src/pemstore.rs b/common/pemstore/src/pemstore.rs index a8d7b4b239..0fbc38b71f 100644 --- a/common/pemstore/src/pemstore.rs +++ b/common/pemstore/src/pemstore.rs @@ -8,7 +8,7 @@ use std::io::prelude::*; use std::path::PathBuf; #[allow(dead_code)] -pub fn read_mix_encryption_keypair_from_disk(_id: String) -> crypto::encryption::x25519::KeyPair { +pub fn read_mix_encryption_keypair_from_disk(_id: String) -> crypto::encryption::KeyPair { unimplemented!() } @@ -31,8 +31,7 @@ impl PemStore { let private_pem = self.read_pem_file(self.private_mix_key.clone())?; let public_pem = self.read_pem_file(self.public_mix_key.clone())?; - let key_pair = - MixIdentityKeyPair::from_bytes(&private_pem.contents, &public_pem.contents); + let key_pair = MixIdentityKeyPair::from_bytes(&private_pem.contents, &public_pem.contents); if key_pair.private_key().pem_type() != private_pem.tag { return Err(io::Error::new( diff --git a/sfw-provider/src/main.rs b/sfw-provider/src/main.rs index ec2dee141e..408ec221b6 100644 --- a/sfw-provider/src/main.rs +++ b/sfw-provider/src/main.rs @@ -1,6 +1,5 @@ use crate::provider::ServiceProvider; use clap::{App, Arg, ArgMatches, SubCommand}; -use crypto::identity::MixIdentityKeyPair; use log::error; use std::net::ToSocketAddrs; use std::path::PathBuf;