Skip to main content

relay_core/
signed.rs

1use serde::{Deserialize, Serialize};
2
3/// A wrapper struct that holds data along with an optional signature.
4#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
5pub struct Signed {
6    pub payload: Vec<u8>,
7    pub sig: Option<Vec<u8>>,
8}
9
10impl Signed {
11    /// Creates a new Signed wrapper without a signature.
12    pub fn unsigned(payload: Vec<u8>) -> Self {
13        Signed { payload, sig: None }
14    }
15
16    /// Creates a new Signed wrapper with a signature.
17    pub fn new(payload: Vec<u8>, sig: Vec<u8>) -> Self {
18        Signed {
19            payload,
20            sig: Some(sig),
21        }
22    }
23
24    pub fn is_signed(&self) -> bool {
25        self.sig.is_some()
26    }
27}
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32
33    #[test]
34    fn new_unsigned() {
35        let payload = vec![1, 2, 3];
36        let signed = Signed::unsigned(payload.clone());
37        assert_eq!(signed.payload, payload);
38        assert!(!signed.is_signed());
39        assert!(signed.sig.is_none());
40    }
41
42    #[test]
43    fn new_signed() {
44        let payload = vec![1, 2, 3];
45        let signature = vec![4, 5, 6];
46        let signed = Signed::new(payload.clone(), signature.clone());
47        assert_eq!(signed.payload, payload);
48        assert!(signed.is_signed());
49        assert_eq!(signed.sig.unwrap(), signature);
50    }
51}