From cfb23e87fc154d40f2b857645de2ba85bf5000f2 Mon Sep 17 00:00:00 2001 From: "kody.low" Date: Tue, 19 Mar 2024 17:34:36 -0700 Subject: [PATCH] fix: cashu endpoints for fedimint 0.3 --- .../src/router/handlers/cashu/info.rs | 2 +- .../src/router/handlers/cashu/melt/method.rs | 22 +++++++++++++-- .../src/router/handlers/cashu/mint/method.rs | 28 +++++++++++++++---- 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/fedimint-clientd/src/router/handlers/cashu/info.rs b/fedimint-clientd/src/router/handlers/cashu/info.rs index 18c2847..7c3dc23 100644 --- a/fedimint-clientd/src/router/handlers/cashu/info.rs +++ b/fedimint-clientd/src/router/handlers/cashu/info.rs @@ -84,7 +84,7 @@ pub async fn handle_info( let response = CashuNUT06InfoResponse { name: config.global.federation_name().unwrap().to_string(), - pubkey: config.global.federation_id().to_string(), + pubkey: config.global.calculate_federation_id().to_string(), version: format!("{:?}", config.global.consensus_version), description: "Cashu <-> Fedimint Soon (tm)".to_string(), description_long: "Cashu <-> Fedimint Soon (tm)".to_string(), diff --git a/fedimint-clientd/src/router/handlers/cashu/melt/method.rs b/fedimint-clientd/src/router/handlers/cashu/melt/method.rs index f8e7894..727b8df 100644 --- a/fedimint-clientd/src/router/handlers/cashu/melt/method.rs +++ b/fedimint-clientd/src/router/handlers/cashu/melt/method.rs @@ -12,7 +12,7 @@ use fedimint_wallet_client::{WalletClientModule, WithdrawState}; use futures_util::StreamExt; use lightning_invoice::Bolt11Invoice; use serde::{Deserialize, Serialize}; -use tracing::info; +use tracing::{error, info}; use crate::error::AppError; use crate::router::handlers::cashu::{Method, Unit}; @@ -69,7 +69,23 @@ pub async fn melt_bolt11( amount_msat: Amount, ) -> Result { let lightning_module = client.get_first_module::(); - lightning_module.select_active_gateway().await?; + let gateway_id = match lightning_module.list_gateways().await.first() { + Some(gateway_announcement) => gateway_announcement.info.gateway_id, + None => { + error!("No gateways available"); + return Err(AppError::new( + StatusCode::INTERNAL_SERVER_ERROR, + anyhow!("No gateways available"), + )) + } + }; + let gateway = lightning_module.select_gateway(&gateway_id).await.ok_or_else(|| { + error!("Failed to select gateway"); + AppError::new( + StatusCode::INTERNAL_SERVER_ERROR, + anyhow!("Failed to select gateway"), + ) + })?; let bolt11 = Bolt11Invoice::from_str(&request)?; let bolt11_amount = Amount::from_msats( @@ -93,7 +109,7 @@ pub async fn melt_bolt11( payment_type, contract_id: _, fee, - } = lightning_module.pay_bolt11_invoice(bolt11, ()).await?; + } = lightning_module.pay_bolt11_invoice(Some(gateway), bolt11, ()).await?; let operation_id = payment_type.operation_id(); info!("Gateway fee: {fee}, payment operation id: {operation_id}"); diff --git a/fedimint-clientd/src/router/handlers/cashu/mint/method.rs b/fedimint-clientd/src/router/handlers/cashu/mint/method.rs index 4bdf801..e0dac51 100644 --- a/fedimint-clientd/src/router/handlers/cashu/mint/method.rs +++ b/fedimint-clientd/src/router/handlers/cashu/mint/method.rs @@ -10,7 +10,9 @@ use fedimint_core::time::now; use fedimint_core::Amount; use fedimint_ln_client::LightningClientModule; use fedimint_wallet_client::WalletClientModule; +use lightning_invoice::{Bolt11InvoiceDescription, Description}; use serde::{Deserialize, Serialize}; +use tracing::error; use crate::error::AppError; use crate::router::handlers::cashu::{Method, Unit}; @@ -63,18 +65,34 @@ pub async fn mint_bolt11( client: ClientHandleArc, amount_msat: Amount, ) -> Result { - let lightning_module = client.get_first_module::(); - lightning_module.select_active_gateway().await?; - let valid_until = now() + Duration::from_secs(DEFAULT_MINT_EXPIRY_OFFSET); let expiry_time = crate::utils::system_time_to_u64(valid_until)?; + let lightning_module = client.get_first_module::(); + let gateway_id = match lightning_module.list_gateways().await.first() { + Some(gateway_announcement) => gateway_announcement.info.gateway_id, + None => { + error!("No gateways available"); + return Err(AppError::new( + StatusCode::INTERNAL_SERVER_ERROR, + anyhow!("No gateways available"), + )) + } + }; + let gateway = lightning_module.select_gateway(&gateway_id).await.ok_or_else(|| { + error!("Failed to select gateway"); + AppError::new( + StatusCode::INTERNAL_SERVER_ERROR, + anyhow!("Failed to select gateway"), + ) + })?; - let (operation_id, invoice) = lightning_module + let (operation_id, invoice, _) = lightning_module .create_bolt11_invoice( amount_msat, - format!("{}, method={:?}", DEFAULT_MINT_DESCRIPTION, Method::Bolt11), + Bolt11InvoiceDescription::Direct(&Description::new(DEFAULT_MINT_DESCRIPTION.to_string())?), Some(expiry_time), (), + Some(gateway), ) .await?;