From e5ad24ec8b3deb3278127ce2cd163e975cfc383e Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Quiros Date: Wed, 5 Oct 2022 11:23:46 +0200 Subject: [PATCH] Plumb relay methods to node --- waku/src/node/mod.rs | 60 +++++++++++++++++++++++++++++++++++++++++- waku/src/node/relay.rs | 36 ++++++++++++------------- 2 files changed, 77 insertions(+), 19 deletions(-) diff --git a/waku/src/node/mod.rs b/waku/src/node/mod.rs index 5e95657..dbc6eca 100644 --- a/waku/src/node/mod.rs +++ b/waku/src/node/mod.rs @@ -4,16 +4,19 @@ mod peers; mod relay; // std +use aes_gcm::{Aes256Gcm, Key}; +use libsecp256k1::{PublicKey, SecretKey}; use multiaddr::Multiaddr; use std::marker::PhantomData; use std::sync::Mutex; use std::time::Duration; // crates // internal -use crate::general::{PeerId, Result}; +use crate::general::{MessageId, PeerId, Result, WakuMessage, WakuPubSubTopic}; 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}; /// Shared flag to check if a waku node is already running in the current process static WAKU_NODE_INITIALIZED: Mutex = Mutex::new(false); @@ -95,6 +98,61 @@ impl WakuNodeHandle { pub fn peers(&self) -> Result { peers::waku_peers() } + + pub fn publish_message( + &mut self, + message: &WakuMessage, + pubsub_topic: Option, + timeout: Duration, + ) -> Result { + relay::waku_relay_publish_message(message, pubsub_topic, timeout) + } + + pub fn publish_encrypt_asymmetric( + &mut self, + message: &WakuMessage, + pubsub_topic: Option, + public_key: &PublicKey, + signing_key: &SecretKey, + timeout: Duration, + ) -> Result { + relay::waku_relay_publish_encrypt_asymmetric( + message, + pubsub_topic, + public_key, + signing_key, + timeout, + ) + } + + pub fn publish_encrypt_symmetric( + &mut self, + message: &WakuMessage, + pubsub_topic: Option, + symmetric_key: &Key, + signing_key: &SecretKey, + timeout: Duration, + ) -> Result { + relay::waku_relay_publish_encrypt_symmetric( + message, + pubsub_topic, + symmetric_key, + signing_key, + timeout, + ) + } + + pub fn enough_peers(&self, pubsub_topic: Option) -> Result { + relay::waku_enough_peers(pubsub_topic) + } + + pub fn subscribe(&mut self, pubsub_topic: Option) -> Result<()> { + relay::waku_relay_subscribe(pubsub_topic) + } + + pub fn unsubscribe(&mut self, pubsub_topic: Option) -> Result<()> { + relay::waku_relay_unsubscribe(pubsub_topic) + } } pub fn waku_new(config: Option) -> Result> { diff --git a/waku/src/node/relay.rs b/waku/src/node/relay.rs index a5f3780..4bc01bb 100644 --- a/waku/src/node/relay.rs +++ b/waku/src/node/relay.rs @@ -71,12 +71,12 @@ pub fn waku_dafault_pubsub_topic() -> WakuPubSubTopic { /// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms) pub fn waku_relay_publish_message( message: &WakuMessage, - pubsub_topic: Option<&str>, + pubsub_topic: Option, timeout: Duration, ) -> Result { let pubsub_topic = pubsub_topic - .map(ToString::to_string) - .unwrap_or_else(|| waku_dafault_pubsub_topic().to_string()); + .unwrap_or_else(waku_dafault_pubsub_topic) + .to_string(); let result = unsafe { CStr::from_ptr(waku_sys::waku_relay_publish( CString::new( @@ -105,7 +105,7 @@ pub fn waku_relay_publish_message( /// 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<&str>, + pubsub_topic: Option, public_key: &PublicKey, signing_key: &SecretKey, timeout: Duration, @@ -113,8 +113,8 @@ pub fn waku_relay_publish_encrypt_asymmetric( let pk = hex::encode(public_key.serialize()); let sk = hex::encode(signing_key.serialize()); let pubsub_topic = pubsub_topic - .map(ToString::to_string) - .unwrap_or_else(|| waku_dafault_pubsub_topic().to_string()); + .unwrap_or_else(waku_dafault_pubsub_topic) + .to_string(); let result = unsafe { CStr::from_ptr(waku_sys::waku_relay_publish_enc_asymmetric( CString::new( @@ -149,7 +149,7 @@ pub fn waku_relay_publish_encrypt_asymmetric( /// 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<&str>, + pubsub_topic: Option, symmetric_key: &Key, signing_key: &SecretKey, timeout: Duration, @@ -157,8 +157,8 @@ pub fn waku_relay_publish_encrypt_symmetric( let symk = hex::encode(symmetric_key.as_slice()); let sk = hex::encode(signing_key.serialize()); let pubsub_topic = pubsub_topic - .map(ToString::to_string) - .unwrap_or_else(|| waku_dafault_pubsub_topic().to_string()); + .unwrap_or_else(waku_dafault_pubsub_topic) + .to_string(); let result = unsafe { CStr::from_ptr(waku_sys::waku_relay_publish_enc_symmetric( CString::new( @@ -189,10 +189,10 @@ pub fn waku_relay_publish_encrypt_symmetric( message_id.into() } -pub fn waku_enough_peers(pubsub_topic: Option<&str>) -> Result { +pub fn waku_enough_peers(pubsub_topic: Option) -> Result { let pubsub_topic = pubsub_topic - .map(ToString::to_string) - .unwrap_or_else(|| waku_dafault_pubsub_topic().to_string()); + .unwrap_or_else(waku_dafault_pubsub_topic) + .to_string(); let result = unsafe { CStr::from_ptr(waku_sys::waku_relay_enough_peers( CString::new(pubsub_topic) @@ -207,10 +207,10 @@ pub fn waku_enough_peers(pubsub_topic: Option<&str>) -> Result { enough_peers.into() } -pub fn waku_relay_subscribe(pubsub_topic: Option<&str>) -> Result<()> { +pub fn waku_relay_subscribe(pubsub_topic: Option) -> Result<()> { let pubsub_topic = pubsub_topic - .map(ToString::to_string) - .unwrap_or_else(|| waku_dafault_pubsub_topic().to_string()); + .unwrap_or_else(waku_dafault_pubsub_topic) + .to_string(); let result = unsafe { CStr::from_ptr(waku_sys::waku_relay_subscribe( CString::new(pubsub_topic) @@ -225,10 +225,10 @@ pub fn waku_relay_subscribe(pubsub_topic: Option<&str>) -> Result<()> { Result::from(enough_peers).map(|_| ()) } -pub fn waku_relay_sunubscribe(pubsub_topic: Option<&str>) -> Result<()> { +pub fn waku_relay_unsubscribe(pubsub_topic: Option) -> Result<()> { let pubsub_topic = pubsub_topic - .map(ToString::to_string) - .unwrap_or_else(|| waku_dafault_pubsub_topic().to_string()); + .unwrap_or_else(waku_dafault_pubsub_topic) + .to_string(); let result = unsafe { CStr::from_ptr(waku_sys::waku_relay_unsubscribe( CString::new(pubsub_topic)