wallet.rs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //! CDK Database
  2. use std::collections::HashMap;
  3. use std::fmt::Debug;
  4. use async_trait::async_trait;
  5. use cashu::KeySet;
  6. use super::{DbTransactionFinalizer, Error};
  7. use crate::common::ProofInfo;
  8. use crate::mint_url::MintUrl;
  9. use crate::nuts::{
  10. CurrencyUnit, Id, KeySetInfo, Keys, MintInfo, PublicKey, SpendingConditions, State,
  11. };
  12. use crate::wallet::{
  13. self, MintQuote as WalletMintQuote, Transaction, TransactionDirection, TransactionId,
  14. };
  15. /// Easy to use Dynamic Database type alias
  16. pub type DynWalletDatabaseTransaction<'a> =
  17. Box<dyn DatabaseTransaction<'a, super::Error> + Sync + Send + 'a>;
  18. /// Database transaction
  19. ///
  20. /// This trait encapsulates all the changes to be done in the wallet
  21. #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
  22. #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
  23. pub trait DatabaseTransaction<'a, Error>: DbTransactionFinalizer<Err = Error> {
  24. /// Add Mint to storage
  25. async fn add_mint(
  26. &mut self,
  27. mint_url: MintUrl,
  28. mint_info: Option<MintInfo>,
  29. ) -> Result<(), Error>;
  30. /// Remove Mint from storage
  31. async fn remove_mint(&mut self, mint_url: MintUrl) -> Result<(), Error>;
  32. /// Update mint url
  33. async fn update_mint_url(
  34. &mut self,
  35. old_mint_url: MintUrl,
  36. new_mint_url: MintUrl,
  37. ) -> Result<(), Error>;
  38. /// Get mint keyset by id
  39. async fn get_keyset_by_id(&mut self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Error>;
  40. /// Get [`Keys`] from storage
  41. async fn get_keys(&mut self, id: &Id) -> Result<Option<Keys>, Self::Err>;
  42. /// Add mint keyset to storage
  43. async fn add_mint_keysets(
  44. &mut self,
  45. mint_url: MintUrl,
  46. keysets: Vec<KeySetInfo>,
  47. ) -> Result<(), Error>;
  48. /// Get mint quote from storage. This function locks the returned minted quote for update
  49. async fn get_mint_quote(
  50. &mut self,
  51. quote_id: &str,
  52. ) -> Result<Option<WalletMintQuote>, Self::Err>;
  53. /// Add mint quote to storage
  54. async fn add_mint_quote(&mut self, quote: WalletMintQuote) -> Result<(), Error>;
  55. /// Remove mint quote from storage
  56. async fn remove_mint_quote(&mut self, quote_id: &str) -> Result<(), Error>;
  57. /// Get melt quote from storage
  58. async fn get_melt_quote(&mut self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Error>;
  59. /// Add melt quote to storage
  60. async fn add_melt_quote(&mut self, quote: wallet::MeltQuote) -> Result<(), Error>;
  61. /// Remove melt quote from storage
  62. async fn remove_melt_quote(&mut self, quote_id: &str) -> Result<(), Error>;
  63. /// Add [`Keys`] to storage
  64. async fn add_keys(&mut self, keyset: KeySet) -> Result<(), Error>;
  65. /// Remove [`Keys`] from storage
  66. async fn remove_keys(&mut self, id: &Id) -> Result<(), Error>;
  67. /// Get proofs from storage and lock them for update
  68. async fn get_proofs(
  69. &mut self,
  70. mint_url: Option<MintUrl>,
  71. unit: Option<CurrencyUnit>,
  72. state: Option<Vec<State>>,
  73. spending_conditions: Option<Vec<SpendingConditions>>,
  74. ) -> Result<Vec<ProofInfo>, Error>;
  75. /// Update the proofs in storage by adding new proofs or removing proofs by
  76. /// their Y value.
  77. async fn update_proofs(
  78. &mut self,
  79. added: Vec<ProofInfo>,
  80. removed_ys: Vec<PublicKey>,
  81. ) -> Result<(), Error>;
  82. /// Update proofs state in storage
  83. async fn update_proofs_state(&mut self, ys: Vec<PublicKey>, state: State) -> Result<(), Error>;
  84. /// Atomically increment Keyset counter and return new value
  85. async fn increment_keyset_counter(&mut self, keyset_id: &Id, count: u32) -> Result<u32, Error>;
  86. /// Add transaction to storage
  87. async fn add_transaction(&mut self, transaction: Transaction) -> Result<(), Error>;
  88. /// Remove transaction from storage
  89. async fn remove_transaction(&mut self, transaction_id: TransactionId) -> Result<(), Error>;
  90. }
  91. /// Wallet Database trait
  92. #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
  93. #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
  94. pub trait Database: Debug {
  95. /// Wallet Database Error
  96. type Err: Into<Error> + From<Error>;
  97. /// Begins a DB transaction
  98. async fn begin_db_transaction<'a>(
  99. &'a self,
  100. ) -> Result<Box<dyn DatabaseTransaction<'a, Self::Err> + Send + Sync + 'a>, Self::Err>;
  101. /// Get mint from storage
  102. async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Self::Err>;
  103. /// Get all mints from storage
  104. async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Self::Err>;
  105. /// Get mint keysets for mint url
  106. async fn get_mint_keysets(
  107. &self,
  108. mint_url: MintUrl,
  109. ) -> Result<Option<Vec<KeySetInfo>>, Self::Err>;
  110. /// Get mint keyset by id
  111. async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Self::Err>;
  112. /// Get mint quote from storage
  113. async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<WalletMintQuote>, Self::Err>;
  114. /// Get mint quotes from storage
  115. async fn get_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Self::Err>;
  116. /// Get melt quote from storage
  117. async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Self::Err>;
  118. /// Get melt quotes from storage
  119. async fn get_melt_quotes(&self) -> Result<Vec<wallet::MeltQuote>, Self::Err>;
  120. /// Get [`Keys`] from storage
  121. async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Self::Err>;
  122. /// Get proofs from storage
  123. async fn get_proofs(
  124. &self,
  125. mint_url: Option<MintUrl>,
  126. unit: Option<CurrencyUnit>,
  127. state: Option<Vec<State>>,
  128. spending_conditions: Option<Vec<SpendingConditions>>,
  129. ) -> Result<Vec<ProofInfo>, Self::Err>;
  130. /// Get balance
  131. async fn get_balance(
  132. &self,
  133. mint_url: Option<MintUrl>,
  134. unit: Option<CurrencyUnit>,
  135. state: Option<Vec<State>>,
  136. ) -> Result<u64, Self::Err>;
  137. /// Get transaction from storage
  138. async fn get_transaction(
  139. &self,
  140. transaction_id: TransactionId,
  141. ) -> Result<Option<Transaction>, Self::Err>;
  142. /// List transactions from storage
  143. async fn list_transactions(
  144. &self,
  145. mint_url: Option<MintUrl>,
  146. direction: Option<TransactionDirection>,
  147. unit: Option<CurrencyUnit>,
  148. ) -> Result<Vec<Transaction>, Self::Err>;
  149. }