wallet.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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::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. /// Wallet Database trait
  16. #[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
  17. #[cfg_attr(not(target_arch = "wasm32"), async_trait)]
  18. pub trait Database: Debug {
  19. /// Wallet Database Error
  20. type Err: Into<Error> + From<Error>;
  21. /// Add Mint to storage
  22. async fn add_mint(
  23. &self,
  24. mint_url: MintUrl,
  25. mint_info: Option<MintInfo>,
  26. ) -> Result<(), Self::Err>;
  27. /// Remove Mint from storage
  28. async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Self::Err>;
  29. /// Get mint from storage
  30. async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Self::Err>;
  31. /// Get all mints from storage
  32. async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Self::Err>;
  33. /// Update mint url
  34. async fn update_mint_url(
  35. &self,
  36. old_mint_url: MintUrl,
  37. new_mint_url: MintUrl,
  38. ) -> Result<(), Self::Err>;
  39. /// Add mint keyset to storage
  40. async fn add_mint_keysets(
  41. &self,
  42. mint_url: MintUrl,
  43. keysets: Vec<KeySetInfo>,
  44. ) -> Result<(), Self::Err>;
  45. /// Get mint keysets for mint url
  46. async fn get_mint_keysets(
  47. &self,
  48. mint_url: MintUrl,
  49. ) -> Result<Option<Vec<KeySetInfo>>, Self::Err>;
  50. /// Get mint keyset by id
  51. async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Self::Err>;
  52. /// Add mint quote to storage
  53. async fn add_mint_quote(&self, quote: WalletMintQuote) -> Result<(), Self::Err>;
  54. /// Get mint quote from storage
  55. async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<WalletMintQuote>, Self::Err>;
  56. /// Get mint quotes from storage
  57. async fn get_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Self::Err>;
  58. /// Remove mint quote from storage
  59. async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Self::Err>;
  60. /// Add melt quote to storage
  61. async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Self::Err>;
  62. /// Get melt quote from storage
  63. async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Self::Err>;
  64. /// Get melt quotes from storage
  65. async fn get_melt_quotes(&self) -> Result<Vec<wallet::MeltQuote>, Self::Err>;
  66. /// Remove melt quote from storage
  67. async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Self::Err>;
  68. /// Add [`Keys`] to storage
  69. async fn add_keys(&self, keyset: KeySet) -> Result<(), Self::Err>;
  70. /// Get [`Keys`] from storage
  71. async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Self::Err>;
  72. /// Remove [`Keys`] from storage
  73. async fn remove_keys(&self, id: &Id) -> Result<(), Self::Err>;
  74. /// Update the proofs in storage by adding new proofs or removing proofs by
  75. /// their Y value.
  76. async fn update_proofs(
  77. &self,
  78. added: Vec<ProofInfo>,
  79. removed_ys: Vec<PublicKey>,
  80. ) -> Result<(), Self::Err>;
  81. /// Get proofs from storage
  82. async fn get_proofs(
  83. &self,
  84. mint_url: Option<MintUrl>,
  85. unit: Option<CurrencyUnit>,
  86. state: Option<Vec<State>>,
  87. spending_conditions: Option<Vec<SpendingConditions>>,
  88. ) -> Result<Vec<ProofInfo>, Self::Err>;
  89. /// Update proofs state in storage
  90. async fn update_proofs_state(&self, ys: Vec<PublicKey>, state: State) -> Result<(), Self::Err>;
  91. /// Atomically increment Keyset counter and return new value
  92. async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<u32, Self::Err>;
  93. /// Add transaction to storage
  94. async fn add_transaction(&self, transaction: Transaction) -> Result<(), Self::Err>;
  95. /// Get transaction from storage
  96. async fn get_transaction(
  97. &self,
  98. transaction_id: TransactionId,
  99. ) -> Result<Option<Transaction>, Self::Err>;
  100. /// List transactions from storage
  101. async fn list_transactions(
  102. &self,
  103. mint_url: Option<MintUrl>,
  104. direction: Option<TransactionDirection>,
  105. unit: Option<CurrencyUnit>,
  106. ) -> Result<Vec<Transaction>, Self::Err>;
  107. /// Remove transaction from storage
  108. async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), Self::Err>;
  109. }