use super::RpcHandler; use crate::wallet::{ecash, fedimint_client, profits}; use anyhow::Result; /// A Cashu token (NUT-00 `cashuA`/`cashuB`, or our legacy `cashuSend_` form) /// always starts with `cashu`. Fedimint ecash notes never do, so a non-`cashu` /// string is routed to the Fedimint reissue path. fn is_cashu_token(token: &str) -> bool { token.trim_start().starts_with("cashu") } impl RpcHandler { pub(super) async fn handle_wallet_ecash_balance(&self) -> Result { let wallet = ecash::load_wallet(&self.config.data_dir).await?; Ok(serde_json::json!({ "balance_sats": wallet.balance(), "proof_count": wallet.proofs.iter().filter(|p| !p.spent && !p.reserved).count(), "mint_url": wallet.mint_url, })) } pub(super) async fn handle_wallet_ecash_mint( &self, params: Option, ) -> Result { let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?; let amount_sats = params .get("amount_sats") .and_then(|v| v.as_u64()) .ok_or_else(|| anyhow::anyhow!("Missing amount_sats"))?; if amount_sats == 0 || amount_sats > 1_000_000 { return Err(anyhow::anyhow!( "Amount must be between 1 and 1,000,000 sats" )); } // Step 1: Get a mint quote (returns Lightning invoice) let quote = ecash::mint_quote(&self.config.data_dir, amount_sats).await?; Ok(serde_json::json!({ "quote_id": quote.quote, "bolt11": quote.request, "state": quote.state, "amount_sats": amount_sats, "message": "Pay the Lightning invoice, then call wallet.ecash-mint-claim with the quote_id", })) } /// Claim minted tokens after paying the Lightning invoice. pub(super) async fn handle_wallet_ecash_mint_claim( &self, params: Option, ) -> Result { let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?; let quote_id = params .get("quote_id") .and_then(|v| v.as_str()) .ok_or_else(|| anyhow::anyhow!("Missing quote_id"))?; let amount_sats = params .get("amount_sats") .and_then(|v| v.as_u64()) .ok_or_else(|| anyhow::anyhow!("Missing amount_sats"))?; let minted = ecash::mint_tokens(&self.config.data_dir, quote_id, amount_sats).await?; Ok(serde_json::json!({ "minted_sats": minted, })) } pub(super) async fn handle_wallet_ecash_melt( &self, params: Option, ) -> Result { let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?; let bolt11 = params .get("bolt11") .and_then(|v| v.as_str()) .ok_or_else(|| anyhow::anyhow!("Missing bolt11 (Lightning invoice)"))?; // Step 1: Get melt quote let quote = ecash::melt_quote(&self.config.data_dir, bolt11).await?; Ok(serde_json::json!({ "quote_id": quote.quote, "amount_sats": quote.amount, "fee_reserve_sats": quote.fee_reserve, "total_needed_sats": quote.amount + quote.fee_reserve, "message": "Call wallet.ecash-melt-confirm with quote_id and bolt11 to execute", })) } /// Confirm and execute a melt (pay Lightning invoice with ecash). pub(super) async fn handle_wallet_ecash_melt_confirm( &self, params: Option, ) -> Result { let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?; let quote_id = params .get("quote_id") .and_then(|v| v.as_str()) .ok_or_else(|| anyhow::anyhow!("Missing quote_id"))?; let bolt11 = params .get("bolt11") .and_then(|v| v.as_str()) .ok_or_else(|| anyhow::anyhow!("Missing bolt11"))?; let melted = ecash::melt_tokens(&self.config.data_dir, quote_id, bolt11).await?; Ok(serde_json::json!({ "melted_sats": melted, })) } pub(super) async fn handle_wallet_ecash_send( &self, params: Option, ) -> Result { let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?; let amount_sats = params .get("amount_sats") .and_then(|v| v.as_u64()) .ok_or_else(|| anyhow::anyhow!("Missing amount_sats"))?; let token_str = ecash::send_token(&self.config.data_dir, amount_sats).await?; Ok(serde_json::json!({ "token": token_str, "amount_sats": amount_sats, })) } pub(super) async fn handle_wallet_ecash_receive( &self, params: Option, ) -> Result { let params = params.ok_or_else(|| anyhow::anyhow!("Missing params"))?; let token = params .get("token") .and_then(|v| v.as_str()) .map(str::trim) .filter(|s| !s.is_empty()) .ok_or_else(|| anyhow::anyhow!("Missing token"))?; // Dual-ecash: one "Receive ecash" box accepts either a Cashu token // (redeemed at the mint) or Fedimint notes (reissued via the fmcd // sidecar). Detect by prefix and route accordingly. if is_cashu_token(token) { let amount = ecash::receive_token(&self.config.data_dir, token).await?; return Ok(serde_json::json!({ "received_sats": amount, "kind": "cashu", })); } let (amount, federation_id) = fedimint_client::reissue_into_any(&self.config.data_dir, token).await?; Ok(serde_json::json!({ "received_sats": amount, "kind": "fedimint", "federation_id": federation_id, })) } pub(super) async fn handle_wallet_ecash_history(&self) -> Result { let wallet = ecash::load_wallet(&self.config.data_dir).await?; Ok(serde_json::json!({ "transactions": wallet.transactions, })) } pub(super) async fn handle_wallet_networking_profits(&self) -> Result { let summary = profits::get_networking_profits(&self.config.data_dir).await?; Ok(serde_json::json!({ "total_sats": summary.total_sats, "content_sales_sats": summary.content_sales_sats, "routing_fees_sats": summary.routing_fees_sats, "streaming_revenue_sats": summary.streaming_revenue_sats, "recent": summary.recent, })) } }