From 47903c3bfd80b8839f80c9fceed214b7a0ad784d Mon Sep 17 00:00:00 2001 From: codingpeanut157 Date: Wed, 26 Mar 2025 16:48:41 +0100 Subject: [PATCH] split MintDatabase into separate narrower scoped traits - MintKeysDatabase - MintQuotesDatabase - MintProofsDatabase - MintSignaturesDatabase This commit splits the MintDatabase trait with 30+ methods into a series of smaller traits, each dedicate to a specific subsystem of the mint service. --- crates/cdk-common/src/database/mint/mod.rs | 54 ++- crates/cdk-common/src/database/mod.rs | 8 +- .../src/init_auth_mint.rs | 2 +- crates/cdk-mintd/src/main.rs | 2 +- crates/cdk-redb/src/mint/mod.rs | 115 ++++--- crates/cdk-sqlite/src/mint/memory.rs | 4 +- crates/cdk-sqlite/src/mint/mod.rs | 321 ++++++++++-------- crates/cdk/src/mint/builder.rs | 4 +- crates/cdk/src/mint/keysets/mod.rs | 2 +- crates/cdk/src/mint/mod.rs | 8 +- crates/cdk/src/mint/subscription/manager.rs | 4 +- .../src/mint/subscription/on_subscription.rs | 4 +- 12 files changed, 301 insertions(+), 227 deletions(-) diff --git a/crates/cdk-common/src/database/mint/mod.rs b/crates/cdk-common/src/database/mint/mod.rs index f5856f18..8aa67481 100644 --- a/crates/cdk-common/src/database/mint/mod.rs +++ b/crates/cdk-common/src/database/mint/mod.rs @@ -20,10 +20,10 @@ mod auth; #[cfg(feature = "auth")] pub use auth::MintAuthDatabase; -/// Mint Database trait +/// Mint Keys Database trait #[async_trait] -pub trait Database { - /// Mint Database Error +pub trait KeysDatabase { + /// Mint Keys Database Error type Err: Into + From; /// Add Active Keyset @@ -32,6 +32,18 @@ pub trait Database { async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result, Self::Err>; /// Get all Active Keyset async fn get_active_keysets(&self) -> Result, Self::Err>; + /// Add [`MintKeySetInfo`] + async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err>; + /// Get [`MintKeySetInfo`] + async fn get_keyset_info(&self, id: &Id) -> Result, Self::Err>; + /// Get [`MintKeySetInfo`]s + async fn get_keyset_infos(&self) -> Result, Self::Err>; +} +/// Mint Quote Database trait +#[async_trait] +pub trait QuotesDatabase { + /// Mint Quotes Database Error + type Err: Into + From; /// Add [`MintMintQuote`] async fn add_mint_quote(&self, quote: MintMintQuote) -> Result<(), Self::Err>; @@ -89,13 +101,13 @@ pub trait Database { &self, quote_id: &Uuid, ) -> Result, PaymentProcessorKey)>, Self::Err>; +} - /// Add [`MintKeySetInfo`] - async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err>; - /// Get [`MintKeySetInfo`] - async fn get_keyset_info(&self, id: &Id) -> Result, Self::Err>; - /// Get [`MintKeySetInfo`]s - async fn get_keyset_infos(&self) -> Result, Self::Err>; +/// Mint Proof Database trait +#[async_trait] +pub trait ProofsDatabase { + /// Mint Proof Database Error + type Err: Into + From; /// Add [`Proofs`] async fn add_proofs(&self, proof: Proofs, quote_id: Option) -> Result<(), Self::Err>; @@ -122,6 +134,13 @@ pub trait Database { &self, keyset_id: &Id, ) -> Result<(Proofs, Vec>), Self::Err>; +} + +#[async_trait] +/// Mint Signatures Database trait +pub trait SignaturesDatabase { + /// Mint Signature Database Error + type Err: Into + From; /// Add [`BlindSignature`] async fn add_blind_signatures( @@ -145,14 +164,23 @@ pub trait Database { &self, quote_id: &Uuid, ) -> Result, Self::Err>; +} +/// Mint Database trait +#[async_trait] +pub trait Database: + KeysDatabase + + QuotesDatabase + + ProofsDatabase + + SignaturesDatabase +{ /// Set [`MintInfo`] - async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), Self::Err>; + async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), Error>; /// Get [`MintInfo`] - async fn get_mint_info(&self) -> Result; + async fn get_mint_info(&self) -> Result; /// Set [`QuoteTTL`] - async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), Self::Err>; + async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), Error>; /// Get [`QuoteTTL`] - async fn get_quote_ttl(&self) -> Result; + async fn get_quote_ttl(&self) -> Result; } diff --git a/crates/cdk-common/src/database/mod.rs b/crates/cdk-common/src/database/mod.rs index 64e6f79c..6dbd1796 100644 --- a/crates/cdk-common/src/database/mod.rs +++ b/crates/cdk-common/src/database/mod.rs @@ -5,10 +5,14 @@ mod mint; #[cfg(feature = "wallet")] mod wallet; -#[cfg(feature = "mint")] -pub use mint::Database as MintDatabase; #[cfg(all(feature = "mint", feature = "auth"))] pub use mint::MintAuthDatabase; +#[cfg(feature = "mint")] +pub use mint::{ + Database as MintDatabase, KeysDatabase as MintKeysDatabase, + ProofsDatabase as MintProofsDatabase, QuotesDatabase as MintQuotesDatabase, + SignaturesDatabase as MintSignaturesDatabase, +}; #[cfg(feature = "wallet")] pub use wallet::Database as WalletDatabase; diff --git a/crates/cdk-integration-tests/src/init_auth_mint.rs b/crates/cdk-integration-tests/src/init_auth_mint.rs index a899fa69..eabbf649 100644 --- a/crates/cdk-integration-tests/src/init_auth_mint.rs +++ b/crates/cdk-integration-tests/src/init_auth_mint.rs @@ -19,7 +19,7 @@ pub async fn start_fake_mint_with_auth( auth_database: A, ) -> Result<()> where - D: MintDatabase + Send + Sync + 'static, + D: MintDatabase + Send + Sync + 'static, A: MintAuthDatabase + Send + Sync + 'static, { let fee_reserve = FeeReserve { diff --git a/crates/cdk-mintd/src/main.rs b/crates/cdk-mintd/src/main.rs index 20ce2758..989d8acd 100644 --- a/crates/cdk-mintd/src/main.rs +++ b/crates/cdk-mintd/src/main.rs @@ -121,7 +121,7 @@ async fn main() -> anyhow::Result<()> { // ENV VARS will take **priority** over those in the config let settings = settings.from_env()?; - let localstore: Arc + Send + Sync> = + let localstore: Arc + Send + Sync> = match settings.database.engine { DatabaseEngine::Sqlite => { let sql_db_path = work_dir.join("cdk-mintd.sqlite"); diff --git a/crates/cdk-redb/src/mint/mod.rs b/crates/cdk-redb/src/mint/mod.rs index 4f232f56..7fe57e75 100644 --- a/crates/cdk-redb/src/mint/mod.rs +++ b/crates/cdk-redb/src/mint/mod.rs @@ -8,7 +8,10 @@ use std::sync::Arc; use async_trait::async_trait; use cdk_common::common::{PaymentProcessorKey, QuoteTTL}; -use cdk_common::database::{self, MintDatabase}; +use cdk_common::database::{ + self, MintDatabase, MintKeysDatabase, MintProofsDatabase, MintQuotesDatabase, + MintSignaturesDatabase, +}; use cdk_common::dhke::hash_to_curve; use cdk_common::mint::{self, MintKeySetInfo, MintQuote}; use cdk_common::nut00::ProofsMethods; @@ -165,7 +168,7 @@ impl MintRedbDatabase { } #[async_trait] -impl MintDatabase for MintRedbDatabase { +impl MintKeysDatabase for MintRedbDatabase { type Err = database::Error; async fn set_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err> { @@ -261,6 +264,11 @@ impl MintDatabase for MintRedbDatabase { Ok(keysets) } +} + +#[async_trait] +impl MintQuotesDatabase for MintRedbDatabase { + type Err = database::Error; async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Self::Err> { let write_txn = self.db.begin_write().map_err(Error::from)?; @@ -524,6 +532,52 @@ impl MintDatabase for MintRedbDatabase { Ok(()) } + /// Add melt request + async fn add_melt_request( + &self, + melt_request: MeltBolt11Request, + ln_key: PaymentProcessorKey, + ) -> Result<(), Self::Err> { + let write_txn = self.db.begin_write().map_err(Error::from)?; + let mut table = write_txn.open_table(MELT_REQUESTS).map_err(Error::from)?; + + table + .insert( + melt_request.quote.as_bytes(), + ( + serde_json::to_string(&melt_request)?.as_str(), + serde_json::to_string(&ln_key)?.as_str(), + ), + ) + .map_err(Error::from)?; + + Ok(()) + } + /// Get melt request + async fn get_melt_request( + &self, + quote_id: &Uuid, + ) -> Result, PaymentProcessorKey)>, Self::Err> { + let read_txn = self.db.begin_read().map_err(Error::from)?; + let table = read_txn.open_table(MELT_REQUESTS).map_err(Error::from)?; + + match table.get(quote_id.as_bytes()).map_err(Error::from)? { + Some(melt_request) => { + let (melt_request_str, ln_key_str) = melt_request.value(); + let melt_request = serde_json::from_str(melt_request_str)?; + let ln_key = serde_json::from_str(ln_key_str)?; + + Ok(Some((melt_request, ln_key))) + } + None => Ok(None), + } + } +} + +#[async_trait] +impl MintProofsDatabase for MintRedbDatabase { + type Err = database::Error; + async fn add_proofs(&self, proofs: Proofs, quote_id: Option) -> Result<(), Self::Err> { let write_txn = self.db.begin_write().map_err(Error::from)?; @@ -745,6 +799,11 @@ impl MintDatabase for MintRedbDatabase { Ok(states) } +} + +#[async_trait] +impl MintSignaturesDatabase for MintRedbDatabase { + type Err = database::Error; async fn add_blind_signatures( &self, @@ -827,47 +886,6 @@ impl MintDatabase for MintRedbDatabase { .collect()) } - /// Add melt request - async fn add_melt_request( - &self, - melt_request: MeltBolt11Request, - ln_key: PaymentProcessorKey, - ) -> Result<(), Self::Err> { - let write_txn = self.db.begin_write().map_err(Error::from)?; - let mut table = write_txn.open_table(MELT_REQUESTS).map_err(Error::from)?; - - table - .insert( - melt_request.quote.as_bytes(), - ( - serde_json::to_string(&melt_request)?.as_str(), - serde_json::to_string(&ln_key)?.as_str(), - ), - ) - .map_err(Error::from)?; - - Ok(()) - } - /// Get melt request - async fn get_melt_request( - &self, - quote_id: &Uuid, - ) -> Result, PaymentProcessorKey)>, Self::Err> { - let read_txn = self.db.begin_read().map_err(Error::from)?; - let table = read_txn.open_table(MELT_REQUESTS).map_err(Error::from)?; - - match table.get(quote_id.as_bytes()).map_err(Error::from)? { - Some(melt_request) => { - let (melt_request_str, ln_key_str) = melt_request.value(); - let melt_request = serde_json::from_str(melt_request_str)?; - let ln_key = serde_json::from_str(ln_key_str)?; - - Ok(Some((melt_request, ln_key))) - } - None => Ok(None), - } - } - /// Get [`BlindSignature`]s for quote async fn get_blind_signatures_for_quote( &self, @@ -897,8 +915,11 @@ impl MintDatabase for MintRedbDatabase { Ok(signatures) } +} - async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), Self::Err> { +#[async_trait] +impl MintDatabase for MintRedbDatabase { + async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), database::Error> { let write_txn = self.db.begin_write().map_err(Error::from)?; { @@ -911,7 +932,7 @@ impl MintDatabase for MintRedbDatabase { Ok(()) } - async fn get_mint_info(&self) -> Result { + async fn get_mint_info(&self) -> Result { let read_txn = self.db.begin_read().map_err(Error::from)?; let table = read_txn.open_table(CONFIG_TABLE).map_err(Error::from)?; @@ -924,7 +945,7 @@ impl MintDatabase for MintRedbDatabase { Err(Error::UnknownMintInfo.into()) } - async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), Self::Err> { + async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), database::Error> { let write_txn = self.db.begin_write().map_err(Error::from)?; { @@ -937,7 +958,7 @@ impl MintDatabase for MintRedbDatabase { Ok(()) } - async fn get_quote_ttl(&self) -> Result { + async fn get_quote_ttl(&self) -> Result { let read_txn = self.db.begin_read().map_err(Error::from)?; let table = read_txn.open_table(CONFIG_TABLE).map_err(Error::from)?; diff --git a/crates/cdk-sqlite/src/mint/memory.rs b/crates/cdk-sqlite/src/mint/memory.rs index 0031ad45..0f11865a 100644 --- a/crates/cdk-sqlite/src/mint/memory.rs +++ b/crates/cdk-sqlite/src/mint/memory.rs @@ -2,7 +2,9 @@ use std::collections::HashMap; use cdk_common::common::PaymentProcessorKey; -use cdk_common::database::{self, MintDatabase}; +use cdk_common::database::{ + self, MintDatabase, MintKeysDatabase, MintProofsDatabase, MintQuotesDatabase, +}; use cdk_common::mint::{self, MintKeySetInfo, MintQuote}; use cdk_common::nuts::{CurrencyUnit, Id, MeltBolt11Request, Proofs}; use cdk_common::MintInfo; diff --git a/crates/cdk-sqlite/src/mint/mod.rs b/crates/cdk-sqlite/src/mint/mod.rs index 57134291..8206e09e 100644 --- a/crates/cdk-sqlite/src/mint/mod.rs +++ b/crates/cdk-sqlite/src/mint/mod.rs @@ -7,7 +7,10 @@ use std::str::FromStr; use async_trait::async_trait; use bitcoin::bip32::DerivationPath; use cdk_common::common::{PaymentProcessorKey, QuoteTTL}; -use cdk_common::database::{self, MintDatabase}; +use cdk_common::database::{ + self, MintDatabase, MintKeysDatabase, MintProofsDatabase, MintQuotesDatabase, + MintSignaturesDatabase, +}; use cdk_common::mint::{self, MintKeySetInfo, MintQuote}; use cdk_common::nut00::ProofsMethods; use cdk_common::nut05::QuoteState; @@ -105,7 +108,7 @@ impl MintSqliteDatabase { } #[async_trait] -impl MintDatabase for MintSqliteDatabase { +impl MintKeysDatabase for MintSqliteDatabase { type Err = database::Error; async fn set_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err> { @@ -244,6 +247,121 @@ WHERE active = 1 } } + async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err> { + let mut transaction = self.pool.begin().await.map_err(Error::from)?; + let res = sqlx::query( + r#" +INSERT INTO keyset +(id, unit, active, valid_from, valid_to, derivation_path, max_order, input_fee_ppk, derivation_path_index) +VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) +ON CONFLICT(id) DO UPDATE SET + unit = excluded.unit, + active = excluded.active, + valid_from = excluded.valid_from, + valid_to = excluded.valid_to, + derivation_path = excluded.derivation_path, + max_order = excluded.max_order, + input_fee_ppk = excluded.input_fee_ppk, + derivation_path_index = excluded.derivation_path_index + "#, + ) + .bind(keyset.id.to_string()) + .bind(keyset.unit.to_string()) + .bind(keyset.active) + .bind(keyset.valid_from as i64) + .bind(keyset.valid_to.map(|v| v as i64)) + .bind(keyset.derivation_path.to_string()) + .bind(keyset.max_order) + .bind(keyset.input_fee_ppk as i64) + .bind(keyset.derivation_path_index) + .execute(&mut *transaction) + .await; + + match res { + Ok(_) => { + transaction.commit().await.map_err(Error::from)?; + Ok(()) + } + Err(err) => { + tracing::error!("SQLite could not add keyset info"); + if let Err(err) = transaction.rollback().await { + tracing::error!("Could not rollback sql transaction: {}", err); + } + + Err(Error::from(err).into()) + } + } + } + + async fn get_keyset_info(&self, id: &Id) -> Result, Self::Err> { + let mut transaction = self.pool.begin().await.map_err(Error::from)?; + let rec = sqlx::query( + r#" +SELECT * +FROM keyset +WHERE id=?; + "#, + ) + .bind(id.to_string()) + .fetch_one(&mut *transaction) + .await; + + match rec { + Ok(rec) => { + transaction.commit().await.map_err(Error::from)?; + Ok(Some(sqlite_row_to_keyset_info(rec)?)) + } + Err(err) => match err { + sqlx::Error::RowNotFound => { + transaction.commit().await.map_err(Error::from)?; + return Ok(None); + } + _ => { + tracing::error!("SQLite could not get keyset info"); + if let Err(err) = transaction.rollback().await { + tracing::error!("Could not rollback sql transaction: {}", err); + } + return Err(Error::SQLX(err).into()); + } + }, + } + } + + async fn get_keyset_infos(&self) -> Result, Self::Err> { + let mut transaction = self.pool.begin().await.map_err(Error::from)?; + let recs = sqlx::query( + r#" +SELECT * +FROM keyset; + "#, + ) + .fetch_all(&mut *transaction) + .await + .map_err(Error::from); + + match recs { + Ok(recs) => { + transaction.commit().await.map_err(Error::from)?; + Ok(recs + .into_iter() + .map(sqlite_row_to_keyset_info) + .collect::>()?) + } + Err(err) => { + tracing::error!("SQLite could not get keyset info"); + if let Err(err) = transaction.rollback().await { + tracing::error!("Could not rollback sql transaction: {}", err); + } + Err(err.into()) + } + } + } +} + +#[async_trait] +impl MintQuotesDatabase for MintSqliteDatabase { + type Err = database::Error; + async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Self::Err> { let mut transaction = self.pool.begin().await.map_err(Error::from)?; @@ -763,33 +881,30 @@ WHERE id=? } } - async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err> { + async fn add_melt_request( + &self, + melt_request: MeltBolt11Request, + ln_key: PaymentProcessorKey, + ) -> Result<(), Self::Err> { let mut transaction = self.pool.begin().await.map_err(Error::from)?; + let res = sqlx::query( r#" -INSERT INTO keyset -(id, unit, active, valid_from, valid_to, derivation_path, max_order, input_fee_ppk, derivation_path_index) -VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) +INSERT INTO melt_request +(id, inputs, outputs, method, unit) +VALUES (?, ?, ?, ?, ?) ON CONFLICT(id) DO UPDATE SET - unit = excluded.unit, - active = excluded.active, - valid_from = excluded.valid_from, - valid_to = excluded.valid_to, - derivation_path = excluded.derivation_path, - max_order = excluded.max_order, - input_fee_ppk = excluded.input_fee_ppk, - derivation_path_index = excluded.derivation_path_index + inputs = excluded.inputs, + outputs = excluded.outputs, + method = excluded.method, + unit = excluded.unit "#, ) - .bind(keyset.id.to_string()) - .bind(keyset.unit.to_string()) - .bind(keyset.active) - .bind(keyset.valid_from as i64) - .bind(keyset.valid_to.map(|v| v as i64)) - .bind(keyset.derivation_path.to_string()) - .bind(keyset.max_order) - .bind(keyset.input_fee_ppk as i64) - .bind(keyset.derivation_path_index) + .bind(melt_request.quote) + .bind(serde_json::to_string(&melt_request.inputs)?) + .bind(serde_json::to_string(&melt_request.outputs)?) + .bind(ln_key.method.to_string()) + .bind(ln_key.unit.to_string()) .execute(&mut *transaction) .await; @@ -799,7 +914,7 @@ ON CONFLICT(id) DO UPDATE SET Ok(()) } Err(err) => { - tracing::error!("SQLite could not add keyset info"); + tracing::error!("SQLite Could not update keyset"); if let Err(err) = transaction.rollback().await { tracing::error!("Could not rollback sql transaction: {}", err); } @@ -809,23 +924,30 @@ ON CONFLICT(id) DO UPDATE SET } } - async fn get_keyset_info(&self, id: &Id) -> Result, Self::Err> { + async fn get_melt_request( + &self, + quote_id: &Uuid, + ) -> Result, PaymentProcessorKey)>, Self::Err> { let mut transaction = self.pool.begin().await.map_err(Error::from)?; + let rec = sqlx::query( r#" SELECT * -FROM keyset +FROM melt_request WHERE id=?; "#, ) - .bind(id.to_string()) + .bind(quote_id.as_hyphenated()) .fetch_one(&mut *transaction) .await; match rec { Ok(rec) => { transaction.commit().await.map_err(Error::from)?; - Ok(Some(sqlite_row_to_keyset_info(rec)?)) + + let (request, key) = sqlite_row_to_melt_request(rec)?; + + Ok(Some((request, key))) } Err(err) => match err { sqlx::Error::RowNotFound => { @@ -833,45 +955,21 @@ WHERE id=?; return Ok(None); } _ => { - tracing::error!("SQLite could not get keyset info"); - if let Err(err) = transaction.rollback().await { - tracing::error!("Could not rollback sql transaction: {}", err); + return { + if let Err(err) = transaction.rollback().await { + tracing::error!("Could not rollback sql transaction: {}", err); + } + Err(Error::SQLX(err).into()) } - return Err(Error::SQLX(err).into()); } }, } } +} - async fn get_keyset_infos(&self) -> Result, Self::Err> { - let mut transaction = self.pool.begin().await.map_err(Error::from)?; - let recs = sqlx::query( - r#" -SELECT * -FROM keyset; - "#, - ) - .fetch_all(&mut *transaction) - .await - .map_err(Error::from); - - match recs { - Ok(recs) => { - transaction.commit().await.map_err(Error::from)?; - Ok(recs - .into_iter() - .map(sqlite_row_to_keyset_info) - .collect::>()?) - } - Err(err) => { - tracing::error!("SQLite could not get keyset info"); - if let Err(err) = transaction.rollback().await { - tracing::error!("Could not rollback sql transaction: {}", err); - } - Err(err.into()) - } - } - } +#[async_trait] +impl MintProofsDatabase for MintSqliteDatabase { + type Err = database::Error; async fn add_proofs(&self, proofs: Proofs, quote_id: Option) -> Result<(), Self::Err> { let mut transaction = self.pool.begin().await.map_err(Error::from)?; @@ -1176,6 +1274,11 @@ WHERE keyset_id=?; Ok(ys.iter().map(|y| current_states.remove(y)).collect()) } +} + +#[async_trait] +impl MintSignaturesDatabase for MintSqliteDatabase { + type Err = database::Error; async fn add_blind_signatures( &self, @@ -1290,91 +1393,6 @@ WHERE keyset_id=?; } } - async fn add_melt_request( - &self, - melt_request: MeltBolt11Request, - ln_key: PaymentProcessorKey, - ) -> Result<(), Self::Err> { - let mut transaction = self.pool.begin().await.map_err(Error::from)?; - - let res = sqlx::query( - r#" -INSERT INTO melt_request -(id, inputs, outputs, method, unit) -VALUES (?, ?, ?, ?, ?) -ON CONFLICT(id) DO UPDATE SET - inputs = excluded.inputs, - outputs = excluded.outputs, - method = excluded.method, - unit = excluded.unit - "#, - ) - .bind(melt_request.quote) - .bind(serde_json::to_string(&melt_request.inputs)?) - .bind(serde_json::to_string(&melt_request.outputs)?) - .bind(ln_key.method.to_string()) - .bind(ln_key.unit.to_string()) - .execute(&mut *transaction) - .await; - - match res { - Ok(_) => { - transaction.commit().await.map_err(Error::from)?; - Ok(()) - } - Err(err) => { - tracing::error!("SQLite Could not update keyset"); - if let Err(err) = transaction.rollback().await { - tracing::error!("Could not rollback sql transaction: {}", err); - } - - Err(Error::from(err).into()) - } - } - } - - async fn get_melt_request( - &self, - quote_id: &Uuid, - ) -> Result, PaymentProcessorKey)>, Self::Err> { - let mut transaction = self.pool.begin().await.map_err(Error::from)?; - - let rec = sqlx::query( - r#" -SELECT * -FROM melt_request -WHERE id=?; - "#, - ) - .bind(quote_id.as_hyphenated()) - .fetch_one(&mut *transaction) - .await; - - match rec { - Ok(rec) => { - transaction.commit().await.map_err(Error::from)?; - - let (request, key) = sqlite_row_to_melt_request(rec)?; - - Ok(Some((request, key))) - } - Err(err) => match err { - sqlx::Error::RowNotFound => { - transaction.commit().await.map_err(Error::from)?; - return Ok(None); - } - _ => { - return { - if let Err(err) = transaction.rollback().await { - tracing::error!("Could not rollback sql transaction: {}", err); - } - Err(Error::SQLX(err).into()) - } - } - }, - } - } - /// Get [`BlindSignature`]s for quote async fn get_blind_signatures_for_quote( &self, @@ -1412,8 +1430,11 @@ WHERE quote_id=?; } } } +} - async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), Self::Err> { +#[async_trait] +impl MintDatabase for MintSqliteDatabase { + async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), database::Error> { let mut transaction = self.pool.begin().await.map_err(Error::from)?; let res = sqlx::query( @@ -1446,7 +1467,7 @@ ON CONFLICT(id) DO UPDATE SET } } } - async fn get_mint_info(&self) -> Result { + async fn get_mint_info(&self) -> Result { let mut transaction = self.pool.begin().await.map_err(Error::from)?; let rec = sqlx::query( @@ -1487,7 +1508,7 @@ WHERE id=?; } } - async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), Self::Err> { + async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), database::Error> { let mut transaction = self.pool.begin().await.map_err(Error::from)?; let res = sqlx::query( @@ -1520,7 +1541,7 @@ ON CONFLICT(id) DO UPDATE SET } } } - async fn get_quote_ttl(&self) -> Result { + async fn get_quote_ttl(&self) -> Result { let mut transaction = self.pool.begin().await.map_err(Error::from)?; let rec = sqlx::query( diff --git a/crates/cdk/src/mint/builder.rs b/crates/cdk/src/mint/builder.rs index 2f415fce..3f04264a 100644 --- a/crates/cdk/src/mint/builder.rs +++ b/crates/cdk/src/mint/builder.rs @@ -32,7 +32,7 @@ pub struct MintBuilder { /// Mint Info pub mint_info: MintInfo, /// Mint Storage backend - localstore: Option + Send + Sync>>, + localstore: Option + Send + Sync>>, /// Mint Storage backend #[cfg(feature = "auth")] auth_localstore: Option + Send + Sync>>, @@ -70,7 +70,7 @@ impl MintBuilder { /// Set localstore pub fn with_localstore( mut self, - localstore: Arc + Send + Sync>, + localstore: Arc + Send + Sync>, ) -> MintBuilder { self.localstore = Some(localstore); self diff --git a/crates/cdk/src/mint/keysets/mod.rs b/crates/cdk/src/mint/keysets/mod.rs index 13a357e5..da3d2d0a 100644 --- a/crates/cdk/src/mint/keysets/mod.rs +++ b/crates/cdk/src/mint/keysets/mod.rs @@ -23,7 +23,7 @@ impl Mint { pub async fn init_keysets( xpriv: Xpriv, secp_ctx: &Secp256k1, - localstore: &Arc + Send + Sync>, + localstore: &Arc + Send + Sync>, supported_units: &HashMap, custom_paths: &HashMap, ) -> Result<(HashMap, Vec), Error> { diff --git a/crates/cdk/src/mint/mod.rs b/crates/cdk/src/mint/mod.rs index 87df17d8..fbdac088 100644 --- a/crates/cdk/src/mint/mod.rs +++ b/crates/cdk/src/mint/mod.rs @@ -49,7 +49,7 @@ pub use verification::Verification; #[derive(Clone)] pub struct Mint { /// Mint Storage backend - pub localstore: Arc + Send + Sync>, + pub localstore: Arc + Send + Sync>, /// Auth Storage backend (only available with auth feature) #[cfg(feature = "auth")] pub auth_localstore: Option + Send + Sync>>, @@ -70,7 +70,7 @@ impl Mint { /// Create new [`Mint`] without authentication pub async fn new( seed: &[u8], - localstore: Arc + Send + Sync>, + localstore: Arc + Send + Sync>, ln: HashMap< PaymentProcessorKey, Arc + Send + Sync>, @@ -96,7 +96,7 @@ impl Mint { #[cfg(feature = "auth")] pub async fn new_with_auth( seed: &[u8], - localstore: Arc + Send + Sync>, + localstore: Arc + Send + Sync>, auth_localstore: Arc + Send + Sync>, ln: HashMap< PaymentProcessorKey, @@ -121,7 +121,7 @@ impl Mint { /// Internal function to create a new [`Mint`] with shared logic async fn new_internal( seed: &[u8], - localstore: Arc + Send + Sync>, + localstore: Arc + Send + Sync>, #[cfg(feature = "auth")] auth_localstore: Option< Arc + Send + Sync>, >, diff --git a/crates/cdk/src/mint/subscription/manager.rs b/crates/cdk/src/mint/subscription/manager.rs index 4df11330..345b94fb 100644 --- a/crates/cdk/src/mint/subscription/manager.rs +++ b/crates/cdk/src/mint/subscription/manager.rs @@ -28,8 +28,8 @@ impl Default for PubSubManager { } } -impl From + Send + Sync>> for PubSubManager { - fn from(val: Arc + Send + Sync>) -> Self { +impl From + Send + Sync>> for PubSubManager { + fn from(val: Arc + Send + Sync>) -> Self { PubSubManager(OnSubscription(Some(val)).into()) } } diff --git a/crates/cdk/src/mint/subscription/on_subscription.rs b/crates/cdk/src/mint/subscription/on_subscription.rs index 08d1f169..829cc6e1 100644 --- a/crates/cdk/src/mint/subscription/on_subscription.rs +++ b/crates/cdk/src/mint/subscription/on_subscription.rs @@ -17,9 +17,7 @@ use crate::nuts::{MeltQuoteBolt11Response, MintQuoteBolt11Response, ProofState, /// This struct triggers code when a new subscription is created. /// /// It is used to send the initial state of the subscription to the client. -pub struct OnSubscription( - pub(crate) Option + Send + Sync>>, -); +pub struct OnSubscription(pub(crate) Option + Send + Sync>>); #[async_trait::async_trait] impl OnNewSubscription for OnSubscription {