1use std::collections::HashMap;
4use std::ops::{Deref, DerefMut};
5
6use async_trait::async_trait;
7use cashu::quote_id::QuoteId;
8use cashu::Amount;
9
10use super::{DbTransactionFinalizer, Error};
11use crate::mint::{
12 self, MeltQuote, MintKeySetInfo, MintQuote as MintMintQuote, Operation, ProofsWithState,
13};
14use crate::nuts::{
15 BlindSignature, BlindedMessage, CurrencyUnit, Id, MeltQuoteState, Proof, Proofs, PublicKey,
16 State,
17};
18use crate::payment::PaymentIdentifier;
19
20mod auth;
21
22#[cfg(feature = "test")]
23pub mod test;
24
25pub use auth::{DynMintAuthDatabase, MintAuthDatabase, MintAuthTransaction};
26
27pub use super::kvstore::{
29 validate_kvstore_params, validate_kvstore_string, KVStore, KVStoreDatabase, KVStoreTransaction,
30 KVSTORE_NAMESPACE_KEY_ALPHABET, KVSTORE_NAMESPACE_KEY_MAX_LEN,
31};
32
33#[derive(Debug)]
62pub struct Acquired<T> {
63 inner: T,
64}
65
66impl<T> From<T> for Acquired<T> {
67 fn from(value: T) -> Self {
72 Acquired { inner: value }
73 }
74}
75
76impl<T> Acquired<T> {
77 pub fn inner(self) -> T {
83 self.inner
84 }
85}
86
87impl<T> Deref for Acquired<T> {
88 type Target = T;
89
90 fn deref(&self) -> &Self::Target {
92 &self.inner
93 }
94}
95
96impl<T> DerefMut for Acquired<T> {
97 fn deref_mut(&mut self) -> &mut Self::Target {
99 &mut self.inner
100 }
101}
102
103#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct MeltRequestInfo {
106 pub inputs_amount: Amount<CurrencyUnit>,
108 pub inputs_fee: Amount<CurrencyUnit>,
110 pub change_outputs: Vec<BlindedMessage>,
112}
113
114#[derive(Debug)]
119pub struct LockedMeltQuotes {
120 pub target: Option<Acquired<MeltQuote>>,
122 pub all_related: Vec<Acquired<MeltQuote>>,
124}
125
126#[async_trait]
128pub trait KeysDatabaseTransaction<'a, Error>: DbTransactionFinalizer<Err = Error> {
129 async fn set_active_keyset(&mut self, unit: CurrencyUnit, id: Id) -> Result<(), Error>;
131
132 async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error>;
134}
135
136#[async_trait]
138pub trait KeysDatabase {
139 type Err: Into<Error> + From<Error>;
141
142 async fn begin_transaction<'a>(
144 &'a self,
145 ) -> Result<Box<dyn KeysDatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Error>;
146
147 async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
149
150 async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err>;
152
153 async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
155
156 async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
158}
159
160#[async_trait]
162pub trait QuotesTransaction {
163 type Err: Into<Error> + From<Error>;
165
166 async fn add_melt_request(
168 &mut self,
169 quote_id: &QuoteId,
170 inputs_amount: Amount<CurrencyUnit>,
171 inputs_fee: Amount<CurrencyUnit>,
172 ) -> Result<(), Self::Err>;
173
174 async fn add_blinded_messages(
176 &mut self,
177 quote_id: Option<&QuoteId>,
178 blinded_messages: &[BlindedMessage],
179 operation: &Operation,
180 ) -> Result<(), Self::Err>;
181
182 async fn delete_blinded_messages(
184 &mut self,
185 blinded_secrets: &[PublicKey],
186 ) -> Result<(), Self::Err>;
187
188 async fn get_melt_request_and_blinded_messages(
190 &mut self,
191 quote_id: &QuoteId,
192 ) -> Result<Option<MeltRequestInfo>, Self::Err>;
193
194 async fn delete_melt_request(&mut self, quote_id: &QuoteId) -> Result<(), Self::Err>;
196
197 async fn get_mint_quote(
199 &mut self,
200 quote_id: &QuoteId,
201 ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
202
203 async fn add_mint_quote(
205 &mut self,
206 quote: MintMintQuote,
207 ) -> Result<Acquired<MintMintQuote>, Self::Err>;
208
209 async fn update_mint_quote(
229 &mut self,
230 quote: &mut Acquired<mint::MintQuote>,
231 ) -> Result<(), Self::Err>;
232
233 async fn get_melt_quote(
235 &mut self,
236 quote_id: &QuoteId,
237 ) -> Result<Option<Acquired<mint::MeltQuote>>, Self::Err>;
238
239 async fn add_melt_quote(&mut self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
241
242 async fn get_melt_quotes_by_request_lookup_id(
258 &mut self,
259 request_lookup_id: &PaymentIdentifier,
260 ) -> Result<Vec<Acquired<MeltQuote>>, Self::Err>;
261
262 async fn lock_melt_quote_and_related(
288 &mut self,
289 quote_id: &QuoteId,
290 ) -> Result<LockedMeltQuotes, Self::Err>;
291
292 async fn update_melt_quote_request_lookup_id(
296 &mut self,
297 quote: &mut Acquired<mint::MeltQuote>,
298 new_request_lookup_id: &PaymentIdentifier,
299 ) -> Result<(), Self::Err>;
300
301 async fn update_melt_quote_state(
306 &mut self,
307 quote: &mut Acquired<mint::MeltQuote>,
308 new_state: MeltQuoteState,
309 payment_proof: Option<String>,
310 ) -> Result<MeltQuoteState, Self::Err>;
311
312 async fn get_mint_quote_by_request(
314 &mut self,
315 request: &str,
316 ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
317
318 async fn get_mint_quote_by_request_lookup_id(
320 &mut self,
321 request_lookup_id: &PaymentIdentifier,
322 ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
323}
324
325#[async_trait]
327pub trait QuotesDatabase {
328 type Err: Into<Error> + From<Error>;
330
331 async fn get_mint_quote(&self, quote_id: &QuoteId) -> Result<Option<MintMintQuote>, Self::Err>;
333
334 async fn get_mint_quote_by_request(
336 &self,
337 request: &str,
338 ) -> Result<Option<MintMintQuote>, Self::Err>;
339 async fn get_mint_quote_by_request_lookup_id(
341 &self,
342 request_lookup_id: &PaymentIdentifier,
343 ) -> Result<Option<MintMintQuote>, Self::Err>;
344 async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
346 async fn get_melt_quote(
348 &self,
349 quote_id: &QuoteId,
350 ) -> Result<Option<mint::MeltQuote>, Self::Err>;
351 async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
353}
354
355#[async_trait]
357pub trait ProofsTransaction {
358 type Err: Into<Error> + From<Error>;
360
361 async fn add_proofs(
366 &mut self,
367 proof: Proofs,
368 quote_id: Option<QuoteId>,
369 operation: &Operation,
370 ) -> Result<Acquired<ProofsWithState>, Self::Err>;
371
372 async fn update_proofs_state(
377 &mut self,
378 proofs: &mut Acquired<ProofsWithState>,
379 new_state: State,
380 ) -> Result<(), Self::Err>;
381
382 async fn get_proofs(
384 &mut self,
385 ys: &[PublicKey],
386 ) -> Result<Acquired<ProofsWithState>, Self::Err>;
387
388 async fn remove_proofs(
390 &mut self,
391 ys: &[PublicKey],
392 quote_id: Option<QuoteId>,
393 ) -> Result<(), Self::Err>;
394
395 async fn get_proof_ys_by_quote_id(
397 &mut self,
398 quote_id: &QuoteId,
399 ) -> Result<Vec<PublicKey>, Self::Err>;
400
401 async fn get_proof_ys_by_operation_id(
403 &mut self,
404 operation_id: &uuid::Uuid,
405 ) -> Result<Vec<PublicKey>, Self::Err>;
406}
407
408#[async_trait]
410pub trait ProofsDatabase {
411 type Err: Into<Error> + From<Error>;
413
414 async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
416 async fn get_proof_ys_by_quote_id(
418 &self,
419 quote_id: &QuoteId,
420 ) -> Result<Vec<PublicKey>, Self::Err>;
421 async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
423
424 async fn get_proofs_by_keyset_id(
426 &self,
427 keyset_id: &Id,
428 ) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
429
430 async fn get_total_redeemed(&self) -> Result<HashMap<Id, Amount>, Self::Err>;
432
433 async fn get_proof_ys_by_operation_id(
435 &self,
436 operation_id: &uuid::Uuid,
437 ) -> Result<Vec<PublicKey>, Self::Err>;
438}
439
440#[async_trait]
441pub trait SignaturesTransaction {
443 type Err: Into<Error> + From<Error>;
445
446 async fn add_blind_signatures(
448 &mut self,
449 blinded_messages: &[PublicKey],
450 blind_signatures: &[BlindSignature],
451 quote_id: Option<QuoteId>,
452 ) -> Result<(), Self::Err>;
453
454 async fn get_blind_signatures(
456 &mut self,
457 blinded_messages: &[PublicKey],
458 ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
459}
460
461#[async_trait]
462pub trait SignaturesDatabase {
464 type Err: Into<Error> + From<Error>;
466
467 async fn get_blind_signatures(
469 &self,
470 blinded_messages: &[PublicKey],
471 ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
472
473 async fn get_blind_signatures_for_keyset(
475 &self,
476 keyset_id: &Id,
477 ) -> Result<Vec<BlindSignature>, Self::Err>;
478
479 async fn get_blind_signatures_for_quote(
481 &self,
482 quote_id: &QuoteId,
483 ) -> Result<Vec<BlindSignature>, Self::Err>;
484
485 async fn get_total_issued(&self) -> Result<HashMap<Id, Amount>, Self::Err>;
487
488 async fn get_blinded_secrets_by_operation_id(
490 &self,
491 operation_id: &uuid::Uuid,
492 ) -> Result<Vec<PublicKey>, Self::Err>;
493}
494
495#[async_trait]
496pub trait SagaTransaction {
498 type Err: Into<Error> + From<Error>;
500
501 async fn get_saga(
503 &mut self,
504 operation_id: &uuid::Uuid,
505 ) -> Result<Option<mint::Saga>, Self::Err>;
506
507 async fn add_saga(&mut self, saga: &mint::Saga) -> Result<(), Self::Err>;
509
510 async fn update_saga(
512 &mut self,
513 operation_id: &uuid::Uuid,
514 new_state: mint::SagaStateEnum,
515 ) -> Result<(), Self::Err>;
516
517 async fn delete_saga(&mut self, operation_id: &uuid::Uuid) -> Result<(), Self::Err>;
519}
520
521#[async_trait]
522pub trait SagaDatabase {
524 type Err: Into<Error> + From<Error>;
526
527 async fn get_incomplete_sagas(
529 &self,
530 operation_kind: mint::OperationKind,
531 ) -> Result<Vec<mint::Saga>, Self::Err>;
532}
533
534#[async_trait]
535pub trait CompletedOperationsTransaction {
537 type Err: Into<Error> + From<Error>;
539
540 async fn add_completed_operation(
542 &mut self,
543 operation: &mint::Operation,
544 fee_by_keyset: &std::collections::HashMap<crate::nuts::Id, crate::Amount>,
545 ) -> Result<(), Self::Err>;
546}
547
548#[async_trait]
549pub trait CompletedOperationsDatabase {
551 type Err: Into<Error> + From<Error>;
553
554 async fn get_completed_operation(
556 &self,
557 operation_id: &uuid::Uuid,
558 ) -> Result<Option<mint::Operation>, Self::Err>;
559
560 async fn get_completed_operations_by_kind(
562 &self,
563 operation_kind: mint::OperationKind,
564 ) -> Result<Vec<mint::Operation>, Self::Err>;
565
566 async fn get_completed_operations(&self) -> Result<Vec<mint::Operation>, Self::Err>;
568}
569
570pub trait Transaction<Error>:
572 DbTransactionFinalizer<Err = Error>
573 + QuotesTransaction<Err = Error>
574 + SignaturesTransaction<Err = Error>
575 + ProofsTransaction<Err = Error>
576 + KVStoreTransaction<Error>
577 + SagaTransaction<Err = Error>
578 + CompletedOperationsTransaction<Err = Error>
579{
580}
581
582#[async_trait]
584pub trait Database<Error>:
585 KVStoreDatabase<Err = Error>
586 + QuotesDatabase<Err = Error>
587 + ProofsDatabase<Err = Error>
588 + SignaturesDatabase<Err = Error>
589 + SagaDatabase<Err = Error>
590 + CompletedOperationsDatabase<Err = Error>
591{
592 async fn begin_transaction(&self) -> Result<Box<dyn Transaction<Error> + Send + Sync>, Error>;
594}
595
596pub type DynMintDatabase = std::sync::Arc<dyn Database<Error> + Send + Sync>;
598
599pub type DynMintTransaction = Box<dyn Transaction<Error> + Send + Sync>;