Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions crates/cdk-common/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ pub use mint::{
#[cfg(all(feature = "mint", feature = "auth"))]
pub use mint::{DynMintAuthDatabase, MintAuthDatabase, MintAuthTransaction};
#[cfg(feature = "wallet")]
pub use wallet::{
Database as WalletDatabase, DatabaseTransaction as WalletDatabaseTransaction,
DynWalletDatabaseTransaction,
};
pub use wallet::Database as WalletDatabase;

/// A wrapper indicating that a resource has been acquired with a database lock.
///
Expand Down
181 changes: 79 additions & 102 deletions crates/cdk-common/src/database/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use std::fmt::Debug;
use async_trait::async_trait;
use cashu::KeySet;

use super::{DbTransactionFinalizer, Error};
use super::Error;
use crate::common::ProofInfo;
use crate::database::{KVStoreDatabase, KVStoreTransaction};
use crate::database::KVStoreDatabase;
use crate::mint_url::MintUrl;
use crate::nuts::{
CurrencyUnit, Id, KeySetInfo, Keys, MintInfo, PublicKey, SpendingConditions, State,
Expand All @@ -20,113 +20,13 @@ use crate::wallet::{
#[cfg(feature = "test")]
pub mod test;

/// Easy to use Dynamic Database type alias
pub type DynWalletDatabaseTransaction = Box<dyn DatabaseTransaction<super::Error> + Sync + Send>;

/// Database transaction
///
/// This trait encapsulates all the changes to be done in the wallet
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait DatabaseTransaction<Error>:
KVStoreTransaction<Error> + DbTransactionFinalizer<Err = Error>
{
/// Add Mint to storage
async fn add_mint(
&mut self,
mint_url: MintUrl,
mint_info: Option<MintInfo>,
) -> Result<(), Error>;

/// Remove Mint from storage
async fn remove_mint(&mut self, mint_url: MintUrl) -> Result<(), Error>;

/// Update mint url
async fn update_mint_url(
&mut self,
old_mint_url: MintUrl,
new_mint_url: MintUrl,
) -> Result<(), Error>;

/// Get mint keyset by id
async fn get_keyset_by_id(&mut self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Error>;

/// Get [`Keys`] from storage
async fn get_keys(&mut self, id: &Id) -> Result<Option<Keys>, Error>;

/// Add mint keyset to storage
async fn add_mint_keysets(
&mut self,
mint_url: MintUrl,
keysets: Vec<KeySetInfo>,
) -> Result<(), Error>;

/// Get mint quote from storage. This function locks the returned minted quote for update
async fn get_mint_quote(&mut self, quote_id: &str) -> Result<Option<WalletMintQuote>, Error>;

/// Add mint quote to storage
async fn add_mint_quote(&mut self, quote: WalletMintQuote) -> Result<(), Error>;

/// Remove mint quote from storage
async fn remove_mint_quote(&mut self, quote_id: &str) -> Result<(), Error>;

/// Get melt quote from storage
async fn get_melt_quote(&mut self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Error>;

/// Add melt quote to storage
async fn add_melt_quote(&mut self, quote: wallet::MeltQuote) -> Result<(), Error>;

/// Remove melt quote from storage
async fn remove_melt_quote(&mut self, quote_id: &str) -> Result<(), Error>;

/// Add [`Keys`] to storage
async fn add_keys(&mut self, keyset: KeySet) -> Result<(), Error>;

/// Remove [`Keys`] from storage
async fn remove_keys(&mut self, id: &Id) -> Result<(), Error>;

/// Get proofs from storage and lock them for update
async fn get_proofs(
&mut self,
mint_url: Option<MintUrl>,
unit: Option<CurrencyUnit>,
state: Option<Vec<State>>,
spending_conditions: Option<Vec<SpendingConditions>>,
) -> Result<Vec<ProofInfo>, Error>;

/// Update the proofs in storage by adding new proofs or removing proofs by
/// their Y value.
async fn update_proofs(
&mut self,
added: Vec<ProofInfo>,
removed_ys: Vec<PublicKey>,
) -> Result<(), Error>;

/// Update proofs state in storage
async fn update_proofs_state(&mut self, ys: Vec<PublicKey>, state: State) -> Result<(), Error>;

/// Atomically increment Keyset counter and return new value
async fn increment_keyset_counter(&mut self, keyset_id: &Id, count: u32) -> Result<u32, Error>;

/// Add transaction to storage
async fn add_transaction(&mut self, transaction: Transaction) -> Result<(), Error>;

/// Remove transaction from storage
async fn remove_transaction(&mut self, transaction_id: TransactionId) -> Result<(), Error>;
}

/// Wallet Database trait
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait Database<Err>: KVStoreDatabase<Err = Err> + Debug
where
Err: Into<Error> + From<Error>,
{
/// Begins a DB transaction
async fn begin_db_transaction(
&self,
) -> Result<Box<dyn DatabaseTransaction<Err> + Send + Sync>, Err>;

/// Get mint from storage
async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Err>;

Expand Down Expand Up @@ -191,4 +91,81 @@ where
direction: Option<TransactionDirection>,
unit: Option<CurrencyUnit>,
) -> Result<Vec<Transaction>, Err>;

/// Update the proofs in storage by adding new proofs or removing proofs by
/// their Y value (without transaction)
async fn update_proofs(
&self,
added: Vec<ProofInfo>,
removed_ys: Vec<PublicKey>,
) -> Result<(), Err>;

/// Update proofs state in storage (without transaction)
async fn update_proofs_state(&self, ys: Vec<PublicKey>, state: State) -> Result<(), Err>;

/// Add transaction to storage (without transaction)
async fn add_transaction(&self, transaction: Transaction) -> Result<(), Err>;

/// Update mint url (without transaction)
async fn update_mint_url(
&self,
old_mint_url: MintUrl,
new_mint_url: MintUrl,
) -> Result<(), Err>;

/// Atomically increment Keyset counter and return new value (without transaction)
async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<u32, Err>;

/// Add Mint to storage
async fn add_mint(&self, mint_url: MintUrl, mint_info: Option<MintInfo>) -> Result<(), Err>;

/// Remove Mint from storage
async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Err>;

/// Add mint keyset to storage
async fn add_mint_keysets(
&self,
mint_url: MintUrl,
keysets: Vec<KeySetInfo>,
) -> Result<(), Err>;

/// Add mint quote to storage
async fn add_mint_quote(&self, quote: WalletMintQuote) -> Result<(), Err>;

/// Remove mint quote from storage
async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Err>;

/// Add melt quote to storage
async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Err>;

/// Remove melt quote from storage
async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Err>;

/// Add [`Keys`] to storage
async fn add_keys(&self, keyset: KeySet) -> Result<(), Err>;

/// Remove [`Keys`] from storage
async fn remove_keys(&self, id: &Id) -> Result<(), Err>;

/// Remove transaction from storage
async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), Err>;

// KV Store write methods (non-transactional)

/// Write a value to the key-value store
async fn kv_write(
&self,
primary_namespace: &str,
secondary_namespace: &str,
key: &str,
value: &[u8],
) -> Result<(), Err>;

/// Remove a value from the key-value store
async fn kv_remove(
&self,
primary_namespace: &str,
secondary_namespace: &str,
key: &str,
) -> Result<(), Err>;
}
Loading