cdk_common/database/mint/
mod.rs1use std::collections::HashMap;
4
5use async_trait::async_trait;
6use cashu::{Amount, MintInfo};
7use uuid::Uuid;
8
9use super::Error;
10use crate::common::QuoteTTL;
11use crate::mint::{self, MintKeySetInfo, MintQuote as MintMintQuote};
12use crate::nuts::{
13 BlindSignature, CurrencyUnit, Id, MeltQuoteState, Proof, Proofs, PublicKey, State,
14};
15use crate::payment::PaymentIdentifier;
16
17#[cfg(feature = "auth")]
18mod auth;
19
20#[cfg(feature = "test")]
21pub mod test;
22
23#[cfg(feature = "auth")]
24pub use auth::{MintAuthDatabase, MintAuthTransaction};
25
26#[async_trait]
28pub trait KeysDatabaseTransaction<'a, Error>: DbTransactionFinalizer<Err = Error> {
29 async fn set_active_keyset(&mut self, unit: CurrencyUnit, id: Id) -> Result<(), Error>;
31
32 async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error>;
34}
35
36#[async_trait]
38pub trait KeysDatabase {
39 type Err: Into<Error> + From<Error>;
41
42 async fn begin_transaction<'a>(
44 &'a self,
45 ) -> Result<Box<dyn KeysDatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Error>;
46
47 async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
49
50 async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err>;
52
53 async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
55
56 async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
58}
59
60#[async_trait]
62pub trait QuotesTransaction<'a> {
63 type Err: Into<Error> + From<Error>;
65
66 async fn get_mint_quote(&mut self, quote_id: &Uuid)
68 -> Result<Option<MintMintQuote>, Self::Err>;
69 async fn add_mint_quote(&mut self, quote: MintMintQuote) -> Result<(), Self::Err>;
71 async fn increment_mint_quote_amount_paid(
73 &mut self,
74 quote_id: &Uuid,
75 amount_paid: Amount,
76 payment_id: String,
77 ) -> Result<Amount, Self::Err>;
78 async fn increment_mint_quote_amount_issued(
80 &mut self,
81 quote_id: &Uuid,
82 amount_issued: Amount,
83 ) -> Result<Amount, Self::Err>;
84 async fn remove_mint_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
86 async fn get_melt_quote(
88 &mut self,
89 quote_id: &Uuid,
90 ) -> Result<Option<mint::MeltQuote>, Self::Err>;
91 async fn add_melt_quote(&mut self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
93
94 async fn update_melt_quote_request_lookup_id(
96 &mut self,
97 quote_id: &Uuid,
98 new_request_lookup_id: &PaymentIdentifier,
99 ) -> Result<(), Self::Err>;
100
101 async fn update_melt_quote_state(
105 &mut self,
106 quote_id: &Uuid,
107 new_state: MeltQuoteState,
108 payment_proof: Option<String>,
109 ) -> Result<(MeltQuoteState, mint::MeltQuote), Self::Err>;
110 async fn remove_melt_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
112 async fn get_mint_quote_by_request(
114 &mut self,
115 request: &str,
116 ) -> Result<Option<MintMintQuote>, Self::Err>;
117
118 async fn get_mint_quote_by_request_lookup_id(
120 &mut self,
121 request_lookup_id: &PaymentIdentifier,
122 ) -> Result<Option<MintMintQuote>, Self::Err>;
123}
124
125#[async_trait]
127pub trait QuotesDatabase {
128 type Err: Into<Error> + From<Error>;
130
131 async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintMintQuote>, Self::Err>;
133
134 async fn get_mint_quote_by_request(
136 &self,
137 request: &str,
138 ) -> Result<Option<MintMintQuote>, Self::Err>;
139 async fn get_mint_quote_by_request_lookup_id(
141 &self,
142 request_lookup_id: &PaymentIdentifier,
143 ) -> Result<Option<MintMintQuote>, Self::Err>;
144 async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
146 async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err>;
148 async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
150}
151
152#[async_trait]
154pub trait ProofsTransaction<'a> {
155 type Err: Into<Error> + From<Error>;
157
158 async fn add_proofs(&mut self, proof: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err>;
163 async fn update_proofs_states(
165 &mut self,
166 ys: &[PublicKey],
167 proofs_state: State,
168 ) -> Result<Vec<Option<State>>, Self::Err>;
169
170 async fn remove_proofs(
172 &mut self,
173 ys: &[PublicKey],
174 quote_id: Option<Uuid>,
175 ) -> Result<(), Self::Err>;
176}
177
178#[async_trait]
180pub trait ProofsDatabase {
181 type Err: Into<Error> + From<Error>;
183
184 async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
186 async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err>;
188 async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
190 async fn get_proofs_by_keyset_id(
192 &self,
193 keyset_id: &Id,
194 ) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
195}
196
197#[async_trait]
198pub trait SignaturesTransaction<'a> {
200 type Err: Into<Error> + From<Error>;
202
203 async fn add_blind_signatures(
205 &mut self,
206 blinded_messages: &[PublicKey],
207 blind_signatures: &[BlindSignature],
208 quote_id: Option<Uuid>,
209 ) -> Result<(), Self::Err>;
210
211 async fn get_blind_signatures(
213 &mut self,
214 blinded_messages: &[PublicKey],
215 ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
216}
217
218#[async_trait]
219pub trait SignaturesDatabase {
221 type Err: Into<Error> + From<Error>;
223
224 async fn get_blind_signatures(
226 &self,
227 blinded_messages: &[PublicKey],
228 ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
229 async fn get_blind_signatures_for_keyset(
231 &self,
232 keyset_id: &Id,
233 ) -> Result<Vec<BlindSignature>, Self::Err>;
234 async fn get_blind_signatures_for_quote(
236 &self,
237 quote_id: &Uuid,
238 ) -> Result<Vec<BlindSignature>, Self::Err>;
239}
240
241#[async_trait]
242pub trait DbTransactionFinalizer {
244 type Err: Into<Error> + From<Error>;
246
247 async fn commit(self: Box<Self>) -> Result<(), Self::Err>;
249
250 async fn rollback(self: Box<Self>) -> Result<(), Self::Err>;
252}
253
254#[async_trait]
256pub trait Transaction<'a, Error>:
257 DbTransactionFinalizer<Err = Error>
258 + QuotesTransaction<'a, Err = Error>
259 + SignaturesTransaction<'a, Err = Error>
260 + ProofsTransaction<'a, Err = Error>
261{
262 async fn set_quote_ttl(&mut self, quote_ttl: QuoteTTL) -> Result<(), Error>;
264
265 async fn set_mint_info(&mut self, mint_info: MintInfo) -> Result<(), Error>;
267}
268
269#[async_trait]
271pub trait Database<Error>:
272 QuotesDatabase<Err = Error> + ProofsDatabase<Err = Error> + SignaturesDatabase<Err = Error>
273{
274 async fn begin_transaction<'a>(
276 &'a self,
277 ) -> Result<Box<dyn Transaction<'a, Error> + Send + Sync + 'a>, Error>;
278
279 async fn get_mint_info(&self) -> Result<MintInfo, Error>;
281
282 async fn get_quote_ttl(&self) -> Result<QuoteTTL, Error>;
284}