From 803fc37e041a54daabc90e3201271964f5e6bdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rich=CE=9Brd?= Date: Tue, 23 May 2023 08:32:23 -0400 Subject: [PATCH] fix: handle missing ephemeral field and unknown encodings in content topics (#61) * fix(example): only attempt to decode messages sent in the toy chat content topic * fix: handle missing ephemeral field and unknown encodings --- examples/toy-chat/src/main.rs | 4 ++++ waku-bindings/src/general/mod.rs | 10 +++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/examples/toy-chat/src/main.rs b/examples/toy-chat/src/main.rs index 87d426b..2e5f3fe 100644 --- a/examples/toy-chat/src/main.rs +++ b/examples/toy-chat/src/main.rs @@ -134,6 +134,10 @@ fn main() -> std::result::Result<(), Box> { let shared_messages = Arc::clone(&app.messages); waku_set_event_callback(move |signal| match signal.event() { waku_bindings::Event::WakuMessage(event) => { + if event.waku_message().content_topic() != &TOY_CHAT_CONTENT_TOPIC { + return; + } + match ::decode(event.waku_message().payload()) { Ok(chat_message) => { shared_messages.write().unwrap().push(chat_message); diff --git a/waku-bindings/src/general/mod.rs b/waku-bindings/src/general/mod.rs index 60fa7c4..2616848 100644 --- a/waku-bindings/src/general/mod.rs +++ b/waku-bindings/src/general/mod.rs @@ -91,6 +91,7 @@ pub struct WakuMessage { timestamp: usize, #[serde(with = "base64_serde", default = "Vec::new")] meta: Vec, + #[serde(default)] ephemeral: bool, // TODO: implement RLN fields #[serde(flatten)] @@ -310,11 +311,12 @@ pub struct MessageIndex { } /// WakuMessage encoding scheme -#[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub enum Encoding { Proto, Rlp, Rfc26, + Unknown(String), } impl Display for Encoding { @@ -323,6 +325,7 @@ impl Display for Encoding { Encoding::Proto => "proto", Encoding::Rlp => "rlp", Encoding::Rfc26 => "rfc26", + Encoding::Unknown(value) => value, }; f.write_str(s) } @@ -336,10 +339,7 @@ impl FromStr for Encoding { "proto" => Ok(Self::Proto), "rlp" => Ok(Self::Rlp), "rfc26" => Ok(Self::Rfc26), - encoding => Err(std::io::Error::new( - std::io::ErrorKind::InvalidInput, - format!("Unrecognized encoding: {encoding}"), - )), + encoding => Ok(Self::Unknown(encoding.to_string())), } } }