Browse Source

refactor: remove proof in nut11

Duplicating proofs across stucks leads to unneeded duplication.
Adding the feilds in nut00 with the feilds behind features reduces this.
thesimplekid 1 year ago
parent
commit
97cf67039b
3 changed files with 14 additions and 57 deletions
  1. 2 4
      crates/cashu/src/nuts/mod.rs
  2. 9 0
      crates/cashu/src/nuts/nut00.rs
  3. 3 53
      crates/cashu/src/nuts/nut11.rs

+ 2 - 4
crates/cashu/src/nuts/mod.rs

@@ -16,9 +16,7 @@ pub mod nut11;
 
 #[cfg(feature = "wallet")]
 pub use nut00::wallet::{PreMint, PreMintSecrets, Token};
-#[cfg(not(feature = "nut11"))]
-pub use nut00::Proof;
-pub use nut00::{BlindedMessage, BlindedSignature, CurrencyUnit, PaymentMethod};
+pub use nut00::{BlindedMessage, BlindedSignature, CurrencyUnit, PaymentMethod, Proof};
 pub use nut01::{Keys, KeysResponse, PublicKey, SecretKey};
 pub use nut02::mint::KeySet as MintKeySet;
 pub use nut02::{Id, KeySet, KeySetInfo, KeysetResponse};
@@ -40,6 +38,6 @@ pub use nut08::{MeltBolt11Request, MeltBolt11Response};
 #[cfg(feature = "nut10")]
 pub use nut10::{Kind, Secret as Nut10Secret, SecretData};
 #[cfg(feature = "nut11")]
-pub use nut11::{P2PKConditions, Proof, SigFlag, Signatures, SigningKey, VerifyingKey};
+pub use nut11::{P2PKConditions, SigFlag, Signatures, SigningKey, VerifyingKey};
 
 pub type Proofs = Vec<Proof>;

+ 9 - 0
crates/cashu/src/nuts/nut00.rs

@@ -471,6 +471,13 @@ pub struct Proof {
     /// Unblinded signature
     #[serde(rename = "C")]
     pub c: PublicKey,
+    #[cfg(feature = "nut11")]
+    /// Witness
+    #[serde(default)]
+    #[serde(skip_serializing_if = "Signatures::is_empty")]
+    #[serde(serialize_with = "witness_serialize")]
+    #[serde(deserialize_with = "witness_deserialize")]
+    pub witness: Signatures,
 }
 
 impl Proof {
@@ -480,6 +487,8 @@ impl Proof {
             keyset_id,
             secret,
             c,
+            #[cfg(feature = "nut11")]
+            witness: Signatures::default(),
         }
     }
 }

+ 3 - 53
crates/cashu/src/nuts/nut11.rs

@@ -3,7 +3,6 @@
 
 use std::collections::HashMap;
 use std::fmt;
-use std::hash::{self, Hasher};
 use std::str::FromStr;
 
 use k256::schnorr::signature::{Signer, Verifier};
@@ -14,13 +13,11 @@ use serde::ser::SerializeSeq;
 use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
 
 use super::nut01::PublicKey;
-use super::nut02::Id;
 use super::nut10::{Secret, SecretData};
-use super::SecretKey;
+use super::{Proof, SecretKey};
 use crate::error::Error;
 use crate::nuts::nut00::BlindedMessage;
 use crate::utils::unix_time;
-use crate::Amount;
 
 #[derive(Default, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct Signatures {
@@ -35,27 +32,6 @@ impl Signatures {
     }
 }
 
-/// Proofs [NUT-11]
-#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
-pub struct Proof {
-    /// Amount in satoshi
-    pub amount: Amount,
-    /// NUT-10 Secret
-    pub secret: crate::secret::Secret,
-    /// Unblinded signature
-    #[serde(rename = "C")]
-    pub c: PublicKey,
-    /// `Keyset id`
-    #[serde(rename = "id")]
-    pub keyset_id: Id,
-    /// Witness
-    #[serde(default)]
-    #[serde(skip_serializing_if = "Signatures::is_empty")]
-    #[serde(serialize_with = "witness_serialize")]
-    #[serde(deserialize_with = "witness_deserialize")]
-    pub witness: Signatures,
-}
-
 pub fn witness_serialize<S>(x: &Signatures, s: S) -> Result<S::Ok, S::Error>
 where
     S: Serializer,
@@ -72,16 +48,6 @@ where
 }
 
 impl Proof {
-    pub fn new(amount: Amount, keyset_id: Id, secret: crate::secret::Secret, c: PublicKey) -> Self {
-        Proof {
-            amount,
-            keyset_id,
-            secret,
-            c,
-            witness: Signatures::default(),
-        }
-    }
-
     pub fn verify_p2pk(&self) -> Result<(), Error> {
         if !self.secret.is_p2pk() {
             return Err(Error::IncorrectSecretKind);
@@ -153,24 +119,6 @@ impl Proof {
     }
 }
 
-impl hash::Hash for Proof {
-    fn hash<H: Hasher>(&self, state: &mut H) {
-        self.secret.hash(state);
-    }
-}
-
-impl Ord for Proof {
-    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
-        self.amount.cmp(&other.amount)
-    }
-}
-
-impl PartialOrd for Proof {
-    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
-        Some(self.cmp(other))
-    }
-}
-
 impl BlindedMessage {
     pub fn sign_p2pk(&mut self, secret_key: SigningKey) -> Result<(), Error> {
         let msg_to_sign = hex::decode(self.b.to_string())?;
@@ -725,6 +673,8 @@ mod tests {
     use std::str::FromStr;
 
     use super::*;
+    use crate::nuts::Id;
+    use crate::Amount;
 
     #[test]
     fn test_secret_ser() {