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
This commit is contained in:
parent
6b35fb7d62
commit
32bd05c6fc
|
@ -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",
|
||||
]
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "waku-bindings"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
authors = [
|
||||
"Daniel Sanchez Quiros <danielsq@status.im>"
|
||||
|
@ -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"
|
||||
|
|
|
@ -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<WakuMessage> {
|
||||
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<Aes256Gcm>,
|
||||
signing_key: Option<&SecretKey>,
|
||||
) -> Result<WakuMessage> {
|
||||
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)
|
||||
}
|
|
@ -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<Aes256Gcm>,
|
||||
signing_key: Option<&SecretKey>,
|
||||
) -> Result<WakuMessage> {
|
||||
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<WakuMessage> {
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>,
|
||||
peer_id: PeerId,
|
||||
public_key: &PublicKey,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>,
|
||||
peer_id: PeerId,
|
||||
symmetric_key: &Key<Aes256Gcm>,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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<Running> {
|
|||
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<WakuPubSubTopic>,
|
||||
public_key: &PublicKey,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>,
|
||||
symmetric_key: &Key<Aes256Gcm>,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>) -> Result<bool> {
|
||||
relay::waku_enough_peers(pubsub_topic)
|
||||
|
@ -250,48 +212,6 @@ impl WakuNodeHandle<Running> {
|
|||
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<WakuPubSubTopic>,
|
||||
peer_id: PeerId,
|
||||
public_key: &PublicKey,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>,
|
||||
peer_id: PeerId,
|
||||
symmetric_key: &Key<Aes256Gcm>,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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(
|
||||
|
|
|
@ -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<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>,
|
||||
public_key: &PublicKey,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>,
|
||||
symmetric_key: &Key<Aes256Gcm>,
|
||||
signing_key: Option<&SecretKey>,
|
||||
timeout: Option<Duration>,
|
||||
) -> Result<MessageId> {
|
||||
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<WakuPubSubTopic>) -> Result<bool> {
|
||||
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<WakuPubSubTopic>) -> Result<bool>
|
|||
|
||||
pub fn waku_relay_subscribe(pubsub_topic: Option<WakuPubSubTopic>) -> 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<WakuPubSubTopic>) -> Result<()>
|
|||
|
||||
pub fn waku_relay_unsubscribe(pubsub_topic: Option<WakuPubSubTopic>) -> 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)
|
||||
|
|
|
@ -7,10 +7,15 @@ use std::ffi::{c_char, CStr};
|
|||
pub fn decode_and_free_response<T: DeserializeOwned>(response_ptr: *mut c_char) -> Result<T> {
|
||||
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<T> =
|
||||
serde_json::from_str(response).expect("JsonResponse should always succeed to deserialize");
|
||||
let response: JsonResponse<T> = serde_json::from_str(response)
|
||||
.map_err(|err| format!("could not deserialize waku JsonResponse: {}", err))?;
|
||||
|
||||
unsafe {
|
||||
waku_sys::waku_utils_free(response_ptr);
|
||||
|
|
|
@ -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<Running>,
|
||||
msg: &WakuMessage,
|
||||
sk: &SecretKey,
|
||||
ssk: &Key<Aes256Gcm>,
|
||||
) -> Result<HashSet<MessageId>, 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<Running>,
|
||||
msg: &WakuMessage,
|
||||
sk: &SecretKey,
|
||||
ssk: &Key<Aes256Gcm>,
|
||||
) -> Result<HashSet<MessageId>, 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();
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "waku-sys"
|
||||
version = "0.2.0"
|
||||
version = "0.3.0"
|
||||
edition = "2021"
|
||||
authors = [
|
||||
"Daniel Sanchez Quiros <danielsq@status.im>"
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit bc6a305759f2f09ffd1426697730562ffdd4a3dd
|
||||
Subproject commit bfcba732e424f1edb315f85ff3695f728d0d1a25
|
Loading…
Reference in New Issue