Skip to main content

syncweb_core/gossip/
signed_topic.rs

1use serde::{Deserialize, Serialize};
2
3use crate::error::{Result, SyncwebError};
4
5/// Anything that can be signed, broadcast, and verified over gossip.
6pub trait SignedGossipMessage: Serialize + for<'de> Deserialize<'de> {
7    /// Verify this message's signature. Returns `Ok(())` if valid.
8    ///
9    /// Types that use bearer-capability auth (e.g. `PrivateLink`) may return
10    /// `Ok(())` unconditionally and enforce auth at the application layer.
11    ///
12    /// # Errors
13    ///
14    /// Returns an error if the signature is missing, malformed, or invalid.
15    fn verify_signature(&self) -> Result<()>;
16
17    /// Serialize for wire transport.
18    ///
19    /// # Errors
20    ///
21    /// Returns an error if serialization fails.
22    fn to_wire_bytes(&self) -> Result<Vec<u8>> {
23        serde_json::to_vec(self).map_err(|error| SyncwebError::operation("serialize gossip message", error))
24    }
25
26    /// Deserialize from wire transport.
27    ///
28    /// # Errors
29    ///
30    /// Returns an error if deserialization fails.
31    fn from_wire_bytes(bytes: &[u8]) -> Result<Self> {
32        serde_json::from_slice(bytes).map_err(|error| SyncwebError::operation("deserialize gossip message", error))
33    }
34}