Эх сурвалжийг харах

refactor: nut11 blinded messages

thesimplekid 1 жил өмнө
parent
commit
c37749dea8

+ 0 - 4
crates/cashu-sdk/src/client/minreq_client.rs

@@ -172,15 +172,11 @@ impl Client for HttpClient {
     ) -> Result<SwapResponse, Error> {
     ) -> Result<SwapResponse, Error> {
         let url = join_url(mint_url, &["v1", "swap"])?;
         let url = join_url(mint_url, &["v1", "swap"])?;
 
 
-        println!("{}", serde_json::to_string(&swap_request).unwrap());
-
         let res = minreq::post(url).with_json(&swap_request)?.send()?;
         let res = minreq::post(url).with_json(&swap_request)?.send()?;
 
 
         let value = res.json::<Value>()?;
         let value = res.json::<Value>()?;
-        println!("{}", value);
         let response: Result<SwapResponse, serde_json::Error> =
         let response: Result<SwapResponse, serde_json::Error> =
             serde_json::from_value(value.clone());
             serde_json::from_value(value.clone());
-        println!("{:?}", response);
 
 
         match response {
         match response {
             Ok(res) => Ok(res),
             Ok(res) => Ok(res),

+ 0 - 8
crates/cashu-sdk/src/wallet/mod.rs

@@ -234,7 +234,6 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
         }
         }
 
 
         let keysets = self.client.get_mint_keysets(mint_url.try_into()?).await?;
         let keysets = self.client.get_mint_keysets(mint_url.try_into()?).await?;
-        println!("{:?}", keysets);
 
 
         self.localstore
         self.localstore
             .add_mint_keysets(
             .add_mint_keysets(
@@ -385,16 +384,12 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
                 pre_swap.pre_mint_secrets.secrets(),
                 pre_swap.pre_mint_secrets.secrets(),
                 &keys,
                 &keys,
             )?;
             )?;
-            //  println!("{:?}", p);
             let mint_proofs = proofs.entry(token.mint).or_default();
             let mint_proofs = proofs.entry(token.mint).or_default();
 
 
             mint_proofs.extend(p);
             mint_proofs.extend(p);
         }
         }
-        //println!("{:?}", proofs);
 
 
         for (mint, p) in proofs {
         for (mint, p) in proofs {
-            println!("{:?}", serde_json::to_string(&p));
-            println!("{:?}", mint);
             self.add_mint(mint.clone()).await?;
             self.add_mint(mint.clone()).await?;
             self.localstore.add_proofs(mint, p).await?;
             self.localstore.add_proofs(mint, p).await?;
         }
         }
