Parcourir la source

refactor: is_p2pk

thesimplekid il y a 1 an
Parent
commit
ca7a5f1d7d
2 fichiers modifiés avec 19 ajouts et 2 suppressions
  1. 3 2
      crates/cashu/src/nuts/nut11.rs
  2. 16 0
      crates/cashu/src/secret.rs

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

@@ -309,11 +309,12 @@ impl TryFrom<Secret> for P2PKConditions {
 
 impl Proof {
     pub fn verify_p2pk(&self) -> Result<(), Error> {
-        let secret: Secret = (&self.secret).try_into()?;
-        if secret.kind.ne(&super::nut10::Kind::P2PK) {
+        if !self.secret.is_p2pk() {
             return Err(Error::IncorrectSecretKind);
         }
 
+        let secret: Secret = self.secret.clone().try_into()?;
+
         let spending_conditions: P2PKConditions = secret.clone().try_into()?;
 
         let mut valid_sigs = 0;

+ 16 - 0
crates/cashu/src/secret.rs

@@ -73,6 +73,22 @@ impl Secret {
             Err(_) => Ok(hex::decode(&self.0)?),
         }
     }
+
+    #[cfg(feature = "nut11")]
+    pub fn is_p2pk(&self) -> bool {
+        use crate::nuts::Kind;
+
+        let secret: Result<crate::nuts::nut10::Secret, serde_json::Error> =
+            serde_json::from_str(&self.0);
+
+        if let Ok(secret) = secret {
+            if secret.kind.eq(&Kind::P2PK) {
+                return true;
+            }
+        }
+
+        false
+    }
 }
 
 impl FromStr for Secret {