refactor: use secret type in proof

This commit is contained in:
thesimplekid
2024-02-18 11:58:04 +00:00
parent 7f60bea5ec
commit d36048847c
6 changed files with 29 additions and 40 deletions

View File

@@ -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(())

View File

@@ -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(),
)?;
}

View File

@@ -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<Vec<u8>> = 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<Vec<u8>> = 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();

View File

@@ -155,7 +155,7 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
proofs
.clone()
.into_iter()
.flat_map(|p| Secret::from_str(&p.secret))
.map(|p| p.secret)
.collect::<Vec<Secret>>()
.clone(),
)
@@ -446,7 +446,7 @@ impl<C: Client, L: LocalStore> Wallet<C, L> {
let proof = Proof {
keyset_id: promise.keyset_id,
amount: promise.amount,
secret: premint.secret.to_string(),
secret: premint.secret,
c: unblinded_sig,
};

View File

@@ -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,
};

View File

@@ -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,