use std::collections::HashMap;
use std::fmt::Debug;
use async_trait::async_trait;
use bitcoin::bip32::DerivationPath;
use cashu::KeySet;
use super::Error;
use crate::mint_url::MintUrl;
use crate::nuts::{
CurrencyUnit, Id, KeySetInfo, Keys, MintInfo, PublicKey, SpendingConditions, State,
};
use crate::wallet::{
self, MintQuote as WalletMintQuote, ProofInfo, Transaction, TransactionDirection, TransactionId,
};
#[cfg(feature = "test")]
pub mod test;
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait Database<Err>: Debug
where
Err: Into<Error> + From<Error>,
{
async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Err>;
async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Err>;
async fn get_mint_keysets(&self, mint_url: MintUrl) -> Result<Option<Vec<KeySetInfo>>, Err>;
async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Err>;
async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<WalletMintQuote>, Err>;
async fn get_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
async fn get_unissued_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Err>;
async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Err>;
async fn get_melt_quotes(&self) -> Result<Vec<wallet::MeltQuote>, Err>;
async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Err>;
async fn get_proofs(
&self,
mint_url: Option<MintUrl>,
unit: Option<CurrencyUnit>,
state: Option<Vec<State>>,
spending_conditions: Option<Vec<SpendingConditions>>,
) -> Result<Vec<ProofInfo>, Err>;
async fn get_proofs_by_ys(&self, ys: Vec<PublicKey>) -> Result<Vec<ProofInfo>, Err>;
async fn get_balance(
&self,
mint_url: Option<MintUrl>,
unit: Option<CurrencyUnit>,
state: Option<Vec<State>>,
) -> Result<u64, Err>;
async fn get_transaction(
&self,
transaction_id: TransactionId,
) -> Result<Option<Transaction>, Err>;
async fn list_transactions(
&self,
mint_url: Option<MintUrl>,
direction: Option<TransactionDirection>,
unit: Option<CurrencyUnit>,
) -> Result<Vec<Transaction>, Err>;
async fn update_proofs(
&self,
added: Vec<ProofInfo>,
removed_ys: Vec<PublicKey>,
) -> Result<(), Err>;
async fn update_proofs_state(&self, ys: Vec<PublicKey>, state: State) -> Result<(), Err>;
async fn add_transaction(&self, transaction: Transaction) -> Result<(), Err>;
async fn update_mint_url(
&self,
old_mint_url: MintUrl,
new_mint_url: MintUrl,
) -> Result<(), Err>;
async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<u32, Err>;
async fn add_mint(&self, mint_url: MintUrl, mint_info: Option<MintInfo>) -> Result<(), Err>;
async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Err>;
async fn add_mint_keysets(
&self,
mint_url: MintUrl,
keysets: Vec<KeySetInfo>,
) -> Result<(), Err>;
async fn add_mint_quote(&self, quote: WalletMintQuote) -> Result<(), Err>;
async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Err>;
async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Err>;
async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Err>;
async fn add_keys(&self, keyset: KeySet) -> Result<(), Err>;
async fn remove_keys(&self, id: &Id) -> Result<(), Err>;
async fn remove_transaction(&self, transaction_id: TransactionId) -> Result<(), Err>;
async fn add_saga(&self, saga: wallet::WalletSaga) -> Result<(), Err>;
async fn get_saga(&self, id: &uuid::Uuid) -> Result<Option<wallet::WalletSaga>, Err>;
async fn update_saga(&self, saga: wallet::WalletSaga) -> Result<bool, Err>;
async fn delete_saga(&self, id: &uuid::Uuid) -> Result<(), Err>;
async fn get_incomplete_sagas(&self) -> Result<Vec<wallet::WalletSaga>, Err>;
async fn reserve_proofs(
&self,
ys: Vec<PublicKey>,
operation_id: &uuid::Uuid,
) -> Result<(), Err>;
async fn release_proofs(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
async fn get_reserved_proofs(&self, operation_id: &uuid::Uuid) -> Result<Vec<ProofInfo>, Err>;
async fn reserve_melt_quote(
&self,
quote_id: &str,
operation_id: &uuid::Uuid,
) -> Result<(), Err>;
async fn release_melt_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
async fn reserve_mint_quote(
&self,
quote_id: &str,
operation_id: &uuid::Uuid,
) -> Result<(), Err>;
async fn release_mint_quote(&self, operation_id: &uuid::Uuid) -> Result<(), Err>;
async fn kv_read(
&self,
primary_namespace: &str,
secondary_namespace: &str,
key: &str,
) -> Result<Option<Vec<u8>>, Err>;
async fn kv_list(
&self,
primary_namespace: &str,
secondary_namespace: &str,
) -> Result<Vec<String>, Err>;
async fn kv_write(
&self,
primary_namespace: &str,
secondary_namespace: &str,
key: &str,
value: &[u8],
) -> Result<(), Err>;
async fn kv_remove(
&self,
primary_namespace: &str,
secondary_namespace: &str,
key: &str,
) -> Result<(), Err>;
async fn add_p2pk_key(
&self,
pubkey: &PublicKey,
derivation_path: DerivationPath,
derivation_index: u32,
) -> Result<(), Err>;
async fn get_p2pk_key(&self, pubkey: &PublicKey)
-> Result<Option<wallet::P2PKSigningKey>, Err>;
async fn list_p2pk_keys(&self) -> Result<Vec<wallet::P2PKSigningKey>, Err>;
async fn latest_p2pk(&self) -> Result<Option<wallet::P2PKSigningKey>, Err>;
}