cdk_sqlite/mint/
memory.rs1use std::collections::HashMap;
3
4use cdk_common::database::{self, MintDatabase, MintKeysDatabase};
5use cdk_common::mint::{self, MintKeySetInfo, MintQuote};
6use cdk_common::nuts::{CurrencyUnit, Id, Proofs};
7use cdk_common::MintInfo;
8
9use super::MintSqliteDatabase;
10
11const CDK_MINT_PRIMARY_NAMESPACE: &str = "cdk_mint";
12const CDK_MINT_CONFIG_SECONDARY_NAMESPACE: &str = "config";
13const CDK_MINT_CONFIG_KV_KEY: &str = "mint_info";
14
15pub async fn empty() -> Result<MintSqliteDatabase, database::Error> {
17 #[cfg(not(feature = "sqlcipher"))]
18 let path = ":memory:";
19 #[cfg(feature = "sqlcipher")]
20 let path = (":memory:", "memory");
21
22 MintSqliteDatabase::new(path).await
23}
24
25#[allow(clippy::too_many_arguments)]
27pub async fn new_with_state(
28 active_keysets: HashMap<CurrencyUnit, Id>,
29 keysets: Vec<MintKeySetInfo>,
30 mint_quotes: Vec<MintQuote>,
31 melt_quotes: Vec<mint::MeltQuote>,
32 pending_proofs: Proofs,
33 spent_proofs: Proofs,
34 mint_info: MintInfo,
35) -> Result<MintSqliteDatabase, database::Error> {
36 let db = empty().await?;
37 let mut tx = MintKeysDatabase::begin_transaction(&db).await?;
38
39 for active_keyset in active_keysets {
40 tx.set_active_keyset(active_keyset.0, active_keyset.1)
41 .await?;
42 }
43
44 for keyset in keysets {
45 tx.add_keyset_info(keyset).await?;
46 }
47 tx.commit().await?;
48
49 let mut tx = MintDatabase::begin_transaction(&db).await?;
50
51 for quote in mint_quotes {
52 tx.add_mint_quote(quote).await?;
53 }
54
55 for quote in melt_quotes {
56 tx.add_melt_quote(quote).await?;
57 }
58
59 tx.add_proofs(pending_proofs, None).await?;
60 tx.add_proofs(spent_proofs, None).await?;
61 let mint_info_bytes = serde_json::to_vec(&mint_info)?;
62 tx.kv_write(
63 CDK_MINT_PRIMARY_NAMESPACE,
64 CDK_MINT_CONFIG_SECONDARY_NAMESPACE,
65 CDK_MINT_CONFIG_KV_KEY,
66 &mint_info_bytes,
67 )
68 .await?;
69 tx.commit().await?;
70
71 Ok(db)
72}