#[cfg(any(feature = "wallet", feature = "mint"))]
use std::collections::HashMap;
use std::fmt::Debug;
#[cfg(any(feature = "wallet", feature = "mint"))]
use async_trait::async_trait;
use thiserror::Error;
#[cfg(feature = "mint")]
use uuid::Uuid;
#[cfg(feature = "mint")]
use crate::mint;
#[cfg(feature = "mint")]
use crate::mint::MintKeySetInfo;
#[cfg(feature = "mint")]
use crate::mint::MintQuote as MintMintQuote;
#[cfg(feature = "wallet")]
use crate::mint_url::MintUrl;
#[cfg(feature = "mint")]
use crate::nuts::MeltBolt11Request;
#[cfg(feature = "mint")]
use crate::nuts::{BlindSignature, MeltQuoteState, MintQuoteState, Proof, Proofs};
#[cfg(any(feature = "wallet", feature = "mint"))]
use crate::nuts::{CurrencyUnit, Id, PublicKey, State};
#[cfg(feature = "wallet")]
use crate::nuts::{KeySetInfo, Keys, MintInfo, SpendingConditions};
#[cfg(feature = "mint")]
use crate::types::LnKey;
#[cfg(feature = "wallet")]
use crate::types::ProofInfo;
#[cfg(feature = "wallet")]
use crate::wallet;
#[cfg(feature = "wallet")]
use crate::wallet::MintQuote as WalletMintQuote;
#[cfg(feature = "mint")]
pub mod mint_memory;
#[cfg(feature = "wallet")]
pub mod wallet_memory;
#[cfg(feature = "wallet")]
pub use wallet_memory::WalletMemoryDatabase;
#[derive(Debug, Error)]
pub enum Error {
#[error(transparent)]
Database(Box<dyn std::error::Error + Send + Sync>),
#[error(transparent)]
DHKE(#[from] crate::dhke::Error),
#[error(transparent)]
NUT00(#[from] crate::nuts::nut00::Error),
#[error(transparent)]
NUT02(#[from] crate::nuts::nut02::Error),
#[error(transparent)]
Serde(#[from] serde_json::Error),
#[error("Unknown Quote")]
UnknownQuote,
}
#[cfg(feature = "wallet")]
#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
pub trait WalletDatabase: Debug {
type Err: Into<Error> + From<Error>;
async fn add_mint(
&self,
mint_url: MintUrl,
mint_info: Option<MintInfo>,
) -> Result<(), Self::Err>;
async fn remove_mint(&self, mint_url: MintUrl) -> Result<(), Self::Err>;
async fn get_mint(&self, mint_url: MintUrl) -> Result<Option<MintInfo>, Self::Err>;
async fn get_mints(&self) -> Result<HashMap<MintUrl, Option<MintInfo>>, Self::Err>;
async fn update_mint_url(
&self,
old_mint_url: MintUrl,
new_mint_url: MintUrl,
) -> Result<(), Self::Err>;
async fn add_mint_keysets(
&self,
mint_url: MintUrl,
keysets: Vec<KeySetInfo>,
) -> Result<(), Self::Err>;
async fn get_mint_keysets(
&self,
mint_url: MintUrl,
) -> Result<Option<Vec<KeySetInfo>>, Self::Err>;
async fn get_keyset_by_id(&self, keyset_id: &Id) -> Result<Option<KeySetInfo>, Self::Err>;
async fn add_mint_quote(&self, quote: WalletMintQuote) -> Result<(), Self::Err>;
async fn get_mint_quote(&self, quote_id: &str) -> Result<Option<WalletMintQuote>, Self::Err>;
async fn get_mint_quotes(&self) -> Result<Vec<WalletMintQuote>, Self::Err>;
async fn remove_mint_quote(&self, quote_id: &str) -> Result<(), Self::Err>;
async fn add_melt_quote(&self, quote: wallet::MeltQuote) -> Result<(), Self::Err>;
async fn get_melt_quote(&self, quote_id: &str) -> Result<Option<wallet::MeltQuote>, Self::Err>;
async fn remove_melt_quote(&self, quote_id: &str) -> Result<(), Self::Err>;
async fn add_keys(&self, keys: Keys) -> Result<(), Self::Err>;
async fn get_keys(&self, id: &Id) -> Result<Option<Keys>, Self::Err>;
async fn remove_keys(&self, id: &Id) -> Result<(), Self::Err>;
async fn update_proofs(
&self,
added: Vec<ProofInfo>,
removed_ys: Vec<PublicKey>,
) -> Result<(), Self::Err>;
async fn set_pending_proofs(&self, ys: Vec<PublicKey>) -> Result<(), Self::Err>;
async fn reserve_proofs(&self, ys: Vec<PublicKey>) -> Result<(), Self::Err>;
async fn set_unspent_proofs(&self, ys: Vec<PublicKey>) -> Result<(), Self::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>, Self::Err>;
async fn increment_keyset_counter(&self, keyset_id: &Id, count: u32) -> Result<(), Self::Err>;
async fn get_keyset_counter(&self, keyset_id: &Id) -> Result<Option<u32>, Self::Err>;
async fn get_nostr_last_checked(
&self,
verifying_key: &PublicKey,
) -> Result<Option<u32>, Self::Err>;
async fn add_nostr_last_checked(
&self,
verifying_key: PublicKey,
last_checked: u32,
) -> Result<(), Self::Err>;
}
#[cfg(feature = "mint")]
#[async_trait]
pub trait MintDatabase {
type Err: Into<Error> + From<Error>;
async fn set_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err>;
async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err>;
async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err>;
async fn add_mint_quote(&self, quote: MintMintQuote) -> Result<(), Self::Err>;
async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintMintQuote>, Self::Err>;
async fn update_mint_quote_state(
&self,
quote_id: &Uuid,
state: MintQuoteState,
) -> Result<MintQuoteState, Self::Err>;
async fn get_mint_quote_by_request(
&self,
request: &str,
) -> Result<Option<MintMintQuote>, Self::Err>;
async fn get_mint_quote_by_request_lookup_id(
&self,
request_lookup_id: &str,
) -> Result<Option<MintMintQuote>, Self::Err>;
async fn get_mint_quotes(&self) -> Result<Vec<MintMintQuote>, Self::Err>;
async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
async fn add_melt_quote(&self, quote: mint::MeltQuote) -> Result<(), Self::Err>;
async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err>;
async fn update_melt_quote_state(
&self,
quote_id: &Uuid,
state: MeltQuoteState,
) -> Result<MeltQuoteState, Self::Err>;
async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err>;
async fn remove_melt_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err>;
async fn add_melt_request(
&self,
melt_request: MeltBolt11Request<Uuid>,
ln_key: LnKey,
) -> Result<(), Self::Err>;
async fn get_melt_request(
&self,
quote_id: &Uuid,
) -> Result<Option<(MeltBolt11Request<Uuid>, LnKey)>, Self::Err>;
async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err>;
async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
async fn add_proofs(&self, proof: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err>;
async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err>;
async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err>;
async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
async fn update_proofs_states(
&self,
ys: &[PublicKey],
proofs_state: State,
) -> Result<Vec<Option<State>>, Self::Err>;
async fn get_proofs_by_keyset_id(
&self,
keyset_id: &Id,
) -> Result<(Proofs, Vec<Option<State>>), Self::Err>;
async fn add_blind_signatures(
&self,
blinded_messages: &[PublicKey],
blind_signatures: &[BlindSignature],
quote_id: Option<Uuid>,
) -> Result<(), Self::Err>;
async fn get_blind_signatures(
&self,
blinded_messages: &[PublicKey],
) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
async fn get_blind_signatures_for_keyset(
&self,
keyset_id: &Id,
) -> Result<Vec<BlindSignature>, Self::Err>;
async fn get_blind_signatures_for_quote(
&self,
quote_id: &Uuid,
) -> Result<Vec<BlindSignature>, Self::Err>;
}