Skip to main content

cashu/nuts/nut10/
secret.rs

1//! NUT-10 Secret Module
2
3use std::fmt;
4use std::str::FromStr;
5
6use serde::de::{self, Deserializer, SeqAccess, Visitor};
7use serde::ser::SerializeTuple;
8use serde::{Deserialize, Serialize, Serializer};
9
10use crate::nut10::Error;
11use crate::{Kind, SecretData};
12
13/// NUT10 Secret
14#[derive(Clone, PartialEq, Eq, Hash)]
15pub struct Secret {
16    ///  Kind of the spending condition
17    kind: Kind,
18    /// Secret Data
19    secret_data: SecretData,
20}
21
22impl fmt::Debug for Secret {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        f.debug_struct("Secret")
25            .field("kind", &self.kind)
26            .field("secret_data", &self.secret_data)
27            .finish()
28    }
29}
30
31impl Secret {
32    /// Create new [`Secret`]
33    pub fn new(kind: Kind, secret_data: SecretData) -> Self {
34        Self { kind, secret_data }
35    }
36
37    /// Get the kind
38    pub fn kind(&self) -> Kind {
39        self.kind
40    }
41
42    /// Get the secret data
43    pub fn secret_data(&self) -> &SecretData {
44        &self.secret_data
45    }
46}
47
48impl Serialize for Secret {
49    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
50    where
51        S: Serializer,
52    {
53        // Create a tuple representing the struct fields
54        let secret_tuple = (&self.kind, &self.secret_data);
55
56        // Serialize the tuple as a JSON array
57        let mut s = serializer.serialize_tuple(2)?;
58
59        s.serialize_element(&secret_tuple.0)?;
60        s.serialize_element(&secret_tuple.1)?;
61        s.end()
62    }
63}
64
65impl TryFrom<Secret> for crate::secret::Secret {
66    type Error = Error;
67    fn try_from(secret: Secret) -> Result<crate::secret::Secret, Self::Error> {
68        Ok(crate::secret::Secret::from_str(&serde_json::to_string(
69            &secret,
70        )?)?)
71    }
72}
73
74// Custom visitor for deserializing Secret
75struct SecretVisitor;
76
77impl<'de> Visitor<'de> for SecretVisitor {
78    type Value = Secret;
79
80    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
81        formatter.write_str("a tuple with two elements: [Kind, SecretData]")
82    }
83
84    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
85    where
86        A: SeqAccess<'de>,
87    {
88        // Deserialize the kind (first element)
89        let kind = seq
90            .next_element()?
91            .ok_or_else(|| de::Error::invalid_length(0, &self))?;
92
93        // Deserialize the secret_data (second element)
94        let secret_data = seq
95            .next_element()?
96            .ok_or_else(|| de::Error::invalid_length(1, &self))?;
97
98        // Make sure there are no additional elements
99        if seq.next_element::<serde::de::IgnoredAny>()?.is_some() {
100            return Err(de::Error::invalid_length(3, &self));
101        }
102
103        Ok(Secret { kind, secret_data })
104    }
105}
106
107impl<'de> Deserialize<'de> for Secret {
108    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
109    where
110        D: Deserializer<'de>,
111    {
112        deserializer.deserialize_seq(SecretVisitor)
113    }
114}