From 02ce74d5209ba8b70b69393ef0afdfd610baf28e Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Fri, 14 Oct 2022 09:20:39 -0500 Subject: [PATCH] Cleaning and adjusting types --- Cargo.lock | 107 ++++++++++++++++++++++++++++++++++++++-- waku/Cargo.toml | 2 + waku/src/decrypt.rs | 4 +- waku/src/general/mod.rs | 2 +- waku/src/lib.rs | 3 +- waku/src/node/mod.rs | 7 +-- waku/src/node/relay.rs | 28 +++++++---- waku/tests/node.rs | 34 +++++++++++-- 8 files changed, 162 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5f37f20..4e997f2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -236,7 +236,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" dependencies = [ "generic-array", - "rand_core", + "rand_core 0.6.4", "typenum", ] @@ -302,6 +302,18 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + +[[package]] +name = "gcc" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f5f3913fa0bfe7ee1fd8248b6b9f42a5af4b9d65ec2dd2c3c26132b950ecfc2" + [[package]] name = "generic-array" version = "0.14.6" @@ -320,7 +332,7 @@ checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6" dependencies = [ "cfg-if", "libc", - "wasi", + "wasi 0.11.0+wasi-snapshot-preview1", ] [[package]] @@ -463,7 +475,7 @@ dependencies = [ "libsecp256k1-core", "libsecp256k1-gen-ecmult", "libsecp256k1-gen-genmult", - "rand", + "rand 0.8.5", "serde", "sha2", "typenum", @@ -673,6 +685,29 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +dependencies = [ + "libc", + "rand 0.4.6", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + [[package]] name = "rand" version = "0.8.5" @@ -681,7 +716,7 @@ checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ "libc", "rand_chacha", - "rand_core", + "rand_core 0.6.4", ] [[package]] @@ -691,9 +726,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.6.4" @@ -703,6 +753,15 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + [[package]] name = "regex" version = "1.6.0" @@ -720,12 +779,31 @@ version = "0.6.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244" +[[package]] +name = "rust-crypto" +version = "0.2.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f76d05d3993fd5f4af9434e8e436db163a12a9d40e1a58a726f27a01dfd12a2a" +dependencies = [ + "gcc", + "libc", + "rand 0.3.23", + "rustc-serialize", + "time", +] + [[package]] name = "rustc-hash" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "rustc-serialize" +version = "0.3.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcf128d1287d2ea9d80910b5f1120d0b8eede3fbf1abe91c40d39ea7d51e6fda" + [[package]] name = "ryu" version = "1.0.11" @@ -882,6 +960,17 @@ dependencies = [ "syn", ] +[[package]] +name = "time" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" +dependencies = [ + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", + "winapi", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -982,6 +1071,8 @@ dependencies = [ "libsecp256k1", "multiaddr", "once_cell", + "rand 0.8.5", + "rust-crypto", "serde", "serde_json", "sscanf", @@ -995,6 +1086,12 @@ dependencies = [ "bindgen", ] +[[package]] +name = "wasi" +version = "0.10.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f" + [[package]] name = "wasi" version = "0.11.0+wasi-snapshot-preview1" diff --git a/waku/Cargo.toml b/waku/Cargo.toml index fb3da29..f3b0aa5 100644 --- a/waku/Cargo.toml +++ b/waku/Cargo.toml @@ -12,6 +12,8 @@ hex = "0.4" libsecp256k1 = "0.7" multiaddr = "0.14" once_cell = "1.15" +rand = "0.8" +rust-crypto = "0.2" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" sscanf = "0.3" diff --git a/waku/src/decrypt.rs b/waku/src/decrypt.rs index d0190cd..c9f67ea 100644 --- a/waku/src/decrypt.rs +++ b/waku/src/decrypt.rs @@ -32,7 +32,7 @@ pub fn waku_decode_symmetric( .to_str() .expect("Response should always succeed to load to a &str"); let response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + serde_json::from_str(result).map_err(|e| format!("{e}"))?; response.into() } @@ -60,6 +60,6 @@ pub fn waku_decode_asymmetric( .to_str() .expect("Response should always succeed to load to a &str"); let response: JsonResponse = - serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize"); + serde_json::from_str(result).map_err(|e| format!("{e}"))?; response.into() } diff --git a/waku/src/general/mod.rs b/waku/src/general/mod.rs index 5e2f3c4..78c3529 100644 --- a/waku/src/general/mod.rs +++ b/waku/src/general/mod.rs @@ -130,7 +130,7 @@ impl WakuMessage { /// Try decode the message with an expected asymmetric key /// /// wrapper around [`crate::decrypt::waku_decode_asymmetric`] - pub fn try_decode_asymmentric(&self, asymmetric_key: &SecretKey) -> Result { + pub fn try_decode_asymmetric(&self, asymmetric_key: &SecretKey) -> Result { waku_decode_asymmetric(self, asymmetric_key) } } diff --git a/waku/src/lib.rs b/waku/src/lib.rs index da5191e..968819b 100644 --- a/waku/src/lib.rs +++ b/waku/src/lib.rs @@ -8,7 +8,8 @@ mod node; pub use node::{ waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic, waku_new, - Initialized, Protocol, Running, WakuNodeConfig, WakuNodeHandle, WakuPeerData, WakuPeers, + waku_store_query, Initialized, Protocol, Running, WakuNodeConfig, WakuNodeHandle, WakuPeerData, + WakuPeers, }; pub use general::{ diff --git a/waku/src/node/mod.rs b/waku/src/node/mod.rs index 72ad005..cefe8a9 100644 --- a/waku/src/node/mod.rs +++ b/waku/src/node/mod.rs @@ -25,6 +25,7 @@ use crate::general::{ pub use config::WakuNodeConfig; pub use peers::{Protocol, WakuPeerData, WakuPeers}; pub use relay::{waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic}; +pub use store::waku_store_query; /// Shared flag to check if a waku node is already running in the current process static WAKU_NODE_INITIALIZED: Mutex = Mutex::new(false); @@ -173,7 +174,7 @@ impl WakuNodeHandle { pubsub_topic: Option, public_key: &PublicKey, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { relay::waku_relay_publish_encrypt_asymmetric( message, @@ -191,9 +192,9 @@ impl WakuNodeHandle { &self, message: &WakuMessage, pubsub_topic: Option, - symmetric_key: &Key, + symmetric_key: &PublicKey, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { relay::waku_relay_publish_encrypt_symmetric( message, diff --git a/waku/src/node/relay.rs b/waku/src/node/relay.rs index 2ca3aa0..ff83738 100644 --- a/waku/src/node/relay.rs +++ b/waku/src/node/relay.rs @@ -114,7 +114,7 @@ pub fn waku_relay_publish_encrypt_asymmetric( pubsub_topic: Option, public_key: &PublicKey, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { let pk = hex::encode(public_key.serialize()); let sk = signing_key @@ -141,9 +141,13 @@ pub fn waku_relay_publish_encrypt_asymmetric( .expect("CString should build properly from hex encoded signing key") .into_raw(), timeout - .as_millis() - .try_into() - .expect("Duration as milliseconds should fit in a i32"), + .map(|timeout| { + timeout + .as_millis() + .try_into() + .expect("Duration as milliseconds should fit in a i32") + }) + .unwrap_or(0), )) .to_str() .expect("Response should always succeed to load to a &str") @@ -158,11 +162,11 @@ pub fn waku_relay_publish_encrypt_asymmetric( pub fn waku_relay_publish_encrypt_symmetric( message: &WakuMessage, pubsub_topic: Option, - symmetric_key: &Key, + symmetric_key: &PublicKey, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { - let symk = hex::encode(symmetric_key.as_slice()); + let symk = hex::encode(symmetric_key.serialize()); let sk = signing_key .map(|signing_key| hex::encode(signing_key.serialize())) .unwrap_or_else(String::new); @@ -187,9 +191,13 @@ pub fn waku_relay_publish_encrypt_symmetric( .expect("CString should build properly from hex encoded signing key") .into_raw(), timeout - .as_millis() - .try_into() - .expect("Duration as milliseconds should fit in a i32"), + .map(|timeout| { + timeout + .as_millis() + .try_into() + .expect("Duration as milliseconds should fit in a i32") + }) + .unwrap_or(0), )) .to_str() .expect("Response should always succeed to load to a &str") diff --git a/waku/tests/node.rs b/waku/tests/node.rs index 0af2c3b..b38a1de 100644 --- a/waku/tests/node.rs +++ b/waku/tests/node.rs @@ -1,4 +1,7 @@ +use aes_gcm::{Aes256Gcm, KeyInit}; +use libsecp256k1::PublicKey; use multiaddr::Multiaddr; +use rand::thread_rng; use std::net::IpAddr; use std::str::FromStr; use std::time::{Duration, SystemTime}; @@ -39,10 +42,31 @@ pub fn main() -> Result<(), String> { assert!(node.peer_count()? >= NODES.len()); assert!(node.relay_enough_peers(None)?); + let sk = libsecp256k1::SecretKey::random(&mut thread_rng()); + let pk = PublicKey::from_secret_key(&sk); + let ssk = Aes256Gcm::generate_key(&mut thread_rng()); - waku_set_event_callback(|signal| match signal.event() { + let content = "Hi from 🦀!"; + let content_callback = content.clone(); + + waku_set_event_callback(move |signal| match signal.event() { Event::WakuMessage(message) => { println!("Message with id [{}] received", message.message_id()); + let message = message.waku_message(); + let payload = if let Ok(message) = message.try_decode_asymmetric(&sk) { + println!("Asymemetry message"); + message.data().to_vec() + } else if let Ok(message) = message.try_decode_symmetric(&ssk) { + println!("Symemetry message"); + message.data().to_vec() + } else { + println!("Unencoded message"); + message.payload().to_vec() + }; + let message_content: String = + String::from_utf8(payload).expect("Message should be able to be read"); + println!("Message content: {message_content}"); + assert_eq!(message_content, content_callback); } _ => { println!("Wtf is this event?"); @@ -59,7 +83,7 @@ pub fn main() -> Result<(), String> { }; let message = WakuMessage::new( - "Hi from 🦀!", + content, content_topic, 1, SystemTime::now() @@ -70,7 +94,11 @@ pub fn main() -> Result<(), String> { .unwrap(), ); - node.relay_publish_message(&message, None, None)?; + // node.relay_publish_message(&message, None, None)?; + + node.relay_publish_encrypt_asymmetric(&message, None, &pk, Some(&sk), None)?; + // node.relay_publish_encrypt_symmetric(&message, None, &pk, None, None)?; + std::thread::sleep(Duration::from_secs(2)); node.stop()?; Ok(())