2022-10-09 16:50:40 +02:00
|
|
|
//! Waku node implementation
|
|
|
|
|
|
2022-10-03 15:21:19 +02:00
|
|
|
mod config;
|
2024-02-13 16:18:16 -04:00
|
|
|
mod events;
|
2022-10-06 15:28:25 +02:00
|
|
|
mod management;
|
|
|
|
|
mod peers;
|
2022-10-06 15:51:00 +02:00
|
|
|
mod relay;
|
2022-10-03 15:21:19 +02:00
|
|
|
|
|
|
|
|
// std
|
2024-02-26 11:13:30 -04:00
|
|
|
pub use aes_gcm::Key;
|
2022-10-19 15:58:09 +02:00
|
|
|
pub use multiaddr::Multiaddr;
|
|
|
|
|
pub use secp256k1::{PublicKey, SecretKey};
|
2022-10-06 15:28:25 +02:00
|
|
|
use std::time::Duration;
|
2022-10-03 15:21:19 +02:00
|
|
|
// internal
|
2024-03-01 11:31:00 -04:00
|
|
|
use crate::general::{MessageHash, Result, WakuMessage};
|
2022-10-03 15:21:19 +02:00
|
|
|
|
2024-04-01 15:15:50 -04:00
|
|
|
pub use config::RLNConfig;
|
2024-02-08 17:16:22 -04:00
|
|
|
pub use config::WakuNodeConfig;
|
2024-11-09 16:14:28 +07:00
|
|
|
pub use events::{Event, WakuMessageEvent, WakuNodeContext};
|
2024-02-19 17:04:41 -04:00
|
|
|
pub use relay::waku_create_content_topic;
|
2022-10-03 15:21:19 +02:00
|
|
|
|
2024-11-05 12:44:44 +01:00
|
|
|
use crate::Encoding;
|
2024-11-18 10:14:03 +01:00
|
|
|
use crate::WakuContentTopic;
|
2024-11-05 12:44:44 +01:00
|
|
|
use std::time::SystemTime;
|
|
|
|
|
|
2024-03-01 11:31:00 -04:00
|
|
|
/// Marker trait to disallow undesired waku node states in the handle
|
|
|
|
|
pub trait WakuNodeState {}
|
|
|
|
|
|
|
|
|
|
/// Waku node initialized state
|
|
|
|
|
pub struct Initialized;
|
|
|
|
|
|
|
|
|
|
/// Waku node running state
|
|
|
|
|
pub struct Running;
|
|
|
|
|
|
|
|
|
|
impl WakuNodeState for Initialized {}
|
|
|
|
|
impl WakuNodeState for Running {}
|
|
|
|
|
|
2022-10-09 16:50:40 +02:00
|
|
|
/// Handle to the underliying waku node
|
2024-11-09 16:14:28 +07:00
|
|
|
pub struct WakuNodeHandle {
|
2024-10-31 13:44:12 +01:00
|
|
|
pub ctx: WakuNodeContext,
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-03-01 11:31:00 -04:00
|
|
|
/// Spawn a new Waku node with the given configuration (default configuration if `None` provided)
|
|
|
|
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_newchar-jsonconfig)
|
2024-11-09 16:14:28 +07:00
|
|
|
pub fn waku_new(config: Option<WakuNodeConfig>) -> Result<WakuNodeHandle> {
|
2024-03-01 11:31:00 -04:00
|
|
|
Ok(WakuNodeHandle {
|
|
|
|
|
ctx: management::waku_new(config)?,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-09 16:14:28 +07:00
|
|
|
pub fn waku_destroy(node: WakuNodeHandle) -> Result<()> {
|
2024-03-11 12:00:30 -04:00
|
|
|
management::waku_destroy(&node.ctx)
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-09 16:14:28 +07:00
|
|
|
// unsafe impl Send for WakuNodeHandle<Running> {}
|
|
|
|
|
|
|
|
|
|
impl WakuNodeHandle {
|
2022-11-02 16:21:15 +01:00
|
|
|
/// Start a Waku node mounting all the protocols that were enabled during the Waku node instantiation.
|
|
|
|
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_start)
|
2024-11-09 16:14:28 +07:00
|
|
|
pub fn start(&self) -> Result<()> {
|
|
|
|
|
management::waku_start(&self.ctx)
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 15:51:00 +02:00
|
|
|
/// Stops a Waku node
|
2022-11-02 16:21:15 +01:00
|
|
|
/// as per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_stop)
|
2024-11-09 16:14:28 +07:00
|
|
|
pub fn stop(&self) -> Result<()> {
|
|
|
|
|
management::waku_stop(&self.ctx)
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|
2022-10-06 15:28:25 +02:00
|
|
|
|
2024-02-21 11:59:40 -04:00
|
|
|
/// Get the multiaddresses the Waku node is listening to
|
|
|
|
|
/// as per [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_listen_addresses)
|
|
|
|
|
pub fn listen_addresses(&self) -> Result<Vec<Multiaddr>> {
|
2024-02-26 11:13:30 -04:00
|
|
|
management::waku_listen_addresses(&self.ctx)
|
2024-02-21 11:59:40 -04:00
|
|
|
}
|
|
|
|
|
|
2024-02-19 17:04:41 -04:00
|
|
|
/// Get the nwaku version
|
|
|
|
|
pub fn version(&self) -> Result<String> {
|
2024-02-26 11:13:30 -04:00
|
|
|
management::waku_version(&self.ctx)
|
2024-02-19 17:04:41 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-06 15:51:00 +02:00
|
|
|
/// Dial peer using a multiaddress
|
|
|
|
|
/// If `timeout` as milliseconds doesn't fit into a `i32` it is clamped to [`i32::MAX`]
|
|
|
|
|
/// If the function execution takes longer than `timeout` value, the execution will be canceled and an error returned.
|
|
|
|
|
/// Use 0 for no timeout
|
2022-11-02 16:21:15 +01:00
|
|
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_connect_peerchar-address-int-timeoutms)
|
2024-02-08 17:16:22 -04:00
|
|
|
pub fn connect(&self, address: &Multiaddr, timeout: Option<Duration>) -> Result<()> {
|
2024-02-26 11:13:30 -04:00
|
|
|
peers::waku_connect(&self.ctx, address, timeout)
|
2022-10-06 15:28:25 +02:00
|
|
|
}
|
2022-10-06 15:51:00 +02:00
|
|
|
|
2024-11-05 12:44:44 +01:00
|
|
|
pub fn relay_publish_txt(
|
|
|
|
|
&self,
|
2024-11-09 16:14:28 +07:00
|
|
|
pubsub_topic: &String,
|
|
|
|
|
msg_txt: &String,
|
2024-11-05 12:44:44 +01:00
|
|
|
content_topic_name: &'static str,
|
|
|
|
|
timeout: Option<Duration>,
|
|
|
|
|
) -> Result<MessageHash> {
|
|
|
|
|
let content_topic = WakuContentTopic::new("waku", "2", content_topic_name, Encoding::Proto);
|
|
|
|
|
let message = WakuMessage::new(
|
|
|
|
|
msg_txt,
|
|
|
|
|
content_topic,
|
|
|
|
|
0,
|
|
|
|
|
SystemTime::now()
|
|
|
|
|
.duration_since(SystemTime::UNIX_EPOCH)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_millis()
|
|
|
|
|
.try_into()
|
|
|
|
|
.unwrap(),
|
|
|
|
|
Vec::new(),
|
|
|
|
|
false,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
relay::waku_relay_publish_message(&self.ctx, &message, pubsub_topic, timeout)
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 15:50:35 -04:00
|
|
|
/// Publish a message using Waku Relay.
|
2022-11-02 16:21:15 +01:00
|
|
|
/// As per the [specification](https://rfc.vac.dev/spec/36/#extern-char-waku_relay_publishchar-messagejson-char-pubsubtopic-int-timeoutms)
|
2023-11-07 15:50:35 -04:00
|
|
|
/// The pubsub_topic parameter is optional and if not specified it will be derived from the contentTopic.
|
2022-10-06 15:51:00 +02:00
|
|
|
pub fn relay_publish_message(
|
|
|
|
|
&self,
|
|
|
|
|
message: &WakuMessage,
|
2024-11-09 16:14:28 +07:00
|
|
|
pubsub_topic: &String,
|
2022-10-17 19:30:07 +02:00
|
|
|
timeout: Option<Duration>,
|
2024-03-01 11:31:00 -04:00
|
|
|
) -> Result<MessageHash> {
|
2024-02-26 11:13:30 -04:00
|
|
|
relay::waku_relay_publish_message(&self.ctx, message, pubsub_topic, timeout)
|
2022-10-06 15:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 15:50:35 -04:00
|
|
|
/// Subscribe to WakuRelay to receive messages matching a content filter.
|
2024-11-09 16:14:28 +07:00
|
|
|
pub fn relay_subscribe(&self, pubsub_topic: &String) -> Result<()> {
|
2024-02-26 11:13:30 -04:00
|
|
|
relay::waku_relay_subscribe(&self.ctx, pubsub_topic)
|
2022-10-06 15:51:00 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 15:50:35 -04:00
|
|
|
/// Closes the pubsub subscription to stop receiving messages matching a content filter. No more messages will be received from this pubsub topic
|
2024-02-14 16:52:21 -04:00
|
|
|
pub fn relay_unsubscribe(&self, pubsub_topic: &String) -> Result<()> {
|
2024-02-26 11:13:30 -04:00
|
|
|
relay::waku_relay_unsubscribe(&self.ctx, pubsub_topic)
|
2023-06-12 09:46:13 -04:00
|
|
|
}
|
2022-10-03 15:21:19 +02:00
|
|
|
}
|