From e54d7444715ba69fe13bfff9d8a3b621e2b98382 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Fri, 14 Oct 2022 12:50:21 -0500 Subject: [PATCH] Fix lightpush --- waku/src/general/mod.rs | 5 +++++ waku/src/node/lightpush.rs | 43 +++++++++++++++++++++++++------------- waku/src/node/mod.rs | 14 +++++++++---- waku/tests/node.rs | 30 ++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 18 deletions(-) diff --git a/waku/src/general/mod.rs b/waku/src/general/mod.rs index 5f8f5de..5e645c0 100644 --- a/waku/src/general/mod.rs +++ b/waku/src/general/mod.rs @@ -86,6 +86,9 @@ pub struct WakuMessage { version: WakuMessageVersion, /// Unix timestamp in nanoseconds timestamp: usize, + // TODO: implement RLN fields + #[serde(flatten)] + _extras: serde_json::Value, } impl WakuMessage { @@ -96,11 +99,13 @@ impl WakuMessage { timestamp: usize, ) -> Self { let payload = payload.as_ref().to_vec(); + Self { payload, content_topic, version, timestamp, + _extras: Default::default(), } } diff --git a/waku/src/node/lightpush.rs b/waku/src/node/lightpush.rs index dcd824d..6f24524 100644 --- a/waku/src/node/lightpush.rs +++ b/waku/src/node/lightpush.rs @@ -14,10 +14,13 @@ use crate::node::waku_dafault_pubsub_topic; /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_lightpush_publishchar-messagejson-char-topic-char-peerid-int-timeoutms) pub fn waku_lightpush_publish( message: &WakuMessage, - pubsub_topic: WakuPubSubTopic, + pubsub_topic: Option, peer_id: PeerId, - timeout: Duration, + timeout: Option, ) -> Result { + let pubsub_topic = pubsub_topic + .unwrap_or_else(waku_dafault_pubsub_topic) + .to_string(); let result = unsafe { CStr::from_ptr(waku_sys::waku_lightpush_publish( CString::new( @@ -33,9 +36,13 @@ pub fn waku_lightpush_publish( .expect("CString should build properly from peer id") .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() @@ -55,9 +62,9 @@ pub fn waku_lightpush_publish_encrypt_asymmetric( peer_id: PeerId, public_key: &PublicKey, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { - let pk = hex::encode(public_key.serialize()); + 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); @@ -85,9 +92,13 @@ pub fn waku_lightpush_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") @@ -105,7 +116,7 @@ pub fn waku_lightpush_publish_encrypt_symmetric( peer_id: PeerId, symmetric_key: &Key, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { let symk = hex::encode(symmetric_key.as_slice()); let sk = signing_key @@ -135,9 +146,13 @@ pub fn waku_lightpush_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/src/node/mod.rs b/waku/src/node/mod.rs index 4f88f44..1735136 100644 --- a/waku/src/node/mod.rs +++ b/waku/src/node/mod.rs @@ -230,6 +230,7 @@ impl WakuNodeHandle { /// /// wrapper around [`store::waku_store_query`] pub fn store_query( + &self, query: &StoreQuery, peer_id: PeerId, timeout: Duration, @@ -241,10 +242,11 @@ impl WakuNodeHandle { /// /// wrapper around [`lightpush::waku_lightpush_publish`] pub fn lightpush_publish( + &self, message: &WakuMessage, - pubsub_topic: WakuPubSubTopic, + pubsub_topic: Option, peer_id: PeerId, - timeout: Duration, + timeout: Option, ) -> Result { lightpush::waku_lightpush_publish(message, pubsub_topic, peer_id, timeout) } @@ -253,12 +255,13 @@ impl WakuNodeHandle { /// /// wrapper around [`lightpush::waku_lightpush_publish_encrypt_asymmetric`] pub fn lightpush_publish_encrypt_asymmetric( + &self, message: &WakuMessage, pubsub_topic: Option, peer_id: PeerId, public_key: &PublicKey, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { lightpush::waku_lightpush_publish_encrypt_asymmetric( message, @@ -274,12 +277,13 @@ impl WakuNodeHandle { /// /// wrapper around [`lightpush::waku_lightpush_publish_encrypt_symmetric`] pub fn lightpush_publish_encrypt_symmetric( + &self, message: &WakuMessage, pubsub_topic: Option, peer_id: PeerId, symmetric_key: &Key, signing_key: Option<&SecretKey>, - timeout: Duration, + timeout: Option, ) -> Result { lightpush::waku_lightpush_publish_encrypt_symmetric( message, @@ -295,6 +299,7 @@ impl WakuNodeHandle { /// /// wrapper around [`filter::waku_filter_subscribe`] pub fn filter_subscribe( + &self, filter_subscription: &FilterSubscription, peer_id: PeerId, timeout: Duration, @@ -306,6 +311,7 @@ impl WakuNodeHandle { /// /// wrapper around [`filter::waku_filter_unsubscribe`] pub fn filter_unsubscribe( + &self, filter_subscription: &FilterSubscription, timeout: Duration, ) -> Result<()> { diff --git a/waku/tests/node.rs b/waku/tests/node.rs index 9cc3468..86cdf83 100644 --- a/waku/tests/node.rs +++ b/waku/tests/node.rs @@ -106,6 +106,36 @@ pub fn main() -> Result<(), String> { node.relay_publish_encrypt_asymmetric(&message, None, &pk, Some(&sk), None)?; node.relay_publish_encrypt_symmetric(&message, None, &ssk, Some(&sk), None)?; + let peer_id = node + .peers() + .unwrap() + .iter() + .map(|peer| peer.peer_id()) + .filter(|id| id.as_str() != node.peer_id().unwrap().as_str()) + .next() + .unwrap() + .clone(); + + node.lightpush_publish(&message, None, peer_id.clone(), None)?; + node.lightpush_publish_encrypt_asymmetric(&message, None, peer_id.clone(), &pk, None, None)?; + node.lightpush_publish_encrypt_asymmetric( + &message, + None, + peer_id.clone(), + &pk, + Some(&sk), + None, + )?; + node.lightpush_publish_encrypt_symmetric(&message, None, peer_id.clone(), &ssk, None, None)?; + node.lightpush_publish_encrypt_symmetric( + &message, + None, + peer_id.clone(), + &ssk, + Some(&sk), + None, + )?; + std::thread::sleep(Duration::from_secs(2)); node.stop()?; Ok(())