mod.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //! CDK Database
  2. use std::collections::HashMap;
  3. use async_trait::async_trait;
  4. use cashu::MintInfo;
  5. use uuid::Uuid;
  6. use super::Error;
  7. use crate::common::QuoteTTL;
  8. use crate::mint::{self, MintKeySetInfo, MintQuote as MintMintQuote};
  9. use crate::nuts::{
  10. BlindSignature, CurrencyUnit, Id, MeltQuoteState, MintQuoteState, Proof, Proofs, PublicKey,
  11. State,
  12. };
  13. #[cfg(feature = "auth")]
  14. mod auth;
  15. #[cfg(feature = "test")]
  16. pub mod test;
  17. #[cfg(feature = "auth")]
  18. pub use auth::{MintAuthDatabase, MintAuthTransaction};
  19. /// KeysDatabaseWriter
  20. #[async_trait]
  21. pub trait KeysDatabaseTransaction<'a, Error>: DbTransactionFinalizer<Err = Error> {
  22. /// Add Active Keyset
  23. async fn set_active_keyset(&mut self, unit: CurrencyUnit, id: Id) -> Result<(), Error>;
  24. /// Add [`MintKeySetInfo`]
  25. async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error>;
  26. }
  27. /// Mint Keys Database trait
  28. #[async_trait]
  29. pub trait KeysDatabase {
  30. /// Mint Keys Database Error
  31. type Err: Into<Error> + From<Error>;
  32. /// Beings a transaction
  33. async fn begin_transaction<'a>(
  34. &'a self,
  35. ) -> Result<Box<dyn KeysDatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Error>;
  36. /// Get Active Keyset
  37. async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
  38. /// Get all Active Keyset
  39. async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, 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 writer trait
  46. #[async_trait]
  47. pub trait QuotesTransaction<'a> {
  48. /// Mint Quotes Database Error
  49. type Err: Into<Error> + From<Error>;
  50. /// Get [`MintMintQuote`] and lock it for update in this transaction
  51. async fn get_mint_quote(&mut self, quote_id: &Uuid)
  52. -> Result<Option<MintMintQuote>, Self::Err>;
  53. /// Add [`MintMintQuote`]
  54. async fn add_or_replace_mint_quote(&mut self, quote: MintMintQuote) -> Result<(), Self::Err>;
  55. /// Update state of [`MintMintQuote`]
  56. async fn update_mint_quote_state(
  57. &mut self,
  58. quote_id: &Uuid,
  59. state: MintQuoteState,
  60. ) -> Result<MintQuoteState, Self::Err>;
  61. /// Remove [`MintMintQuote`]
  62. async fn remove_mint_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
  63. /// Get [`mint::MeltQuote`] and lock it for update in this transaction
  64. async fn get_melt_quote(
  65. &mut self,
  66. quote_id: &Uuid,
  67. ) -> Result<Option<mint::MeltQuote>, Self::Err>;
  68. /// Add [`mint::MeltQuote`]
  69. async fn add_melt_quote(&mut self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
  70. /// Updates the request lookup id for a melt quote
  71. async fn update_melt_quote_request_lookup_id(
  72. &mut self,
  73. quote_id: &Uuid,
  74. new_request_lookup_id: &str,
  75. ) -> Result<(), Self::Err>;
  76. /// Update [`mint::MeltQuote`] state
  77. ///
  78. /// It is expected for this function to fail if the state is already set to the new state
  79. async fn update_melt_quote_state(
  80. &mut self,
  81. quote_id: &Uuid,
  82. new_state: MeltQuoteState,
  83. ) -> Result<(MeltQuoteState, mint::MeltQuote), Self::Err>;
  84. /// Remove [`mint::MeltQuote`]
  85. async fn remove_melt_quote(&mut self, quote_id: &Uuid) -> Result<(), Self::Err>;
  86. /// Get all [`MintMintQuote`]s and lock it for update in this transaction
  87. async fn get_mint_quote_by_request(
  88. &mut self,
  89. request: &str,
  90. ) -> Result<Option<MintMintQuote>, Self::Err>;
  91. }
  92. /// Mint Quote Database trait
  93. #[async_trait]
  94. pub trait QuotesDatabase {
  95. /// Mint Quotes Database Error
  96. type Err: Into<Error> + From<Error>;
  97. /// Get [`MintMintQuote`]
  98. async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintMintQuote>, Self::Err>;
  99. /// Get all [`MintMintQuote`]s
  100. async fn get_mint_quote_by_request(
  101. &self,
  102. request: &str,
  103. ) -> Result<Option<MintMintQuote>, Self::Err>;
  104. /// Get all [`MintMintQuote`]s
  105. async fn get_mint_quote_by_request_lookup_id(
  106. &self,
  107. request_lookup_id: &str,
  108. ) -> Result<Option<MintMintQuote>, Self::Err>;
  109. /// Get Mint Quotes
  110. async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
  111. /// Get Mint Quotes with state
  112. async fn get_mint_quotes_with_state(
  113. &self,
  114. state: MintQuoteState,
  115. ) -> Result<Vec<MintMintQuote>, Self::Err>;
  116. /// Get [`mint::MeltQuote`]
  117. async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err>;
  118. /// Get all [`mint::MeltQuote`]s
  119. async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
  120. }
  121. /// Mint Proof Transaction trait
  122. #[async_trait]
  123. pub trait ProofsTransaction<'a> {
  124. /// Mint Proof Database Error
  125. type Err: Into<Error> + From<Error>;
  126. /// Add [`Proofs`]
  127. ///
  128. /// Adds proofs to the database. The database should error if the proof already exits, with a
  129. /// `AttemptUpdateSpentProof` if the proof is already spent or a `Duplicate` error otherwise.
  130. async fn add_proofs(&mut self, proof: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err>;
  131. /// Updates the proofs to a given states and return the previous states
  132. async fn update_proofs_states(
  133. &mut self,
  134. ys: &[PublicKey],
  135. proofs_state: State,
  136. ) -> Result<Vec<Option<State>>, Self::Err>;
  137. /// Remove [`Proofs`]
  138. async fn remove_proofs(
  139. &mut self,
  140. ys: &[PublicKey],
  141. quote_id: Option<Uuid>,
  142. ) -> Result<(), Self::Err>;
  143. }
  144. /// Mint Proof Database trait
  145. #[async_trait]
  146. pub trait ProofsDatabase {
  147. /// Mint Proof Database Error
  148. type Err: Into<Error> + From<Error>;
  149. /// Get [`Proofs`] by ys
  150. async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
  151. /// Get ys by quote id
  152. async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err>;
  153. /// Get [`Proofs`] state
  154. async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
  155. /// Get [`Proofs`] by state
  156. async fn get_proofs_by_keyset_id(
  157. &self,
  158. keyset_id: &Id,
  159. ) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
  160. }
  161. #[async_trait]
  162. /// Mint Signatures Transaction trait
  163. pub trait SignaturesTransaction<'a> {
  164. /// Mint Signature Database Error
  165. type Err: Into<Error> + From<Error>;
  166. /// Add [`BlindSignature`]
  167. async fn add_blind_signatures(
  168. &mut self,
  169. blinded_messages: &[PublicKey],
  170. blind_signatures: &[BlindSignature],
  171. quote_id: Option<Uuid>,
  172. ) -> Result<(), Self::Err>;
  173. /// Get [`BlindSignature`]s
  174. async fn get_blind_signatures(
  175. &mut self,
  176. blinded_messages: &[PublicKey],
  177. ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
  178. }
  179. #[async_trait]
  180. /// Mint Signatures Database trait
  181. pub trait SignaturesDatabase {
  182. /// Mint Signature Database Error
  183. type Err: Into<Error> + From<Error>;
  184. /// Get [`BlindSignature`]s
  185. async fn get_blind_signatures(
  186. &self,
  187. blinded_messages: &[PublicKey],
  188. ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
  189. /// Get [`BlindSignature`]s for keyset_id
  190. async fn get_blind_signatures_for_keyset(
  191. &self,
  192. keyset_id: &Id,
  193. ) -> Result<Vec<BlindSignature>, Self::Err>;
  194. /// Get [`BlindSignature`]s for quote
  195. async fn get_blind_signatures_for_quote(
  196. &self,
  197. quote_id: &Uuid,
  198. ) -> Result<Vec<BlindSignature>, Self::Err>;
  199. }
  200. #[async_trait]
  201. /// Commit and Rollback
  202. pub trait DbTransactionFinalizer {
  203. /// Mint Signature Database Error
  204. type Err: Into<Error> + From<Error>;
  205. /// Commits all the changes into the database
  206. async fn commit(self: Box<Self>) -> Result<(), Self::Err>;
  207. /// Rollbacks the write transaction
  208. async fn rollback(self: Box<Self>) -> Result<(), Self::Err>;
  209. }
  210. /// Base database writer
  211. #[async_trait]
  212. pub trait Transaction<'a, Error>:
  213. DbTransactionFinalizer<Err = Error>
  214. + QuotesTransaction<'a, Err = Error>
  215. + SignaturesTransaction<'a, Err = Error>
  216. + ProofsTransaction<'a, Err = Error>
  217. {
  218. /// Set [`QuoteTTL`]
  219. async fn set_quote_ttl(&mut self, quote_ttl: QuoteTTL) -> Result<(), Error>;
  220. /// Set [`MintInfo`]
  221. async fn set_mint_info(&mut self, mint_info: MintInfo) -> Result<(), Error>;
  222. }
  223. /// Mint Database trait
  224. #[async_trait]
  225. pub trait Database<Error>:
  226. QuotesDatabase<Err = Error> + ProofsDatabase<Err = Error> + SignaturesDatabase<Err = Error>
  227. {
  228. /// Beings a transaction
  229. async fn begin_transaction<'a>(
  230. &'a self,
  231. ) -> Result<Box<dyn Transaction<'a, Error> + Send + Sync + 'a>, Error>;
  232. /// Get [`MintInfo`]
  233. async fn get_mint_info(&self) -> Result<MintInfo, Error>;
  234. /// Get [`QuoteTTL`]
  235. async fn get_quote_ttl(&self) -> Result<QuoteTTL, Error>;
  236. }