From 7f60bea5ecf3abe953f674eb545f84cf257d04e9 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 18 Feb 2024 11:56:24 +0000 Subject: [PATCH] refactor: new nut10 secret --- crates/cashu/src/nuts/nut10.rs | 25 +++++++++++++++++++++---- crates/cashu/src/nuts/nut11.rs | 3 --- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/cashu/src/nuts/nut10.rs b/crates/cashu/src/nuts/nut10.rs index a19e34ad..5953ada7 100644 --- a/crates/cashu/src/nuts/nut10.rs +++ b/crates/cashu/src/nuts/nut10.rs @@ -20,8 +20,8 @@ pub struct SecretData { /// Expresses the spending condition specific to each kind pub data: String, /// Additional data committed to and can be used for feature extensions - #[serde(skip_serializing_if = "Option::is_none")] - pub tags: Option>>, + #[serde(skip_serializing_if = "Vec::is_empty")] + pub tags: Vec>, } #[derive(Debug, Default, Clone, Deserialize, PartialEq, Eq)] @@ -31,6 +31,22 @@ pub struct Secret { pub secret_data: SecretData, } +impl Secret { + pub fn new(kind: Kind, data: S, tags: Vec>) -> Self + where + S: Into, + { + let nonce = crate::secret::Secret::new().to_string(); + let secret_data = SecretData { + nonce, + data: data.into(), + tags, + }; + + Self { kind, secret_data } + } +} + impl Serialize for Secret { fn serialize(&self, serializer: S) -> Result where @@ -47,6 +63,7 @@ impl Serialize for Secret { s.end() } } + #[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct UncheckedSecret(String); @@ -117,11 +134,11 @@ mod tests { nonce: "5d11913ee0f92fefdc82a6764fd2457a".to_string(), data: "026562efcfadc8e86d44da6a8adf80633d974302e62c850774db1fb36ff4cc7198" .to_string(), - tags: Some(vec![vec![ + tags: vec![vec![ "key".to_string(), "value1".to_string(), "value2".to_string(), - ]]), + ]], }, }; diff --git a/crates/cashu/src/nuts/nut11.rs b/crates/cashu/src/nuts/nut11.rs index f543caa7..49bc1810 100644 --- a/crates/cashu/src/nuts/nut11.rs +++ b/crates/cashu/src/nuts/nut11.rs @@ -88,8 +88,6 @@ impl TryFrom for Secret { tags.push(Tag::SigFlag(sig_flag).as_vec()); - let tags = if tags.len().gt(&0) { Some(tags) } else { None }; - Ok(Secret { kind: super::nut10::Kind::P2PK, secret_data: SecretData { @@ -108,7 +106,6 @@ impl TryFrom for P2PKConditions { .clone() .secret_data .tags - .unwrap_or_default() .into_iter() .map(|t| { let tag = Tag::try_from(t).unwrap();