From 7b7ba2975daedc58f9d36269b889a2b87780ced2 Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Date: Wed, 28 Sep 2022 16:03:47 +0200 Subject: [PATCH] General types (#1) * Added JsonResponse type * Add dependencies * Added wakumessage * Adjust go build to link with missing simbols on osx * Added general types * Nitpick in docs --- Cargo.lock | 56 ++++++++++++++++++ waku-sys/Cargo.toml | 3 + waku-sys/vendor | 2 +- waku/Cargo.toml | 4 +- waku/src/general/mod.rs | 128 ++++++++++++++++++++++++++++++++++++++++ waku/src/lib.rs | 2 +- 6 files changed, 192 insertions(+), 3 deletions(-) create mode 100644 waku/src/general/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 7db4e79..60280c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -163,6 +163,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + [[package]] name = "lazy_static" version = "1.4.0" @@ -281,6 +287,43 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2" +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728eb6351430bccb993660dfffc5a72f91ccc1295abaa8ce19b27ebe4f75568b" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.145" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "81fa1584d3d1bcacd84c277a0dfe21f5b0f6accf4a23d04d4c6d61f1af522b4c" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + [[package]] name = "shlex" version = "1.1.0" @@ -293,6 +336,17 @@ version = "0.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +[[package]] +name = "syn" +version = "1.0.100" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52205623b1b0f064a4e71182c3b18ae902267282930c6d5462c91b859668426e" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "termcolor" version = "1.1.3" @@ -318,6 +372,8 @@ checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" name = "waku" version = "0.1.0" dependencies = [ + "serde", + "serde_json", "waku-sys", ] diff --git a/waku-sys/Cargo.toml b/waku-sys/Cargo.toml index 0200d9f..5239a44 100644 --- a/waku-sys/Cargo.toml +++ b/waku-sys/Cargo.toml @@ -3,6 +3,9 @@ name = "waku-sys" version = "0.1.0" edition = "2021" +[lib] +crate-type = ["rlib"] + # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/waku-sys/vendor b/waku-sys/vendor index b6ff038..5b42c98 160000 --- a/waku-sys/vendor +++ b/waku-sys/vendor @@ -1 +1 @@ -Subproject commit b6ff0386129fcbc34c04f08ac45ea3e19cfd3e44 +Subproject commit 5b42c98780fe938ff79c54229aa80cdd47a17568 diff --git a/waku/Cargo.toml b/waku/Cargo.toml index 33a6bca..341bf33 100644 --- a/waku/Cargo.toml +++ b/waku/Cargo.toml @@ -6,4 +6,6 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -waku-sys = { path = "../waku-sys" } \ No newline at end of file +waku-sys = { path = "../waku-sys" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" \ No newline at end of file diff --git a/waku/src/general/mod.rs b/waku/src/general/mod.rs new file mode 100644 index 0000000..3fbb5cd --- /dev/null +++ b/waku/src/general/mod.rs @@ -0,0 +1,128 @@ +// std +// crates +use serde::{Deserialize, Serialize}; +// internal + +/// JsonResponse wrapper. +/// `go-waku` ffi returns this type as a `char *` as per the [specification](https://rfc.vac.dev/spec/36/#jsonresponse-type) +/// This is internal, as it is better to use rust plain `Result` type. +#[derive(Deserialize)] +#[serde(rename_all = "snake_case")] +pub(crate) enum JsonResponse { + Result(T), + Error(String), +} + +/// Waku response, just a `Result` with an `String` error. +/// Convenient we can transform a [`JsonResponse`] into a [`Response`] (`Result`) +type Response = Result; + +impl From> for Response { + fn from(response: JsonResponse) -> Self { + match response { + JsonResponse::Result(t) => Ok(t), + JsonResponse::Error(e) => Err(e), + } + } +} + +/// JsonMessage, Waku message in JSON format. +/// as per the [specification](https://rfc.vac.dev/spec/36/#jsonmessage-type) +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct WakuMessage { + payload: Box<[u8]>, + /// The content topic to be set on the message + content_topic: String, + /// The Waku Message version number + version: usize, + /// Unix timestamp in nanoseconds + timestamp: usize, +} + +/// A payload once decoded, used when a received Waku Message is encrypted +pub struct DecodedPayload { + /// Public key that signed the message (optional), hex encoded with 0x prefix + public_key: Option, + /// Message signature (optional), hex encoded with 0x prefix + signature: Option, + /// Decrypted message payload base64 encoded + data: String, + /// Padding base64 encoded + padding: String, +} + +/// The content topic of a Waku message +/// as per the [specification](https://rfc.vac.dev/spec/36/#contentfilter-type) +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct ContentFilter { + /// The content topic of a Waku message + content_topic: String, +} + +/// The criteria to create subscription to a light node in JSON Format +/// as per the [specification](https://rfc.vac.dev/spec/36/#filtersubscription-type) +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct FilterSubscription { + /// Array of [`ContentFilter`] being subscribed to / unsubscribed from + content_filters: Vec, + /// Optional pubsub topic + pubsub_topic: Option, +} + +/// Criteria used to retrieve historical messages +#[derive(Serialize)] +#[serde(rename_all = "camelCase")] +pub struct StoreQuery { + /// The pubsub topic on which messages are published + pubsub_topic: Option, + /// Array of [`ContentFilter`] to query for historical messages + content_filters: Vec, + /// The inclusive lower bound on the timestamp of queried messages. + /// This field holds the Unix epoch time in nanoseconds + start_time: Option, + /// The inclusive upper bound on the timestamp of queried messages. + /// This field holds the Unix epoch time in nanoseconds + end_time: Option, + /// Paging information in [`PagingOptions`] format + paging_options: Option, +} + +/// The response received after doing a query to a store node +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct StoreResponse { + /// Array of retrieved historical messages in [`WakuMessage`] format + messages: Vec, + /// Paging information in [`PagingOptions`] format from which to resume further historical queries + paging_options: Option, +} + +/// Paging information +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct PagingOptions { + /// Number of messages to retrieve per page + page_size: usize, + /// Message Index from which to perform pagination. + /// If not included and forward is set to `true`, paging will be performed from the beginning of the list. + /// If not included and forward is set to `false`, paging will be performed from the end of the list + cursor: Option, + /// `true` if paging forward, `false` if paging backward + forward: bool, +} + +#[derive(Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct MessageIndex { + /// Hash of the message at this [`MessageIndex`] + digest: String, + /// UNIX timestamp in nanoseconds at which the message at this [`MessageIndex`] was received + receiver_time: usize, + /// UNIX timestamp in nanoseconds at which the message is generated by its sender + sender_time: usize, + /// The pubsub topic of the message at this [`MessageIndex`] + pubsub_topic: String, +} diff --git a/waku/src/lib.rs b/waku/src/lib.rs index ffeafdf..0d6c116 100644 --- a/waku/src/lib.rs +++ b/waku/src/lib.rs @@ -1,4 +1,4 @@ -use waku_sys; +mod general; #[cfg(test)] mod tests {