Skip to main content

fedimint_walletv2_client/
db.rs

1use fedimint_core::core::Account;
2use fedimint_core::encoding::{Decodable, Encodable};
3use fedimint_core::{impl_db_lookup, impl_db_record};
4use serde::Serialize;
5use strum_macros::EnumIter;
6
7#[repr(u8)]
8#[derive(Clone, Debug, EnumIter)]
9pub enum DbKeyPrefix {
10    NextOutputIndex = 0x31,
11    ValidAddressIndex = 0x32,
12}
13
14impl std::fmt::Display for DbKeyPrefix {
15    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
16        write!(f, "{self:?}")
17    }
18}
19
20/// How far the scanner has walked the federation's output stream. The stream is
21/// federation-wide and one sweep serves every account, so this is a single
22/// cursor rather than one per account.
23#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
24pub struct NextOutputIndexKey;
25
26impl_db_record!(
27    key = NextOutputIndexKey,
28    value = u64,
29    db_prefix = DbKeyPrefix::NextOutputIndex
30);
31
32/// Address indices whose derived script the federation can pay, split by the
33/// key's leading [`Account`]. Iterating a federation's whole prefix yields
34/// `(account, index)` pairs — exactly what the scanner's address map wants.
35#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
36pub struct ValidAddressIndexKey(pub Account, pub u64);
37
38impl_db_record!(
39    key = ValidAddressIndexKey,
40    value = (),
41    db_prefix = DbKeyPrefix::ValidAddressIndex
42);
43
44/// Every account's indices, in account order.
45#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
46pub struct ValidAddressIndexPrefix;
47
48impl_db_lookup!(
49    key = ValidAddressIndexKey,
50    query_prefix = ValidAddressIndexPrefix
51);
52
53#[derive(Clone, Debug, Encodable, Decodable, Serialize)]
54pub struct ValidAddressIndexAccountPrefix(pub Account);
55
56impl_db_lookup!(
57    key = ValidAddressIndexKey,
58    query_prefix = ValidAddressIndexAccountPrefix
59);