fedimint_gateway_server/
client.rs

1use std::collections::BTreeSet;
2use std::fmt::Debug;
3use std::path::PathBuf;
4use std::sync::Arc;
5
6use fedimint_bip39::{Bip39RootSecretStrategy, Mnemonic};
7use fedimint_client::db::ClientConfigKey;
8use fedimint_client::module_init::ClientModuleInitRegistry;
9use fedimint_client::{Client, ClientBuilder, RootSecret};
10use fedimint_client_module::secret::{PlainRootSecretStrategy, RootSecretStrategy};
11use fedimint_connectors::ConnectorRegistry;
12use fedimint_core::config::FederationId;
13use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped};
14use fedimint_core::module::registry::ModuleDecoderRegistry;
15use fedimint_derive_secret::DerivableSecret;
16use fedimint_gateway_common::FederationConfig;
17use fedimint_gateway_server_db::GatewayDbExt as _;
18use fedimint_gw_client::GatewayClientInit;
19use fedimint_gwv2_client::GatewayClientInitV2;
20
21use crate::config::DatabaseBackend;
22use crate::error::AdminGatewayError;
23use crate::{AdminResult, Gateway};
24
25#[derive(Debug, Clone)]
26pub struct GatewayClientBuilder {
27    work_dir: PathBuf,
28    registry: ClientModuleInitRegistry,
29    db_backend: DatabaseBackend,
30    connectors: ConnectorRegistry,
31}
32
33impl GatewayClientBuilder {
34    pub async fn new(
35        work_dir: PathBuf,
36        registry: ClientModuleInitRegistry,
37        db_backend: DatabaseBackend,
38    ) -> anyhow::Result<Self> {
39        Ok(Self {
40            connectors: ConnectorRegistry::build_from_client_env()?.bind().await?,
41            work_dir,
42            registry,
43            db_backend,
44        })
45    }
46
47    pub fn data_dir(&self) -> PathBuf {
48        self.work_dir.clone()
49    }
50
51    /// Reads a plain root secret from a database to construct a database.
52    /// Only used for "legacy" federations before v0.5.0
53    async fn client_plainrootsecret(&self, db: &Database) -> AdminResult<DerivableSecret> {
54        let client_secret = Client::load_decodable_client_secret::<[u8; 64]>(db)
55            .await
56            .map_err(AdminGatewayError::ClientCreationError)?;
57        Ok(PlainRootSecretStrategy::to_root_secret(&client_secret))
58    }
59
60    /// Constructs the client builder with the modules, database, and connector
61    /// used to create clients for connected federations.
62    async fn create_client_builder(
63        &self,
64        federation_config: &FederationConfig,
65        gateway: Arc<Gateway>,
66    ) -> AdminResult<ClientBuilder> {
67        let FederationConfig {
68            federation_index,
69            connector,
70            ..
71        } = federation_config.to_owned();
72
73        let mut registry = self.registry.clone();
74
75        registry.attach(GatewayClientInit {
76            federation_index,
77            lightning_manager: gateway.clone(),
78        });
79
80        registry.attach(GatewayClientInitV2 {
81            gateway: gateway.clone(),
82        });
83
84        let mut client_builder = Client::builder()
85            .await
86            .map_err(AdminGatewayError::ClientCreationError)?
87            .with_iroh_enable_dht(true)
88            .with_iroh_enable_next(true);
89        client_builder.with_module_inits(registry);
90        client_builder.with_connector(connector);
91        Ok(client_builder)
92    }
93
94    /// Recovers a client with the provided mnemonic. This function will wait
95    /// for the recoveries to finish, but a new client must be created
96    /// afterwards and waited on until the state machines have finished
97    /// for a balance to be present.
98    pub async fn recover(
99        &self,
100        config: FederationConfig,
101        gateway: Arc<Gateway>,
102        mnemonic: &Mnemonic,
103    ) -> AdminResult<()> {
104        let federation_id = config.invite_code.federation_id();
105        let db = gateway.gateway_db.get_client_database(&federation_id);
106        let client_builder = self.create_client_builder(&config, gateway.clone()).await?;
107        let root_secret = RootSecret::StandardDoubleDerive(
108            Bip39RootSecretStrategy::<12>::to_root_secret(mnemonic),
109        );
110        let client = client_builder
111            .preview(self.connectors.clone(), &config.invite_code)
112            .await?
113            .recover(db, root_secret, None)
114            .await
115            .map(Arc::new)
116            .map_err(AdminGatewayError::ClientCreationError)?;
117        client
118            .wait_for_all_recoveries()
119            .await
120            .map_err(AdminGatewayError::ClientCreationError)?;
121        Ok(())
122    }
123
124    /// Builds a new client with the provided `FederationConfig` and `Mnemonic`.
125    /// Only used for newly joined federations.
126    pub async fn build(
127        &self,
128        config: FederationConfig,
129        gateway: Arc<Gateway>,
130        mnemonic: &Mnemonic,
131    ) -> AdminResult<fedimint_client::ClientHandleArc> {
132        let invite_code = config.invite_code.clone();
133        let federation_id = invite_code.federation_id();
134        let db_path = self.work_dir.join(format!("{federation_id}.db"));
135
136        let (db, root_secret) = if db_path.exists() {
137            let db = match self.db_backend {
138                DatabaseBackend::RocksDb => {
139                    let rocksdb = fedimint_rocksdb::RocksDb::open(db_path.clone())
140                        .await
141                        .map_err(AdminGatewayError::ClientCreationError)?;
142                    Database::new(rocksdb, ModuleDecoderRegistry::default())
143                }
144                DatabaseBackend::CursedRedb => {
145                    let cursed_redb = fedimint_cursed_redb::MemAndRedb::new(db_path.clone())
146                        .await
147                        .map_err(AdminGatewayError::ClientCreationError)?;
148                    Database::new(cursed_redb, ModuleDecoderRegistry::default())
149                }
150            };
151            let root_secret = RootSecret::Custom(self.client_plainrootsecret(&db).await?);
152            (db, root_secret)
153        } else {
154            let db = gateway.gateway_db.get_client_database(&federation_id);
155
156            let root_secret = RootSecret::StandardDoubleDerive(
157                Bip39RootSecretStrategy::<12>::to_root_secret(mnemonic),
158            );
159            (db, root_secret)
160        };
161
162        Self::verify_client_config(&db, federation_id).await?;
163
164        let client_builder = self.create_client_builder(&config, gateway).await?;
165
166        if Client::is_initialized(&db).await {
167            client_builder
168                .open(self.connectors.clone(), db, root_secret)
169                .await
170        } else {
171            client_builder
172                .preview(self.connectors.clone(), &invite_code)
173                .await?
174                .join(db, root_secret)
175                .await
176        }
177        .map(Arc::new)
178        .map_err(AdminGatewayError::ClientCreationError)
179    }
180
181    /// Verifies that the saved `ClientConfig` contains the expected
182    /// federation's config.
183    async fn verify_client_config(db: &Database, federation_id: FederationId) -> AdminResult<()> {
184        let mut dbtx = db.begin_transaction_nc().await;
185        if let Some(config) = dbtx.get_value(&ClientConfigKey).await
186            && config.calculate_federation_id() != federation_id
187        {
188            return Err(AdminGatewayError::ClientCreationError(anyhow::anyhow!(
189                "Federation Id did not match saved federation ID".to_string()
190            )));
191        }
192        Ok(())
193    }
194
195    /// Returns a vector of "legacy" federations which did not derive their
196    /// client secret's from the gateway's mnemonic.
197    pub fn legacy_federations(&self, all_federations: BTreeSet<FederationId>) -> Vec<FederationId> {
198        all_federations
199            .into_iter()
200            .filter(|federation_id| {
201                let db_path = self.work_dir.join(format!("{federation_id}.db"));
202                db_path.exists()
203            })
204            .collect::<Vec<FederationId>>()
205    }
206}