fedimint_mint_server/
db.rs

1use fedimint_core::encoding::{Decodable, Encodable};
2use fedimint_core::{Amount, OutPoint, impl_db_lookup, impl_db_record};
3use fedimint_mint_common::{BlindNonce, MintOutputOutcome, Nonce, RecoveryItem};
4use serde::Serialize;
5use strum_macros::EnumIter;
6
7#[repr(u8)]
8#[derive(Clone, EnumIter, Debug)]
9pub enum DbKeyPrefix {
10    NoteNonce = 0x10,
11    OutputOutcome = 0x13,
12    MintAuditItem = 0x14,
13    // 0x15 was previously used for e-cash backups, but removed in DB migration 1
14    BlindNonce = 0x16,
15    RecoveryItem = 0x17,
16    RecoveryBlindNonceOutpoint = 0x18,
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#[derive(Debug, Clone, Copy, Encodable, Decodable, Serialize)]
96pub struct RecoveryItemKey(pub u64);
97
98#[derive(Debug, Encodable, Decodable)]
99pub struct RecoveryItemKeyPrefix;
100
101impl_db_record!(
102    key = RecoveryItemKey,
103    value = RecoveryItem,
104    db_prefix = DbKeyPrefix::RecoveryItem,
105);
106impl_db_lookup!(key = RecoveryItemKey, query_prefix = RecoveryItemKeyPrefix);
107
108/// Maps blind nonce to outpoint for recovery
109#[derive(Debug, Encodable, Decodable, Serialize)]
110pub struct RecoveryBlindNonceOutpointKey(pub BlindNonce);
111
112#[derive(Debug, Encodable, Decodable)]
113pub struct RecoveryBlindNonceOutpointKeyPrefix;
114
115impl_db_record!(
116    key = RecoveryBlindNonceOutpointKey,
117    value = OutPoint,
118    db_prefix = DbKeyPrefix::RecoveryBlindNonceOutpoint,
119);
120impl_db_lookup!(
121    key = RecoveryBlindNonceOutpointKey,
122    query_prefix = RecoveryBlindNonceOutpointKeyPrefix
123);