Cleaning and adjusting types

This commit is contained in:
Daniel Sanchez Quiros 2022-10-14 09:20:39 -05:00
parent e718a19713
commit 02ce74d520
8 changed files with 162 additions and 25 deletions

107
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -32,7 +32,7 @@ pub fn waku_decode_symmetric(
.to_str()
.expect("Response should always succeed to load to a &str");
let response: JsonResponse<DecodedPayload> =
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<DecodedPayload> =
serde_json::from_str(result).expect("JsonResponse should always succeed to deserialize");
serde_json::from_str(result).map_err(|e| format!("{e}"))?;
response.into()
}

View File

@ -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<DecodedPayload> {
pub fn try_decode_asymmetric(&self, asymmetric_key: &SecretKey) -> Result<DecodedPayload> {
waku_decode_asymmetric(self, asymmetric_key)
}
}

View File

@ -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::{

View File

@ -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<bool> = Mutex::new(false);
@ -173,7 +174,7 @@ impl WakuNodeHandle<Running> {
pubsub_topic: Option<WakuPubSubTopic>,
public_key: &PublicKey,
signing_key: Option<&SecretKey>,
timeout: Duration,
timeout: Option<Duration>,
) -> Result<MessageId> {
relay::waku_relay_publish_encrypt_asymmetric(
message,
@ -191,9 +192,9 @@ impl WakuNodeHandle<Running> {
&self,
message: &WakuMessage,
pubsub_topic: Option<WakuPubSubTopic>,
symmetric_key: &Key<Aes256Gcm>,
symmetric_key: &PublicKey,
signing_key: Option<&SecretKey>,
timeout: Duration,
timeout: Option<Duration>,
) -> Result<MessageId> {
relay::waku_relay_publish_encrypt_symmetric(
message,

View File

@ -114,7 +114,7 @@ pub fn waku_relay_publish_encrypt_asymmetric(
pubsub_topic: Option<WakuPubSubTopic>,
public_key: &PublicKey,
signing_key: Option<&SecretKey>,
timeout: Duration,
timeout: Option<Duration>,
) -> Result<MessageId> {
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<WakuPubSubTopic>,
symmetric_key: &Key<Aes256Gcm>,
symmetric_key: &PublicKey,
signing_key: Option<&SecretKey>,
timeout: Duration,
timeout: Option<Duration>,
) -> Result<MessageId> {
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")

View File

@ -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(())