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