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