fedimint_mint_server/
db.rs

1use std::time::SystemTime;
2
3use fedimint_core::encoding::{Decodable, Encodable};
4use fedimint_core::{impl_db_lookup, impl_db_record, Amount, OutPoint};
5use fedimint_mint_common::{BlindNonce, MintOutputOutcome, Nonce};
6use serde::{Deserialize, Serialize};
7use strum_macros::EnumIter;
8
9#[repr(u8)]
10#[derive(Clone, EnumIter, Debug)]
11pub enum DbKeyPrefix {
12    NoteNonce = 0x10,
13    OutputOutcome = 0x13,
14    MintAuditItem = 0x14,
15    EcashBackup = 0x15,
16    BlindNonce = 0x16,
17}
18
19impl std::fmt::Display for DbKeyPrefix {
20    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
21        write!(f, "{self:?}")
22    }
23}
24
25/// Index for all the spent e-cash note nonces to prevent double spends.
26/// **Extremely safety critical!**
27#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash, Serialize)]
28pub struct NonceKey(pub Nonce);
29
30#[derive(Debug, Encodable, Decodable)]
31pub struct NonceKeyPrefix;
32
33impl_db_record!(
34    key = NonceKey,
35    value = (),
36    db_prefix = DbKeyPrefix::NoteNonce,
37);
38impl_db_lookup!(key = NonceKey, query_prefix = NonceKeyPrefix);
39
40/// Index for all the previously used blind nonces. Just a safety net for
41/// clients to not accidentally burn money.
42#[derive(Debug, Encodable, Decodable, Serialize)]
43pub struct BlindNonceKey(pub BlindNonce);
44
45#[derive(Debug, Encodable, Decodable)]
46pub struct BlindNonceKeyPrefix;
47
48impl_db_record!(
49    key = BlindNonceKey,
50    value = (),
51    db_prefix = DbKeyPrefix::BlindNonce,
52);
53impl_db_lookup!(key = BlindNonceKey, query_prefix = BlindNonceKeyPrefix);
54
55/// Transaction id and output index identifying an output outcome
56#[derive(Debug, Clone, Copy, Encodable, Decodable, Serialize)]
57pub struct MintOutputOutcomeKey(pub OutPoint);
58
59#[derive(Debug, Encodable, Decodable)]
60pub struct MintOutputOutcomePrefix;
61
62impl_db_record!(
63    key = MintOutputOutcomeKey,
64    value = MintOutputOutcome,
65    db_prefix = DbKeyPrefix::OutputOutcome,
66);
67impl_db_lookup!(
68    key = MintOutputOutcomeKey,
69    query_prefix = MintOutputOutcomePrefix
70);
71
72/// Represents the amounts of issued (signed) and redeemed (verified) notes for
73/// auditing
74#[derive(Debug, Clone, Encodable, Decodable, Serialize)]
75pub enum MintAuditItemKey {
76    Issuance(OutPoint),
77    IssuanceTotal,
78    Redemption(NonceKey),
79    RedemptionTotal,
80}
81
82#[derive(Debug, Encodable, Decodable)]
83pub struct MintAuditItemKeyPrefix;
84
85impl_db_record!(
86    key = MintAuditItemKey,
87    value = Amount,
88    db_prefix = DbKeyPrefix::MintAuditItem,
89);
90impl_db_lookup!(
91    key = MintAuditItemKey,
92    query_prefix = MintAuditItemKeyPrefix
93);
94
95/// Key used to store user's ecash backups
96#[derive(Debug, Clone, Copy, Encodable, Decodable, Serialize)]
97pub struct EcashBackupKey(pub fedimint_core::secp256k1::PublicKey);
98
99#[derive(Debug, Encodable, Decodable)]
100pub struct EcashBackupKeyPrefix;
101
102impl_db_record!(
103    key = EcashBackupKey,
104    value = ECashUserBackupSnapshot,
105    db_prefix = DbKeyPrefix::EcashBackup,
106);
107impl_db_lookup!(key = EcashBackupKey, query_prefix = EcashBackupKeyPrefix);
108
109/// User's backup, received at certain time, containing encrypted payload
110#[derive(Debug, Clone, PartialEq, Eq, Encodable, Decodable, Serialize, Deserialize)]
111pub struct ECashUserBackupSnapshot {
112    pub timestamp: SystemTime,
113    #[serde(with = "fedimint_core::hex::serde")]
114    pub data: Vec<u8>,
115}