cdk_common/database/wallet/
mod.rs1use std::collections::HashMap;
4use std::fmt::Debug;
5
6use async_trait::async_trait;
7use cashu::KeySet;
8
9use super::Error;
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, ProofInfo, Transaction, TransactionDirection, TransactionId,
16};
17
18#[cfg(feature = "test")]
19pub mod test;
20
21#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
23#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
24pub trait Database<Err>: Debug
25where
26 Err: Into<Error> + From<Error>,
27{
28 async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Err>;
30
31 async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Err>;
33
34 async fn get_mint_keysets(&self, mint_url: MintUrl) -> Result<Option<Vec<KeySetInfo>>, Err>;
36
37 async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Err>;
39
40 async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<WalletMintQuote>, Err>;
42
43 async fn get_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
45 async fn get_unissued_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
49
50 async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Err>;
52
53 async fn get_melt_quotes(&self) -> Result<Vec<wallet::MeltQuote>, Err>;
55
56 async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Err>;
58
59 async fn get_proofs(
61 &self,
62 mint_url: Option<MintUrl>,
63 unit: Option<CurrencyUnit>,
64 state: Option<Vec<State>>,
65 spending_conditions: Option<Vec<SpendingConditions>>,
66 ) -> Result<Vec<ProofInfo>, Err>;
67
68 async fn get_proofs_by_ys(&self, ys: Vec<PublicKey>) -> Result<Vec<ProofInfo>, Err>;
70
71 async fn get_balance(
73 &self,
74 mint_url: Option<MintUrl>,
75 unit: Option<CurrencyUnit>,
76 state: Option<Vec<State>>,
77 ) -> Result<u64, Err>;
78
79 async fn get_transaction(
81 &self,
82 transaction_id: TransactionId,
83 ) -> Result<Option<Transaction>, Err>;
84
85 async fn list_transactions(
87 &self,
88 mint_url: Option<MintUrl>,
89 direction: Option<TransactionDirection>,
90 unit: Option<CurrencyUnit>,
91 ) -> Result<Vec<Transaction>, Err>;
92
93 async fn update_proofs(
96 &self,
97 added: Vec<ProofInfo>,
98 removed_ys: Vec<PublicKey>,
99 ) -> Result<(), Err>;
100
101 async fn update_proofs_state(&self, ys: Vec<PublicKey>, state: State) -> Result<(), Err>;
103
104 async fn add_transaction(&self, transaction: Transaction) -> Result<(), Err>;
106
107 async fn update_mint_url(
109 &self,
110 old_mint_url: MintUrl,
111 new_mint_url: MintUrl,
112 ) -> Result<(), Err>;
113
114 async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<u32, Err>;
116
117 async fn add_mint(&self, mint_url: MintUrl, mint_info: Option<MintInfo>) -> Result<(), Err>;
119
120 async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Err>;
122
123 async fn add_mint_keysets(
125 &self,
126 mint_url: MintUrl,
127 keysets: Vec<KeySetInfo>,
128 ) -> Result<(), Err>;
129
130 async fn add_mint_quote(&self, quote: WalletMintQuote) -> Result<(), Err>;
132
133 async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Err>;
135
136 async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Err>;
138
139 async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Err>;
141
142 async fn add_keys(&self, keyset: KeySet) -> Result<(), Err>;
144
145 async fn remove_keys(&self, id: &Id) -> Result<(), Err>;
147
148 async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), Err>;
150
151 async fn add_saga(&self, saga: wallet::WalletSaga) -> Result<(), Err>;
156
157 async fn get_saga(&self, id: &uuid::Uuid) -> Result<Option<wallet::WalletSaga>, Err>;
159
160 async fn update_saga(&self, saga: wallet::WalletSaga) -> Result<bool, Err>;
165
166 async fn delete_saga(&self, id: &uuid::Uuid) -> Result<(), Err>;
168
169 async fn get_incomplete_sagas(&self) -> Result<Vec<wallet::WalletSaga>, Err>;
171
172 async fn reserve_proofs(
174 &self,
175 ys: Vec<PublicKey>,
176 operation_id: &uuid::Uuid,
177 ) -> Result<(), Err>;
178
179 async fn release_proofs(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
181
182 async fn get_reserved_proofs(&self, operation_id: &uuid::Uuid) -> Result<Vec<ProofInfo>, Err>;
184
185 async fn reserve_melt_quote(
187 &self,
188 quote_id: &str,
189 operation_id: &uuid::Uuid,
190 ) -> Result<(), Err>;
191
192 async fn release_melt_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
194
195 async fn reserve_mint_quote(
197 &self,
198 quote_id: &str,
199 operation_id: &uuid::Uuid,
200 ) -> Result<(), Err>;
201
202 async fn release_mint_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
204
205 async fn kv_read(
207 &self,
208 primary_namespace: &str,
209 secondary_namespace: &str,
210 key: &str,
211 ) -> Result<Option<Vec<u8>>, Err>;
212
213 async fn kv_list(
215 &self,
216 primary_namespace: &str,
217 secondary_namespace: &str,
218 ) -> Result<Vec<String>, Err>;
219
220 async fn kv_write(
222 &self,
223 primary_namespace: &str,
224 secondary_namespace: &str,
225 key: &str,
226 value: &[u8],
227 ) -> Result<(), Err>;
228
229 async fn kv_remove(
231 &self,
232 primary_namespace: &str,
233 secondary_namespace: &str,
234 key: &str,
235 ) -> Result<(), Err>;
236}