Skip to content

Commit

Permalink
Proofs trait: consolidate listing of pks
Browse files Browse the repository at this point in the history
  • Loading branch information
ok300 committed Oct 16, 2024
1 parent ee2b79f commit c481f78
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 27 deletions.
6 changes: 2 additions & 4 deletions crates/cdk-redb/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use async_trait::async_trait;
use cdk::cdk_database::MintDatabase;
use cdk::dhke::hash_to_curve;
use cdk::mint::{MintKeySetInfo, MintQuote};
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::{
BlindSignature, CurrencyUnit, Id, MeltBolt11Request, MeltQuoteState, MintQuoteState, Proof,
Proofs, PublicKey, State,
Expand Down Expand Up @@ -603,10 +604,7 @@ impl MintDatabase for MintRedbDatabase {
.filter(|p| &p.keyset_id == keyset_id)
.collect::<Proofs>();

let proof_ys = proofs_for_id
.iter()
.map(|p| p.y())
.collect::<Result<Vec<PublicKey>, _>>()?;
let proof_ys = proofs_for_id.ys()?;

assert_eq!(proofs_for_id.len(), proof_ys.len());

Expand Down
6 changes: 2 additions & 4 deletions crates/cdk-sqlite/src/mint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use bitcoin::bip32::DerivationPath;
use cdk::cdk_database::{self, MintDatabase};
use cdk::mint::{MintKeySetInfo, MintQuote};
use cdk::mint_url::MintUrl;
use cdk::nuts::nut00::ProofsMethods;
use cdk::nuts::nut05::QuoteState;
use cdk::nuts::{
BlindSignature, BlindSignatureDleq, CurrencyUnit, Id, MeltBolt11Request, MeltQuoteState,
Expand Down Expand Up @@ -838,10 +839,7 @@ WHERE quote_id=?;
.map(sqlite_row_to_proof)
.collect::<Result<Vec<Proof>, _>>()?;

proofs
.iter()
.map(|p| p.y())
.collect::<Result<Vec<PublicKey>, _>>()?
proofs.ys()?
}
Err(err) => match err {
sqlx::Error::RowNotFound => {
Expand Down
6 changes: 2 additions & 4 deletions crates/cdk/src/cdk_database/mint_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use tokio::sync::{Mutex, RwLock};
use super::{Error, MintDatabase};
use crate::dhke::hash_to_curve;
use crate::mint::{self, MintKeySetInfo, MintQuote};
use crate::nuts::nut00::ProofsMethods;
use crate::nuts::nut07::State;
use crate::nuts::{
nut07, BlindSignature, CurrencyUnit, Id, MeltBolt11Request, MeltQuoteState, MintQuoteState,
Expand Down Expand Up @@ -346,10 +347,7 @@ impl MintDatabase for MintMemoryDatabase {
.cloned()
.collect();

let proof_ys = proofs_for_id
.iter()
.map(|p| p.y())
.collect::<Result<Vec<PublicKey>, _>>()?;
let proof_ys = proofs_for_id.ys()?;

assert_eq!(proofs_for_id.len(), proof_ys.len());

Expand Down
10 changes: 10 additions & 0 deletions crates/cdk/src/nuts/nut00/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,22 @@ pub type Proofs = Vec<Proof>;
pub trait ProofsMethods {
/// Try to sum up the amounts of all [Proof]s
fn total_amount(&self) -> Result<Amount, Error>;

/// Try to fetch the pubkeys of all [Proof]s
fn ys(&self) -> Result<Vec<PublicKey>, Error>;
}

impl ProofsMethods for Proofs {
fn total_amount(&self) -> Result<Amount, Error> {
Amount::try_sum(self.iter().map(|p| p.amount)).map_err(Into::into)
}

fn ys(&self) -> Result<Vec<PublicKey>, Error> {
self.iter()
.map(|p| p.y())
.collect::<Result<Vec<PublicKey>, _>>()
.map_err(Into::into)
}
}

/// NUT00 Error
Expand Down
12 changes: 3 additions & 9 deletions crates/cdk/src/wallet/melt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use tracing::instrument;
use crate::nuts::nut00::ProofsMethods;
use crate::{
dhke::construct_proofs,
nuts::{CurrencyUnit, MeltQuoteBolt11Response, PreMintSecrets, Proofs, PublicKey, State},
nuts::{CurrencyUnit, MeltQuoteBolt11Response, PreMintSecrets, Proofs, State},
types::{Melted, ProofInfo},
util::unix_time,
Amount, Error, Wallet,
Expand Down Expand Up @@ -127,10 +127,7 @@ impl Wallet {
return Err(Error::InsufficientFunds);
}

let ys = proofs
.iter()
.map(|p| p.y())
.collect::<Result<Vec<PublicKey>, _>>()?;
let ys = proofs.ys()?;
self.localstore.set_pending_proofs(ys).await?;

let active_keyset_id = self.get_active_mint_keyset().await?.id;
Expand Down Expand Up @@ -239,10 +236,7 @@ impl Wallet {

self.localstore.remove_melt_quote(&quote_info.id).await?;

let deleted_ys = proofs
.iter()
.map(|p| p.y())
.collect::<Result<Vec<PublicKey>, _>>()?;
let deleted_ys = proofs.ys()?;
self.localstore
.update_proofs(change_proof_infos, deleted_ys)
.await?;
Expand Down
7 changes: 2 additions & 5 deletions crates/cdk/src/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use tracing::instrument;
use crate::nuts::nut00::ProofsMethods;
use crate::{
amount::SplitTarget,
nuts::{Proofs, PublicKey, SpendingConditions, State, Token},
nuts::{Proofs, SpendingConditions, State, Token},
Amount, Error, Wallet,
};

Expand All @@ -13,10 +13,7 @@ impl Wallet {
/// Send specific proofs
#[instrument(skip(self))]
pub async fn send_proofs(&self, memo: Option<String>, proofs: Proofs) -> Result<Token, Error> {
let ys = proofs
.iter()
.map(|p| p.y())
.collect::<Result<Vec<PublicKey>, _>>()?;
let ys = proofs.ys()?;
self.localstore.reserve_proofs(ys).await?;

Ok(Token::new(
Expand Down
2 changes: 1 addition & 1 deletion crates/cdk/src/wallet/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ impl Wallet {
// Desired amount is either amount passed or value of all proof
let proofs_total = proofs.total_amount()?;

let ys: Vec<PublicKey> = proofs.iter().map(|p| p.y()).collect::<Result<_, _>>()?;
let ys: Vec<PublicKey> = proofs.ys()?;
self.localstore.set_pending_proofs(ys).await?;

let fee = self.get_proofs_fee(&proofs).await?;
Expand Down

0 comments on commit c481f78

Please sign in to comment.