cdk_common/database/wallet/
mod.rs1use std::collections::HashMap;
4use std::fmt::Debug;
5
6use async_trait::async_trait;
7use bitcoin::bip32::DerivationPath;
8use cashu::KeySet;
9
10use super::Error;
11use crate::mint_url::MintUrl;
12use crate::nuts::{
13 CurrencyUnit, Id, KeySetInfo, Keys, MintInfo, PublicKey, SpendingConditions, State,
14};
15use crate::wallet::{
16 self, MintQuote as WalletMintQuote, ProofInfo, Transaction, TransactionDirection, TransactionId,
17};
18
19#[cfg(feature = "test")]
20pub mod test;
21
22#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
24#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
25pub trait Database<Err>: Debug
26where
27 Err: Into<Error> + From<Error>,
28{
29 async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Err>;
31
32 async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Err>;
34
35 async fn get_mint_keysets(&self, mint_url: MintUrl) -> Result<Option<Vec<KeySetInfo>>, Err>;
37
38 async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Err>;
40
41 async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<WalletMintQuote>, Err>;
43
44 async fn get_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
46 async fn get_unissued_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
50
51 async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Err>;
53
54 async fn get_melt_quotes(&self) -> Result<Vec<wallet::MeltQuote>, Err>;
56
57 async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Err>;
59
60 async fn get_proofs(
62 &self,
63 mint_url: Option<MintUrl>,
64 unit: Option<CurrencyUnit>,
65 state: Option<Vec<State>>,
66 spending_conditions: Option<Vec<SpendingConditions>>,
67 ) -> Result<Vec<ProofInfo>, Err>;
68
69 async fn get_proofs_by_ys(&self, ys: Vec<PublicKey>) -> Result<Vec<ProofInfo>, Err>;
71
72 async fn get_balance(
74 &self,
75 mint_url: Option<MintUrl>,
76 unit: Option<CurrencyUnit>,
77 state: Option<Vec<State>>,
78 ) -> Result<u64, Err>;
79
80 async fn get_transaction(
82 &self,
83 transaction_id: TransactionId,
84 ) -> Result<Option<Transaction>, Err>;
85
86 async fn list_transactions(
88 &self,
89 mint_url: Option<MintUrl>,
90 direction: Option<TransactionDirection>,
91 unit: Option<CurrencyUnit>,
92 ) -> Result<Vec<Transaction>, Err>;
93
94 async fn update_proofs(
97 &self,
98 added: Vec<ProofInfo>,
99 removed_ys: Vec<PublicKey>,
100 ) -> Result<(), Err>;
101
102 async fn update_proofs_state(&self, ys: Vec<PublicKey>, state: State) -> Result<(), Err>;
104
105 async fn add_transaction(&self, transaction: Transaction) -> Result<(), Err>;
107
108 async fn update_mint_url(
110 &self,
111 old_mint_url: MintUrl,
112 new_mint_url: MintUrl,
113 ) -> Result<(), Err>;
114
115 async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<u32, Err>;
117
118 async fn add_mint(&self, mint_url: MintUrl, mint_info: Option<MintInfo>) -> Result<(), Err>;
120
121 async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Err>;
123
124 async fn add_mint_keysets(
126 &self,
127 mint_url: MintUrl,
128 keysets: Vec<KeySetInfo>,
129 ) -> Result<(), Err>;
130
131 async fn add_mint_quote(&self, quote: WalletMintQuote) -> Result<(), Err>;
133
134 async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Err>;
136
137 async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Err>;
139
140 async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Err>;
142
143 async fn add_keys(&self, keyset: KeySet) -> Result<(), Err>;
145
146 async fn remove_keys(&self, id: &Id) -> Result<(), Err>;
148
149 async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), Err>;
151
152 async fn add_saga(&self, saga: wallet::WalletSaga) -> Result<(), Err>;
157
158 async fn get_saga(&self, id: &uuid::Uuid) -> Result<Option<wallet::WalletSaga>, Err>;
160
161 async fn update_saga(&self, saga: wallet::WalletSaga) -> Result<bool, Err>;
166
167 async fn delete_saga(&self, id: &uuid::Uuid) -> Result<(), Err>;
169
170 async fn get_incomplete_sagas(&self) -> Result<Vec<wallet::WalletSaga>, Err>;
172
173 async fn reserve_proofs(
175 &self,
176 ys: Vec<PublicKey>,
177 operation_id: &uuid::Uuid,
178 ) -> Result<(), Err>;
179
180 async fn release_proofs(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
182
183 async fn get_reserved_proofs(&self, operation_id: &uuid::Uuid) -> Result<Vec<ProofInfo>, Err>;
185
186 async fn reserve_melt_quote(
188 &self,
189 quote_id: &str,
190 operation_id: &uuid::Uuid,
191 ) -> Result<(), Err>;
192
193 async fn release_melt_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
195
196 async fn reserve_mint_quote(
198 &self,
199 quote_id: &str,
200 operation_id: &uuid::Uuid,
201 ) -> Result<(), Err>;
202
203 async fn release_mint_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
205
206 async fn kv_read(
208 &self,
209 primary_namespace: &str,
210 secondary_namespace: &str,
211 key: &str,
212 ) -> Result<Option<Vec<u8>>, Err>;
213
214 async fn kv_list(
216 &self,
217 primary_namespace: &str,
218 secondary_namespace: &str,
219 ) -> Result<Vec<String>, Err>;
220
221 async fn kv_write(
223 &self,
224 primary_namespace: &str,
225 secondary_namespace: &str,
226 key: &str,
227 value: &[u8],
228 ) -> Result<(), Err>;
229
230 async fn kv_remove(
232 &self,
233 primary_namespace: &str,
234 secondary_namespace: &str,
235 key: &str,
236 ) -> Result<(), Err>;
237
238 async fn add_p2pk_key(
242 &self,
243 pubkey: &PublicKey,
244 derivation_path: DerivationPath,
245 derivation_index: u32,
246 ) -> Result<(), Err>;
247
248 async fn get_p2pk_key(&self, pubkey: &PublicKey)
250 -> Result<Option<wallet::P2PKSigningKey>, Err>;
251
252 async fn list_p2pk_keys(&self) -> Result<Vec<wallet::P2PKSigningKey>, Err>;
254
255 async fn latest_p2pk(&self) -> Result<Option<wallet::P2PKSigningKey>, Err>;
257}