mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-09 23:26:10 +01:00
feat: store mint info
This commit is contained in:
@@ -3,15 +3,16 @@ use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use cashu::nuts::nut02::mint::KeySet;
|
||||
use cashu::nuts::{CurrencyUnit, Id, Proof, Proofs};
|
||||
use cashu::nuts::{CurrencyUnit, Id, MintInfo, Proof, Proofs};
|
||||
use cashu::secret::Secret;
|
||||
use cashu::types::{MeltQuote, MintQuote};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use super::{Error, LocalStore};
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct MemoryLocalStore {
|
||||
mint_info: Arc<Mutex<MintInfo>>,
|
||||
active_keysets: Arc<Mutex<HashMap<CurrencyUnit, Id>>>,
|
||||
keysets: Arc<Mutex<HashMap<Id, KeySet>>>,
|
||||
mint_quotes: Arc<Mutex<HashMap<String, MintQuote>>>,
|
||||
@@ -22,6 +23,7 @@ pub struct MemoryLocalStore {
|
||||
|
||||
impl MemoryLocalStore {
|
||||
pub fn new(
|
||||
mint_info: MintInfo,
|
||||
active_keysets: HashMap<CurrencyUnit, Id>,
|
||||
keysets: Vec<KeySet>,
|
||||
mint_quotes: Vec<MintQuote>,
|
||||
@@ -30,6 +32,7 @@ impl MemoryLocalStore {
|
||||
spent_proofs: Proofs,
|
||||
) -> Self {
|
||||
Self {
|
||||
mint_info: Arc::new(Mutex::new(mint_info)),
|
||||
active_keysets: Arc::new(Mutex::new(active_keysets)),
|
||||
keysets: Arc::new(Mutex::new(keysets.into_iter().map(|k| (k.id, k)).collect())),
|
||||
mint_quotes: Arc::new(Mutex::new(
|
||||
@@ -56,6 +59,14 @@ impl MemoryLocalStore {
|
||||
|
||||
#[async_trait]
|
||||
impl LocalStore for MemoryLocalStore {
|
||||
async fn set_mint_info(&self, mint_info: &MintInfo) -> Result<(), Error> {
|
||||
let mut mi = self.mint_info.lock().await;
|
||||
*mi = mint_info.clone();
|
||||
Ok(())
|
||||
}
|
||||
async fn get_mint_info(&self) -> Result<MintInfo, Error> {
|
||||
Ok(self.mint_info.lock().await.clone())
|
||||
}
|
||||
async fn add_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Error> {
|
||||
self.active_keysets.lock().await.insert(unit, id);
|
||||
Ok(())
|
||||
|
||||
@@ -6,7 +6,7 @@ use std::collections::HashMap;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use cashu::nuts::nut02::mint::KeySet;
|
||||
use cashu::nuts::{CurrencyUnit, Id, Proof};
|
||||
use cashu::nuts::{CurrencyUnit, Id, MintInfo, Proof};
|
||||
use cashu::secret::Secret;
|
||||
use cashu::types::{MeltQuote, MintQuote};
|
||||
pub use memory::MemoryLocalStore;
|
||||
@@ -37,10 +37,15 @@ pub enum Error {
|
||||
#[cfg(all(not(target_arch = "wasm32"), feature = "redb"))]
|
||||
#[error("`{0}`")]
|
||||
Serde(#[from] serde_json::Error),
|
||||
#[error("Unknown Mint Info")]
|
||||
UnknownMintInfo,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
pub trait LocalStore {
|
||||
async fn set_mint_info(&self, mint_info: &MintInfo) -> Result<(), Error>;
|
||||
async fn get_mint_info(&self) -> Result<MintInfo, Error>;
|
||||
|
||||
async fn add_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Error>;
|
||||
async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Error>;
|
||||
async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Error>;
|
||||
|
||||
@@ -3,7 +3,7 @@ use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_trait::async_trait;
|
||||
use cashu::nuts::{CurrencyUnit, Id, MintKeySet as KeySet, Proof};
|
||||
use cashu::nuts::{CurrencyUnit, Id, MintInfo, MintKeySet as KeySet, Proof};
|
||||
use cashu::secret::Secret;
|
||||
use cashu::types::{MeltQuote, MintQuote};
|
||||
use redb::{Database, ReadableTable, TableDefinition};
|
||||
@@ -17,6 +17,7 @@ const MINT_QUOTES_TABLE: TableDefinition<&str, &str> = TableDefinition::new("min
|
||||
const MELT_QUOTES_TABLE: TableDefinition<&str, &str> = TableDefinition::new("melt_quotes");
|
||||
const PENDING_PROOFS_TABLE: TableDefinition<&str, &str> = TableDefinition::new("pending_proofs");
|
||||
const SPENT_PROOFS_TABLE: TableDefinition<&str, &str> = TableDefinition::new("spent_proofs");
|
||||
const CONFIG_TABLE: TableDefinition<&str, &str> = TableDefinition::new("config");
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RedbLocalStore {
|
||||
@@ -35,6 +36,7 @@ impl RedbLocalStore {
|
||||
let _ = write_txn.open_table(MELT_QUOTES_TABLE)?;
|
||||
let _ = write_txn.open_table(PENDING_PROOFS_TABLE)?;
|
||||
let _ = write_txn.open_table(SPENT_PROOFS_TABLE)?;
|
||||
let _ = write_txn.open_table(CONFIG_TABLE)?;
|
||||
}
|
||||
write_txn.commit()?;
|
||||
|
||||
@@ -46,6 +48,30 @@ impl RedbLocalStore {
|
||||
|
||||
#[async_trait]
|
||||
impl LocalStore for RedbLocalStore {
|
||||
async fn set_mint_info(&self, mint_info: &MintInfo) -> Result<(), Error> {
|
||||
let db = self.db.lock().await;
|
||||
|
||||
let write_txn = db.begin_write()?;
|
||||
|
||||
{
|
||||
let mut table = write_txn.open_table(CONFIG_TABLE)?;
|
||||
table.insert("mint_info", serde_json::to_string(mint_info)?.as_str())?;
|
||||
}
|
||||
write_txn.commit()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn get_mint_info(&self) -> Result<MintInfo, Error> {
|
||||
let db = self.db.lock().await;
|
||||
let read_txn = db.begin_read()?;
|
||||
let table = read_txn.open_table(CONFIG_TABLE)?;
|
||||
|
||||
let mint_info = table.get("mint_info")?.ok_or(Error::UnknownMintInfo)?;
|
||||
|
||||
Ok(serde_json::from_str(mint_info.value())?)
|
||||
}
|
||||
|
||||
async fn add_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Error> {
|
||||
let db = self.db.lock().await;
|
||||
|
||||
|
||||
@@ -599,6 +599,10 @@ impl Mint {
|
||||
fee_reserve: u64::from(quote.fee_reserve),
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn mint_info(&self) -> Result<MintInfo, Error> {
|
||||
Ok(self.localstore.get_mint_info().await?)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
|
||||
Reference in New Issue
Block a user