@@ -780,7 +775,6 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
         let mut change_proofs = vec![];
         let mut change_proofs = vec![];
 
 
         for proof in post_swap_proofs {
         for proof in post_swap_proofs {
-            println!("post swap proof: {:?}", proof);
             let conditions: Result<cashu::nuts::nut10::Secret, _> = (&proof.secret).try_into();
             let conditions: Result<cashu::nuts::nut10::Secret, _> = (&proof.secret).try_into();
             if conditions.is_ok() {
             if conditions.is_ok() {
                 send_proofs.push(proof);
                 send_proofs.push(proof);
@@ -849,14 +843,12 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
                 {
                 {
                     let conditions: Result<P2PKConditions, _> = secret.try_into();
                     let conditions: Result<P2PKConditions, _> = secret.try_into();
                     if let Ok(conditions) = conditions {
                     if let Ok(conditions) = conditions {
-                        println!("{:?}", conditions);
                         let pubkeys = conditions.pubkeys;
                         let pubkeys = conditions.pubkeys;
 
 
                         for pubkey in pubkeys {
                         for pubkey in pubkeys {
                             if let Some(signing) = pubkey_secret_key.get(&pubkey.to_string()) {
                             if let Some(signing) = pubkey_secret_key.get(&pubkey.to_string()) {
                                 proof.sign_p2pk_proof(signing.clone()).unwrap();
                                 proof.sign_p2pk_proof(signing.clone()).unwrap();
                                 proof.verify_p2pk().unwrap();
                                 proof.verify_p2pk().unwrap();
-                                println!("v");
                             }
                             }
                         }
                         }
 
 

+ 5 - 3
crates/cashu/src/nuts/mod.rs

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

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

@@ -8,11 +8,7 @@ use std::str::FromStr;
 use serde::{Deserialize, Serialize};
 use serde::{Deserialize, Serialize};
 
 
 use super::{Id, Proofs, PublicKey};
 use super::{Id, Proofs, PublicKey};
-#[cfg(feature = "nut11")]
-use super::{Signatures, SigningKey};
 use crate::error::Error;
 use crate::error::Error;
-#[cfg(feature = "nut11")]
-use crate::nuts::nut11::{witness_deserialize, witness_serialize};
 use crate::secret::Secret;
 use crate::secret::Secret;
 use crate::url::UncheckedUrl;
 use crate::url::UncheckedUrl;
 use crate::Amount;
 use crate::Amount;
@@ -28,13 +24,6 @@ pub struct BlindedMessage {
     /// encrypted secret message (B_)
     /// encrypted secret message (B_)
     #[serde(rename = "B_")]
     #[serde(rename = "B_")]
     pub b: PublicKey,
     pub b: PublicKey,
-    /// Witness
-    #[cfg(feature = "nut11")]
-    #[serde(default)]
-    #[serde(skip_serializing_if = "Signatures::is_empty")]
-    #[serde(serialize_with = "witness_serialize")]
-    #[serde(deserialize_with = "witness_deserialize")]
-    pub witness: Signatures,
 }
 }
 
 
 impl BlindedMessage {
 impl BlindedMessage {
@@ -43,24 +32,8 @@ impl BlindedMessage {
             amount,
             amount,
             keyset_id,
             keyset_id,
             b,
             b,
-            #[cfg(feature = "nut11")]
-            witness: Signatures::default(),
         }
         }
     }
     }
-
-    #[cfg(feature = "nut11")]
-    pub fn sign_p2pk_blinded_message(&mut self, secret_key: SigningKey) -> Result<(), Error> {
-        let msg_to_sign = hex::decode(self.b.to_string())?;
-
-        println!("{:?}", msg_to_sign);
-
-        let signature = secret_key.sign(&msg_to_sign);
-
-        self.witness
-            .signatures
-            .push(hex::encode(signature.to_bytes()));
-        Ok(())
-    }
 }
 }
 
 
 #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, hash::Hash)]
 #[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, hash::Hash)]

+ 42 - 7
crates/cashu/src/nuts/nut11.rs

@@ -99,6 +99,48 @@ impl PartialOrd for Proof {
     }
     }
 }
 }
 
 
+/// Blinded Message [NUT-00]
+#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
+pub struct BlindedMessage {
+    /// Amount
+    pub amount: Amount,
+    /// Keyset Id
+    #[serde(rename = "id")]
+    pub keyset_id: Id,
+    /// encrypted secret message (B_)
+    #[serde(rename = "B_")]
+    pub b: PublicKey,
+    /// Witness
+    #[serde(default)]
+    #[serde(skip_serializing_if = "Signatures::is_empty")]
+    #[serde(serialize_with = "witness_serialize")]
+    #[serde(deserialize_with = "witness_deserialize")]
+    pub witness: Signatures,
+}
+
+impl BlindedMessage {
+    pub fn new(amount: Amount, keyset_id: Id, b: PublicKey) -> Self {
+        Self {
+            amount,
+            keyset_id,
+            b,
+            witness: Signatures::default(),
+        }
+    }
+
+    #[cfg(feature = "nut11")]
+    pub fn sign_p2pk_blinded_message(&mut self, secret_key: SigningKey) -> Result<(), Error> {
+        let msg_to_sign = hex::decode(self.b.to_string())?;
+
+        let signature = secret_key.sign(&msg_to_sign);
+
+        self.witness
+            .signatures
+            .push(hex::encode(signature.to_bytes()));
+        Ok(())
+    }
+}
+
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
 pub struct P2PKConditions {
 pub struct P2PKConditions {
     #[serde(skip_serializing_if = "Option::is_none")]
     #[serde(skip_serializing_if = "Option::is_none")]
@@ -277,12 +319,6 @@ impl Proof {
 
 
         let mut valid_sigs = 0;
         let mut valid_sigs = 0;
 
 
-        println!("{:?}", self.secret.to_string());
-        println!(
-            "sec bytes: {:?}",
-            self.secret.to_string().into_bytes().len()
-        );
-
         let msg = &self.secret.to_bytes().unwrap();
         let msg = &self.secret.to_bytes().unwrap();
 
 
         for signature in &self.witness.signatures {
         for signature in &self.witness.signatures {
@@ -301,7 +337,6 @@ impl Proof {
         }
         }
 
 
         if valid_sigs.ge(&spending_conditions.num_sigs.unwrap_or(1)) {
         if valid_sigs.ge(&spending_conditions.num_sigs.unwrap_or(1)) {
-            println!("valid sigs: {}", valid_sigs);
             return Ok(());
             return Ok(());
         }
         }
 
 

+ 1 - 1
crates/cashu/src/secret.rs

@@ -69,7 +69,7 @@ impl Secret {
             serde_json::from_str(&self.0);
             serde_json::from_str(&self.0);
 
 
         match secret {
         match secret {
-            Ok(_) => Ok(self.0.clone().replace('\\', "").into_bytes()),
+            Ok(_) => Ok(self.0.clone().into_bytes()),
             Err(_) => Ok(hex::decode(&self.0)?),
             Err(_) => Ok(hex::decode(&self.0)?),
         }
         }
     }
     }