Skip to main content

fedimint_client_rpc/
lib.rs

1use std::collections::HashMap;
2use std::str::FromStr;
3use std::sync::Arc;
4use std::time::Duration;
5
6use anyhow::Context;
7use async_stream::try_stream;
8use fedimint_bip39::{Bip39RootSecretStrategy, Mnemonic};
9use fedimint_client::module::ClientModule;
10use fedimint_client::secret::RootSecretStrategy;
11use fedimint_client::{ClientHandleArc, ClientPreview, RootSecret};
12use fedimint_connectors::ConnectorRegistry;
13use fedimint_core::config::{FederationId, FederationIdPrefix};
14use fedimint_core::db::{Database, IDatabaseTransactionOpsCoreTyped};
15use fedimint_core::encoding::{Decodable, Encodable};
16use fedimint_core::invite_code::InviteCode;
17use fedimint_core::task::{MaybeSend, MaybeSync};
18use fedimint_core::util::{BoxFuture, BoxStream};
19use fedimint_core::{Amount, TieredCounts, impl_db_record};
20use fedimint_derive_secret::{ChildId, DerivableSecret};
21use fedimint_ln_client::{LightningClientInit, LightningClientModule};
22use fedimint_meta_client::MetaClientInit;
23use fedimint_mint_client::{MintClientInit, MintClientModule, OOBNotes};
24use fedimint_wallet_client::{WalletClientInit, WalletClientModule};
25use futures::StreamExt;
26use futures::future::{AbortHandle, Abortable};
27use lightning_invoice::Bolt11InvoiceDescriptionRef;
28use rand::thread_rng;
29use serde::{Deserialize, Serialize};
30use serde_json::json;
31use tokio::sync::Mutex;
32use tracing::info;
33
34// Key prefixes for the unified database
35#[repr(u8)]
36#[derive(Clone, Copy, Debug)]
37pub enum DbKeyPrefix {
38    ClientDatabase = 0x00,
39    Mnemonic = 0x01,
40}
41
42#[derive(Debug, Clone, Encodable, Decodable, Eq, PartialEq, Hash)]
43pub struct MnemonicKey;
44
45impl_db_record!(
46    key = MnemonicKey,
47    value = Vec<u8>,
48    db_prefix = DbKeyPrefix::Mnemonic,
49);
50
51/// Parsed details from an OOB note.
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct ParsedNoteDetails {
54    /// Total amount of all notes in the OOB notes
55    pub total_amount: Amount,
56    /// Federation ID prefix (always present)
57    pub federation_id_prefix: FederationIdPrefix,
58    /// Full federation ID (if invite is present)
59    pub federation_id: Option<FederationId>,
60    /// Invite code to join the federation (if present)
61    pub invite_code: Option<InviteCode>,
62    /// Number of notes per denomination
63    pub note_counts: TieredCounts,
64}
65
66#[derive(Serialize, Deserialize)]
67#[serde(rename_all = "snake_case")]
68pub struct RpcRequest {
69    pub request_id: u64,
70    #[serde(flatten)]
71    pub kind: RpcRequestKind,
72}
73
74#[derive(Serialize, Deserialize)]
75#[serde(tag = "type", rename_all = "snake_case")]
76pub enum RpcRequestKind {
77    SetMnemonic {
78        words: Vec<String>,
79    },
80    GenerateMnemonic,
81    GetMnemonic,
82    HasMnemonicSet,
83    /// Join federation (requires mnemonic to be set first)
84    JoinFederation {
85        invite_code: String,
86        force_recover: bool,
87        client_name: String,
88    },
89    OpenClient {
90        client_name: String,
91    },
92    CloseClient {
93        client_name: String,
94    },
95    ClientRpc {
96        client_name: String,
97        module: String,
98        method: String,
99        payload: serde_json::Value,
100    },
101    CancelRpc {
102        cancel_request_id: u64,
103    },
104    ParseInviteCode {
105        invite_code: String,
106    },
107    ParseBolt11Invoice {
108        invoice: String,
109    },
110    PreviewFederation {
111        invite_code: String,
112    },
113    ParseOobNotes {
114        oob_notes: String,
115    },
116    ParseLightningAddress {
117        address: String,
118    },
119}
120
121#[derive(Serialize, Deserialize, Clone, Debug)]
122pub struct RpcResponse {
123    pub request_id: u64,
124    #[serde(flatten)]
125    pub kind: RpcResponseKind,
126}
127
128#[derive(Serialize, Deserialize, Clone, Debug)]
129#[serde(tag = "type", rename_all = "snake_case")]
130pub enum RpcResponseKind {
131    Data { data: serde_json::Value },
132    Error { error: String },
133    Aborted {},
134    End {},
135}
136
137pub trait RpcResponseHandler: MaybeSend + MaybeSync {
138    fn handle_response(&self, response: RpcResponse);
139}
140
141pub struct RpcGlobalState {
142    /// Endpoints used for all global-state functionality
143    connectors: ConnectorRegistry,
144    clients: Mutex<HashMap<String, ClientHandleArc>>,
145    rpc_handles: std::sync::Mutex<HashMap<u64, AbortHandle>>,
146    unified_database: Database,
147    preview_cache: std::sync::Mutex<Option<ClientPreview>>,
148}
149
150pub struct HandledRpc<'a> {
151    pub task: Option<BoxFuture<'a, ()>>,
152}
153
154impl RpcGlobalState {
155    pub fn new(connectors: ConnectorRegistry, unified_database: Database) -> Self {
156        Self {
157            connectors,
158            clients: Mutex::new(HashMap::new()),
159            rpc_handles: std::sync::Mutex::new(HashMap::new()),
160            unified_database,
161            preview_cache: std::sync::Mutex::new(None),
162        }
163    }
164
165    async fn add_client(&self, client_name: String, client: ClientHandleArc) {
166        let mut clients = self.clients.lock().await;
167        clients.insert(client_name, client);
168    }
169
170    async fn get_client(&self, client_name: &str) -> Option<ClientHandleArc> {
171        let clients = self.clients.lock().await;
172        clients.get(client_name).cloned()
173    }
174
175    fn add_rpc_handle(&self, request_id: u64, handle: AbortHandle) {
176        let mut handles = self.rpc_handles.lock().unwrap();
177        if handles.insert(request_id, handle).is_some() {
178            tracing::error!("RPC CLIENT ERROR: request id reuse detected");
179        }
180    }
181
182    fn remove_rpc_handle(&self, request_id: u64) -> Option<AbortHandle> {
183        let mut handles = self.rpc_handles.lock().unwrap();
184        handles.remove(&request_id)
185    }
186
187    async fn client_builder() -> Result<fedimint_client::ClientBuilder, anyhow::Error> {
188        let mut builder = fedimint_client::Client::builder().await?;
189        builder.with_module(MintClientInit);
190        builder.with_module(LightningClientInit::default());
191        builder.with_module(WalletClientInit(None));
192        builder.with_module(MetaClientInit);
193        Ok(builder)
194    }
195
196    /// Get client-specific database with proper prefix
197    async fn client_db(&self, client_name: String) -> anyhow::Result<Database> {
198        assert_eq!(client_name.len(), 36);
199
200        let unified_db = &self.unified_database;
201        let mut client_prefix = vec![DbKeyPrefix::ClientDatabase as u8];
202        client_prefix.extend_from_slice(client_name.as_bytes());
203        Ok(unified_db.with_prefix(client_prefix))
204    }
205
206    /// Handle joining federation using unified database
207    async fn handle_join_federation(
208        &self,
209
210        invite_code: String,
211        client_name: String,
212        force_recover: bool,
213    ) -> anyhow::Result<()> {
214        // Check if wallet mnemonic is set
215        let mnemonic = self
216            .get_mnemonic_from_db()
217            .await?
218            .context("No wallet mnemonic set. Please set or generate a mnemonic first.")?;
219
220        let client_db = self.client_db(client_name.clone()).await?;
221
222        let invite_code = InviteCode::from_str(&invite_code)?;
223        let federation_id = invite_code.federation_id();
224
225        // Derive federation-specific secret from wallet mnemonic
226        let federation_secret = self.derive_federation_secret(&mnemonic, &federation_id);
227
228        // Try to consume cached preview, otherwise create new one
229        let cached_preview = self.preview_cache.lock().unwrap().take();
230        let preview = match cached_preview {
231            Some(preview) if preview.config().calculate_federation_id() == federation_id => preview,
232            _ => {
233                let builder = Self::client_builder().await?;
234                builder
235                    .preview(self.connectors.clone(), &invite_code)
236                    .await?
237            }
238        };
239
240        // Check if backup exists
241        #[allow(deprecated)]
242        let backup = preview
243            .download_backup_from_federation(RootSecret::StandardDoubleDerive(
244                federation_secret.clone(),
245            ))
246            .await?;
247
248        let client = if force_recover || backup.is_some() {
249            Arc::new(
250                preview
251                    .recover(
252                        client_db,
253                        RootSecret::StandardDoubleDerive(federation_secret),
254                        backup,
255                    )
256                    .await?,
257            )
258        } else {
259            Arc::new(
260                preview
261                    .join(
262                        client_db,
263                        RootSecret::StandardDoubleDerive(federation_secret),
264                    )
265                    .await?,
266            )
267        };
268
269        self.add_client(client_name, client).await;
270        Ok(())
271    }
272
273    async fn handle_open_client(&self, client_name: String) -> anyhow::Result<()> {
274        // Check if wallet mnemonic is set
275        let mnemonic = self
276            .get_mnemonic_from_db()
277            .await?
278            .context("No wallet mnemonic set. Please set or generate a mnemonic first.")?;
279
280        let client_db = self.client_db(client_name.clone()).await?;
281
282        if !fedimint_client::Client::is_initialized(&client_db).await {
283            anyhow::bail!("client is not initialized for this database");
284        }
285
286        // Get the client config to retrieve the federation ID
287        let client_config = fedimint_client::Client::get_config_from_db(&client_db)
288            .await
289            .context("Client config not found in database")?;
290
291        let federation_id = client_config.calculate_federation_id();
292
293        // Derive federation-specific secret from wallet mnemonic
294        let federation_secret = self.derive_federation_secret(&mnemonic, &federation_id);
295
296        let builder = Self::client_builder().await?;
297        let client = Arc::new(
298            builder
299                .open(
300                    self.connectors.clone(),
301                    client_db,
302                    RootSecret::StandardDoubleDerive(federation_secret),
303                )
304                .await?,
305        );
306
307        self.add_client(client_name, client).await;
308        Ok(())
309    }
310
311    async fn handle_close_client(&self, client_name: String) -> anyhow::Result<()> {
312        let mut clients = self.clients.lock().await;
313        let mut client = clients.remove(&client_name).context("client not found")?;
314
315        // RPC calls might have cloned the client Arc before we remove the client.
316        for attempt in 0.. {
317            info!(attempt, "waiting for RPCs to drop the federation object");
318            match Arc::try_unwrap(client) {
319                Ok(client) => {
320                    client.shutdown().await;
321                    break;
322                }
323                Err(client_val) => client = client_val,
324            }
325            fedimint_core::task::sleep(Duration::from_millis(100)).await;
326        }
327        Ok(())
328    }
329
330    fn handle_client_rpc(
331        self: Arc<Self>,
332        client_name: String,
333        module: String,
334        method: String,
335        payload: serde_json::Value,
336    ) -> BoxStream<'static, anyhow::Result<serde_json::Value>> {
337        Box::pin(try_stream! {
338            let client = self
339                .get_client(&client_name)
340                .await
341                .with_context(|| format!("Client not found: {client_name}"))?;
342            match module.as_str() {
343                "" => {
344                    let mut stream = client.handle_global_rpc(method, payload);
345                    while let Some(item) = stream.next().await {
346                        yield item?;
347                    }
348                }
349                "ln" => {
350                    let ln = client.get_first_module::<LightningClientModule>()?.inner();
351                    let mut stream = ln.handle_rpc(method, payload).await;
352                    while let Some(item) = stream.next().await {
353                        yield item?;
354                    }
355                }
356                "mint" => {
357                    let mint = client.get_first_module::<MintClientModule>()?.inner();
358                    let mut stream = mint.handle_rpc(method, payload).await;
359                    while let Some(item) = stream.next().await {
360                        yield item?;
361                    }
362                }
363                "wallet" => {
364                    let wallet = client
365                        .get_first_module::<WalletClientModule>()?
366                        .inner();
367                    let mut stream = wallet.handle_rpc(method, payload).await;
368                    while let Some(item) = stream.next().await {
369                        yield item?;
370                    }
371                }
372                _ => {
373                    Err(anyhow::format_err!("module not found: {module}"))?;
374                },
375            };
376        })
377    }
378
379    fn parse_invite_code(&self, invite_code: String) -> anyhow::Result<serde_json::Value> {
380        let invite_code = InviteCode::from_str(&invite_code)?;
381
382        Ok(json!({
383            "url": invite_code.url(),
384            "federation_id": invite_code.federation_id(),
385        }))
386    }
387
388    fn parse_bolt11_invoice(&self, invoice_str: String) -> anyhow::Result<serde_json::Value> {
389        let invoice = lightning_invoice::Bolt11Invoice::from_str(&invoice_str)
390            .map_err(|e| anyhow::anyhow!("Failed to parse Lightning invoice: {}", e))?;
391
392        let amount_msat = invoice.amount_milli_satoshis().unwrap_or(0);
393        let amount_sat = amount_msat as f64 / 1000.0;
394
395        let expiry_seconds = invoice.expiry_time().as_secs();
396
397        // memo
398        let description = match invoice.description() {
399            Bolt11InvoiceDescriptionRef::Direct(desc) => desc.to_string(),
400            Bolt11InvoiceDescriptionRef::Hash(_) => "Description hash only".to_string(),
401        };
402
403        Ok(json!({
404            "amount": amount_sat,
405            "expiry": expiry_seconds,
406            "memo": description,
407        }))
408    }
409
410    async fn preview_federation(&self, invite_code: String) -> anyhow::Result<serde_json::Value> {
411        let invite = InviteCode::from_str(&invite_code)?;
412        let federation_id = invite.federation_id();
413
414        let builder = Self::client_builder().await?;
415        let preview = builder.preview(self.connectors.clone(), &invite).await?;
416
417        let json_config = preview.config().to_json();
418        // Store in cache
419        *self.preview_cache.lock().unwrap() = Some(preview);
420
421        Ok(json!({
422            "config": json_config,
423            "federation_id": federation_id.to_string(),
424        }))
425    }
426
427    fn handle_rpc_inner(
428        self: Arc<Self>,
429        request: RpcRequest,
430    ) -> Option<BoxStream<'static, anyhow::Result<serde_json::Value>>> {
431        match request.kind {
432            RpcRequestKind::SetMnemonic { words } => Some(Box::pin(try_stream! {
433                self.set_mnemonic(words).await?;
434                yield serde_json::json!({ "success": true });
435            })),
436            RpcRequestKind::GenerateMnemonic => Some(Box::pin(try_stream! {
437                let words = self.generate_mnemonic().await?;
438                yield serde_json::json!({ "mnemonic": words });
439            })),
440            RpcRequestKind::GetMnemonic => Some(Box::pin(try_stream! {
441                let words = self.get_mnemonic_words().await?;
442                yield serde_json::json!({ "mnemonic": words });
443            })),
444            RpcRequestKind::HasMnemonicSet => Some(Box::pin(try_stream! {
445                let is_set = self.has_mnemonic_set().await?;
446                yield serde_json::json!(is_set);
447            })),
448            RpcRequestKind::JoinFederation {
449                invite_code,
450                client_name,
451                force_recover,
452            } => Some(Box::pin(try_stream! {
453                self.handle_join_federation(invite_code, client_name, force_recover)
454                    .await?;
455                yield serde_json::json!(null);
456            })),
457            RpcRequestKind::OpenClient { client_name } => Some(Box::pin(try_stream! {
458                self.handle_open_client(client_name).await?;
459                yield serde_json::json!(null);
460            })),
461            RpcRequestKind::CloseClient { client_name } => Some(Box::pin(try_stream! {
462                self.handle_close_client(client_name).await?;
463                yield serde_json::json!(null);
464            })),
465            RpcRequestKind::ClientRpc {
466                client_name,
467                module,
468                method,
469                payload,
470            } => Some(self.handle_client_rpc(client_name, module, method, payload)),
471            RpcRequestKind::ParseInviteCode { invite_code } => Some(Box::pin(try_stream! {
472                let result = self.parse_invite_code(invite_code)?;
473                yield result;
474            })),
475            RpcRequestKind::ParseBolt11Invoice { invoice } => Some(Box::pin(try_stream! {
476                let result = self.parse_bolt11_invoice(invoice)?;
477                yield result;
478            })),
479            RpcRequestKind::PreviewFederation { invite_code } => Some(Box::pin(try_stream! {
480                let result = self.preview_federation(invite_code).await?;
481                yield result;
482            })),
483            RpcRequestKind::ParseOobNotes { oob_notes } => Some(Box::pin(try_stream! {
484                let parsed = parse_oob_notes(&oob_notes)?;
485                yield serde_json::to_value(parsed)?;
486            })),
487            RpcRequestKind::ParseLightningAddress { address } => Some(Box::pin(try_stream! {
488                let url = fedimint_lnurl::parse_address(&address)
489                    .context("Invalid Lightning Address")?;
490                let metadata = fedimint_lnurl::request(&url).await
491                    .map_err(|e| anyhow::anyhow!(e))?;
492
493                yield serde_json::to_value(metadata)?;
494            })),
495            RpcRequestKind::CancelRpc { cancel_request_id } => {
496                if let Some(handle) = self.remove_rpc_handle(cancel_request_id) {
497                    handle.abort();
498                }
499                None
500            }
501        }
502    }
503
504    pub fn handle_rpc(
505        self: Arc<Self>,
506        request: RpcRequest,
507        handler: impl RpcResponseHandler + 'static,
508    ) -> HandledRpc<'static> {
509        let request_id = request.request_id;
510
511        let Some(stream) = self.clone().handle_rpc_inner(request) else {
512            return HandledRpc { task: None };
513        };
514
515        let (abort_handle, abort_registration) = AbortHandle::new_pair();
516        self.add_rpc_handle(request_id, abort_handle);
517
518        let task = Box::pin(async move {
519            let mut stream = Abortable::new(stream, abort_registration);
520
521            while let Some(result) = stream.next().await {
522                let response = match result {
523                    Ok(value) => RpcResponse {
524                        request_id,
525                        kind: RpcResponseKind::Data { data: value },
526                    },
527                    Err(e) => RpcResponse {
528                        request_id,
529                        kind: RpcResponseKind::Error {
530                            error: e.to_string(),
531                        },
532                    },
533                };
534                handler.handle_response(response);
535            }
536
537            // Clean up abort handle and send end message
538            let _ = self.remove_rpc_handle(request_id);
539            handler.handle_response(RpcResponse {
540                request_id,
541                kind: if stream.is_aborted() {
542                    RpcResponseKind::Aborted {}
543                } else {
544                    RpcResponseKind::End {}
545                },
546            });
547        });
548
549        HandledRpc { task: Some(task) }
550    }
551
552    /// Retrieve the wallet-level mnemonic words.
553    /// Returns the mnemonic as a vector of words, or None if no mnemonic is
554    /// set.
555    async fn get_mnemonic_words(&self) -> anyhow::Result<Option<Vec<String>>> {
556        let mnemonic = self.get_mnemonic_from_db().await?;
557
558        if let Some(mnemonic) = mnemonic {
559            let words = mnemonic.words().map(|w| w.to_string()).collect();
560            Ok(Some(words))
561        } else {
562            Ok(None)
563        }
564    }
565    /// Set a mnemonic from user-provided words
566    /// Returns an error if a mnemonic is already set
567    async fn set_mnemonic(&self, words: Vec<String>) -> anyhow::Result<()> {
568        let all_words = words.join(" ");
569        let mnemonic =
570            Mnemonic::parse_in_normalized(fedimint_bip39::Language::English, &all_words)?;
571
572        let mut dbtx = self.unified_database.begin_transaction().await;
573
574        if dbtx.get_value(&MnemonicKey).await.is_some() {
575            anyhow::bail!(
576                "Wallet mnemonic already exists. Please clear existing data before setting a new mnemonic."
577            );
578        }
579
580        dbtx.insert_new_entry(&MnemonicKey, &mnemonic.to_entropy())
581            .await;
582
583        dbtx.commit_tx().await;
584
585        Ok(())
586    }
587
588    /// Generate a new random mnemonic and set it
589    /// Returns an error if a mnemonic is already set
590    async fn generate_mnemonic(&self) -> anyhow::Result<Vec<String>> {
591        let mnemonic = Bip39RootSecretStrategy::<12>::random(&mut thread_rng());
592        let words: Vec<String> = mnemonic.words().map(|w| w.to_string()).collect();
593
594        let mut dbtx = self.unified_database.begin_transaction().await;
595
596        if dbtx.get_value(&MnemonicKey).await.is_some() {
597            anyhow::bail!(
598                "Wallet mnemonic already exists. Please clear existing data before generating a new mnemonic."
599            );
600        }
601
602        dbtx.insert_new_entry(&MnemonicKey, &mnemonic.to_entropy())
603            .await;
604
605        dbtx.commit_tx().await;
606
607        Ok(words)
608    }
609
610    /// Derive federation-specific secret from wallet mnemonic
611    fn derive_federation_secret(
612        &self,
613        mnemonic: &Mnemonic,
614        federation_id: &FederationId,
615    ) -> DerivableSecret {
616        let global_root_secret = Bip39RootSecretStrategy::<12>::to_root_secret(mnemonic);
617        let multi_federation_root_secret = global_root_secret.child_key(ChildId(0));
618        let federation_root_secret = multi_federation_root_secret.federation_key(federation_id);
619        let federation_wallet_root_secret = federation_root_secret.child_key(ChildId(0));
620        federation_wallet_root_secret.child_key(ChildId(0))
621    }
622
623    /// Fetch mnemonic from database
624    async fn get_mnemonic_from_db(&self) -> anyhow::Result<Option<Mnemonic>> {
625        let mut dbtx = self.unified_database.begin_transaction_nc().await;
626
627        if let Some(mnemonic_entropy) = dbtx.get_value(&MnemonicKey).await {
628            let mnemonic = Mnemonic::from_entropy(&mnemonic_entropy)?;
629            Ok(Some(mnemonic))
630        } else {
631            Ok(None)
632        }
633    }
634
635    /// Check if mnemonic is set
636    async fn has_mnemonic_set(&self) -> anyhow::Result<bool> {
637        let mnemonic = self.get_mnemonic_from_db().await?;
638        Ok(mnemonic.is_some())
639    }
640}
641
642pub fn parse_oob_notes(oob_notes_str: &str) -> anyhow::Result<ParsedNoteDetails> {
643    let oob_notes =
644        OOBNotes::from_str(oob_notes_str).context("Failed to parse OOB notes string")?;
645
646    let total_amount = oob_notes.total_amount();
647    let federation_id_prefix = oob_notes.federation_id_prefix();
648    let invite_code = oob_notes.federation_invite();
649    let federation_id = invite_code.as_ref().map(|inv| inv.federation_id());
650
651    // Get note counts by denomination
652    let notes = oob_notes.notes();
653    let mut note_counts = TieredCounts::default();
654    for (amount, _note) in notes.iter_items() {
655        note_counts.inc(amount, 1);
656    }
657
658    Ok(ParsedNoteDetails {
659        total_amount,
660        federation_id_prefix,
661        federation_id,
662        invite_code,
663        note_counts,
664    })
665}