Skip to main content

cdk_common/database/wallet/
mod.rs

1//! CDK Database
2
3use 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/// Wallet Database trait
23#[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    /// Get mint from storage
30    async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Err>;
31
32    /// Get all mints from storage
33    async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Err>;
34
35    /// Get mint keysets for mint url
36    async fn get_mint_keysets(&self, mint_url: MintUrl) -> Result<Option<Vec<KeySetInfo>>, Err>;
37
38    /// Get mint keyset by id
39    async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Err>;
40
41    /// Get mint quote from storage
42    async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<WalletMintQuote>, Err>;
43
44    /// Get mint quotes from storage
45    async fn get_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
46    /// Get unissued mint quotes from storage
47    /// Returns bolt11 quotes where nothing has been issued yet (amount_issued = 0) and all bolt12 quotes.
48    /// Includes unpaid bolt11 quotes to allow checking with the mint if they've been paid (wallet state may be outdated).
49    async fn get_unissued_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
50
51    /// Get melt quote from storage
52    async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Err>;
53
54    /// Get melt quotes from storage
55    async fn get_melt_quotes(&self) -> Result<Vec<wallet::MeltQuote>, Err>;
56
57    /// Get [`Keys`] from storage
58    async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Err>;
59
60    /// Get proofs from storage
61    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    /// Get proofs by Y values
70    async fn get_proofs_by_ys(&self, ys: Vec<PublicKey>) -> Result<Vec<ProofInfo>, Err>;
71
72    /// Get balance
73    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    /// Get transaction from storage
81    async fn get_transaction(
82        &self,
83        transaction_id: TransactionId,
84    ) -> Result<Option<Transaction>, Err>;
85
86    /// List transactions from storage
87    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    /// Update the proofs in storage by adding new proofs or removing proofs by
95    /// their Y value
96    async fn update_proofs(
97        &self,
98        added: Vec<ProofInfo>,
99        removed_ys: Vec<PublicKey>,
100    ) -> Result<(), Err>;
101
102    /// Update proofs state in storage
103    async fn update_proofs_state(&self, ys: Vec<PublicKey>, state: State) -> Result<(), Err>;
104
105    /// Add transaction to storage
106    async fn add_transaction(&self, transaction: Transaction) -> Result<(), Err>;
107
108    /// Update mint url
109    async fn update_mint_url(
110        &self,
111        old_mint_url: MintUrl,
112        new_mint_url: MintUrl,
113    ) -> Result<(), Err>;
114
115    /// Atomically increment Keyset counter and return new value
116    async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<u32, Err>;
117
118    /// Add Mint to storage
119    async fn add_mint(&self, mint_url: MintUrl, mint_info: Option<MintInfo>) -> Result<(), Err>;
120
121    /// Remove Mint from storage
122    async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Err>;
123
124    /// Add mint keyset to storage
125    async fn add_mint_keysets(
126        &self,
127        mint_url: MintUrl,
128        keysets: Vec<KeySetInfo>,
129    ) -> Result<(), Err>;
130
131    /// Add mint quote to storage
132    async fn add_mint_quote(&self, quote: WalletMintQuote) -> Result<(), Err>;
133
134    /// Remove mint quote from storage
135    async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Err>;
136
137    /// Add melt quote to storage
138    async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Err>;
139
140    /// Remove melt quote from storage
141    async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Err>;
142
143    /// Add [`Keys`] to storage
144    async fn add_keys(&self, keyset: KeySet) -> Result<(), Err>;
145
146    /// Remove [`Keys`] from storage
147    async fn remove_keys(&self, id: &Id) -> Result<(), Err>;
148
149    /// Remove transaction from storage
150    async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), Err>;
151
152    /// Add a wallet saga to storage.
153    ///
154    /// The saga should be created with `WalletSaga::new()` which initializes
155    /// `version = 0`. This is the starting point for optimistic locking.
156    async fn add_saga(&self, saga: wallet::WalletSaga) -> Result<(), Err>;
157
158    /// Get a wallet saga by ID.
159    async fn get_saga(&self, id: &uuid::Uuid) -> Result<Option<wallet::WalletSaga>, Err>;
160
161    /// Update a wallet saga with optimistic locking.
162    ///
163    /// Returns `Ok(true)` if the update succeeded (version match), or `Ok(false)`
164    /// if another instance modified the saga first (version mismatch).
165    async fn update_saga(&self, saga: wallet::WalletSaga) -> Result<bool, Err>;
166
167    /// Delete a wallet saga.
168    async fn delete_saga(&self, id: &uuid::Uuid) -> Result<(), Err>;
169
170    /// Get all incomplete sagas.
171    async fn get_incomplete_sagas(&self) -> Result<Vec<wallet::WalletSaga>, Err>;
172
173    /// Reserve proofs for an operation
174    async fn reserve_proofs(
175        &self,
176        ys: Vec<PublicKey>,
177        operation_id: &uuid::Uuid,
178    ) -> Result<(), Err>;
179
180    /// Release proofs reserved by an operation
181    async fn release_proofs(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
182
183    /// Get proofs reserved by an operation
184    async fn get_reserved_proofs(&self, operation_id: &uuid::Uuid) -> Result<Vec<ProofInfo>, Err>;
185
186    /// Reserve a melt quote for an operation.
187    async fn reserve_melt_quote(
188        &self,
189        quote_id: &str,
190        operation_id: &uuid::Uuid,
191    ) -> Result<(), Err>;
192
193    /// Release a melt quote reserved by an operation.
194    async fn release_melt_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
195
196    /// Reserve a mint quote for an operation.
197    async fn reserve_mint_quote(
198        &self,
199        quote_id: &str,
200        operation_id: &uuid::Uuid,
201    ) -> Result<(), Err>;
202
203    /// Release a mint quote reserved by an operation.
204    async fn release_mint_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
205
206    /// Read a value from the key-value store
207    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    /// List keys in a namespace
215    async fn kv_list(
216        &self,
217        primary_namespace: &str,
218        secondary_namespace: &str,
219    ) -> Result<Vec<String>, Err>;
220
221    /// Write a value to the key-value store
222    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    /// Remove a value from the key-value store
231    async fn kv_remove(
232        &self,
233        primary_namespace: &str,
234        secondary_namespace: &str,
235        key: &str,
236    ) -> Result<(), Err>;
237
238    // P2PK signing key methods
239
240    /// Store a P2PK signing key for the wallet
241    async fn add_p2pk_key(
242        &self,
243        pubkey: &PublicKey,
244        derivation_path: DerivationPath,
245        derivation_index: u32,
246    ) -> Result<(), Err>;
247
248    /// Get a stored P2PK signing key by pubkey.
249    async fn get_p2pk_key(&self, pubkey: &PublicKey)
250        -> Result<Option<wallet::P2PKSigningKey>, Err>;
251
252    /// List all stored P2PK signing keys.
253    async fn list_p2pk_keys(&self) -> Result<Vec<wallet::P2PKSigningKey>, Err>;
254
255    /// Tries to get the latest p2pk key generated
256    async fn latest_p2pk(&self) -> Result<Option<wallet::P2PKSigningKey>, Err>;
257}