From 32bd05c6fc74507c007e6f104716c3edb9a71b8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?rich=CE=9Brd?= Date: Wed, 27 Sep 2023 19:19:59 -0400 Subject: [PATCH] feat: update go-waku version (#70) - removes encoding functions from relay and lightpush - adds `encode_symmetric` and `encode_asymmetric` to `WakuMessage` - don't panic when unserializing json --- Cargo.lock | 4 +- waku-bindings/Cargo.toml | 4 +- waku-bindings/src/encrypt.rs | 76 ++++++++++++++++ waku-bindings/src/general/mod.rs | 55 ++++++++++++ waku-bindings/src/lib.rs | 3 +- waku-bindings/src/node/lightpush.rs | 132 +--------------------------- waku-bindings/src/node/mod.rs | 82 +---------------- waku-bindings/src/node/relay.rs | 129 ++------------------------- waku-bindings/src/utils.rs | 11 ++- waku-bindings/tests/node.rs | 37 ++------ waku-sys/Cargo.toml | 2 +- waku-sys/build.rs | 3 +- waku-sys/vendor | 2 +- 13 files changed, 166 insertions(+), 374 deletions(-) create mode 100644 waku-bindings/src/encrypt.rs diff --git a/Cargo.lock b/Cargo.lock index 5c348da..9e30724 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1674,7 +1674,7 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "waku-bindings" -version = "0.2.0" +version = "0.3.0" dependencies = [ "aes-gcm", "base64 0.21.0", @@ -1697,7 +1697,7 @@ dependencies = [ [[package]] name = "waku-sys" -version = "0.2.0" +version = "0.3.0" dependencies = [ "bindgen", ] diff --git a/waku-bindings/Cargo.toml b/waku-bindings/Cargo.toml index 235405d..a2d9a07 100644 --- a/waku-bindings/Cargo.toml +++ b/waku-bindings/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "waku-bindings" -version = "0.2.0" +version = "0.3.0" edition = "2021" authors = [ "Daniel Sanchez Quiros " @@ -26,7 +26,7 @@ serde_json = "1.0" sscanf = "0.4" smart-default = "0.6" url = "2.3" -waku-sys = { version = "0.2.0", path = "../waku-sys" } +waku-sys = { version = "0.3.0", path = "../waku-sys" } [dev-dependencies] futures = "0.3.25" diff --git a/waku-bindings/src/encrypt.rs b/waku-bindings/src/encrypt.rs new file mode 100644 index 0000000..2a8bd9a --- /dev/null +++ b/waku-bindings/src/encrypt.rs @@ -0,0 +1,76 @@ +// std +use std::ffi::CString; +// crates +use aes_gcm::{Aes256Gcm, Key}; +use secp256k1::{PublicKey, SecretKey}; +// internal +use crate::general::{Result, WakuMessage}; +use crate::utils::decode_and_free_response; + +/// Optionally sign and encrypt a message using asymmetric encryption +pub fn waku_encode_asymmetric( + message: &WakuMessage, + public_key: &PublicKey, + signing_key: Option<&SecretKey>, +) -> Result { + let pk = hex::encode(public_key.serialize_uncompressed()); + let sk = signing_key + .map(|signing_key| hex::encode(signing_key.secret_bytes())) + .unwrap_or_else(String::new); + let message_ptr = CString::new( + serde_json::to_string(&message) + .expect("WakuMessages should always be able to success serializing"), + ) + .expect("CString should build properly from the serialized waku message") + .into_raw(); + let pk_ptr = CString::new(pk) + .expect("CString should build properly from hex encoded public key") + .into_raw(); + let sk_ptr = CString::new(sk) + .expect("CString should build properly from hex encoded signing key") + .into_raw(); + + let result_ptr = unsafe { + let res = waku_sys::waku_encode_asymmetric(message_ptr, pk_ptr, sk_ptr); + drop(CString::from_raw(message_ptr)); + drop(CString::from_raw(pk_ptr)); + drop(CString::from_raw(sk_ptr)); + res + }; + + decode_and_free_response(result_ptr) +} + +/// Optionally sign and encrypt a message using symmetric encryption +pub fn waku_encode_symmetric( + message: &WakuMessage, + symmetric_key: &Key, + signing_key: Option<&SecretKey>, +) -> Result { + let symk = hex::encode(symmetric_key.as_slice()); + let sk = signing_key + .map(|signing_key| hex::encode(signing_key.secret_bytes())) + .unwrap_or_else(String::new); + let message_ptr = CString::new( + serde_json::to_string(&message) + .expect("WakuMessages should always be able to success serializing"), + ) + .expect("CString should build properly from the serialized waku message") + .into_raw(); + let symk_ptr = CString::new(symk) + .expect("CString should build properly from hex encoded symmetric key") + .into_raw(); + let sk_ptr = CString::new(sk) + .expect("CString should build properly from hex encoded signing key") + .into_raw(); + + let result_ptr = unsafe { + let res = waku_sys::waku_encode_symmetric(message_ptr, symk_ptr, sk_ptr); + drop(CString::from_raw(message_ptr)); + drop(CString::from_raw(symk_ptr)); + drop(CString::from_raw(sk_ptr)); + res + }; + + decode_and_free_response(result_ptr) +} diff --git a/waku-bindings/src/general/mod.rs b/waku-bindings/src/general/mod.rs index 2616848..ae35658 100644 --- a/waku-bindings/src/general/mod.rs +++ b/waku-bindings/src/general/mod.rs @@ -12,6 +12,7 @@ use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; use sscanf::{scanf, RegexRepresentation}; // internal use crate::decrypt::{waku_decode_asymmetric, waku_decode_symmetric}; +use crate::encrypt::{waku_encode_asymmetric, waku_encode_symmetric}; /// Waku message version pub type WakuMessageVersion = usize; @@ -145,6 +146,15 @@ impl WakuMessage { self.ephemeral } + /// Optionally sign and encrypt a message using symmetric encryption + pub fn encode_symmetric( + &self, + symmetric_key: &Key, + signing_key: Option<&SecretKey>, + ) -> Result { + waku_encode_symmetric(self, symmetric_key, signing_key) + } + /// Try decode the message with an expected symmetric key /// /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_decode_symmetricchar-messagejson-char-symmetrickey) @@ -152,6 +162,15 @@ impl WakuMessage { waku_decode_symmetric(self, symmetric_key) } + /// Optionally sign and encrypt a message using asymmetric encryption + pub fn encode_asymmetric( + &self, + public_key: &PublicKey, + signing_key: Option<&SecretKey>, + ) -> Result { + waku_encode_asymmetric(self, public_key, signing_key) + } + /// Try decode the message with an expected asymmetric key /// /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_decode_asymmetricchar-messagejson-char-privatekey) @@ -563,6 +582,8 @@ where mod tests { use super::*; use crate::WakuPubSubTopic; + use secp256k1::{rand, Secp256k1}; + use std::time::SystemTime; #[test] fn parse_waku_topic() { let s = "/waku/2/default-waku/proto"; @@ -574,4 +595,38 @@ mod tests { let message = "{\"payload\":\"SGkgZnJvbSDwn6aAIQ==\",\"contentTopic\":\"/toychat/2/huilong/proto\",\"timestamp\":1665580926660,\"ephemeral\":true,\"meta\":\"SGkgZnJvbSDwn6aAIQ==\"}"; let _: WakuMessage = serde_json::from_str(message).unwrap(); } + + #[test] + fn encode_decode() { + let content_topic = WakuContentTopic::new("hello", 2, "world", Encoding::Proto); + let message = WakuMessage::new( + "hello", + content_topic, + 1, + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap() + .as_millis() + .try_into() + .unwrap(), + Vec::new(), + false, + ); + + let secp = Secp256k1::new(); + let signing_key = SecretKey::new(&mut rand::thread_rng()); + let encrypt_key = SecretKey::new(&mut rand::thread_rng()); + let public_key = PublicKey::from_secret_key(&secp, &encrypt_key); + + let encoded_message = message + .encode_asymmetric(&public_key, Some(&signing_key)) + .expect("could not encode"); + let decoded_message = encoded_message + .try_decode_asymmetric(&encrypt_key) + .expect("could not decode"); + + assert!(message.payload() != encoded_message.payload()); + assert!(encoded_message.version() == 1); + assert!(message.payload() == decoded_message.data()); + } } diff --git a/waku-bindings/src/lib.rs b/waku-bindings/src/lib.rs index 46064b2..4debe07 100644 --- a/waku-bindings/src/lib.rs +++ b/waku-bindings/src/lib.rs @@ -2,13 +2,14 @@ //! //! Implementation on top of [`waku-bindings`](https://rfc.vac.dev/spec/36/) mod decrypt; +mod encrypt; mod events; mod general; mod node; mod utils; pub use node::{ - waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic, + waku_create_content_topic, waku_create_pubsub_topic, waku_default_pubsub_topic, waku_discv5_update_bootnodes, waku_dns_discovery, waku_new, Aes256Gcm, DnsInfo, GossipSubParams, Initialized, Key, Multiaddr, Protocol, PublicKey, Running, SecretKey, WakuLogLevel, WakuNodeConfig, WakuNodeHandle, WakuPeerData, WakuPeers, WebsocketParams, diff --git a/waku-bindings/src/node/lightpush.rs b/waku-bindings/src/node/lightpush.rs index 7dc2cfd..858424f 100644 --- a/waku-bindings/src/node/lightpush.rs +++ b/waku-bindings/src/node/lightpush.rs @@ -3,12 +3,9 @@ // std use std::ffi::CString; use std::time::Duration; -// crates -use aes_gcm::{Aes256Gcm, Key}; -use secp256k1::{PublicKey, SecretKey}; // internal use crate::general::{MessageId, PeerId, Result, WakuMessage, WakuPubSubTopic}; -use crate::node::waku_dafault_pubsub_topic; +use crate::node::waku_default_pubsub_topic; use crate::utils::decode_and_free_response; /// Publish a message using Waku Lightpush @@ -20,7 +17,7 @@ pub fn waku_lightpush_publish( timeout: Option, ) -> Result { let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) + .unwrap_or_else(waku_default_pubsub_topic) .to_string(); let message_ptr = CString::new( serde_json::to_string(&message) @@ -56,128 +53,3 @@ pub fn waku_lightpush_publish( decode_and_free_response(result_ptr) } - -/// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Lightpush -/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_lightpush_publish_enc_asymmetricchar-messagejson-char-pubsubtopic-char-peerid-char-publickey-char-optionalsigningkey-int-timeoutms) -pub fn waku_lightpush_publish_encrypt_asymmetric( - message: &WakuMessage, - pubsub_topic: Option, - peer_id: PeerId, - public_key: &PublicKey, - signing_key: Option<&SecretKey>, - timeout: Option, -) -> Result { - let pk = hex::encode(public_key.serialize_uncompressed()); - let sk = signing_key - .map(|signing_key| hex::encode(signing_key.secret_bytes())) - .unwrap_or_else(String::new); - let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) - .to_string(); - - let pubsub_topic_ptr = CString::new(pubsub_topic) - .expect("CString should build properly from pubsub topic") - .into_raw(); - let peer_id_ptr = CString::new(peer_id) - .expect("CString should build properly from peer id") - .into_raw(); - let pk_ptr = CString::new(pk) - .expect("CString should build properly from hex encoded public key") - .into_raw(); - let sk_ptr = CString::new(sk) - .expect("CString should build properly from hex encoded signing key") - .into_raw(); - let message_ptr = CString::new( - serde_json::to_string(&message) - .expect("WakuMessages should always be able to success serializing"), - ) - .expect("CString should build properly from the serialized waku message") - .into_raw(); - let result_ptr = unsafe { - let res = waku_sys::waku_lightpush_publish_enc_asymmetric( - message_ptr, - pubsub_topic_ptr, - peer_id_ptr, - pk_ptr, - sk_ptr, - timeout - .map(|timeout| { - timeout - .as_millis() - .try_into() - .expect("Duration as milliseconds should fit in a i32") - }) - .unwrap_or(0), - ); - drop(CString::from_raw(message_ptr)); - drop(CString::from_raw(pubsub_topic_ptr)); - drop(CString::from_raw(peer_id_ptr)); - drop(CString::from_raw(pk_ptr)); - drop(CString::from_raw(sk_ptr)); - res - }; - - decode_and_free_response(result_ptr) -} - -/// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Lightpush -/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_lightpush_publish_enc_symmetricchar-messagejson-char-pubsubtopic-char-peerid-char-symmetrickey-char-optionalsigningkey-int-timeoutms) -pub fn waku_lightpush_publish_encrypt_symmetric( - message: &WakuMessage, - pubsub_topic: Option, - peer_id: PeerId, - symmetric_key: &Key, - signing_key: Option<&SecretKey>, - timeout: Option, -) -> Result { - let symk = hex::encode(symmetric_key.as_slice()); - let sk = signing_key - .map(|signing_key| hex::encode(signing_key.secret_bytes())) - .unwrap_or_else(String::new); - let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) - .to_string(); - let message_ptr = CString::new( - serde_json::to_string(&message) - .expect("WakuMessages should always be able to success serializing"), - ) - .expect("CString should build properly from the serialized waku message") - .into_raw(); - let pubsub_topic_ptr = CString::new(pubsub_topic) - .expect("CString should build properly from pubsub topic") - .into_raw(); - let peer_id_ptr = CString::new(peer_id) - .expect("CString should build properly from peer id") - .into_raw(); - let symk_ptr = CString::new(symk) - .expect("CString should build properly from hex encoded symmetric key") - .into_raw(); - let sk_ptr = CString::new(sk) - .expect("CString should build properly from hex encoded signing key") - .into_raw(); - let result_ptr = unsafe { - let res = waku_sys::waku_lightpush_publish_enc_symmetric( - message_ptr, - pubsub_topic_ptr, - peer_id_ptr, - symk_ptr, - sk_ptr, - timeout - .map(|timeout| { - timeout - .as_millis() - .try_into() - .expect("Duration as milliseconds should fit in a i32") - }) - .unwrap_or(0), - ); - drop(CString::from_raw(message_ptr)); - drop(CString::from_raw(pubsub_topic_ptr)); - drop(CString::from_raw(peer_id_ptr)); - drop(CString::from_raw(symk_ptr)); - drop(CString::from_raw(sk_ptr)); - res - }; - - decode_and_free_response(result_ptr) -} diff --git a/waku-bindings/src/node/mod.rs b/waku-bindings/src/node/mod.rs index cb675a6..2597273 100644 --- a/waku-bindings/src/node/mod.rs +++ b/waku-bindings/src/node/mod.rs @@ -27,7 +27,7 @@ use crate::general::{ pub use config::{GossipSubParams, WakuLogLevel, WakuNodeConfig, WebsocketParams}; pub use discovery::{waku_discv5_update_bootnodes, waku_dns_discovery, DnsInfo}; pub use peers::{Protocol, WakuPeerData, WakuPeers}; -pub use relay::{waku_create_content_topic, waku_create_pubsub_topic, waku_dafault_pubsub_topic}; +pub use relay::{waku_create_content_topic, waku_create_pubsub_topic, waku_default_pubsub_topic}; pub use store::{waku_local_store_query, waku_store_query}; /// Shared flag to check if a waku node is already running in the current process @@ -159,44 +159,6 @@ impl WakuNodeHandle { relay::waku_relay_publish_message(message, pubsub_topic, timeout) } - /// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Relay - /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publish_enc_asymmetricchar-messagejson-char-pubsubtopic-char-publickey-char-optionalsigningkey-int-timeoutms) - pub fn relay_publish_encrypt_asymmetric( - &self, - message: &WakuMessage, - pubsub_topic: Option, - public_key: &PublicKey, - signing_key: Option<&SecretKey>, - timeout: Option, - ) -> Result { - relay::waku_relay_publish_encrypt_asymmetric( - message, - pubsub_topic, - public_key, - signing_key, - timeout, - ) - } - - /// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Relay - /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publish_enc_symmetricchar-messagejson-char-pubsubtopic-char-symmetrickey-char-optionalsigningkey-int-timeoutms) - pub fn relay_publish_encrypt_symmetric( - &self, - message: &WakuMessage, - pubsub_topic: Option, - symmetric_key: &Key, - signing_key: Option<&SecretKey>, - timeout: Option, - ) -> Result { - relay::waku_relay_publish_encrypt_symmetric( - message, - pubsub_topic, - symmetric_key, - signing_key, - timeout, - ) - } - /// Determine if there are enough peers to publish a message on a given pubsub topic pub fn relay_enough_peers(&self, pubsub_topic: Option) -> Result { relay::waku_enough_peers(pubsub_topic) @@ -250,48 +212,6 @@ impl WakuNodeHandle { lightpush::waku_lightpush_publish(message, pubsub_topic, peer_id, timeout) } - /// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Lightpush - /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_lightpush_publish_enc_asymmetricchar-messagejson-char-pubsubtopic-char-peerid-char-publickey-char-optionalsigningkey-int-timeoutms) - pub fn lightpush_publish_encrypt_asymmetric( - &self, - message: &WakuMessage, - pubsub_topic: Option, - peer_id: PeerId, - public_key: &PublicKey, - signing_key: Option<&SecretKey>, - timeout: Option, - ) -> Result { - lightpush::waku_lightpush_publish_encrypt_asymmetric( - message, - pubsub_topic, - peer_id, - public_key, - signing_key, - timeout, - ) - } - - /// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Lightpush - /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_lightpush_publish_enc_symmetricchar-messagejson-char-pubsubtopic-char-peerid-char-symmetrickey-char-optionalsigningkey-int-timeoutms) - pub fn lightpush_publish_encrypt_symmetric( - &self, - message: &WakuMessage, - pubsub_topic: Option, - peer_id: PeerId, - symmetric_key: &Key, - signing_key: Option<&SecretKey>, - timeout: Option, - ) -> Result { - lightpush::waku_lightpush_publish_encrypt_symmetric( - message, - pubsub_topic, - peer_id, - symmetric_key, - signing_key, - timeout, - ) - } - /// Creates a subscription in a lightnode for messages that matches a content filter and optionally a [`WakuPubSubTopic`](`crate::general::WakuPubSubTopic`) /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_filter_subscribechar-filterjson-char-peerid-int-timeoutms) pub fn filter_subscribe( diff --git a/waku-bindings/src/node/relay.rs b/waku-bindings/src/node/relay.rs index 1d64a59..e4b226a 100644 --- a/waku-bindings/src/node/relay.rs +++ b/waku-bindings/src/node/relay.rs @@ -3,9 +3,6 @@ // std use std::ffi::{CStr, CString}; use std::time::Duration; -// crates -use aes_gcm::{Aes256Gcm, Key}; -use secp256k1::{PublicKey, SecretKey}; // internal use crate::general::{Encoding, MessageId, Result, WakuContentTopic, WakuMessage, WakuPubSubTopic}; use crate::utils::decode_and_free_response; @@ -83,7 +80,7 @@ pub fn waku_create_pubsub_topic(topic_name: &str, encoding: Encoding) -> WakuPub } /// Default pubsub topic used for exchanging waku messages defined in [RFC 10](https://rfc.vac.dev/spec/10/) -pub fn waku_dafault_pubsub_topic() -> WakuPubSubTopic { +pub fn waku_default_pubsub_topic() -> WakuPubSubTopic { let result_ptr = unsafe { waku_sys::waku_default_pubsub_topic() }; let result = unsafe { CStr::from_ptr(result_ptr) } @@ -112,7 +109,7 @@ pub fn waku_relay_publish_message( timeout: Option, ) -> Result { let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) + .unwrap_or_else(waku_default_pubsub_topic) .to_string(); let message_ptr = CString::new( @@ -146,125 +143,9 @@ pub fn waku_relay_publish_message( decode_and_free_response(result_ptr) } -/// Optionally sign, encrypt using asymmetric encryption and publish a message using Waku Relay -/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publish_enc_asymmetricchar-messagejson-char-pubsubtopic-char-publickey-char-optionalsigningkey-int-timeoutms) -pub fn waku_relay_publish_encrypt_asymmetric( - message: &WakuMessage, - pubsub_topic: Option, - public_key: &PublicKey, - signing_key: Option<&SecretKey>, - timeout: Option, -) -> Result { - let pk = hex::encode(public_key.serialize_uncompressed()); - let sk = signing_key - .map(|signing_key| hex::encode(signing_key.secret_bytes())) - .unwrap_or_else(String::new); - let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) - .to_string(); - - let message_ptr = CString::new( - serde_json::to_string(&message) - .expect("WakuMessages should always be able to success serializing"), - ) - .expect("CString should build properly from the serialized waku message") - .into_raw(); - let pubsub_topic_ptr = CString::new(pubsub_topic) - .expect("CString should build properly from pubsub topic") - .into_raw(); - let pk_ptr = CString::new(pk) - .expect("CString should build properly from hex encoded public key") - .into_raw(); - let sk_ptr = CString::new(sk) - .expect("CString should build properly from hex encoded signing key") - .into_raw(); - - let result_ptr = unsafe { - let res = waku_sys::waku_relay_publish_enc_asymmetric( - message_ptr, - pubsub_topic_ptr, - pk_ptr, - sk_ptr, - timeout - .map(|timeout| { - timeout - .as_millis() - .try_into() - .expect("Duration as milliseconds should fit in a i32") - }) - .unwrap_or(0), - ); - drop(CString::from_raw(message_ptr)); - drop(CString::from_raw(pubsub_topic_ptr)); - drop(CString::from_raw(pk_ptr)); - drop(CString::from_raw(sk_ptr)); - res - }; - - decode_and_free_response(result_ptr) -} - -/// Optionally sign, encrypt using symmetric encryption and publish a message using Waku Relay -/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publish_enc_symmetricchar-messagejson-char-pubsubtopic-char-symmetrickey-char-optionalsigningkey-int-timeoutms) -pub fn waku_relay_publish_encrypt_symmetric( - message: &WakuMessage, - pubsub_topic: Option, - symmetric_key: &Key, - signing_key: Option<&SecretKey>, - timeout: Option, -) -> Result { - let symk = hex::encode(symmetric_key.as_slice()); - let sk = signing_key - .map(|signing_key| hex::encode(signing_key.secret_bytes())) - .unwrap_or_else(String::new); - let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) - .to_string(); - - let message_ptr = CString::new( - serde_json::to_string(&message) - .expect("WakuMessages should always be able to success serializing"), - ) - .expect("CString should build properly from the serialized waku message") - .into_raw(); - let pubsub_topic_ptr = CString::new(pubsub_topic) - .expect("CString should build properly from pubsub topic") - .into_raw(); - let symk_ptr = CString::new(symk) - .expect("CString should build properly from hex encoded symmetric key") - .into_raw(); - let sk_ptr = CString::new(sk) - .expect("CString should build properly from hex encoded signing key") - .into_raw(); - - let result_ptr = unsafe { - let res = waku_sys::waku_relay_publish_enc_symmetric( - message_ptr, - pubsub_topic_ptr, - symk_ptr, - sk_ptr, - timeout - .map(|timeout| { - timeout - .as_millis() - .try_into() - .expect("Duration as milliseconds should fit in a i32") - }) - .unwrap_or(0), - ); - drop(CString::from_raw(message_ptr)); - drop(CString::from_raw(pubsub_topic_ptr)); - drop(CString::from_raw(symk_ptr)); - drop(CString::from_raw(sk_ptr)); - res - }; - - decode_and_free_response(result_ptr) -} - pub fn waku_enough_peers(pubsub_topic: Option) -> Result { let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) + .unwrap_or_else(waku_default_pubsub_topic) .to_string(); let pubsub_topic_ptr = CString::new(pubsub_topic) @@ -282,7 +163,7 @@ pub fn waku_enough_peers(pubsub_topic: Option) -> Result pub fn waku_relay_subscribe(pubsub_topic: Option) -> Result<()> { let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) + .unwrap_or_else(waku_default_pubsub_topic) .to_string(); let pubsub_topic_ptr = CString::new(pubsub_topic) @@ -300,7 +181,7 @@ pub fn waku_relay_subscribe(pubsub_topic: Option) -> Result<()> pub fn waku_relay_unsubscribe(pubsub_topic: Option) -> Result<()> { let pubsub_topic = pubsub_topic - .unwrap_or_else(waku_dafault_pubsub_topic) + .unwrap_or_else(waku_default_pubsub_topic) .to_string(); let pubsub_topic_ptr = CString::new(pubsub_topic) diff --git a/waku-bindings/src/utils.rs b/waku-bindings/src/utils.rs index 4744c46..e1a3a17 100644 --- a/waku-bindings/src/utils.rs +++ b/waku-bindings/src/utils.rs @@ -7,10 +7,15 @@ use std::ffi::{c_char, CStr}; pub fn decode_and_free_response(response_ptr: *mut c_char) -> Result { let response = unsafe { CStr::from_ptr(response_ptr) } .to_str() - .expect("Response should always succeed to load to a &str"); + .map_err(|err| { + format!( + "could not retrieve response from pointer returned by waku: {}", + err + ) + })?; - let response: JsonResponse = - serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize"); + let response: JsonResponse = serde_json::from_str(response) + .map_err(|err| format!("could not deserialize waku JsonResponse: {}", err))?; unsafe { waku_sys::waku_utils_free(response_ptr); diff --git a/waku-bindings/tests/node.rs b/waku-bindings/tests/node.rs index b50cf38..e565c6d 100644 --- a/waku-bindings/tests/node.rs +++ b/waku-bindings/tests/node.rs @@ -1,7 +1,7 @@ use aes_gcm::{Aes256Gcm, KeyInit}; use multiaddr::Multiaddr; use rand::thread_rng; -use secp256k1::{PublicKey, Secp256k1, SecretKey}; +use secp256k1::SecretKey; use serial_test::serial; use std::net::IpAddr; use std::str::FromStr; @@ -10,7 +10,7 @@ use std::{collections::HashSet, str::from_utf8}; use tokio::sync::mpsc::{self, Sender}; use tokio::time; use waku_bindings::{ - waku_dafault_pubsub_topic, waku_new, waku_set_event_callback, Encoding, Event, GossipSubParams, + waku_default_pubsub_topic, waku_new, waku_set_event_callback, Encoding, Event, GossipSubParams, Key, MessageId, ProtocolId, Running, WakuContentTopic, WakuLogLevel, WakuMessage, WakuNodeConfig, WakuNodeHandle, WakuPubSubTopic, }; @@ -27,28 +27,16 @@ const NODES: &[&str] = &[ fn try_publish_relay_messages( node: &WakuNodeHandle, msg: &WakuMessage, - sk: &SecretKey, - ssk: &Key, ) -> Result, String> { - let pk = PublicKey::from_secret_key(&Secp256k1::new(), sk); - - Ok(HashSet::from([ - node.relay_publish_message(msg, None, None)?, - node.relay_publish_encrypt_asymmetric(msg, None, &pk, None, None)?, - node.relay_publish_encrypt_symmetric(msg, None, ssk, None, None)?, - node.relay_publish_encrypt_asymmetric(msg, None, &pk, Some(sk), None)?, - node.relay_publish_encrypt_symmetric(msg, None, ssk, Some(sk), None)?, - ])) + Ok(HashSet::from( + [node.relay_publish_message(msg, None, None)?], + )) } fn try_publish_lightpush_messages( node: &WakuNodeHandle, msg: &WakuMessage, - sk: &SecretKey, - ssk: &Key, ) -> Result, String> { - let pk = PublicKey::from_secret_key(&Secp256k1::new(), sk); - let peer_id = node .peers() .unwrap() @@ -59,11 +47,7 @@ fn try_publish_lightpush_messages( .clone(); Ok(HashSet::from([ - node.lightpush_publish(msg, None, peer_id.clone(), None)?, - node.lightpush_publish_encrypt_asymmetric(msg, None, peer_id.clone(), &pk, None, None)?, - node.lightpush_publish_encrypt_asymmetric(msg, None, peer_id.clone(), &pk, Some(sk), None)?, - node.lightpush_publish_encrypt_symmetric(msg, None, peer_id.clone(), ssk, None, None)?, - node.lightpush_publish_encrypt_symmetric(msg, None, peer_id, ssk, Some(sk), None)?, + node.lightpush_publish(msg, None, peer_id, None)? ])) } @@ -126,12 +110,9 @@ async fn test_echo_messages( let (tx, mut rx) = mpsc::channel(1); set_callback(tx, sk, ssk); - let mut ids = - try_publish_relay_messages(node, &message, &sk, &ssk).expect("send relay messages"); + let mut ids = try_publish_relay_messages(node, &message).expect("send relay messages"); - ids.extend( - try_publish_lightpush_messages(node, &message, &sk, &ssk).expect("send lightpush messages"), - ); + ids.extend(try_publish_lightpush_messages(node, &message).expect("send lightpush messages")); while let Some(res) = rx.recv().await { if ids.take(&res.id).is_some() { @@ -180,7 +161,7 @@ async fn discv5_echo() -> Result<(), String> { let content_topic = WakuContentTopic::new("toychat", 2, "huilong", Encoding::Proto); let topics = node.relay_topics()?; - let default_topic = waku_dafault_pubsub_topic(); + let default_topic = waku_default_pubsub_topic(); assert!(topics.len() == 1); let topic: WakuPubSubTopic = topics[0].parse().unwrap(); diff --git a/waku-sys/Cargo.toml b/waku-sys/Cargo.toml index 6f691ef..71d43dc 100644 --- a/waku-sys/Cargo.toml +++ b/waku-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "waku-sys" -version = "0.2.0" +version = "0.3.0" edition = "2021" authors = [ "Daniel Sanchez Quiros " diff --git a/waku-sys/build.rs b/waku-sys/build.rs index f01b898..9b34747 100644 --- a/waku-sys/build.rs +++ b/waku-sys/build.rs @@ -39,9 +39,10 @@ fn build_go_waku_lib(go_bin: &str, project_dir: &Path) { cmd.env("CGO_ENABLED", "1") .arg("build") .arg("-buildmode=c-archive") + .arg("-tags=gowaku_no_rln") .arg("-o") .arg(out_dir.join("libgowaku.a")) - .arg("./library"); + .arg("./library/c"); // Setting `GOCACHE=/tmp/` for crates.io job that builds documentation // when a crate is being published or updated. diff --git a/waku-sys/vendor b/waku-sys/vendor index bc6a305..bfcba73 160000 --- a/waku-sys/vendor +++ b/waku-sys/vendor @@ -1 +1 @@ -Subproject commit bc6a305759f2f09ffd1426697730562ffdd4a3dd +Subproject commit bfcba732e424f1edb315f85ff3695f728d0d1a25