mirror of
https://github.com/aljazceru/cdk.git
synced 2025-12-20 14:14:49 +01:00
refactor: add_blind_signature to add_blind_signatures
This commit is contained in:
@@ -27,8 +27,10 @@
|
|||||||
### Summary
|
### Summary
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- cdk(wallet): `fn send` returns `Token` so the user can use the struct of convert it to a v3 or v4 string.
|
- cdk(wallet): `fn send` returns `Token` so the user can use the struct of convert it to a v3 or v4 string ([thesimplekid]).
|
||||||
- cdk(wallet): Publicly export `MultiMintWallet` ([thesimplekid]).
|
- cdk(wallet): Publicly export `MultiMintWallet` ([thesimplekid]).
|
||||||
|
- cdk-database: Get `pending` and `spent` `proofs` by `ys` or `secrets` instead of a single proofs ([thesimplekid]).
|
||||||
|
- cdk-database: Change `add_blind_signature` to `add_blind_signatures` ([thesimplekid]).
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
- cdk(NUT-11): Add `Copy` on `SigFlag` ([thesimplekid]).
|
- cdk(NUT-11): Add `Copy` on `SigFlag` ([thesimplekid]).
|
||||||
|
|||||||
@@ -660,10 +660,10 @@ impl MintDatabase for MintRedbDatabase {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_blinded_signature(
|
async fn add_blind_signatures(
|
||||||
&self,
|
&self,
|
||||||
blinded_message: PublicKey,
|
blinded_messages: &[PublicKey],
|
||||||
blinded_signature: BlindSignature,
|
blind_signatures: &[BlindSignature],
|
||||||
) -> Result<(), Self::Err> {
|
) -> Result<(), Self::Err> {
|
||||||
let db = self.db.lock().await;
|
let db = self.db.lock().await;
|
||||||
let write_txn = db.begin_write().map_err(Error::from)?;
|
let write_txn = db.begin_write().map_err(Error::from)?;
|
||||||
@@ -672,15 +672,19 @@ impl MintDatabase for MintRedbDatabase {
|
|||||||
let mut table = write_txn
|
let mut table = write_txn
|
||||||
.open_table(BLINDED_SIGNATURES)
|
.open_table(BLINDED_SIGNATURES)
|
||||||
.map_err(Error::from)?;
|
.map_err(Error::from)?;
|
||||||
|
|
||||||
|
for (blinded_message, blind_signature) in blinded_messages.iter().zip(blind_signatures)
|
||||||
|
{
|
||||||
table
|
table
|
||||||
.insert(
|
.insert(
|
||||||
blinded_message.to_bytes(),
|
blinded_message.to_bytes(),
|
||||||
serde_json::to_string(&blinded_signature)
|
serde_json::to_string(&blind_signature)
|
||||||
.map_err(Error::from)?
|
.map_err(Error::from)?
|
||||||
.as_str(),
|
.as_str(),
|
||||||
)
|
)
|
||||||
.map_err(Error::from)?;
|
.map_err(Error::from)?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
write_txn.commit().map_err(Error::from)?;
|
write_txn.commit().map_err(Error::from)?;
|
||||||
|
|
||||||
|
|||||||
@@ -689,11 +689,13 @@ AND state="PENDING";
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_blinded_signature(
|
async fn add_blind_signatures(
|
||||||
&self,
|
&self,
|
||||||
blinded_message: PublicKey,
|
blinded_messages: &[PublicKey],
|
||||||
blinded_signature: BlindSignature,
|
blinded_signatures: &[BlindSignature],
|
||||||
) -> Result<(), Self::Err> {
|
) -> Result<(), Self::Err> {
|
||||||
|
let mut transaction = self.pool.begin().await.map_err(Error::from)?;
|
||||||
|
for (message, signature) in blinded_messages.iter().zip(blinded_signatures) {
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO blind_signature
|
INSERT INTO blind_signature
|
||||||
@@ -701,13 +703,16 @@ INSERT INTO blind_signature
|
|||||||
VALUES (?, ?, ?, ?);
|
VALUES (?, ?, ?, ?);
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.bind(blinded_message.to_bytes().to_vec())
|
.bind(message.to_bytes().to_vec())
|
||||||
.bind(u64::from(blinded_signature.amount) as i64)
|
.bind(u64::from(signature.amount) as i64)
|
||||||
.bind(blinded_signature.keyset_id.to_string())
|
.bind(signature.keyset_id.to_string())
|
||||||
.bind(blinded_signature.c.to_bytes().to_vec())
|
.bind(signature.c.to_bytes().to_vec())
|
||||||
.execute(&self.pool)
|
.execute(&mut transaction)
|
||||||
.await
|
.await
|
||||||
.map_err(Error::from)?;
|
.map_err(Error::from)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
transaction.commit().await.map_err(Error::from)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -315,15 +315,17 @@ impl MintDatabase for MintMemoryDatabase {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn add_blinded_signature(
|
async fn add_blind_signatures(
|
||||||
&self,
|
&self,
|
||||||
blinded_message: PublicKey,
|
blinded_message: &[PublicKey],
|
||||||
blinded_signature: BlindSignature,
|
blind_signatures: &[BlindSignature],
|
||||||
) -> Result<(), Self::Err> {
|
) -> Result<(), Self::Err> {
|
||||||
self.blinded_signatures
|
let mut current_blinded_signatures = self.blinded_signatures.write().await;
|
||||||
.write()
|
|
||||||
.await
|
for (blinded_message, blind_signature) in blinded_message.iter().zip(blind_signatures) {
|
||||||
.insert(blinded_message.to_bytes(), blinded_signature);
|
current_blinded_signatures.insert(blinded_message.to_bytes(), blind_signature.clone());
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -246,10 +246,10 @@ pub trait MintDatabase {
|
|||||||
async fn remove_pending_proofs(&self, secret: Vec<&Secret>) -> Result<(), Self::Err>;
|
async fn remove_pending_proofs(&self, secret: Vec<&Secret>) -> Result<(), Self::Err>;
|
||||||
|
|
||||||
/// Add [`BlindSignature`]
|
/// Add [`BlindSignature`]
|
||||||
async fn add_blinded_signature(
|
async fn add_blind_signatures(
|
||||||
&self,
|
&self,
|
||||||
blinded_message: PublicKey,
|
blinded_messages: &[PublicKey],
|
||||||
blinded_signature: BlindSignature,
|
blind_signatures: &[BlindSignature],
|
||||||
) -> Result<(), Self::Err>;
|
) -> Result<(), Self::Err>;
|
||||||
/// Get [`BlindSignature`]
|
/// Get [`BlindSignature`]
|
||||||
async fn get_blinded_signature(
|
async fn get_blinded_signature(
|
||||||
|
|||||||
@@ -495,14 +495,22 @@ impl Mint {
|
|||||||
|
|
||||||
let mut blind_signatures = Vec::with_capacity(mint_request.outputs.len());
|
let mut blind_signatures = Vec::with_capacity(mint_request.outputs.len());
|
||||||
|
|
||||||
for blinded_message in mint_request.outputs.into_iter() {
|
for blinded_message in mint_request.outputs.iter() {
|
||||||
let blinded_signature = self.blind_sign(&blinded_message).await?;
|
let blind_signature = self.blind_sign(blinded_message).await?;
|
||||||
self.localstore
|
blind_signatures.push(blind_signature);
|
||||||
.add_blinded_signature(blinded_message.blinded_secret, blinded_signature.clone())
|
|
||||||
.await?;
|
|
||||||
blind_signatures.push(blinded_signature);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.localstore
|
||||||
|
.add_blind_signatures(
|
||||||
|
&mint_request
|
||||||
|
.outputs
|
||||||
|
.iter()
|
||||||
|
.map(|p| p.blinded_secret)
|
||||||
|
.collect::<Vec<PublicKey>>(),
|
||||||
|
&blind_signatures,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
self.localstore
|
self.localstore
|
||||||
.update_mint_quote_state(&mint_request.quote, MintQuoteState::Issued)
|
.update_mint_quote_state(&mint_request.quote, MintQuoteState::Issued)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -704,14 +712,22 @@ impl Mint {
|
|||||||
|
|
||||||
let mut promises = Vec::with_capacity(swap_request.outputs.len());
|
let mut promises = Vec::with_capacity(swap_request.outputs.len());
|
||||||
|
|
||||||
for blinded_message in swap_request.outputs {
|
for blinded_message in swap_request.outputs.iter() {
|
||||||
let blinded_signature = self.blind_sign(&blinded_message).await?;
|
let blinded_signature = self.blind_sign(blinded_message).await?;
|
||||||
self.localstore
|
|
||||||
.add_blinded_signature(blinded_message.blinded_secret, blinded_signature.clone())
|
|
||||||
.await?;
|
|
||||||
promises.push(blinded_signature);
|
promises.push(blinded_signature);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.localstore
|
||||||
|
.add_blind_signatures(
|
||||||
|
&swap_request
|
||||||
|
.outputs
|
||||||
|
.iter()
|
||||||
|
.map(|o| o.blinded_secret)
|
||||||
|
.collect::<Vec<PublicKey>>(),
|
||||||
|
&promises,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(SwapResponse::new(promises))
|
Ok(SwapResponse::new(promises))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1008,20 +1024,25 @@ impl Mint {
|
|||||||
amounts.sort_by(|a, b| b.cmp(a));
|
amounts.sort_by(|a, b| b.cmp(a));
|
||||||
}
|
}
|
||||||
|
|
||||||
for (amount, blinded_message) in amounts.iter().zip(outputs) {
|
let mut outputs = outputs;
|
||||||
let mut blinded_message = blinded_message;
|
|
||||||
|
for (amount, blinded_message) in amounts.iter().zip(&mut outputs) {
|
||||||
blinded_message.amount = *amount;
|
blinded_message.amount = *amount;
|
||||||
|
|
||||||
let blinded_signature = self.blind_sign(&blinded_message).await?;
|
let blinded_signature = self.blind_sign(blinded_message).await?;
|
||||||
self.localstore
|
|
||||||
.add_blinded_signature(
|
|
||||||
blinded_message.blinded_secret,
|
|
||||||
blinded_signature.clone(),
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
change_sigs.push(blinded_signature)
|
change_sigs.push(blinded_signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.localstore
|
||||||
|
.add_blind_signatures(
|
||||||
|
&outputs[0..change_sigs.len()]
|
||||||
|
.iter()
|
||||||
|
.map(|o| o.blinded_secret)
|
||||||
|
.collect::<Vec<PublicKey>>(),
|
||||||
|
&change_sigs,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
change = Some(change_sigs);
|
change = Some(change_sigs);
|
||||||
} else {
|
} else {
|
||||||
tracing::info!(
|
tracing::info!(
|
||||||
|
|||||||
Reference in New Issue
Block a user