Skip to main content

cdk_common/database/mint/
mod.rs

1//! CDK Database
2
3use 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
27// Re-export KVStore types from shared module for backward compatibility
28pub 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/// A wrapper indicating that a resource has been acquired with a database lock.
34///
35/// This type is returned by database operations that lock rows for update
36/// (e.g., `SELECT ... FOR UPDATE`). It serves as a compile-time marker that
37/// the wrapped resource was properly locked before being returned, ensuring
38/// that subsequent modifications are safe from race conditions.
39///
40/// # Usage
41///
42/// When you need to modify a database record, first acquire it using a locking
43/// query method. The returned `Acquired<T>` guarantees the row is locked for
44/// the duration of the transaction.
45///
46/// ```ignore
47/// // Acquire a quote with a row lock
48/// let mut quote: Acquired<MintQuote> = tx.get_mint_quote_for_update(&quote_id).await?;
49///
50/// // Safely modify the quote (row is locked)
51/// quote.state = QuoteState::Paid;
52///
53/// // Persist the changes
54/// tx.update_mint_quote(&mut quote).await?;
55/// ```
56///
57/// # Deref Behavior
58///
59/// `Acquired<T>` implements `Deref` and `DerefMut`, allowing transparent access
60/// to the inner value's methods and fields.
61#[derive(Debug)]
62pub struct Acquired<T> {
63    inner: T,
64}
65
66impl<T> From<T> for Acquired<T> {
67    /// Wraps a value to indicate it has been acquired with a lock.
68    ///
69    /// This is typically called by database layer implementations after
70    /// executing a locking query.
71    fn from(value: T) -> Self {
72        Acquired { inner: value }
73    }
74}
75
76impl<T> Acquired<T> {
77    /// Consumes the wrapper and returns the inner resource.
78    ///
79    /// Use this when you need to take ownership of the inner value,
80    /// for example when passing it to a function that doesn't accept
81    /// `Acquired<T>`.
82    pub fn inner(self) -> T {
83        self.inner
84    }
85}
86
87impl<T> Deref for Acquired<T> {
88    type Target = T;
89
90    /// Returns a reference to the inner resource.
91    fn deref(&self) -> &Self::Target {
92        &self.inner
93    }
94}
95
96impl<T> DerefMut for Acquired<T> {
97    /// Returns a mutable reference to the inner resource.
98    fn deref_mut(&mut self) -> &mut Self::Target {
99        &mut self.inner
100    }
101}
102
103/// Information about a melt request stored in the database
104#[derive(Debug, Clone, PartialEq, Eq)]
105pub struct MeltRequestInfo {
106    /// Total amount of all input proofs in the melt request
107    pub inputs_amount: Amount<CurrencyUnit>,
108    /// Fee amount associated with the input proofs
109    pub inputs_fee: Amount<CurrencyUnit>,
110    /// Blinded messages for change outputs
111    pub change_outputs: Vec<BlindedMessage>,
112}
113
114/// Result of locking a melt quote and all related quotes atomically.
115///
116/// This struct is returned by [`QuotesTransaction::lock_melt_quote_and_related`]
117/// and contains both the target quote and all quotes sharing the same `request_lookup_id`.
118#[derive(Debug)]
119pub struct LockedMeltQuotes {
120    /// The target quote that was requested, if found
121    pub target: Option<Acquired<MeltQuote>>,
122    /// All quotes sharing the same `request_lookup_id` (including the target)
123    pub all_related: Vec<Acquired<MeltQuote>>,
124}
125
126/// KeysDatabaseWriter
127#[async_trait]
128pub trait KeysDatabaseTransaction<'a, Error>: DbTransactionFinalizer<Err = Error> {
129    /// Add Active Keyset
130    async fn set_active_keyset(&mut self, unit: CurrencyUnit, id: Id) -> Result<(), Error>;
131
132    /// Add [`MintKeySetInfo`]
133    async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error>;
134}
135
136/// Mint Keys Database trait
137#[async_trait]
138pub trait KeysDatabase {
139    /// Mint Keys Database Error
140    type Err: Into<Error> + From<Error>;
141
142    /// Begins a transaction
143    async fn begin_transaction<'a>(
144        &'a self,
145    ) -> Result<Box<dyn KeysDatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Error>;
146
147    /// Get Active Keyset
148    async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
149
150    /// Get all Active Keyset
151    async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err>;
152
153    /// Get [`MintKeySetInfo`]
154    async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
155
156    /// Get [`MintKeySetInfo`]s
157    async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
158}
159
160/// Mint Quote Database writer trait
161#[async_trait]
162pub trait QuotesTransaction {
163    /// Mint Quotes Database Error
164    type Err: Into<Error> + From<Error>;
165
166    /// Add melt_request with quote_id, inputs_amount, and inputs_fee
167    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    /// Add blinded_messages for a quote_id
175    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    /// Delete blinded_messages by their blinded secrets
183    async fn delete_blinded_messages(
184        &mut self,
185        blinded_secrets: &[PublicKey],
186    ) -> Result<(), Self::Err>;
187
188    /// Get melt_request and associated blinded_messages by quote_id
189    async fn get_melt_request_and_blinded_messages(
190        &mut self,
191        quote_id: &QuoteId,
192    ) -> Result<Option<MeltRequestInfo>, Self::Err>;
193
194    /// Delete melt_request and associated blinded_messages by quote_id
195    async fn delete_melt_request(&mut self, quote_id: &QuoteId) -> Result<(), Self::Err>;
196
197    /// Get [`MintMintQuote`] and lock it for update in this transaction
198    async fn get_mint_quote(
199        &mut self,
200        quote_id: &QuoteId,
201    ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
202
203    /// Get multiple [`MintMintQuote`]s by their IDs and lock them for update in this transaction.
204    ///
205    /// Returns results in the same order as the input IDs, with `None` for any IDs not found.
206    /// This method locks all found quotes to prevent race conditions during concurrent modifications.
207    async fn get_mint_quotes_by_ids(
208        &mut self,
209        quote_ids: &[QuoteId],
210    ) -> Result<Vec<Option<Acquired<MintMintQuote>>>, Self::Err>;
211
212    /// Add [`MintMintQuote`]
213    async fn add_mint_quote(
214        &mut self,
215        quote: MintMintQuote,
216    ) -> Result<Acquired<MintMintQuote>, Self::Err>;
217
218    /// Persists any pending changes made to the mint quote.
219    ///
220    /// This method extracts changes accumulated in the quote (via [`mint::MintQuote::take_changes`])
221    /// and persists them to the database. Changes may include new payments received or new
222    /// issuances recorded against the quote.
223    ///
224    /// If no changes are pending, this method returns successfully without performing
225    /// any database operations.
226    ///
227    /// # Arguments
228    ///
229    /// * `quote` - A mutable reference to an acquired (row-locked) mint quote. The quote
230    ///   must be locked to ensure transactional consistency when persisting changes.
231    ///
232    /// # Implementation Notes
233    ///
234    /// Implementations should call [`mint::MintQuote::take_changes`] to retrieve pending
235    /// changes, then persist each payment and issuance record, and finally update the
236    /// quote's aggregate counters (`amount_paid`, `amount_issued`) in the database.
237    async fn update_mint_quote(
238        &mut self,
239        quote: &mut Acquired<mint::MintQuote>,
240    ) -> Result<(), Self::Err>;
241
242    /// Get [`mint::MeltQuote`] and lock it for update in this transaction
243    async fn get_melt_quote(
244        &mut self,
245        quote_id: &QuoteId,
246    ) -> Result<Option<Acquired<mint::MeltQuote>>, Self::Err>;
247
248    /// Add [`mint::MeltQuote`]
249    async fn add_melt_quote(&mut self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
250
251    /// Retrieves all melt quotes matching a payment lookup identifier and locks them for update.
252    ///
253    /// This method returns multiple quotes because certain payment methods (notably BOLT12 offers)
254    /// can generate multiple payment attempts that share the same lookup identifier. Locking all
255    /// related quotes prevents race conditions where concurrent melt operations could interfere
256    /// with each other, potentially leading to double-spending or state inconsistencies.
257    ///
258    /// The returned quotes are locked within the current transaction to ensure safe concurrent
259    /// modification. This is essential during melt saga initiation and finalization to guarantee
260    /// atomic state transitions across all related quotes.
261    ///
262    /// # Arguments
263    ///
264    /// * `request_lookup_id` - The payment identifier used by the Lightning backend to track
265    ///   payment state (e.g., payment hash, offer ID, or label).
266    async fn get_melt_quotes_by_request_lookup_id(
267        &mut self,
268        request_lookup_id: &PaymentIdentifier,
269    ) -> Result<Vec<Acquired<MeltQuote>>, Self::Err>;
270
271    /// Locks a melt quote and all related quotes sharing the same request_lookup_id atomically.
272    ///
273    /// This method prevents deadlocks by acquiring all locks in a single query with consistent
274    /// ordering, rather than locking the target quote first and then related quotes separately.
275    ///
276    /// # Deadlock Prevention
277    ///
278    /// When multiple transactions try to melt quotes sharing the same `request_lookup_id`,
279    /// acquiring locks in two steps (first the target quote, then all related quotes) can cause
280    /// circular wait deadlocks. This method avoids that by:
281    /// 1. Using a subquery to find the `request_lookup_id` for the target quote
282    /// 2. Locking ALL quotes with that `request_lookup_id` in one atomic operation
283    /// 3. Ordering locks consistently by quote ID
284    ///
285    /// # Arguments
286    ///
287    /// * `quote_id` - The ID of the target melt quote
288    ///
289    /// # Returns
290    ///
291    /// A [`LockedMeltQuotes`] containing:
292    /// - `target`: The target quote (if found)
293    /// - `all_related`: All quotes sharing the same `request_lookup_id` (including the target)
294    ///
295    /// If the quote has no `request_lookup_id`, only the target quote is returned and locked.
296    async fn lock_melt_quote_and_related(
297        &mut self,
298        quote_id: &QuoteId,
299    ) -> Result<LockedMeltQuotes, Self::Err>;
300
301    /// Updates the request lookup id for a melt quote.
302    ///
303    /// Requires an [`Acquired`] melt quote to ensure the row is locked before modification.
304    async fn update_melt_quote_request_lookup_id(
305        &mut self,
306        quote: &mut Acquired<mint::MeltQuote>,
307        new_request_lookup_id: &PaymentIdentifier,
308    ) -> Result<(), Self::Err>;
309
310    /// Update [`mint::MeltQuote`] state.
311    ///
312    /// Requires an [`Acquired`] melt quote to ensure the row is locked before modification.
313    /// Returns the previous state.
314    async fn update_melt_quote_state(
315        &mut self,
316        quote: &mut Acquired<mint::MeltQuote>,
317        new_state: MeltQuoteState,
318        payment_proof: Option<String>,
319    ) -> Result<MeltQuoteState, Self::Err>;
320
321    /// Get all [`MintMintQuote`]s and lock it for update in this transaction
322    async fn get_mint_quote_by_request(
323        &mut self,
324        request: &str,
325    ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
326
327    /// Get all [`MintMintQuote`]s
328    async fn get_mint_quote_by_request_lookup_id(
329        &mut self,
330        request_lookup_id: &PaymentIdentifier,
331    ) -> Result<Option<Acquired<MintMintQuote>>, Self::Err>;
332}
333
334/// Mint Quote Database trait
335#[async_trait]
336pub trait QuotesDatabase {
337    /// Mint Quotes Database Error
338    type Err: Into<Error> + From<Error>;
339
340    /// Get [`MintMintQuote`]
341    async fn get_mint_quote(&self, quote_id: &QuoteId) -> Result<Option<MintMintQuote>, Self::Err>;
342
343    /// Get multiple [`MintMintQuote`]s by their IDs.
344    ///
345    /// Returns results in the same order as the input IDs, with `None` for any IDs not found.
346    async fn get_mint_quotes_by_ids(
347        &self,
348        quote_ids: &[QuoteId],
349    ) -> Result<Vec<Option<MintMintQuote>>, Self::Err>;
350
351    /// Get all [`MintMintQuote`]s
352    async fn get_mint_quote_by_request(
353        &self,
354        request: &str,
355    ) -> Result<Option<MintMintQuote>, Self::Err>;
356    /// Get all [`MintMintQuote`]s
357    async fn get_mint_quote_by_request_lookup_id(
358        &self,
359        request_lookup_id: &PaymentIdentifier,
360    ) -> Result<Option<MintMintQuote>, Self::Err>;
361    /// Get Mint Quotes
362    async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
363    /// Get [`mint::MeltQuote`]
364    async fn get_melt_quote(
365        &self,
366        quote_id: &QuoteId,
367    ) -> Result<Option<mint::MeltQuote>, Self::Err>;
368    /// Get all [`mint::MeltQuote`]s
369    async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
370}
371
372/// Mint Proof Transaction trait
373#[async_trait]
374pub trait ProofsTransaction {
375    /// Mint Proof Database Error
376    type Err: Into<Error> + From<Error>;
377
378    /// Add  [`Proofs`]
379    ///
380    /// Adds proofs to the database. The database should error if the proof already exits, with a
381    /// `AttemptUpdateSpentProof` if the proof is already spent or a `Duplicate` error otherwise.
382    async fn add_proofs(
383        &mut self,
384        proof: Proofs,
385        quote_id: Option<QuoteId>,
386        operation: &Operation,
387    ) -> Result<Acquired<ProofsWithState>, Self::Err>;
388
389    /// Updates the proofs to the given state in the database.
390    ///
391    /// Also updates the `state` field on the [`ProofsWithState`] wrapper to reflect
392    /// the new state after the database update succeeds.
393    async fn update_proofs_state(
394        &mut self,
395        proofs: &mut Acquired<ProofsWithState>,
396        new_state: State,
397    ) -> Result<(), Self::Err>;
398
399    /// get proofs states
400    async fn get_proofs(
401        &mut self,
402        ys: &[PublicKey],
403    ) -> Result<Acquired<ProofsWithState>, Self::Err>;
404
405    /// Remove [`Proofs`]
406    async fn remove_proofs(
407        &mut self,
408        ys: &[PublicKey],
409        quote_id: Option<QuoteId>,
410    ) -> Result<(), Self::Err>;
411
412    /// Get ys by quote id
413    async fn get_proof_ys_by_quote_id(
414        &mut self,
415        quote_id: &QuoteId,
416    ) -> Result<Vec<PublicKey>, Self::Err>;
417
418    /// Get proof ys by operation id
419    async fn get_proof_ys_by_operation_id(
420        &mut self,
421        operation_id: &uuid::Uuid,
422    ) -> Result<Vec<PublicKey>, Self::Err>;
423}
424
425/// Mint Proof Database trait
426#[async_trait]
427pub trait ProofsDatabase {
428    /// Mint Proof Database Error
429    type Err: Into<Error> + From<Error>;
430
431    /// Get [`Proofs`] by ys
432    async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
433    /// Get ys by quote id
434    async fn get_proof_ys_by_quote_id(
435        &self,
436        quote_id: &QuoteId,
437    ) -> Result<Vec<PublicKey>, Self::Err>;
438    /// Get [`Proofs`] state
439    async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
440
441    /// Get [`Proofs`] by state
442    async fn get_proofs_by_keyset_id(
443        &self,
444        keyset_id: &Id,
445    ) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
446
447    /// Get total proofs redeemed by keyset id
448    async fn get_total_redeemed(&self) -> Result<HashMap<Id, Amount>, Self::Err>;
449
450    /// Get proof ys by operation id
451    async fn get_proof_ys_by_operation_id(
452        &self,
453        operation_id: &uuid::Uuid,
454    ) -> Result<Vec<PublicKey>, Self::Err>;
455}
456
457#[async_trait]
458/// Mint Signatures Transaction trait
459pub trait SignaturesTransaction {
460    /// Mint Signature Database Error
461    type Err: Into<Error> + From<Error>;
462
463    /// Add [`BlindSignature`]
464    async fn add_blind_signatures(
465        &mut self,
466        blinded_messages: &[PublicKey],
467        blind_signatures: &[BlindSignature],
468        quote_id: Option<QuoteId>,
469    ) -> Result<(), Self::Err>;
470
471    /// Get [`BlindSignature`]s
472    async fn get_blind_signatures(
473        &mut self,
474        blinded_messages: &[PublicKey],
475    ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
476}
477
478#[async_trait]
479/// Mint Signatures Database trait
480pub trait SignaturesDatabase {
481    /// Mint Signature Database Error
482    type Err: Into<Error> + From<Error>;
483
484    /// Get [`BlindSignature`]s
485    async fn get_blind_signatures(
486        &self,
487        blinded_messages: &[PublicKey],
488    ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
489
490    /// Get [`BlindSignature`]s for keyset_id
491    async fn get_blind_signatures_for_keyset(
492        &self,
493        keyset_id: &Id,
494    ) -> Result<Vec<BlindSignature>, Self::Err>;
495
496    /// Get [`BlindSignature`]s for quote
497    async fn get_blind_signatures_for_quote(
498        &self,
499        quote_id: &QuoteId,
500    ) -> Result<Vec<BlindSignature>, Self::Err>;
501
502    /// Get total amount issued by keyset id
503    async fn get_total_issued(&self) -> Result<HashMap<Id, Amount>, Self::Err>;
504
505    /// Get blinded secrets (B values) by operation id
506    async fn get_blinded_secrets_by_operation_id(
507        &self,
508        operation_id: &uuid::Uuid,
509    ) -> Result<Vec<PublicKey>, Self::Err>;
510}
511
512#[async_trait]
513/// Saga Transaction trait
514pub trait SagaTransaction {
515    /// Saga Database Error
516    type Err: Into<Error> + From<Error>;
517
518    /// Get saga by operation_id
519    async fn get_saga(
520        &mut self,
521        operation_id: &uuid::Uuid,
522    ) -> Result<Option<mint::Saga>, Self::Err>;
523
524    /// Add saga
525    async fn add_saga(&mut self, saga: &mint::Saga) -> Result<(), Self::Err>;
526
527    /// Update saga state (only updates state and updated_at fields)
528    async fn update_saga(
529        &mut self,
530        operation_id: &uuid::Uuid,
531        new_state: mint::SagaStateEnum,
532    ) -> Result<(), Self::Err>;
533
534    /// Delete saga
535    async fn delete_saga(&mut self, operation_id: &uuid::Uuid) -> Result<(), Self::Err>;
536}
537
538#[async_trait]
539/// Saga Database trait
540pub trait SagaDatabase {
541    /// Saga Database Error
542    type Err: Into<Error> + From<Error>;
543
544    /// Get all incomplete sagas for a given operation kind
545    async fn get_incomplete_sagas(
546        &self,
547        operation_kind: mint::OperationKind,
548    ) -> Result<Vec<mint::Saga>, Self::Err>;
549}
550
551#[async_trait]
552/// Completed Operations Transaction trait
553pub trait CompletedOperationsTransaction {
554    /// Completed Operations Database Error
555    type Err: Into<Error> + From<Error>;
556
557    /// Add completed operation
558    async fn add_completed_operation(
559        &mut self,
560        operation: &mint::Operation,
561        fee_by_keyset: &std::collections::HashMap<crate::nuts::Id, crate::Amount>,
562    ) -> Result<(), Self::Err>;
563}
564
565#[async_trait]
566/// Completed Operations Database trait
567pub trait CompletedOperationsDatabase {
568    /// Completed Operations Database Error
569    type Err: Into<Error> + From<Error>;
570
571    /// Get completed operation by operation_id
572    async fn get_completed_operation(
573        &self,
574        operation_id: &uuid::Uuid,
575    ) -> Result<Option<mint::Operation>, Self::Err>;
576
577    /// Get completed operations by operation kind
578    async fn get_completed_operations_by_kind(
579        &self,
580        operation_kind: mint::OperationKind,
581    ) -> Result<Vec<mint::Operation>, Self::Err>;
582
583    /// Get all completed operations
584    async fn get_completed_operations(&self) -> Result<Vec<mint::Operation>, Self::Err>;
585}
586
587/// Base database writer
588pub trait Transaction<Error>:
589    DbTransactionFinalizer<Err = Error>
590    + QuotesTransaction<Err = Error>
591    + SignaturesTransaction<Err = Error>
592    + ProofsTransaction<Err = Error>
593    + KVStoreTransaction<Error>
594    + SagaTransaction<Err = Error>
595    + CompletedOperationsTransaction<Err = Error>
596{
597}
598
599/// Mint Database trait
600#[async_trait]
601pub trait Database<Error>:
602    KVStoreDatabase<Err = Error>
603    + QuotesDatabase<Err = Error>
604    + ProofsDatabase<Err = Error>
605    + SignaturesDatabase<Err = Error>
606    + SagaDatabase<Err = Error>
607    + CompletedOperationsDatabase<Err = Error>
608{
609    /// Begins a transaction
610    async fn begin_transaction(&self) -> Result<Box<dyn Transaction<Error> + Send + Sync>, Error>;
611}
612
613/// Type alias for Mint Database
614pub type DynMintDatabase = std::sync::Arc<dyn Database<Error> + Send + Sync>;
615
616/// Type alias for Mint Transaction
617pub type DynMintTransaction = Box<dyn Transaction<Error> + Send + Sync>;