From 4f3248495ac6c648bd1132522b262bdde733dbe6 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Tue, 16 Jan 2024 22:35:32 +0000 Subject: [PATCH] feat: check mint quote --- crates/cashu-sdk/src/mint/mod.rs | 52 +++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/cashu-sdk/src/mint/mod.rs b/crates/cashu-sdk/src/mint/mod.rs index 51198c18..678c5f55 100644 --- a/crates/cashu-sdk/src/mint/mod.rs +++ b/crates/cashu-sdk/src/mint/mod.rs @@ -42,6 +42,8 @@ pub enum Error { TokenSpent, #[error("Token Pending")] TokenPending, + #[error("Quote not paid")] + UnpaidQuote, #[error("`{0}`")] Custom(String), #[error("`{0}`")] @@ -113,9 +115,23 @@ impl Mint { Ok(quote) } + pub async fn check_mint_quote(&self, quote_id: &str) -> Result { + let quote = self + .localstore + .get_mint_quote(quote_id) + .await? + .ok_or(Error::UnknownQuote)?; + + Ok(MintQuoteBolt11Response { + quote: quote.id, + request: quote.request, + paid: quote.paid, + expiry: quote.expiry, + }) + } + pub async fn update_mint_quote(&self, quote: MintQuote) -> Result<(), Error> { self.localstore.add_mint_quote(quote).await?; - Ok(()) } @@ -229,6 +245,16 @@ impl Mint { &mut self, mint_request: nut04::MintBolt11Request, ) -> Result { + let quote = self + .localstore + .get_mint_quote(&mint_request.quote) + .await? + .ok_or(Error::UnknownQuote)?; + + if !quote.paid { + return Err(Error::UnpaidQuote); + } + let mut blind_signatures = Vec::with_capacity(mint_request.outputs.len()); for blinded_message in mint_request.outputs { @@ -396,13 +422,13 @@ impl Mint { } #[cfg(feature = "nut07")] - pub async fn check_spendable( + pub async fn check_state( &self, - check_spendable: &CheckStateRequest, + check_state: &CheckStateRequest, ) -> Result { - let mut states = Vec::with_capacity(check_spendable.secrets.len()); + let mut states = Vec::with_capacity(check_state.secrets.len()); - for secret in &check_spendable.secrets { + for secret in &check_state.secrets { let state = if self.localstore.get_spent_proof(secret).await?.is_some() { State::Spent } else if self.localstore.get_pending_proof(secret).await?.is_some() { @@ -558,6 +584,22 @@ impl Mint { change, }) } + + pub async fn check_melt_quote(&self, quote_id: &str) -> Result { + let quote = self + .localstore + .get_melt_quote(quote_id) + .await? + .ok_or(Error::UnknownQuote)?; + + Ok(MeltQuoteBolt11Response { + quote: quote.id, + paid: quote.paid, + expiry: quote.expiry, + amount: u64::from(quote.amount), + fee_reserve: u64::from(quote.fee_reserve), + }) + } } #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]