1use fedimint_client::db::DbKeyPrefix as ClientDbKeyPrefix;
7use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped};
8use fedimint_core::encoding::{Decodable, Encodable};
9use fedimint_core::{PeerId, impl_db_record};
10use serde::{Deserialize, Serialize};
11
12const CLI_USER_DATA_SUB_PREFIX: u8 = 0x00;
14
15pub fn cli_database(db: &Database) -> Database {
17 db.with_prefix(vec![
18 ClientDbKeyPrefix::UserData as u8,
19 CLI_USER_DATA_SUB_PREFIX,
20 ])
21}
22
23#[repr(u8)]
25#[derive(Clone, Debug)]
26pub enum CliDbKeyPrefix {
27 AdminCreds = 0x00,
28}
29
30impl std::fmt::Display for CliDbKeyPrefix {
31 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
32 write!(
33 f,
34 "{}",
35 match self {
36 CliDbKeyPrefix::AdminCreds => "AdminCreds",
37 }
38 )
39 }
40}
41
42#[derive(Debug, Clone, Encodable, Decodable)]
44pub struct AdminCredsKey;
45
46#[derive(Debug, Clone, Encodable, Decodable, Serialize, Deserialize)]
48pub struct StoredAdminCreds {
49 pub peer_id: PeerId,
51 pub auth: String,
54}
55
56impl_db_record!(
57 key = AdminCredsKey,
58 value = StoredAdminCreds,
59 db_prefix = CliDbKeyPrefix::AdminCreds,
60);
61
62pub async fn load_admin_creds(db: &Database) -> Option<StoredAdminCreds> {
64 let cli_db = cli_database(db);
65 cli_db
66 .begin_transaction_nc()
67 .await
68 .get_value(&AdminCredsKey)
69 .await
70}
71
72pub async fn store_admin_creds(db: &Database, creds: &StoredAdminCreds) {
74 let cli_db = cli_database(db);
75 let mut dbtx = cli_db.begin_transaction().await;
76 dbtx.insert_entry(&AdminCredsKey, creds).await;
77 dbtx.commit_tx().await;
78}