cdk_common/database/mint/
mod.rs1use std::collections::HashMap;
4
5use async_trait::async_trait;
6use cashu::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, MintQuoteState, Proof, Proofs, PublicKey,
14 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, 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_or_replace_mint_quote(&mut self, quote: MintMintQuote) -> Result<(), Self::Err>;
71 async fn update_mint_quote_state(
73 &mut self,
74 quote_id: &Uuid,
75 state: MintQuoteState,
76 ) -> Result<MintQuoteState, Self::Err>;
77 async fn remove_mint_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
79 async fn get_melt_quote(
81 &mut self,
82 quote_id: &Uuid,
83 ) -> Result<Option<mint::MeltQuote>, Self::Err>;
84 async fn add_melt_quote(&mut self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
86
87 async fn update_melt_quote_request_lookup_id(
89 &mut self,
90 quote_id: &Uuid,
91 new_request_lookup_id: &str,
92 ) -> Result<(), Self::Err>;
93
94 async fn update_melt_quote_state(
98 &mut self,
99 quote_id: &Uuid,
100 new_state: MeltQuoteState,
101 ) -> Result<(MeltQuoteState, mint::MeltQuote), Self::Err>;
102 async fn remove_melt_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
104 async fn get_mint_quote_by_request(
106 &mut self,
107 request: &str,
108 ) -> Result<Option<MintMintQuote>, Self::Err>;
109}
110
111#[async_trait]
113pub trait QuotesDatabase {
114 type Err: Into<Error> + From<Error>;
116
117 async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintMintQuote>, Self::Err>;
119
120 async fn get_mint_quote_by_request(
122 &self,
123 request: &str,
124 ) -> Result<Option<MintMintQuote>, Self::Err>;
125 async fn get_mint_quote_by_request_lookup_id(
127 &self,
128 request_lookup_id: &str,
129 ) -> Result<Option<MintMintQuote>, Self::Err>;
130 async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
132 async fn get_mint_quotes_with_state(
134 &self,
135 state: MintQuoteState,
136 ) -> Result<Vec<MintMintQuote>, Self::Err>;
137 async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err>;
139 async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
141}
142
143#[async_trait]
145pub trait ProofsTransaction<'a> {
146 type Err: Into<Error> + From<Error>;
148
149 async fn add_proofs(&mut self, proof: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err>;
154 async fn update_proofs_states(
156 &mut self,
157 ys: &[PublicKey],
158 proofs_state: State,
159 ) -> Result<Vec<Option<State>>, Self::Err>;
160
161 async fn remove_proofs(
163 &mut self,
164 ys: &[PublicKey],
165 quote_id: Option<Uuid>,
166 ) -> Result<(), Self::Err>;
167}
168
169#[async_trait]
171pub trait ProofsDatabase {
172 type Err: Into<Error> + From<Error>;
174
175 async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
177 async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err>;
179 async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
181 async fn get_proofs_by_keyset_id(
183 &self,
184 keyset_id: &Id,
185 ) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
186}
187
188#[async_trait]
189pub trait SignaturesTransaction<'a> {
191 type Err: Into<Error> + From<Error>;
193
194 async fn add_blind_signatures(
196 &mut self,
197 blinded_messages: &[PublicKey],
198 blind_signatures: &[BlindSignature],
199 quote_id: Option<Uuid>,
200 ) -> Result<(), Self::Err>;
201
202 async fn get_blind_signatures(
204 &mut self,
205 blinded_messages: &[PublicKey],
206 ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
207}
208
209#[async_trait]
210pub trait SignaturesDatabase {
212 type Err: Into<Error> + From<Error>;
214
215 async fn get_blind_signatures(
217 &self,
218 blinded_messages: &[PublicKey],
219 ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
220 async fn get_blind_signatures_for_keyset(
222 &self,
223 keyset_id: &Id,
224 ) -> Result<Vec<BlindSignature>, Self::Err>;
225 async fn get_blind_signatures_for_quote(
227 &self,
228 quote_id: &Uuid,
229 ) -> Result<Vec<BlindSignature>, Self::Err>;
230}
231
232#[async_trait]
233pub trait DbTransactionFinalizer {
235 type Err: Into<Error> + From<Error>;
237
238 async fn commit(self: Box<Self>) -> Result<(), Self::Err>;
240
241 async fn rollback(self: Box<Self>) -> Result<(), Self::Err>;
243}
244
245#[async_trait]
247pub trait Transaction<'a, Error>:
248 DbTransactionFinalizer<Err = Error>
249 + QuotesTransaction<'a, Err = Error>
250 + SignaturesTransaction<'a, Err = Error>
251 + ProofsTransaction<'a, Err = Error>
252{
253 async fn set_quote_ttl(&mut self, quote_ttl: QuoteTTL) -> Result<(), Error>;
255
256 async fn set_mint_info(&mut self, mint_info: MintInfo) -> Result<(), Error>;
258}
259
260#[async_trait]
262pub trait Database<Error>:
263 QuotesDatabase<Err = Error> + ProofsDatabase<Err = Error> + SignaturesDatabase<Err = Error>
264{
265 async fn begin_transaction<'a>(
267 &'a self,
268 ) -> Result<Box<dyn Transaction<'a, Error> + Send + Sync + 'a>, Error>;
269
270 async fn get_mint_info(&self) -> Result<MintInfo, Error>;
272
273 async fn get_quote_ttl(&self) -> Result<QuoteTTL, Error>;
275}