Skip to main content

cdk_common/database/mint/auth/
mod.rs

1//! Mint in memory database use std::collections::HashMap;
2
3use std::collections::HashMap;
4
5use async_trait::async_trait;
6use cashu::{AuthRequired, ProtectedEndpoint};
7
8use super::DbTransactionFinalizer;
9use crate::database::Error;
10use crate::mint::MintKeySetInfo;
11use crate::nuts::nut07::State;
12use crate::nuts::{AuthProof, BlindSignature, Id, PublicKey};
13
14/// Mint Database transaction
15#[async_trait]
16pub trait MintAuthTransaction<Error>: DbTransactionFinalizer<Err = Error> {
17    /// Add Active Keyset
18    async fn set_active_keyset(&mut self, id: Id) -> Result<(), Error>;
19
20    /// Add [`MintKeySetInfo`]
21    async fn add_keyset_info(&mut self, keyset: MintKeySetInfo) -> Result<(), Error>;
22
23    /// Add spent [`AuthProof`]
24    async fn add_proof(&mut self, proof: AuthProof) -> Result<(), Error>;
25
26    /// Update [`AuthProof`]s state
27    async fn update_proof_state(
28        &mut self,
29        y: &PublicKey,
30        proofs_state: State,
31    ) -> Result<Option<State>, Error>;
32
33    /// Add [`BlindSignature`]
34    async fn add_blind_signatures(
35        &mut self,
36        blinded_messages: &[PublicKey],
37        blind_signatures: &[BlindSignature],
38    ) -> Result<(), Error>;
39
40    /// Add protected endpoints
41    async fn add_protected_endpoints(
42        &mut self,
43        protected_endpoints: HashMap<ProtectedEndpoint, AuthRequired>,
44    ) -> Result<(), Error>;
45
46    /// Removed Protected endpoints
47    async fn remove_protected_endpoints(
48        &mut self,
49        protected_endpoints: Vec<ProtectedEndpoint>,
50    ) -> Result<(), Error>;
51}
52
53/// Mint Database trait
54#[async_trait]
55pub trait MintAuthDatabase {
56    /// Mint Database Error
57    type Err: Into<Error> + From<Error>;
58
59    /// Begins a transaction
60    async fn begin_transaction<'a>(
61        &'a self,
62    ) -> Result<Box<dyn MintAuthTransaction<Self::Err> + Send + Sync + 'a>, Self::Err>;
63
64    /// Get Active Keyset
65    async fn get_active_keyset_id(&self) -> Result<Option<Id>, Self::Err>;
66
67    /// Get [`MintKeySetInfo`]
68    async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err>;
69    /// Get [`MintKeySetInfo`]s
70    async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err>;
71
72    /// Get [`AuthProof`] state
73    async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err>;
74
75    /// Get [`BlindSignature`]s
76    async fn get_blind_signatures(
77        &self,
78        blinded_messages: &[PublicKey],
79    ) -> Result<Vec<Option<BlindSignature>>, Self::Err>;
80
81    /// Get auth for protected_endpoint
82    async fn get_auth_for_endpoint(
83        &self,
84        protected_endpoint: ProtectedEndpoint,
85    ) -> Result<Option<AuthRequired>, Self::Err>;
86    /// Get protected endpoints
87    async fn get_auth_for_endpoints(
88        &self,
89    ) -> Result<HashMap<ProtectedEndpoint, Option<AuthRequired>>, Self::Err>;
90}
91
92/// Type alias for trait objects
93pub type DynMintAuthDatabase =
94    std::sync::Arc<dyn MintAuthDatabase<Err = super::Error> + Send + Sync>;