cdk_common/database/mint/
mod.rs

1//! CDK Database
2
3use std::collections::HashMap;
4
5use async_trait::async_trait;
6use cashu::MintInfo;
7use uuid::Uuid;
8
9use super::Error;
10use crate::common::{PaymentProcessorKey, QuoteTTL};
11use crate::mint::{self, MintKeySetInfo, MintQuote as MintMintQuote};
12use crate::nuts::{
13    BlindSignature, CurrencyUnit, Id, MeltBolt11Request, MeltQuoteState, MintQuoteState, Proof,
14    Proofs, PublicKey, State,
15};
16
17#[cfg(feature = "auth")]
18mod auth;
19
20#[cfg(feature = "auth")]
21pub use auth::MintAuthDatabase;
22
23/// Mint Keys Database trait
24#[async_trait]
25pub trait KeysDatabase {
26    /// Mint Keys Database Error
27    type Err: Into<Error> + From<Error>;
28
29    /// Add Active Keyset
30    async fn set_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err>;
31    /// Get Active Keyset
32    async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
33    /// Get all Active Keyset
34    async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err>;
35    /// Add [`MintKeySetInfo`]
36    async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err>;
37    /// Get [`MintKeySetInfo`]
38    async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
39    /// Get [`MintKeySetInfo`]s
40    async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
41}
42/// Mint Quote Database trait
43#[async_trait]
44pub trait QuotesDatabase {
45    /// Mint Quotes Database Error
46    type Err: Into<Error> + From<Error>;
47
48    /// Add [`MintMintQuote`]
49    async fn add_mint_quote(&self, quote: MintMintQuote) -> Result<(), Self::Err>;
50    /// Get [`MintMintQuote`]
51    async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintMintQuote>, Self::Err>;
52    /// Update state of [`MintMintQuote`]
53    async fn update_mint_quote_state(
54        &self,
55        quote_id: &Uuid,
56        state: MintQuoteState,
57    ) -> Result<MintQuoteState, Self::Err>;
58    /// Get all [`MintMintQuote`]s
59    async fn get_mint_quote_by_request(
60        &self,
61        request: &str,
62    ) -> Result<Option<MintMintQuote>, Self::Err>;
63    /// Get all [`MintMintQuote`]s
64    async fn get_mint_quote_by_request_lookup_id(
65        &self,
66        request_lookup_id: &str,
67    ) -> Result<Option<MintMintQuote>, Self::Err>;
68    /// Get Mint Quotes
69    async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
70    /// Get Mint Quotes with state
71    async fn get_mint_quotes_with_state(
72        &self,
73        state: MintQuoteState,
74    ) -> Result<Vec<MintMintQuote>, Self::Err>;
75    /// Remove [`MintMintQuote`]
76    async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
77
78    /// Add [`mint::MeltQuote`]
79    async fn add_melt_quote(&self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
80    /// Get [`mint::MeltQuote`]
81    async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err>;
82    /// Update [`mint::MeltQuote`] state
83    async fn update_melt_quote_state(
84        &self,
85        quote_id: &Uuid,
86        state: MeltQuoteState,
87    ) -> Result<MeltQuoteState, Self::Err>;
88    /// Get all [`mint::MeltQuote`]s
89    async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
90    /// Remove [`mint::MeltQuote`]
91    async fn remove_melt_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
92
93    /// Add melt request
94    async fn add_melt_request(
95        &self,
96        melt_request: MeltBolt11Request<Uuid>,
97        ln_key: PaymentProcessorKey,
98    ) -> Result<(), Self::Err>;
99    /// Get melt request
100    async fn get_melt_request(
101        &self,
102        quote_id: &Uuid,
103    ) -> Result<Option<(MeltBolt11Request<Uuid>, PaymentProcessorKey)>, Self::Err>;
104}
105
106/// Mint Proof Database trait
107#[async_trait]
108pub trait ProofsDatabase {
109    /// Mint Proof Database Error
110    type Err: Into<Error> + From<Error>;
111
112    /// Add  [`Proofs`]
113    async fn add_proofs(&self, proof: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err>;
114    /// Remove [`Proofs`]
115    async fn remove_proofs(
116        &self,
117        ys: &[PublicKey],
118        quote_id: Option<Uuid>,
119    ) -> Result<(), Self::Err>;
120    /// Get [`Proofs`] by ys
121    async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
122    /// Get ys by quote id
123    async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err>;
124    /// Get [`Proofs`] state
125    async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
126    /// Get [`Proofs`] state
127    async fn update_proofs_states(
128        &self,
129        ys: &[PublicKey],
130        proofs_state: State,
131    ) -> Result<Vec<Option<State>>, Self::Err>;
132    /// Get [`Proofs`] by state
133    async fn get_proofs_by_keyset_id(
134        &self,
135        keyset_id: &Id,
136    ) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
137}
138
139#[async_trait]
140/// Mint Signatures Database trait
141pub trait SignaturesDatabase {
142    /// Mint Signature Database Error
143    type Err: Into<Error> + From<Error>;
144
145    /// Add [`BlindSignature`]
146    async fn add_blind_signatures(
147        &self,
148        blinded_messages: &[PublicKey],
149        blind_signatures: &[BlindSignature],
150        quote_id: Option<Uuid>,
151    ) -> Result<(), Self::Err>;
152    /// Get [`BlindSignature`]s
153    async fn get_blind_signatures(
154        &self,
155        blinded_messages: &[PublicKey],
156    ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
157    /// Get [`BlindSignature`]s for keyset_id
158    async fn get_blind_signatures_for_keyset(
159        &self,
160        keyset_id: &Id,
161    ) -> Result<Vec<BlindSignature>, Self::Err>;
162    /// Get [`BlindSignature`]s for quote
163    async fn get_blind_signatures_for_quote(
164        &self,
165        quote_id: &Uuid,
166    ) -> Result<Vec<BlindSignature>, Self::Err>;
167}
168
169/// Mint Database trait
170#[async_trait]
171pub trait Database<Error>:
172    KeysDatabase<Err = Error>
173    + QuotesDatabase<Err = Error>
174    + ProofsDatabase<Err = Error>
175    + SignaturesDatabase<Err = Error>
176{
177    /// Set [`MintInfo`]
178    async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), Error>;
179    /// Get [`MintInfo`]
180    async fn get_mint_info(&self) -> Result<MintInfo, Error>;
181
182    /// Set [`QuoteTTL`]
183    async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), Error>;
184    /// Get [`QuoteTTL`]
185    async fn get_quote_ttl(&self) -> Result<QuoteTTL, Error>;
186}