refactor: mint info as part of mint not in db

This commit is contained in:
thesimplekid
2024-06-05 00:47:29 +01:00
parent ddeef0c414
commit 2e86dbb630
4 changed files with 16 additions and 54 deletions

View File

@@ -7,7 +7,7 @@ use cdk::cdk_database;
use cdk::cdk_database::MintDatabase;
use cdk::dhke::hash_to_curve;
use cdk::mint::MintKeySetInfo;
use cdk::nuts::{BlindSignature, CurrencyUnit, Id, MintInfo, Proof, PublicKey};
use cdk::nuts::{BlindSignature, CurrencyUnit, Id, Proof, PublicKey};
use cdk::secret::Secret;
use cdk::types::{MeltQuote, MintQuote};
use redb::{Database, ReadableTable, TableDefinition};
@@ -82,40 +82,6 @@ impl MintRedbDatabase {
impl MintDatabase for MintRedbDatabase {
type Err = cdk_database::Error;
async fn set_mint_info(&self, mint_info: &MintInfo) -> Result<(), Self::Err> {
let db = self.db.lock().await;
let write_txn = db.begin_write().map_err(Error::from)?;
{
let mut table = write_txn.open_table(CONFIG_TABLE).map_err(Error::from)?;
table
.insert(
"mint_info",
serde_json::to_string(mint_info)
.map_err(Error::from)?
.as_str(),
)
.map_err(Error::from)?;
}
write_txn.commit().map_err(Error::from)?;
Ok(())
}
async fn get_mint_info(&self) -> Result<MintInfo, Self::Err> {
let db = self.db.lock().await;
let read_txn = db.begin_read().map_err(Error::from)?;
let table = read_txn.open_table(CONFIG_TABLE).map_err(Error::from)?;
let mint_info = table
.get("mint_info")
.map_err(Error::from)?
.ok_or(Error::UnknownMintInfo)?;
Ok(serde_json::from_str(mint_info.value()).map_err(Error::from)?)
}
async fn add_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err> {
let db = self.db.lock().await;

View File

@@ -7,13 +7,12 @@ use tokio::sync::RwLock;
use super::{Error, MintDatabase};
use crate::dhke::hash_to_curve;
use crate::mint::MintKeySetInfo;
use crate::nuts::{BlindSignature, CurrencyUnit, Id, MintInfo, Proof, Proofs, PublicKey};
use crate::nuts::{BlindSignature, CurrencyUnit, Id, Proof, Proofs, PublicKey};
use crate::secret::Secret;
use crate::types::{MeltQuote, MintQuote};
#[derive(Debug, Clone)]
pub struct MintMemoryDatabase {
mint_info: Arc<RwLock<MintInfo>>,
active_keysets: Arc<RwLock<HashMap<CurrencyUnit, Id>>>,
keysets: Arc<RwLock<HashMap<Id, MintKeySetInfo>>>,
mint_quotes: Arc<RwLock<HashMap<String, MintQuote>>>,
@@ -26,7 +25,6 @@ pub struct MintMemoryDatabase {
impl MintMemoryDatabase {
#[allow(clippy::too_many_arguments)]
pub fn new(
mint_info: MintInfo,
active_keysets: HashMap<CurrencyUnit, Id>,
keysets: Vec<MintKeySetInfo>,
mint_quotes: Vec<MintQuote>,
@@ -36,7 +34,6 @@ impl MintMemoryDatabase {
blinded_signatures: HashMap<[u8; 33], BlindSignature>,
) -> Result<Self, Error> {
Ok(Self {
mint_info: Arc::new(RwLock::new(mint_info)),
active_keysets: Arc::new(RwLock::new(active_keysets)),
keysets: Arc::new(RwLock::new(
keysets.into_iter().map(|k| (k.id, k)).collect(),
@@ -68,14 +65,6 @@ impl MintMemoryDatabase {
impl MintDatabase for MintMemoryDatabase {
type Err = Error;
async fn set_mint_info(&self, mint_info: &MintInfo) -> Result<(), Self::Err> {
let mut mi = self.mint_info.write().await;
*mi = mint_info.clone();
Ok(())
}
async fn get_mint_info(&self) -> Result<MintInfo, Self::Err> {
Ok(self.mint_info.read().await.clone())
}
async fn add_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err> {
self.active_keysets.write().await.insert(unit, id);
Ok(())

View File

@@ -14,9 +14,9 @@ use crate::nuts::State;
#[cfg(feature = "mint")]
use crate::nuts::{BlindSignature, Proof};
#[cfg(any(feature = "wallet", feature = "mint"))]
use crate::nuts::{CurrencyUnit, Id, MintInfo, PublicKey};
use crate::nuts::{CurrencyUnit, Id, PublicKey};
#[cfg(feature = "wallet")]
use crate::nuts::{KeySetInfo, Keys, Proofs, SpendingConditions};
use crate::nuts::{KeySetInfo, Keys, MintInfo, Proofs, SpendingConditions};
#[cfg(feature = "mint")]
use crate::secret::Secret;
#[cfg(feature = "wallet")]
@@ -112,9 +112,6 @@ pub trait WalletDatabase {
pub trait MintDatabase {
type Err: Into<Error> + From<Error>;
async fn set_mint_info(&self, mint_info: &MintInfo) -> Result<(), Self::Err>;
async fn get_mint_info(&self) -> Result<MintInfo, Self::Err>;
async fn add_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>;

View File

@@ -21,6 +21,7 @@ pub mod error;
#[derive(Clone)]
pub struct Mint {
mint_info: MintInfo,
keysets: Arc<RwLock<HashMap<Id, MintKeySet>>>,
secp_ctx: Secp256k1<secp256k1::All>,
xpriv: ExtendedPrivKey,
@@ -31,6 +32,7 @@ pub struct Mint {
impl Mint {
pub async fn new(
seed: &[u8],
mint_info: MintInfo,
localstore: Arc<dyn MintDatabase<Err = cdk_database::Error> + Send + Sync>,
min_fee_reserve: Amount,
percent_fee_reserve: f32,
@@ -62,6 +64,7 @@ impl Mint {
min_fee_reserve,
percent_fee_reserve,
},
mint_info,
})
}
@@ -654,10 +657,17 @@ impl Mint {
})
}
pub async fn mint_info(&self) -> Result<MintInfo, Error> {
Ok(self.localstore.get_mint_info().await?)
/// Set Mint Info
pub fn set_mint_info(&mut self, mint_info: MintInfo) {
self.mint_info = mint_info;
}
/// Get Mint Info
pub fn mint_info(&self) -> Result<MintInfo, Error> {
Ok(self.mint_info.clone())
}
/// Restore
pub async fn restore(&self, request: RestoreRequest) -> Result<RestoreResponse, Error> {
let output_len = request.outputs.len();