mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-19 21:55:03 +01:00
refactor: remove get single blinded sig
This commit is contained in:
@@ -647,27 +647,9 @@ impl MintDatabase for MintRedbDatabase {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_blinded_signature(
|
||||
&self,
|
||||
blinded_message: &PublicKey,
|
||||
) -> Result<Option<BlindSignature>, Self::Err> {
|
||||
let db = self.db.lock().await;
|
||||
let read_txn = db.begin_read().map_err(Error::from)?;
|
||||
let table = read_txn
|
||||
.open_table(BLINDED_SIGNATURES)
|
||||
.map_err(Error::from)?;
|
||||
|
||||
match table.get(blinded_message.to_bytes()).map_err(Error::from)? {
|
||||
Some(blind_signature) => {
|
||||
Ok(serde_json::from_str(blind_signature.value()).map_err(Error::from)?)
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
async fn get_blinded_signatures(
|
||||
&self,
|
||||
blinded_messages: Vec<PublicKey>,
|
||||
blinded_messages: &[PublicKey],
|
||||
) -> Result<Vec<Option<BlindSignature>>, Self::Err> {
|
||||
let db = self.db.lock().await;
|
||||
let read_txn = db.begin_read().map_err(Error::from)?;
|
||||
|
||||
@@ -657,34 +657,9 @@ VALUES (?, ?, ?, ?);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
async fn get_blinded_signature(
|
||||
&self,
|
||||
blinded_message: &PublicKey,
|
||||
) -> Result<Option<BlindSignature>, Self::Err> {
|
||||
let rec = sqlx::query(
|
||||
r#"
|
||||
SELECT *
|
||||
FROM blind_signature
|
||||
WHERE y=?;
|
||||
"#,
|
||||
)
|
||||
.bind(blinded_message.to_bytes().to_vec())
|
||||
.fetch_one(&self.pool)
|
||||
.await;
|
||||
|
||||
let rec = match rec {
|
||||
Ok(rec) => rec,
|
||||
Err(err) => match err {
|
||||
sqlx::Error::RowNotFound => return Ok(None),
|
||||
_ => return Err(Error::SQLX(err).into()),
|
||||
},
|
||||
};
|
||||
|
||||
Ok(Some(sqlite_row_to_blind_signature(rec)?))
|
||||
}
|
||||
async fn get_blinded_signatures(
|
||||
&self,
|
||||
blinded_messages: Vec<PublicKey>,
|
||||
blinded_messages: &[PublicKey],
|
||||
) -> Result<Vec<Option<BlindSignature>>, Self::Err> {
|
||||
let mut signatures = Vec::with_capacity(blinded_messages.len());
|
||||
for message in blinded_messages {
|
||||
|
||||
@@ -287,21 +287,9 @@ impl MintDatabase for MintMemoryDatabase {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_blinded_signature(
|
||||
&self,
|
||||
blinded_message: &PublicKey,
|
||||
) -> Result<Option<BlindSignature>, Self::Err> {
|
||||
Ok(self
|
||||
.blinded_signatures
|
||||
.read()
|
||||
.await
|
||||
.get(&blinded_message.to_bytes())
|
||||
.cloned())
|
||||
}
|
||||
|
||||
async fn get_blinded_signatures(
|
||||
&self,
|
||||
blinded_messages: Vec<PublicKey>,
|
||||
blinded_messages: &[PublicKey],
|
||||
) -> Result<Vec<Option<BlindSignature>>, Self::Err> {
|
||||
let mut signatures = Vec::with_capacity(blinded_messages.len());
|
||||
|
||||
|
||||
@@ -232,14 +232,9 @@ pub trait MintDatabase {
|
||||
blinded_messages: &[PublicKey],
|
||||
blind_signatures: &[BlindSignature],
|
||||
) -> Result<(), Self::Err>;
|
||||
/// Get [`BlindSignature`]
|
||||
async fn get_blinded_signature(
|
||||
&self,
|
||||
blinded_message: &PublicKey,
|
||||
) -> Result<Option<BlindSignature>, Self::Err>;
|
||||
/// Get [`BlindSignature`]s
|
||||
async fn get_blinded_signatures(
|
||||
&self,
|
||||
blinded_messages: Vec<PublicKey>,
|
||||
blinded_messages: &[PublicKey],
|
||||
) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
|
||||
}
|
||||
|
||||
@@ -470,27 +470,31 @@ impl Mint {
|
||||
MintQuoteState::Paid => (),
|
||||
}
|
||||
|
||||
for blinded_message in &mint_request.outputs {
|
||||
if self
|
||||
.localstore
|
||||
.get_blinded_signature(&blinded_message.blinded_secret)
|
||||
.await?
|
||||
.is_some()
|
||||
{
|
||||
tracing::info!(
|
||||
"Output has already been signed: {}",
|
||||
blinded_message.blinded_secret
|
||||
);
|
||||
tracing::info!(
|
||||
"Mint {} did not succeed returning quote to Paid state",
|
||||
mint_request.quote
|
||||
);
|
||||
let blinded_messages: Vec<PublicKey> = mint_request
|
||||
.outputs
|
||||
.iter()
|
||||
.map(|b| b.blinded_secret)
|
||||
.collect();
|
||||
|
||||
self.localstore
|
||||
.update_mint_quote_state(&mint_request.quote, MintQuoteState::Paid)
|
||||
.await?;
|
||||
return Err(Error::BlindedMessageAlreadySigned);
|
||||
}
|
||||
if self
|
||||
.localstore
|
||||
.get_blinded_signatures(&blinded_messages)
|
||||
.await?
|
||||
.iter()
|
||||
.flatten()
|
||||
.next()
|
||||
.is_some()
|
||||
{
|
||||
tracing::info!("Output has already been signed",);
|
||||
tracing::info!(
|
||||
"Mint {} did not succeed returning quote to Paid state",
|
||||
mint_request.quote
|
||||
);
|
||||
|
||||
self.localstore
|
||||
.update_mint_quote_state(&mint_request.quote, MintQuoteState::Paid)
|
||||
.await?;
|
||||
return Err(Error::BlindedMessageAlreadySigned);
|
||||
}
|
||||
|
||||
let mut blind_signatures = Vec::with_capacity(mint_request.outputs.len());
|
||||
@@ -577,19 +581,24 @@ impl Mint {
|
||||
&self,
|
||||
swap_request: SwapRequest,
|
||||
) -> Result<SwapResponse, Error> {
|
||||
for blinded_message in &swap_request.outputs {
|
||||
if self
|
||||
.localstore
|
||||
.get_blinded_signature(&blinded_message.blinded_secret)
|
||||
.await?
|
||||
.is_some()
|
||||
{
|
||||
tracing::error!(
|
||||
"Output has already been signed: {}",
|
||||
blinded_message.blinded_secret
|
||||
);
|
||||
return Err(Error::BlindedMessageAlreadySigned);
|
||||
}
|
||||
let blinded_messages: Vec<PublicKey> = swap_request
|
||||
.outputs
|
||||
.iter()
|
||||
.map(|b| b.blinded_secret)
|
||||
.collect();
|
||||
|
||||
if self
|
||||
.localstore
|
||||
.get_blinded_signatures(&blinded_messages)
|
||||
.await?
|
||||
.iter()
|
||||
.flatten()
|
||||
.next()
|
||||
.is_some()
|
||||
{
|
||||
tracing::info!("Output has already been signed",);
|
||||
|
||||
return Err(Error::BlindedMessageAlreadySigned);
|
||||
}
|
||||
|
||||
let proofs_total = swap_request.input_amount();
|
||||
@@ -959,19 +968,21 @@ impl Mint {
|
||||
.ok_or(Error::UnknownQuote)?;
|
||||
|
||||
if let Some(outputs) = &melt_request.outputs {
|
||||
for blinded_message in outputs {
|
||||
if self
|
||||
.localstore
|
||||
.get_blinded_signature(&blinded_message.blinded_secret)
|
||||
.await?
|
||||
.is_some()
|
||||
{
|
||||
tracing::error!(
|
||||
"Output has already been signed: {}",
|
||||
blinded_message.blinded_secret
|
||||
);
|
||||
return Err(Error::BlindedMessageAlreadySigned);
|
||||
}
|
||||
let blinded_messages: Vec<PublicKey> =
|
||||
outputs.iter().map(|b| b.blinded_secret).collect();
|
||||
|
||||
if self
|
||||
.localstore
|
||||
.get_blinded_signatures(&blinded_messages)
|
||||
.await?
|
||||
.iter()
|
||||
.flatten()
|
||||
.next()
|
||||
.is_some()
|
||||
{
|
||||
tracing::info!("Output has already been signed",);
|
||||
|
||||
return Err(Error::BlindedMessageAlreadySigned);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1061,7 +1072,7 @@ impl Mint {
|
||||
|
||||
let blinded_signatures = self
|
||||
.localstore
|
||||
.get_blinded_signatures(blinded_message)
|
||||
.get_blinded_signatures(&blinded_message)
|
||||
.await?;
|
||||
|
||||
assert_eq!(blinded_signatures.len(), output_len);
|
||||
|
||||
Reference in New Issue
Block a user