| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523 |
- //! SQLite Mint
- use std::collections::HashMap;
- use std::ops::DerefMut;
- use std::path::Path;
- use std::str::FromStr;
- use async_rusqlite::{query, DatabaseExecutor};
- use async_trait::async_trait;
- use bitcoin::bip32::DerivationPath;
- use cdk_common::common::{PaymentProcessorKey, QuoteTTL};
- 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;
- use cdk_common::secret::Secret;
- use cdk_common::state::check_state_transition;
- use cdk_common::util::unix_time;
- use cdk_common::{
- Amount, BlindSignature, BlindSignatureDleq, CurrencyUnit, Id, MeltQuoteState, MeltRequest,
- MintInfo, MintQuoteState, PaymentMethod, Proof, Proofs, PublicKey, SecretKey, State,
- };
- use error::Error;
- use lightning_invoice::Bolt11Invoice;
- use uuid::Uuid;
- use crate::common::{create_sqlite_pool, migrate};
- use crate::stmt::Column;
- use crate::{
- column_as_nullable_number, column_as_nullable_string, column_as_number, column_as_string,
- unpack_into,
- };
- mod async_rusqlite;
- #[cfg(feature = "auth")]
- mod auth;
- pub mod error;
- pub mod memory;
- #[rustfmt::skip]
- mod migrations;
- #[cfg(feature = "auth")]
- pub use auth::MintSqliteAuthDatabase;
- /// Mint SQLite Database
- #[derive(Debug, Clone)]
- pub struct MintSqliteDatabase {
- pool: async_rusqlite::AsyncRusqlite,
- }
- impl MintSqliteDatabase {
- /// Create new [`MintSqliteDatabase`]
- #[cfg(not(feature = "sqlcipher"))]
- pub async fn new<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
- let pool = create_sqlite_pool(path.as_ref().to_str().ok_or(Error::InvalidDbPath)?);
- migrate(pool.get()?.deref_mut(), migrations::MIGRATIONS)?;
- Ok(Self {
- pool: async_rusqlite::AsyncRusqlite::new(pool),
- })
- }
- /// Create new [`MintSqliteDatabase`]
- #[cfg(feature = "sqlcipher")]
- pub async fn new<P: AsRef<Path>>(path: P, password: String) -> Result<Self, Error> {
- let pool = create_sqlite_pool(
- path.as_ref().to_str().ok_or(Error::InvalidDbPath)?,
- password,
- );
- migrate(pool.get()?.deref_mut(), migrations::MIGRATIONS)?;
- Ok(Self {
- pool: async_rusqlite::AsyncRusqlite::new(pool),
- })
- }
- #[inline(always)]
- async fn get_current_states<C>(
- &self,
- conn: &C,
- ys: &[PublicKey],
- ) -> Result<HashMap<PublicKey, State>, Error>
- where
- C: DatabaseExecutor + Send + Sync,
- {
- query(r#"SELECT y, state FROM proof WHERE y IN (:ys)"#)
- .bind_vec(":ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
- .fetch_all(conn)
- .await?
- .into_iter()
- .map(|row| {
- Ok((
- column_as_string!(&row[0], PublicKey::from_hex, PublicKey::from_slice),
- column_as_string!(&row[1], State::from_str),
- ))
- })
- .collect::<Result<HashMap<_, _>, _>>()
- }
- #[inline(always)]
- async fn set_to_config<T>(&self, id: &str, value: &T) -> Result<(), Error>
- where
- T: ?Sized + serde::Serialize,
- {
- query(
- r#"
- INSERT INTO config (id, value) VALUES (:id, :value)
- ON CONFLICT(id) DO UPDATE SET value = excluded.value
- "#,
- )
- .bind(":id", id.to_owned())
- .bind(":value", serde_json::to_string(&value)?)
- .execute(&self.pool)
- .await?;
- Ok(())
- }
- #[inline(always)]
- async fn fetch_from_config<T>(&self, id: &str) -> Result<T, Error>
- where
- T: serde::de::DeserializeOwned,
- {
- let value = column_as_string!(query(r#"SELECT value FROM config WHERE id = :id LIMIT 1"#)
- .bind(":id", id.to_owned())
- .pluck(&self.pool)
- .await?
- .ok_or::<Error>(Error::UnknownQuoteTTL)?);
- Ok(serde_json::from_str(&value)?)
- }
- }
- #[async_trait]
- impl MintKeysDatabase for MintSqliteDatabase {
- type Err = database::Error;
- async fn set_active_keyset(&self, unit: CurrencyUnit, id: Id) -> Result<(), Self::Err> {
- let transaction = self.pool.begin().await?;
- query(r#"UPDATE keyset SET active=FALSE WHERE unit IS :unit"#)
- .bind(":unit", unit.to_string())
- .execute(&transaction)
- .await?;
- query(r#"UPDATE keyset SET active=TRUE WHERE unit IS :unit AND id IS :id"#)
- .bind(":unit", unit.to_string())
- .bind(":id", id.to_string())
- .execute(&transaction)
- .await?;
- transaction.commit().await?;
- Ok(())
- }
- async fn get_active_keyset_id(&self, unit: &CurrencyUnit) -> Result<Option<Id>, Self::Err> {
- Ok(
- query(r#" SELECT id FROM keyset WHERE active = 1 AND unit IS :unit"#)
- .bind(":unit", unit.to_string())
- .pluck(&self.pool)
- .await?
- .map(|id| match id {
- Column::Text(text) => Ok(Id::from_str(&text)?),
- Column::Blob(id) => Ok(Id::from_bytes(&id)?),
- _ => Err(Error::InvalidKeysetId),
- })
- .transpose()?,
- )
- }
- async fn get_active_keysets(&self) -> Result<HashMap<CurrencyUnit, Id>, Self::Err> {
- Ok(query(r#"SELECT id, unit FROM keyset WHERE active = 1"#)
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(|row| {
- Ok((
- column_as_string!(&row[1], CurrencyUnit::from_str),
- column_as_string!(&row[0], Id::from_str, Id::from_bytes),
- ))
- })
- .collect::<Result<HashMap<_, _>, Error>>()?)
- }
- async fn add_keyset_info(&self, keyset: MintKeySetInfo) -> Result<(), Self::Err> {
- query(
- r#"
- INSERT INTO
- keyset (
- id, unit, active, valid_from, valid_to, derivation_path,
- max_order, input_fee_ppk, derivation_path_index
- )
- VALUES (
- :id, :unit, :active, :valid_from, :valid_to, :derivation_path,
- :max_order, :input_fee_ppk, :derivation_path_index
- )
- 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(":id", keyset.id.to_string())
- .bind(":unit", keyset.unit.to_string())
- .bind(":active", keyset.active)
- .bind(":valid_from", keyset.valid_from as i64)
- .bind(":valid_to", keyset.valid_to.map(|v| v as i64))
- .bind(":derivation_path", keyset.derivation_path.to_string())
- .bind(":max_order", keyset.max_order)
- .bind(":input_fee_ppk", keyset.input_fee_ppk as i64)
- .bind(":derivation_path_index", keyset.derivation_path_index)
- .execute(&self.pool)
- .await?;
- Ok(())
- }
- async fn get_keyset_info(&self, id: &Id) -> Result<Option<MintKeySetInfo>, Self::Err> {
- Ok(query(
- r#"SELECT
- id,
- unit,
- active,
- valid_from,
- valid_to,
- derivation_path,
- derivation_path_index,
- max_order,
- input_fee_ppk
- FROM
- keyset
- WHERE id=:id"#,
- )
- .bind(":id", id.to_string())
- .fetch_one(&self.pool)
- .await?
- .map(sqlite_row_to_keyset_info)
- .transpose()?)
- }
- async fn get_keyset_infos(&self) -> Result<Vec<MintKeySetInfo>, Self::Err> {
- Ok(query(
- r#"SELECT
- id,
- unit,
- active,
- valid_from,
- valid_to,
- derivation_path,
- derivation_path_index,
- max_order,
- input_fee_ppk
- FROM
- keyset
- "#,
- )
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_keyset_info)
- .collect::<Result<Vec<_>, _>>()?)
- }
- }
- #[async_trait]
- impl MintQuotesDatabase for MintSqliteDatabase {
- type Err = database::Error;
- async fn add_mint_quote(&self, quote: MintQuote) -> Result<(), Self::Err> {
- query(
- r#"
- INSERT INTO mint_quote (
- id, amount, unit, request, state, expiry, request_lookup_id,
- pubkey, created_time, paid_time, issued_time
- )
- VALUES (
- :id, :amount, :unit, :request, :state, :expiry, :request_lookup_id,
- :pubkey, :created_time, :paid_time, :issued_time
- )
- ON CONFLICT(id) DO UPDATE SET
- amount = excluded.amount,
- unit = excluded.unit,
- request = excluded.request,
- state = excluded.state,
- expiry = excluded.expiry,
- request_lookup_id = excluded.request_lookup_id,
- created_time = excluded.created_time,
- paid_time = excluded.paid_time,
- issued_time = excluded.issued_time
- ON CONFLICT(request_lookup_id) DO UPDATE SET
- amount = excluded.amount,
- unit = excluded.unit,
- request = excluded.request,
- state = excluded.state,
- expiry = excluded.expiry,
- id = excluded.id,
- created_time = excluded.created_time,
- paid_time = excluded.paid_time,
- issued_time = excluded.issued_time
- "#,
- )
- .bind(":id", quote.id.to_string())
- .bind(":amount", u64::from(quote.amount) as i64)
- .bind(":unit", quote.unit.to_string())
- .bind(":request", quote.request)
- .bind(":state", quote.state.to_string())
- .bind(":expiry", quote.expiry as i64)
- .bind(":request_lookup_id", quote.request_lookup_id)
- .bind(":pubkey", quote.pubkey.map(|p| p.to_string()))
- .bind(":created_time", quote.created_time as i64)
- .bind(":paid_time", quote.paid_time.map(|t| t as i64))
- .bind(":issued_time", quote.issued_time.map(|t| t as i64))
- .execute(&self.pool)
- .await?;
- Ok(())
- }
- async fn get_mint_quote(&self, quote_id: &Uuid) -> Result<Option<MintQuote>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- amount,
- unit,
- request,
- state,
- expiry,
- request_lookup_id,
- pubkey,
- created_time,
- paid_time,
- issued_time
- FROM
- mint_quote
- WHERE id = :id"#,
- )
- .bind(":id", quote_id.as_hyphenated().to_string())
- .fetch_one(&self.pool)
- .await?
- .map(sqlite_row_to_mint_quote)
- .transpose()?)
- }
- async fn get_mint_quote_by_request(
- &self,
- request: &str,
- ) -> Result<Option<MintQuote>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- amount,
- unit,
- request,
- state,
- expiry,
- request_lookup_id,
- pubkey,
- created_time,
- paid_time,
- issued_time
- FROM
- mint_quote
- WHERE request = :request"#,
- )
- .bind(":request", request.to_owned())
- .fetch_one(&self.pool)
- .await?
- .map(sqlite_row_to_mint_quote)
- .transpose()?)
- }
- async fn get_mint_quote_by_request_lookup_id(
- &self,
- request_lookup_id: &str,
- ) -> Result<Option<MintQuote>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- amount,
- unit,
- request,
- state,
- expiry,
- request_lookup_id,
- pubkey,
- created_time,
- paid_time,
- issued_time
- FROM
- mint_quote
- WHERE request_lookup_id = :request_lookup_id"#,
- )
- .bind(":request_lookup_id", request_lookup_id.to_owned())
- .fetch_one(&self.pool)
- .await?
- .map(sqlite_row_to_mint_quote)
- .transpose()?)
- }
- async fn update_mint_quote_state(
- &self,
- quote_id: &Uuid,
- state: MintQuoteState,
- ) -> Result<MintQuoteState, Self::Err> {
- let transaction = self.pool.begin().await?;
- let quote = query(
- r#"
- SELECT
- id,
- amount,
- unit,
- request,
- state,
- expiry,
- request_lookup_id,
- pubkey,
- created_time,
- paid_time,
- issued_time
- FROM
- mint_quote
- WHERE id = :id"#,
- )
- .bind(":id", quote_id.as_hyphenated().to_string())
- .fetch_one(&transaction)
- .await?
- .map(sqlite_row_to_mint_quote)
- .ok_or(Error::QuoteNotFound)??;
- let update_query = match state {
- MintQuoteState::Paid => {
- r#"UPDATE mint_quote SET state = :state, paid_time = :current_time WHERE id = :quote_id"#
- }
- MintQuoteState::Issued => {
- r#"UPDATE mint_quote SET state = :state, issued_time = :current_time WHERE id = :quote_id"#
- }
- _ => r#"UPDATE mint_quote SET state = :state WHERE id = :quote_id"#,
- };
- let current_time = unix_time();
- let update = match state {
- MintQuoteState::Paid => query(update_query)
- .bind(":state", state.to_string())
- .bind(":current_time", current_time as i64)
- .bind(":quote_id", quote_id.as_hyphenated().to_string()),
- MintQuoteState::Issued => query(update_query)
- .bind(":state", state.to_string())
- .bind(":current_time", current_time as i64)
- .bind(":quote_id", quote_id.as_hyphenated().to_string()),
- _ => query(update_query)
- .bind(":state", state.to_string())
- .bind(":quote_id", quote_id.as_hyphenated().to_string()),
- };
- match update.execute(&transaction).await {
- Ok(_) => {
- transaction.commit().await?;
- Ok(quote.state)
- }
- Err(err) => {
- tracing::error!("SQLite Could not update keyset: {:?}", err);
- if let Err(err) = transaction.rollback().await {
- tracing::error!("Could not rollback sql transaction: {}", err);
- }
- return Err(err.into());
- }
- }
- }
- async fn get_mint_quotes(&self) -> Result<Vec<MintQuote>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- amount,
- unit,
- request,
- state,
- expiry,
- request_lookup_id,
- pubkey,
- created_time,
- paid_time,
- issued_time
- FROM
- mint_quote
- "#,
- )
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_mint_quote)
- .collect::<Result<Vec<_>, _>>()?)
- }
- async fn get_mint_quotes_with_state(
- &self,
- state: MintQuoteState,
- ) -> Result<Vec<MintQuote>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- amount,
- unit,
- request,
- state,
- expiry,
- request_lookup_id,
- pubkey,
- created_time,
- paid_time,
- issued_time
- FROM
- mint_quote
- WHERE
- state = :state
- "#,
- )
- .bind(":state", state.to_string())
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_mint_quote)
- .collect::<Result<Vec<_>, _>>()?)
- }
- async fn remove_mint_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
- query(
- r#"
- DELETE FROM mint_quote
- WHERE id=?
- "#,
- )
- .bind(":id", quote_id.as_hyphenated().to_string())
- .execute(&self.pool)
- .await?;
- Ok(())
- }
- async fn add_melt_quote(&self, quote: mint::MeltQuote) -> Result<(), Self::Err> {
- query(
- r#"
- INSERT INTO melt_quote
- (
- id, unit, amount, request, fee_reserve, state,
- expiry, payment_preimage, request_lookup_id, msat_to_pay,
- created_time, paid_time
- )
- VALUES
- (
- :id, :unit, :amount, :request, :fee_reserve, :state,
- :expiry, :payment_preimage, :request_lookup_id, :msat_to_pay,
- :created_time, :paid_time
- )
- ON CONFLICT(id) DO UPDATE SET
- unit = excluded.unit,
- amount = excluded.amount,
- request = excluded.request,
- fee_reserve = excluded.fee_reserve,
- state = excluded.state,
- expiry = excluded.expiry,
- payment_preimage = excluded.payment_preimage,
- request_lookup_id = excluded.request_lookup_id,
- msat_to_pay = excluded.msat_to_pay,
- created_time = excluded.created_time,
- paid_time = excluded.paid_time
- ON CONFLICT(request_lookup_id) DO UPDATE SET
- unit = excluded.unit,
- amount = excluded.amount,
- request = excluded.request,
- fee_reserve = excluded.fee_reserve,
- state = excluded.state,
- expiry = excluded.expiry,
- payment_preimage = excluded.payment_preimage,
- id = excluded.id,
- created_time = excluded.created_time,
- paid_time = excluded.paid_time;
- "#,
- )
- .bind(":id", quote.id.to_string())
- .bind(":unit", quote.unit.to_string())
- .bind(":amount", u64::from(quote.amount) as i64)
- .bind(":request", quote.request)
- .bind(":fee_reserve", u64::from(quote.fee_reserve) as i64)
- .bind(":state", quote.state.to_string())
- .bind(":expiry", quote.expiry as i64)
- .bind(":payment_preimage", quote.payment_preimage)
- .bind(":request_lookup_id", quote.request_lookup_id)
- .bind(
- ":msat_to_pay",
- quote.msat_to_pay.map(|a| u64::from(a) as i64),
- )
- .bind(":created_time", quote.created_time as i64)
- .bind(":paid_time", quote.paid_time.map(|t| t as i64))
- .execute(&self.pool)
- .await?;
- Ok(())
- }
- async fn get_melt_quote(&self, quote_id: &Uuid) -> Result<Option<mint::MeltQuote>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- unit,
- amount,
- request,
- fee_reserve,
- state,
- expiry,
- payment_preimage,
- request_lookup_id,
- msat_to_pay,
- created_time,
- paid_time
- FROM
- melt_quote
- WHERE
- id=:id
- "#,
- )
- .bind(":id", quote_id.as_hyphenated().to_string())
- .fetch_one(&self.pool)
- .await?
- .map(sqlite_row_to_melt_quote)
- .transpose()?)
- }
- async fn get_melt_quotes(&self) -> Result<Vec<mint::MeltQuote>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- unit,
- amount,
- request,
- fee_reserve,
- state,
- expiry,
- payment_preimage,
- request_lookup_id,
- msat_to_pay,
- created_time,
- paid_time
- FROM
- melt_quote
- "#,
- )
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_melt_quote)
- .collect::<Result<Vec<_>, _>>()?)
- }
- async fn update_melt_quote_state(
- &self,
- quote_id: &Uuid,
- state: MeltQuoteState,
- ) -> Result<MeltQuoteState, Self::Err> {
- let transaction = self.pool.begin().await?;
- let quote = query(
- r#"
- SELECT
- id,
- unit,
- amount,
- request,
- fee_reserve,
- state,
- expiry,
- payment_preimage,
- request_lookup_id,
- msat_to_pay,
- created_time,
- paid_time
- FROM
- melt_quote
- WHERE
- id=:id
- "#,
- )
- .bind(":id", quote_id.as_hyphenated().to_string())
- .fetch_one(&transaction)
- .await?
- .map(sqlite_row_to_melt_quote)
- .transpose()?
- .ok_or(Error::QuoteNotFound)?;
- let rec = if state == MeltQuoteState::Paid {
- let current_time = unix_time();
- query(r#"UPDATE melt_quote SET state = :state, paid_time = :paid_time WHERE id = :id"#)
- .bind(":state", state.to_string())
- .bind(":paid_time", current_time as i64)
- .bind(":id", quote_id.as_hyphenated().to_string())
- .execute(&transaction)
- .await
- } else {
- query(r#"UPDATE melt_quote SET state = :state WHERE id = :id"#)
- .bind(":state", state.to_string())
- .bind(":id", quote_id.as_hyphenated().to_string())
- .execute(&transaction)
- .await
- };
- match rec {
- Ok(_) => {
- transaction.commit().await?;
- }
- Err(err) => {
- tracing::error!("SQLite Could not update melt quote");
- transaction.rollback().await?;
- return Err(err.into());
- }
- };
- Ok(quote.state)
- }
- async fn remove_melt_quote(&self, quote_id: &Uuid) -> Result<(), Self::Err> {
- query(
- r#"
- DELETE FROM melt_quote
- WHERE id=?
- "#,
- )
- .bind(":id", quote_id.as_hyphenated().to_string())
- .execute(&self.pool)
- .await?;
- Ok(())
- }
- async fn add_melt_request(
- &self,
- melt_request: MeltRequest<Uuid>,
- ln_key: PaymentProcessorKey,
- ) -> Result<(), Self::Err> {
- query(
- r#"
- INSERT INTO melt_request
- (id, inputs, outputs, method, unit)
- VALUES
- (:id, :inputs, :outputs, :method, :unit)
- ON CONFLICT(id) DO UPDATE SET
- inputs = excluded.inputs,
- outputs = excluded.outputs,
- method = excluded.method,
- unit = excluded.unit
- "#,
- )
- .bind(":id", melt_request.quote().to_string())
- .bind(":inputs", serde_json::to_string(&melt_request.inputs())?)
- .bind(":outputs", serde_json::to_string(&melt_request.outputs())?)
- .bind(":method", ln_key.method.to_string())
- .bind(":unit", ln_key.unit.to_string())
- .execute(&self.pool)
- .await?;
- Ok(())
- }
- async fn get_melt_request(
- &self,
- quote_id: &Uuid,
- ) -> Result<Option<(MeltRequest<Uuid>, PaymentProcessorKey)>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- id,
- inputs,
- outputs,
- method,
- unit
- FROM
- melt_request
- WHERE
- id=?;
- "#,
- )
- .bind(":id", quote_id.hyphenated().to_string())
- .fetch_one(&self.pool)
- .await?
- .map(sqlite_row_to_melt_request)
- .transpose()?)
- }
- }
- #[async_trait]
- impl MintProofsDatabase for MintSqliteDatabase {
- type Err = database::Error;
- async fn add_proofs(&self, proofs: Proofs, quote_id: Option<Uuid>) -> Result<(), Self::Err> {
- let transaction = self.pool.begin().await?;
- let current_time = unix_time();
- for proof in proofs {
- query(
- r#"
- INSERT OR IGNORE INTO proof
- (y, amount, keyset_id, secret, c, witness, state, quote_id, created_time)
- VALUES
- (:y, :amount, :keyset_id, :secret, :c, :witness, :state, :quote_id, :created_time)
- "#,
- )
- .bind(":y", proof.y()?.to_bytes().to_vec())
- .bind(":amount", u64::from(proof.amount) as i64)
- .bind(":keyset_id", proof.keyset_id.to_string())
- .bind(":secret", proof.secret.to_string())
- .bind(":c", proof.c.to_bytes().to_vec())
- .bind(
- ":witness",
- proof.witness.map(|w| serde_json::to_string(&w).unwrap()),
- )
- .bind(":state", "UNSPENT".to_string())
- .bind(":quote_id", quote_id.map(|q| q.hyphenated().to_string()))
- .bind(":created_time", current_time as i64)
- .execute(&transaction)
- .await?;
- }
- transaction.commit().await?;
- Ok(())
- }
- async fn remove_proofs(
- &self,
- ys: &[PublicKey],
- _quote_id: Option<Uuid>,
- ) -> Result<(), Self::Err> {
- let transaction = self.pool.begin().await?;
- let total_deleted = query(
- r#"
- DELETE FROM proof WHERE y IN (:ys) AND state != 'SPENT'
- "#,
- )
- .bind_vec(":ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
- .execute(&transaction)
- .await?;
- if total_deleted != ys.len() {
- transaction.rollback().await?;
- return Err(Self::Err::AttemptRemoveSpentProof);
- }
- transaction.commit().await?;
- Ok(())
- }
- async fn get_proofs_by_ys(&self, ys: &[PublicKey]) -> Result<Vec<Option<Proof>>, Self::Err> {
- let mut proofs = query(
- r#"
- SELECT
- amount,
- keyset_id,
- secret,
- c,
- witness,
- y
- FROM
- proof
- WHERE
- y IN (:ys)
- "#,
- )
- .bind_vec(":ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(|mut row| {
- Ok((
- column_as_string!(
- row.pop().ok_or(Error::InvalidDbPath)?,
- PublicKey::from_hex,
- PublicKey::from_slice
- ),
- sqlite_row_to_proof(row)?,
- ))
- })
- .collect::<Result<HashMap<_, _>, Error>>()?;
- Ok(ys.iter().map(|y| proofs.remove(y)).collect())
- }
- async fn get_proof_ys_by_quote_id(&self, quote_id: &Uuid) -> Result<Vec<PublicKey>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- amount,
- keyset_id,
- secret,
- c,
- witness
- FROM
- proof
- WHERE
- quote_id = :quote_id
- "#,
- )
- .bind(":quote_id", quote_id.as_hyphenated().to_string())
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_proof)
- .collect::<Result<Vec<Proof>, _>>()?
- .ys()?)
- }
- async fn get_proofs_states(&self, ys: &[PublicKey]) -> Result<Vec<Option<State>>, Self::Err> {
- let mut current_states = self.get_current_states(&self.pool, ys).await?;
- Ok(ys.iter().map(|y| current_states.remove(y)).collect())
- }
- async fn get_proofs_by_keyset_id(
- &self,
- keyset_id: &Id,
- ) -> Result<(Proofs, Vec<Option<State>>), Self::Err> {
- Ok(query(
- r#"
- SELECT
- keyset_id,
- amount,
- secret,
- c,
- witness
- state
- FROM
- proof
- WHERE
- keyset_id=?
- "#,
- )
- .bind(":keyset_id", keyset_id.to_string())
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_proof_with_state)
- .collect::<Result<Vec<_>, _>>()?
- .into_iter()
- .unzip())
- }
- async fn update_proofs_states(
- &self,
- ys: &[PublicKey],
- new_state: State,
- ) -> Result<Vec<Option<State>>, Self::Err> {
- let transaction = self.pool.begin().await?;
- let mut current_states = self.get_current_states(&transaction, ys).await?;
- if current_states.len() != ys.len() {
- transaction.rollback().await?;
- tracing::warn!("Attempted to update state of non-existent proof");
- return Err(database::Error::ProofNotFound);
- }
- for state in current_states.values() {
- check_state_transition(*state, new_state)?;
- }
- query(r#"UPDATE proof SET state = :new_state WHERE y IN (:ys)"#)
- .bind(":new_state", new_state.to_string())
- .bind_vec(":ys", ys.iter().map(|y| y.to_bytes().to_vec()).collect())
- .execute(&transaction)
- .await?;
- transaction.commit().await?;
- 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,
- blinded_messages: &[PublicKey],
- blind_signatures: &[BlindSignature],
- quote_id: Option<Uuid>,
- ) -> Result<(), Self::Err> {
- let transaction = self.pool.begin().await?;
- let current_time = unix_time();
- for (message, signature) in blinded_messages.iter().zip(blind_signatures) {
- query(
- r#"
- INSERT INTO blind_signature
- (y, amount, keyset_id, c, quote_id, dleq_e, dleq_s, created_time)
- VALUES
- (:y, :amount, :keyset_id, :c, :quote_id, :dleq_e, :dleq_s, :created_time)
- "#,
- )
- .bind(":y", message.to_bytes().to_vec())
- .bind(":amount", u64::from(signature.amount) as i64)
- .bind(":keyset_id", signature.keyset_id.to_string())
- .bind(":c", signature.c.to_bytes().to_vec())
- .bind(":quote_id", quote_id.map(|q| q.hyphenated().to_string()))
- .bind(
- ":dleq_e",
- signature.dleq.as_ref().map(|dleq| dleq.e.to_secret_hex()),
- )
- .bind(
- ":dleq_s",
- signature.dleq.as_ref().map(|dleq| dleq.s.to_secret_hex()),
- )
- .bind(":created_time", current_time as i64)
- .execute(&transaction)
- .await
- .expect("fasdas");
- }
- transaction.commit().await?;
- Ok(())
- }
- async fn get_blind_signatures(
- &self,
- blinded_messages: &[PublicKey],
- ) -> Result<Vec<Option<BlindSignature>>, Self::Err> {
- let mut blinded_signatures = query(
- r#"SELECT
- keyset_id,
- amount,
- c,
- dleq_e,
- dleq_s,
- y
- FROM
- blind_signature
- WHERE y IN (:y)
- "#,
- )
- .bind_vec(
- ":y",
- blinded_messages
- .iter()
- .map(|y| y.to_bytes().to_vec())
- .collect(),
- )
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(|mut row| {
- Ok((
- column_as_string!(
- &row.pop().ok_or(Error::InvalidDbResponse)?,
- PublicKey::from_hex,
- PublicKey::from_slice
- ),
- sqlite_row_to_blind_signature(row)?,
- ))
- })
- .collect::<Result<HashMap<_, _>, Error>>()?;
- Ok(blinded_messages
- .iter()
- .map(|y| blinded_signatures.remove(y))
- .collect())
- }
- async fn get_blind_signatures_for_keyset(
- &self,
- keyset_id: &Id,
- ) -> Result<Vec<BlindSignature>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- keyset_id,
- amount,
- c,
- dleq_e,
- dleq_s
- FROM
- blind_signature
- WHERE
- keyset_id=:keyset_id
- "#,
- )
- .bind(":keyset_id", keyset_id.to_string())
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_blind_signature)
- .collect::<Result<Vec<BlindSignature>, _>>()?)
- }
- /// Get [`BlindSignature`]s for quote
- async fn get_blind_signatures_for_quote(
- &self,
- quote_id: &Uuid,
- ) -> Result<Vec<BlindSignature>, Self::Err> {
- Ok(query(
- r#"
- SELECT
- keyset_id,
- amount,
- c,
- dleq_e,
- dleq_s
- FROM
- blind_signature
- WHERE
- quote_id=:quote_id
- "#,
- )
- .bind(":quote_id", quote_id.to_string())
- .fetch_all(&self.pool)
- .await?
- .into_iter()
- .map(sqlite_row_to_blind_signature)
- .collect::<Result<Vec<BlindSignature>, _>>()?)
- }
- }
- #[async_trait]
- impl MintDatabase<database::Error> for MintSqliteDatabase {
- async fn set_mint_info(&self, mint_info: MintInfo) -> Result<(), database::Error> {
- Ok(self.set_to_config("mint_info", &mint_info).await?)
- }
- async fn get_mint_info(&self) -> Result<MintInfo, database::Error> {
- Ok(self.fetch_from_config("mint_info").await?)
- }
- async fn set_quote_ttl(&self, quote_ttl: QuoteTTL) -> Result<(), database::Error> {
- Ok(self.set_to_config("quote_ttl", "e_ttl).await?)
- }
- async fn get_quote_ttl(&self) -> Result<QuoteTTL, database::Error> {
- Ok(self.fetch_from_config("quote_ttl").await?)
- }
- }
- fn sqlite_row_to_keyset_info(row: Vec<Column>) -> Result<MintKeySetInfo, Error> {
- unpack_into!(
- let (
- id,
- unit,
- active,
- valid_from,
- valid_to,
- derivation_path,
- derivation_path_index,
- max_order,
- row_keyset_ppk
- ) = row
- );
- Ok(MintKeySetInfo {
- id: column_as_string!(id, Id::from_str, Id::from_bytes),
- unit: column_as_string!(unit, CurrencyUnit::from_str),
- active: matches!(active, Column::Integer(1)),
- valid_from: column_as_number!(valid_from),
- valid_to: column_as_nullable_number!(valid_to),
- derivation_path: column_as_string!(derivation_path, DerivationPath::from_str),
- derivation_path_index: column_as_nullable_number!(derivation_path_index),
- max_order: column_as_number!(max_order),
- input_fee_ppk: column_as_number!(row_keyset_ppk),
- })
- }
- fn sqlite_row_to_mint_quote(row: Vec<Column>) -> Result<MintQuote, Error> {
- unpack_into!(
- let (
- id, amount, unit, request, state, expiry, request_lookup_id,
- pubkey, created_time, paid_time, issued_time
- ) = row
- );
- let request = column_as_string!(&request);
- let request_lookup_id = column_as_nullable_string!(&request_lookup_id).unwrap_or_else(|| {
- Bolt11Invoice::from_str(&request)
- .map(|invoice| invoice.payment_hash().to_string())
- .unwrap_or_else(|_| request.clone())
- });
- let pubkey = column_as_nullable_string!(&pubkey)
- .map(|pk| PublicKey::from_hex(&pk))
- .transpose()?;
- let id = column_as_string!(id);
- let amount: u64 = column_as_number!(amount);
- Ok(MintQuote {
- id: Uuid::parse_str(&id).map_err(|_| Error::InvalidUuid(id))?,
- amount: Amount::from(amount),
- unit: column_as_string!(unit, CurrencyUnit::from_str),
- request,
- state: column_as_string!(state, MintQuoteState::from_str),
- expiry: column_as_number!(expiry),
- request_lookup_id,
- pubkey,
- created_time: column_as_number!(created_time),
- paid_time: column_as_nullable_number!(paid_time).map(|p| p),
- issued_time: column_as_nullable_number!(issued_time).map(|p| p),
- })
- }
- fn sqlite_row_to_melt_quote(row: Vec<Column>) -> Result<mint::MeltQuote, Error> {
- unpack_into!(
- let (
- id,
- unit,
- amount,
- request,
- fee_reserve,
- state,
- expiry,
- payment_preimage,
- request_lookup_id,
- msat_to_pay,
- created_time,
- paid_time
- ) = row
- );
- let id = column_as_string!(id);
- let amount: u64 = column_as_number!(amount);
- let fee_reserve: u64 = column_as_number!(fee_reserve);
- let request = column_as_string!(&request);
- let request_lookup_id = column_as_nullable_string!(&request_lookup_id).unwrap_or_else(|| {
- Bolt11Invoice::from_str(&request)
- .map(|invoice| invoice.payment_hash().to_string())
- .unwrap_or_else(|_| request.clone())
- });
- let msat_to_pay: Option<u64> = column_as_nullable_number!(msat_to_pay);
- Ok(mint::MeltQuote {
- id: Uuid::parse_str(&id).map_err(|_| Error::InvalidUuid(id))?,
- amount: Amount::from(amount),
- fee_reserve: Amount::from(fee_reserve),
- unit: column_as_string!(unit, CurrencyUnit::from_str),
- request,
- payment_preimage: column_as_nullable_string!(payment_preimage),
- msat_to_pay: msat_to_pay.map(Amount::from),
- state: column_as_string!(state, QuoteState::from_str),
- expiry: column_as_number!(expiry),
- request_lookup_id,
- created_time: column_as_number!(created_time),
- paid_time: column_as_nullable_number!(paid_time).map(|p| p),
- })
- }
- fn sqlite_row_to_proof(row: Vec<Column>) -> Result<Proof, Error> {
- unpack_into!(
- let (
- amount,
- keyset_id,
- secret,
- c,
- witness
- ) = row
- );
- let amount: u64 = column_as_number!(amount);
- Ok(Proof {
- amount: Amount::from(amount),
- keyset_id: column_as_string!(keyset_id, Id::from_str),
- secret: column_as_string!(secret, Secret::from_str),
- c: column_as_string!(c, PublicKey::from_hex, PublicKey::from_slice),
- witness: column_as_nullable_string!(witness).and_then(|w| serde_json::from_str(&w).ok()),
- dleq: None,
- })
- }
- fn sqlite_row_to_proof_with_state(row: Vec<Column>) -> Result<(Proof, Option<State>), Error> {
- unpack_into!(
- let (
- keyset_id, amount, secret, c, witness, state
- ) = row
- );
- let amount: u64 = column_as_number!(amount);
- let state = column_as_nullable_string!(state).and_then(|s| State::from_str(&s).ok());
- Ok((
- Proof {
- amount: Amount::from(amount),
- keyset_id: column_as_string!(keyset_id, Id::from_str, Id::from_bytes),
- secret: column_as_string!(secret, Secret::from_str),
- c: column_as_string!(c, PublicKey::from_hex, PublicKey::from_slice),
- witness: column_as_nullable_string!(witness)
- .and_then(|w| serde_json::from_str(&w).ok()),
- dleq: None,
- },
- state,
- ))
- }
- fn sqlite_row_to_blind_signature(row: Vec<Column>) -> Result<BlindSignature, Error> {
- unpack_into!(
- let (
- keyset_id, amount, c, dleq_e, dleq_s
- ) = row
- );
- let dleq = match (
- column_as_nullable_string!(dleq_e),
- column_as_nullable_string!(dleq_s),
- ) {
- (Some(e), Some(s)) => Some(BlindSignatureDleq {
- e: SecretKey::from_hex(e)?,
- s: SecretKey::from_hex(s)?,
- }),
- _ => None,
- };
- let amount: u64 = column_as_number!(amount);
- Ok(BlindSignature {
- amount: Amount::from(amount),
- keyset_id: column_as_string!(keyset_id, Id::from_str, Id::from_bytes),
- c: column_as_string!(c, PublicKey::from_hex, PublicKey::from_slice),
- dleq,
- })
- }
- fn sqlite_row_to_melt_request(
- row: Vec<Column>,
- ) -> Result<(MeltRequest<Uuid>, PaymentProcessorKey), Error> {
- unpack_into!(
- let (
- id,
- inputs,
- outputs,
- method,
- unit
- ) = row
- );
- let id = column_as_string!(id);
- let melt_request = MeltRequest::new(
- Uuid::parse_str(&id).map_err(|_| Error::InvalidUuid(id))?,
- column_as_string!(&inputs, serde_json::from_str),
- column_as_nullable_string!(&outputs).and_then(|w| serde_json::from_str(&w).ok()),
- );
- let ln_key = PaymentProcessorKey {
- unit: column_as_string!(&unit, CurrencyUnit::from_str),
- method: column_as_string!(&method, PaymentMethod::from_str),
- };
- Ok((melt_request, ln_key))
- }
- #[cfg(test)]
- mod tests {
- use cdk_common::mint::MintKeySetInfo;
- use cdk_common::{mint_db_test, Amount};
- use super::*;
- #[tokio::test]
- async fn test_remove_spent_proofs() {
- let db = memory::empty().await.unwrap();
- // Create a keyset and add it to the database
- let keyset_id = Id::from_str("00916bbf7ef91a36").unwrap();
- let keyset_info = MintKeySetInfo {
- id: keyset_id,
- unit: CurrencyUnit::Sat,
- active: true,
- valid_from: 0,
- valid_to: None,
- derivation_path: bitcoin::bip32::DerivationPath::from_str("m/0'/0'/0'").unwrap(),
- derivation_path_index: Some(0),
- max_order: 32,
- input_fee_ppk: 0,
- };
- db.add_keyset_info(keyset_info).await.unwrap();
- let proofs = vec![
- Proof {
- amount: Amount::from(100),
- keyset_id,
- secret: Secret::generate(),
- c: SecretKey::generate().public_key(),
- witness: None,
- dleq: None,
- },
- Proof {
- amount: Amount::from(200),
- keyset_id,
- secret: Secret::generate(),
- c: SecretKey::generate().public_key(),
- witness: None,
- dleq: None,
- },
- ];
- // Add proofs to database
- db.add_proofs(proofs.clone(), None).await.unwrap();
- // Mark one proof as spent
- db.update_proofs_states(&[proofs[0].y().unwrap()], State::Spent)
- .await
- .unwrap();
- // Try to remove both proofs - should fail because one is spent
- let result = db
- .remove_proofs(&[proofs[0].y().unwrap(), proofs[1].y().unwrap()], None)
- .await;
- assert!(result.is_err());
- assert!(matches!(
- result.unwrap_err(),
- database::Error::AttemptRemoveSpentProof
- ));
- // Verify both proofs still exist
- let states = db
- .get_proofs_states(&[proofs[0].y().unwrap(), proofs[1].y().unwrap()])
- .await
- .unwrap();
- assert_eq!(states.len(), 2);
- assert_eq!(states[0], Some(State::Spent));
- assert_eq!(states[1], Some(State::Unspent));
- }
- #[tokio::test]
- async fn test_update_spent_proofs() {
- let db = memory::empty().await.unwrap();
- // Create a keyset and add it to the database
- let keyset_id = Id::from_str("00916bbf7ef91a36").unwrap();
- let keyset_info = MintKeySetInfo {
- id: keyset_id,
- unit: CurrencyUnit::Sat,
- active: true,
- valid_from: 0,
- valid_to: None,
- derivation_path: bitcoin::bip32::DerivationPath::from_str("m/0'/0'/0'").unwrap(),
- derivation_path_index: Some(0),
- max_order: 32,
- input_fee_ppk: 0,
- };
- db.add_keyset_info(keyset_info).await.unwrap();
- let proofs = vec![
- Proof {
- amount: Amount::from(100),
- keyset_id,
- secret: Secret::generate(),
- c: SecretKey::generate().public_key(),
- witness: None,
- dleq: None,
- },
- Proof {
- amount: Amount::from(200),
- keyset_id,
- secret: Secret::generate(),
- c: SecretKey::generate().public_key(),
- witness: None,
- dleq: None,
- },
- ];
- // Add proofs to database
- db.add_proofs(proofs.clone(), None).await.unwrap();
- // Mark one proof as spent
- db.update_proofs_states(&[proofs[0].y().unwrap()], State::Spent)
- .await
- .unwrap();
- // Try to update both proofs - should fail because one is spent
- let result = db
- .update_proofs_states(&[proofs[0].y().unwrap()], State::Unspent)
- .await;
- assert!(result.is_err());
- assert!(matches!(
- result.unwrap_err(),
- database::Error::AttemptUpdateSpentProof
- ));
- // Verify states haven't changed
- let states = db
- .get_proofs_states(&[proofs[0].y().unwrap(), proofs[1].y().unwrap()])
- .await
- .unwrap();
- assert_eq!(states.len(), 2);
- assert_eq!(states[0], Some(State::Spent));
- assert_eq!(states[1], Some(State::Unspent));
- }
- async fn provide_db() -> MintSqliteDatabase {
- memory::empty().await.unwrap()
- }
- mint_db_test!(provide_db);
- }
|