relay-core 0.1.1-alpha.1

The core components of the Relay Protocol.
Documentation
use serde::{Deserialize, Serialize};

/// A wrapper struct that holds data along with an optional signature.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Signed {
    pub payload: Vec<u8>,
    pub sig: Option<Vec<u8>>,
}

impl Signed {
    /// Creates a new Signed wrapper without a signature.
    pub fn unsigned(payload: Vec<u8>) -> Self {
        Signed { payload, sig: None }
    }

    /// Creates a new Signed wrapper with a signature.
    pub fn new(payload: Vec<u8>, sig: Vec<u8>) -> Self {
        Signed {
            payload,
            sig: Some(sig),
        }
    }

    pub fn is_signed(&self) -> bool {
        self.sig.is_some()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn new_unsigned() {
        let payload = vec![1, 2, 3];
        let signed = Signed::unsigned(payload.clone());
        assert_eq!(signed.payload, payload);
        assert!(!signed.is_signed());
        assert!(signed.sig.is_none());
    }

    #[test]
    fn new_signed() {
        let payload = vec![1, 2, 3];
        let signature = vec![4, 5, 6];
        let signed = Signed::new(payload.clone(), signature.clone());
        assert_eq!(signed.payload, payload);
        assert!(signed.is_signed());
        assert_eq!(signed.sig.unwrap(), signature);
    }
}