cdk_common/database/
wallet.rs

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