ソースを参照

refactor: secret into bytes

thesimplekid 1 年間 前
コミット
a8ea0d9bdc

+ 7 - 7
crates/cashu-sdk/src/mint/localstore/memory.rs

@@ -48,7 +48,7 @@ impl MemoryLocalStore {
                     .into_iter()
                     .map(|p| {
                         (
-                            hash_to_curve(&p.secret.to_bytes().unwrap())
+                            hash_to_curve(&p.secret.to_bytes())
                                 .unwrap()
                                 .to_sec1_bytes()
                                 .to_vec(),
@@ -62,7 +62,7 @@ impl MemoryLocalStore {
                     .into_iter()
                     .map(|p| {
                         (
-                            hash_to_curve(&p.secret.to_bytes().unwrap())
+                            hash_to_curve(&p.secret.to_bytes())
                                 .unwrap()
                                 .to_sec1_bytes()
                                 .to_vec(),
@@ -156,7 +156,7 @@ impl LocalStore for MemoryLocalStore {
     }
 
     async fn add_spent_proof(&self, proof: Proof) -> Result<(), Error> {
-        let secret_point = hash_to_curve(&proof.secret.to_bytes()?)?;
+        let secret_point = hash_to_curve(&proof.secret.to_bytes())?;
         self.spent_proofs
             .lock()
             .await
@@ -169,7 +169,7 @@ impl LocalStore for MemoryLocalStore {
             .spent_proofs
             .lock()
             .await
-            .get(&hash_to_curve(&secret.to_bytes()?)?.to_sec1_bytes().to_vec())
+            .get(&hash_to_curve(&secret.to_bytes())?.to_sec1_bytes().to_vec())
             .cloned())
     }
 
@@ -187,7 +187,7 @@ impl LocalStore for MemoryLocalStore {
 
     async fn add_pending_proof(&self, proof: Proof) -> Result<(), Error> {
         self.pending_proofs.lock().await.insert(
-            hash_to_curve(&proof.secret.to_bytes()?)?
+            hash_to_curve(&proof.secret.to_bytes())?
                 .to_sec1_bytes()
                 .to_vec(),
             proof,
@@ -196,7 +196,7 @@ impl LocalStore for MemoryLocalStore {
     }
 
     async fn get_pending_proof_by_secret(&self, secret: &Secret) -> Result<Option<Proof>, Error> {
-        let secret_point = hash_to_curve(&secret.to_bytes()?)?;
+        let secret_point = hash_to_curve(&secret.to_bytes())?;
         Ok(self
             .pending_proofs
             .lock()
@@ -218,7 +218,7 @@ impl LocalStore for MemoryLocalStore {
     }
 
     async fn remove_pending_proof(&self, secret: &Secret) -> Result<(), Error> {
-        let secret_point = hash_to_curve(&secret.to_bytes()?)?;
+        let secret_point = hash_to_curve(&secret.to_bytes())?;
         self.pending_proofs
             .lock()
             .await

+ 5 - 5
crates/cashu-sdk/src/mint/localstore/redb_store.rs

@@ -285,7 +285,7 @@ impl LocalStore for RedbLocalStore {
         {
             let mut table = write_txn.open_table(SPENT_PROOFS_TABLE)?;
             table.insert(
-                hash_to_curve(&proof.secret.to_bytes()?)?
+                hash_to_curve(&proof.secret.to_bytes())?
                     .to_sec1_bytes()
                     .as_ref(),
                 serde_json::to_string(&proof)?.as_str(),
@@ -319,7 +319,7 @@ impl LocalStore for RedbLocalStore {
         let read_txn = db.begin_read()?;
         let table = read_txn.open_table(SPENT_PROOFS_TABLE)?;
 
-        let secret_hash = hash_to_curve(&secret.to_bytes()?)?;
+        let secret_hash = hash_to_curve(&secret.to_bytes())?;
 
         let proof = table.get(secret_hash.to_sec1_bytes().as_ref())?;
 
@@ -340,7 +340,7 @@ impl LocalStore for RedbLocalStore {
         {
             let mut table = write_txn.open_table(PENDING_PROOFS_TABLE)?;
             table.insert(
-                hash_to_curve(&proof.secret.to_bytes()?)?
+                hash_to_curve(&proof.secret.to_bytes())?
                     .to_sec1_bytes()
                     .as_ref(),
                 serde_json::to_string(&proof)?.as_str(),
@@ -373,7 +373,7 @@ impl LocalStore for RedbLocalStore {
         let read_txn = db.begin_read()?;
         let table = read_txn.open_table(PENDING_PROOFS_TABLE)?;
 
-        let secret_hash = hash_to_curve(&secret.to_bytes()?)?;
+        let secret_hash = hash_to_curve(&secret.to_bytes())?;
 
         let proof = table.get(secret_hash.to_sec1_bytes().as_ref())?;
 
@@ -391,7 +391,7 @@ impl LocalStore for RedbLocalStore {
 
         {
             let mut table = write_txn.open_table(PENDING_PROOFS_TABLE)?;
-            let secret_hash = hash_to_curve(&secret.to_bytes()?)?;
+            let secret_hash = hash_to_curve(&secret.to_bytes())?;
             table.remove(secret_hash.to_sec1_bytes().as_ref())?;
         }
         write_txn.commit()?;

+ 4 - 6
crates/cashu-sdk/src/mint/mod.rs

@@ -362,8 +362,7 @@ impl Mint {
         let secrets: HashSet<Vec<u8>> = swap_request
             .inputs
             .iter()
-            .flat_map(|p| p.secret.to_bytes())
-            .flat_map(|p| hash_to_curve(&p))
+            .flat_map(|p| hash_to_curve(&p.secret.to_bytes()))
             .map(|p| p.to_sec1_bytes().to_vec())
             .collect();
 
@@ -474,7 +473,7 @@ impl Mint {
             }
         }
 
-        let y = hash_to_curve(&proof.secret.to_bytes()?)?;
+        let y = hash_to_curve(&proof.secret.to_bytes())?;
 
         if self.localstore.get_spent_proof_by_hash(&y).await?.is_some() {
             return Err(Error::TokenSpent);
@@ -502,7 +501,7 @@ impl Mint {
         verify_message(
             keypair.secret_key.clone().into(),
             proof.c.clone().into(),
-            &proof.secret,
+            &proof.secret.to_bytes(),
         )?;
 
         Ok(())
@@ -611,8 +610,7 @@ impl Mint {
         let secrets: HashSet<Vec<u8>> = melt_request
             .inputs
             .iter()
-            .flat_map(|p| p.secret.to_bytes())
-            .flat_map(|p| hash_to_curve(&p))
+            .flat_map(|p| hash_to_curve(&p.secret.to_bytes()))
             .map(|p| p.to_sec1_bytes().to_vec())
             .collect();
 

+ 6 - 12
crates/cashu/src/dhke.rs

@@ -120,7 +120,6 @@ mod wallet {
 
 #[cfg(feature = "mint")]
 mod mint {
-    use std::fmt::Debug;
     use std::ops::Mul;
 
     use k256::{Scalar, SecretKey};
@@ -141,18 +140,13 @@ mod mint {
     }
 
     /// Verify Message
-    pub fn verify_message<V>(
+    pub fn verify_message(
         a: SecretKey,
         unblinded_message: k256::PublicKey,
-        msg: V,
-    ) -> Result<(), error::mint::Error>
-    where
-        V: TryInto<Vec<u8>>,
-        <V as TryInto<Vec<u8>>>::Error: Debug,
-        error::mint::Error: From<<V as TryInto<Vec<u8>>>::Error>,
-    {
+        msg: &[u8],
+    ) -> Result<(), error::mint::Error> {
         // Y
-        let y = hash_to_curve(&msg.try_into()?)?;
+        let y = hash_to_curve(msg)?;
 
         if unblinded_message
             == k256::PublicKey::try_from(*y.as_affine() * Scalar::from(a.as_scalar_primitive()))?
@@ -369,7 +363,7 @@ mod tests {
             let x = Secret::new();
 
             // Y
-            let y = hash_to_curve(&x.to_bytes().unwrap()).unwrap();
+            let y = hash_to_curve(&x.to_bytes()).unwrap();
 
             // B_
             let blinded = blind_message(&y.to_sec1_bytes(), None).unwrap();
@@ -380,7 +374,7 @@ mod tests {
             // C
             let c = unblind_message(signed.into(), blinded.1, bob_pub.into()).unwrap();
 
-            assert!(verify_message(bob_sec, c.into(), &x).is_ok());
+            assert!(verify_message(bob_sec, c.into(), &x.to_bytes()).is_ok());
         }
     }
 }

+ 5 - 6
crates/cashu/src/nuts/nut00.rs

@@ -155,7 +155,7 @@ pub mod wallet {
 
             for amount in amount_split {
                 let secret = Secret::new();
-                let (blinded, r) = blind_message(&secret.to_bytes()?, None)?;
+                let (blinded, r) = blind_message(&secret.to_bytes(), None)?;
 
                 let blinded_message = BlindedMessage::new(amount, keyset_id, blinded);
 
@@ -178,7 +178,7 @@ pub mod wallet {
             let mut output = Vec::with_capacity(secrets.len());
 
             for (secret, amount) in secrets.into_iter().zip(amounts) {
-                let (blinded, r) = blind_message(&secret.to_bytes()?, None)?;
+                let (blinded, r) = blind_message(&secret.to_bytes(), None)?;
 
                 let blinded_message = BlindedMessage::new(amount, keyset_id, blinded);
 
@@ -201,7 +201,7 @@ pub mod wallet {
 
             for _i in 0..count {
                 let secret = Secret::new();
-                let (blinded, r) = blind_message(&secret.to_bytes()?, None)?;
+                let (blinded, r) = blind_message(&secret.to_bytes(), None)?;
 
                 let blinded_message = BlindedMessage::new(Amount::ZERO, keyset_id, blinded);
 
@@ -233,8 +233,7 @@ pub mod wallet {
                 let secret = Secret::from_seed(mnemonic, keyset_id, counter);
                 let blinding_factor = SecretKey::from_seed(mnemonic, keyset_id, counter);
 
-                let (blinded, r) =
-                    blind_message(&secret.to_bytes()?, Some(blinding_factor.into()))?;
+                let (blinded, r) = blind_message(&secret.to_bytes(), Some(blinding_factor.into()))?;
 
                 let blinded_message = BlindedMessage::new(amount, keyset_id, blinded);
 
@@ -264,7 +263,7 @@ pub mod wallet {
 
             for amount in amount_split {
                 let secret: Secret = conditions.clone().try_into()?;
-                let (blinded, r) = blind_message(&secret.to_bytes()?, None)?;
+                let (blinded, r) = blind_message(&secret.to_bytes(), None)?;
 
                 let blinded_message = BlindedMessage::new(amount, keyset_id, blinded);
 

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

@@ -319,7 +319,7 @@ impl Proof {
 
         let mut valid_sigs = 0;
 
-        let msg = &self.secret.to_bytes()?;
+        let msg = &self.secret.to_bytes();
 
         for signature in &self.witness.signatures {
             let mut pubkeys = spending_conditions.pubkeys.clone();
@@ -365,7 +365,7 @@ impl Proof {
     }
 
     pub fn sign_p2pk_proof(&mut self, secret_key: SigningKey) -> Result<(), Error> {
-        let msg_to_sign = &self.secret.to_bytes()?;
+        let msg_to_sign = &self.secret.to_bytes();
 
         let signature = secret_key.sign(msg_to_sign);
 

+ 6 - 20
crates/cashu/src/secret.rs

@@ -58,20 +58,8 @@ impl Secret {
         Self(hex::encode(xpriv.private_key().to_bytes()))
     }
 
-    #[cfg(not(feature = "nut10"))]
-    pub fn to_bytes(&self) -> Result<Vec<u8>, Error> {
-        Ok(hex::decode(&self.0)?)
-    }
-
-    #[cfg(feature = "nut10")]
-    pub fn to_bytes(&self) -> Result<Vec<u8>, Error> {
-        let secret: Result<crate::nuts::nut10::Secret, serde_json::Error> =
-            serde_json::from_str(&self.0);
-
-        match secret {
-            Ok(_) => Ok(self.0.clone().into_bytes()),
-            Err(_) => Ok(hex::decode(&self.0)?),
-        }
+    pub fn to_bytes(&self) -> Vec<u8> {
+        self.0.clone().into_bytes()
     }
 
     #[cfg(feature = "nut11")]
@@ -105,16 +93,14 @@ impl ToString for Secret {
     }
 }
 
-impl TryFrom<Secret> for Vec<u8> {
-    type Error = Error;
-    fn try_from(value: Secret) -> Result<Vec<u8>, Error> {
+impl From<Secret> for Vec<u8> {
+    fn from(value: Secret) -> Vec<u8> {
         value.to_bytes()
     }
 }
 
-impl TryFrom<&Secret> for Vec<u8> {
-    type Error = Error;
-    fn try_from(value: &Secret) -> Result<Vec<u8>, Error> {
+impl From<&Secret> for Vec<u8> {
+    fn from(value: &Secret) -> Vec<u8> {
         value.to_bytes()
     }
 }