From a8ea0d9bdccbed2f4ad5c610c0ff16391c1d2641 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sat, 2 Mar 2024 21:30:54 +0000 Subject: [PATCH] refactor: secret into bytes --- .../cashu-sdk/src/mint/localstore/memory.rs | 14 +++++----- .../src/mint/localstore/redb_store.rs | 10 +++---- crates/cashu-sdk/src/mint/mod.rs | 10 +++---- crates/cashu/src/dhke.rs | 18 +++++-------- crates/cashu/src/nuts/nut00.rs | 11 ++++---- crates/cashu/src/nuts/nut11.rs | 4 +-- crates/cashu/src/secret.rs | 26 +++++-------------- 7 files changed, 35 insertions(+), 58 deletions(-) diff --git a/crates/cashu-sdk/src/mint/localstore/memory.rs b/crates/cashu-sdk/src/mint/localstore/memory.rs index 6c5bfb7e..b4ccd1d8 100644 --- a/crates/cashu-sdk/src/mint/localstore/memory.rs +++ b/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, 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 diff --git a/crates/cashu-sdk/src/mint/localstore/redb_store.rs b/crates/cashu-sdk/src/mint/localstore/redb_store.rs index 7ef64898..0646e987 100644 --- a/crates/cashu-sdk/src/mint/localstore/redb_store.rs +++ b/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()?; diff --git a/crates/cashu-sdk/src/mint/mod.rs b/crates/cashu-sdk/src/mint/mod.rs index 0239f8be..c73d0a77 100644 --- a/crates/cashu-sdk/src/mint/mod.rs +++ b/crates/cashu-sdk/src/mint/mod.rs @@ -362,8 +362,7 @@ impl Mint { let secrets: HashSet> = 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> = 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(); diff --git a/crates/cashu/src/dhke.rs b/crates/cashu/src/dhke.rs index 4bd68c3a..41c908fa 100644 --- a/crates/cashu/src/dhke.rs +++ b/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( + pub fn verify_message( a: SecretKey, unblinded_message: k256::PublicKey, - msg: V, - ) -> Result<(), error::mint::Error> - where - V: TryInto>, - >>::Error: Debug, - error::mint::Error: From<>>::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()); } } } diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index a06199a3..edd51fe2 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/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); diff --git a/crates/cashu/src/nuts/nut11.rs b/crates/cashu/src/nuts/nut11.rs index c2f03705..f6630aa6 100644 --- a/crates/cashu/src/nuts/nut11.rs +++ b/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); diff --git a/crates/cashu/src/secret.rs b/crates/cashu/src/secret.rs index bf526226..bdc6380d 100644 --- a/crates/cashu/src/secret.rs +++ b/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, Error> { - Ok(hex::decode(&self.0)?) - } - - #[cfg(feature = "nut10")] - pub fn to_bytes(&self) -> Result, Error> { - let secret: Result = - 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 { + self.0.clone().into_bytes() } #[cfg(feature = "nut11")] @@ -105,16 +93,14 @@ impl ToString for Secret { } } -impl TryFrom for Vec { - type Error = Error; - fn try_from(value: Secret) -> Result, Error> { +impl From for Vec { + fn from(value: Secret) -> Vec { value.to_bytes() } } -impl TryFrom<&Secret> for Vec { - type Error = Error; - fn try_from(value: &Secret) -> Result, Error> { +impl From<&Secret> for Vec { + fn from(value: &Secret) -> Vec { value.to_bytes() } }