From d36048847c0dfdaad30fc5ba26b4d96809134e5f Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Sun, 18 Feb 2024 11:58:04 +0000 Subject: [PATCH] refactor: use secret type in proof --- .../cashu-sdk/src/mint/localstore/memory.rs | 29 ++++++++----------- .../src/mint/localstore/redb_store.rs | 10 ++++--- crates/cashu-sdk/src/mint/mod.rs | 21 ++++---------- crates/cashu-sdk/src/wallet/mod.rs | 4 +-- crates/cashu/src/dhke.rs | 2 +- crates/cashu/src/nuts/nut00.rs | 3 +- 6 files changed, 29 insertions(+), 40 deletions(-) diff --git a/crates/cashu-sdk/src/mint/localstore/memory.rs b/crates/cashu-sdk/src/mint/localstore/memory.rs index fd34e91a..6c5bfb7e 100644 --- a/crates/cashu-sdk/src/mint/localstore/memory.rs +++ b/crates/cashu-sdk/src/mint/localstore/memory.rs @@ -1,5 +1,4 @@ use std::collections::HashMap; -use std::str::FromStr; use std::sync::Arc; use async_trait::async_trait; @@ -49,12 +48,10 @@ impl MemoryLocalStore { .into_iter() .map(|p| { ( - hash_to_curve( - &Secret::from_str(&p.secret).unwrap().to_bytes().unwrap(), - ) - .unwrap() - .to_sec1_bytes() - .to_vec(), + hash_to_curve(&p.secret.to_bytes().unwrap()) + .unwrap() + .to_sec1_bytes() + .to_vec(), p, ) }) @@ -65,12 +62,10 @@ impl MemoryLocalStore { .into_iter() .map(|p| { ( - hash_to_curve( - &Secret::from_str(&p.secret).unwrap().to_bytes().unwrap(), - ) - .unwrap() - .to_sec1_bytes() - .to_vec(), + hash_to_curve(&p.secret.to_bytes().unwrap()) + .unwrap() + .to_sec1_bytes() + .to_vec(), p, ) }) @@ -161,8 +156,7 @@ impl LocalStore for MemoryLocalStore { } async fn add_spent_proof(&self, proof: Proof) -> Result<(), Error> { - let secret = Secret::from_str(&proof.secret)?; - let secret_point = hash_to_curve(&secret.to_bytes()?)?; + let secret_point = hash_to_curve(&proof.secret.to_bytes()?)?; self.spent_proofs .lock() .await @@ -192,9 +186,10 @@ impl LocalStore for MemoryLocalStore { } async fn add_pending_proof(&self, proof: Proof) -> Result<(), Error> { - let secret = Secret::from_str(&proof.secret)?; self.pending_proofs.lock().await.insert( - hash_to_curve(&secret.to_bytes()?)?.to_sec1_bytes().to_vec(), + hash_to_curve(&proof.secret.to_bytes()?)? + .to_sec1_bytes() + .to_vec(), proof, ); Ok(()) diff --git a/crates/cashu-sdk/src/mint/localstore/redb_store.rs b/crates/cashu-sdk/src/mint/localstore/redb_store.rs index ac2ea801..f6043ec3 100644 --- a/crates/cashu-sdk/src/mint/localstore/redb_store.rs +++ b/crates/cashu-sdk/src/mint/localstore/redb_store.rs @@ -270,7 +270,6 @@ impl LocalStore for RedbLocalStore { } async fn add_spent_proof(&self, proof: Proof) -> Result<(), Error> { - let secret = Secret::from_str(&proof.secret)?; let db = self.db.lock().await; let write_txn = db.begin_write()?; @@ -278,7 +277,9 @@ impl LocalStore for RedbLocalStore { { let mut table = write_txn.open_table(SPENT_PROOFS_TABLE)?; table.insert( - hash_to_curve(&secret.to_bytes()?)?.to_sec1_bytes().as_ref(), + hash_to_curve(&proof.secret.to_bytes()?)? + .to_sec1_bytes() + .as_ref(), serde_json::to_string(&proof)?.as_str(), )?; } @@ -316,7 +317,6 @@ impl LocalStore for RedbLocalStore { } async fn add_pending_proof(&self, proof: Proof) -> Result<(), Error> { - let secret = Secret::from_str(&proof.secret)?; let db = self.db.lock().await; let write_txn = db.begin_write()?; @@ -324,7 +324,9 @@ impl LocalStore for RedbLocalStore { { let mut table = write_txn.open_table(PENDING_PROOFS_TABLE)?; table.insert( - hash_to_curve(&secret.to_bytes()?)?.to_sec1_bytes().as_ref(), + hash_to_curve(&proof.secret.to_bytes()?)? + .to_sec1_bytes() + .as_ref(), serde_json::to_string(&proof)?.as_str(), )?; } diff --git a/crates/cashu-sdk/src/mint/mod.rs b/crates/cashu-sdk/src/mint/mod.rs index 6aa2e79a..4ca65d10 100644 --- a/crates/cashu-sdk/src/mint/mod.rs +++ b/crates/cashu-sdk/src/mint/mod.rs @@ -1,5 +1,4 @@ use std::collections::HashSet; -use std::str::FromStr; use std::sync::Arc; use cashu::dhke::{hash_to_curve, sign_message, verify_message}; @@ -12,7 +11,6 @@ use cashu::nuts::{ }; #[cfg(feature = "nut07")] use cashu::nuts::{CheckStateRequest, CheckStateResponse}; -use cashu::secret::Secret; use cashu::types::{MeltQuote, MintQuote}; use cashu::Amount; use http::StatusCode; @@ -359,8 +357,7 @@ impl Mint { let secrets: HashSet> = swap_request .inputs .iter() - .flat_map(|p| Secret::from_str(&p.secret)) - .flat_map(|p| p.to_bytes()) + .flat_map(|p| p.secret.to_bytes()) .flat_map(|p| hash_to_curve(&p)) .map(|p| p.to_sec1_bytes().to_vec()) .collect(); @@ -424,19 +421,14 @@ impl Mint { } async fn verify_proof(&self, proof: &Proof) -> Result<(), Error> { - let secret = Secret::from_str(&proof.secret)?; - if self - .localstore - .get_spent_proof_by_secret(&secret) - .await? - .is_some() - { + let y = hash_to_curve(&proof.secret.to_bytes()?).unwrap(); + if self.localstore.get_spent_proof_by_hash(&y).await?.is_some() { return Err(Error::TokenSpent); } if self .localstore - .get_pending_proof_by_secret(&secret) + .get_pending_proof_by_hash(&y) .await? .is_some() { @@ -456,7 +448,7 @@ impl Mint { verify_message( keypair.secret_key.clone().into(), proof.c.clone().into(), - &secret, + &proof.secret, )?; Ok(()) @@ -565,8 +557,7 @@ impl Mint { let secrets: HashSet> = melt_request .inputs .iter() - .flat_map(|p| Secret::from_str(&p.secret)) - .flat_map(|p| p.to_bytes()) + .flat_map(|p| p.secret.to_bytes()) .flat_map(|p| hash_to_curve(&p)) .map(|p| p.to_sec1_bytes().to_vec()) .collect(); diff --git a/crates/cashu-sdk/src/wallet/mod.rs b/crates/cashu-sdk/src/wallet/mod.rs index 588663d5..903b3712 100644 --- a/crates/cashu-sdk/src/wallet/mod.rs +++ b/crates/cashu-sdk/src/wallet/mod.rs @@ -155,7 +155,7 @@ impl Wallet { proofs .clone() .into_iter() - .flat_map(|p| Secret::from_str(&p.secret)) + .map(|p| p.secret) .collect::>() .clone(), ) @@ -446,7 +446,7 @@ impl Wallet { let proof = Proof { keyset_id: promise.keyset_id, amount: promise.amount, - secret: premint.secret.to_string(), + secret: premint.secret, c: unblinded_sig, }; diff --git a/crates/cashu/src/dhke.rs b/crates/cashu/src/dhke.rs index c76b4a2a..dc196300 100644 --- a/crates/cashu/src/dhke.rs +++ b/crates/cashu/src/dhke.rs @@ -106,7 +106,7 @@ mod wallet { let proof = Proof { keyset_id: promise.keyset_id, amount: promise.amount, - secret: secrets[i].clone().to_string(), + secret: secrets[i].clone(), c: unblinded_signature, }; diff --git a/crates/cashu/src/nuts/nut00.rs b/crates/cashu/src/nuts/nut00.rs index 95ed1aee..813dcdc8 100644 --- a/crates/cashu/src/nuts/nut00.rs +++ b/crates/cashu/src/nuts/nut00.rs @@ -9,6 +9,7 @@ use serde::{Deserialize, Serialize}; use super::{Id, Proofs, PublicKey}; use crate::error::Error; +use crate::secret::Secret; use crate::url::UncheckedUrl; use crate::Amount; @@ -431,7 +432,7 @@ pub struct Proof { #[serde(rename = "id")] pub keyset_id: Id, /// Secret message - pub secret: String, + pub secret: Secret, /// Unblinded signature #[serde(rename = "C")] pub c: PublicKey,