From f78fd32a952748fb8fa2587753439dda42884694 Mon Sep 17 00:00:00 2001 From: thesimplekid Date: Thu, 3 Oct 2024 15:24:00 +0200 Subject: [PATCH] feat: bolt12 mint endpoints --- crates/cdk-axum/src/lib.rs | 2 ++ crates/cdk-axum/src/router_handlers.rs | 29 ++++++++++++++++++++++++++ crates/cdk-cln/src/lib.rs | 15 +++++++++++-- crates/cdk-fake-wallet/src/lib.rs | 15 +++++++++++-- crates/cdk-lnbits/src/lib.rs | 15 +++++++++++-- crates/cdk-lnd/src/lib.rs | 15 +++++++++++-- crates/cdk-phoenixd/src/lib.rs | 15 +++++++++++-- crates/cdk-strike/src/lib.rs | 15 +++++++++++-- crates/cdk/src/cdk_lightning/mod.rs | 23 +++++++++++++++++++- crates/cdk/src/mint/mint_nut04.rs | 6 +++--- 10 files changed, 134 insertions(+), 16 deletions(-) diff --git a/crates/cdk-axum/src/lib.rs b/crates/cdk-axum/src/lib.rs index d2a87037..0f90543e 100644 --- a/crates/cdk-axum/src/lib.rs +++ b/crates/cdk-axum/src/lib.rs @@ -28,6 +28,8 @@ pub async fn create_mint_router(mint: Arc) -> Result { get(get_check_mint_bolt11_quote), ) .route("/mint/bolt11", post(post_mint_bolt11)) + .route("/mint/quote/bolt12", post(get_mint_bolt12_quote)) + .route("/mint/bolt12", post(post_mint_bolt12)) .route("/melt/quote/bolt11", post(get_melt_bolt11_quote)) .route( "/melt/quote/bolt11/:quote_id", diff --git a/crates/cdk-axum/src/router_handlers.rs b/crates/cdk-axum/src/router_handlers.rs index 64281558..9f2cb1bd 100644 --- a/crates/cdk-axum/src/router_handlers.rs +++ b/crates/cdk-axum/src/router_handlers.rs @@ -57,6 +57,19 @@ pub async fn get_mint_bolt11_quote( Ok(Json(quote)) } +pub async fn get_mint_bolt12_quote( + State(state): State, + Json(payload): Json, +) -> Result, Response> { + let quote = state + .mint + .get_mint_bolt11_quote(payload) + .await + .map_err(into_response)?; + + Ok(Json(quote)) +} + pub async fn get_check_mint_bolt11_quote( State(state): State, Path(quote_id): Path, @@ -89,6 +102,22 @@ pub async fn post_mint_bolt11( Ok(Json(res)) } +pub async fn post_mint_bolt12( + State(state): State, + Json(payload): Json, +) -> Result, Response> { + let res = state + .mint + .process_mint_request(payload) + .await + .map_err(|err| { + tracing::error!("Could not process mint: {}", err); + into_response(err) + })?; + + Ok(Json(res)) +} + pub async fn get_melt_bolt11_quote( State(state): State, Json(payload): Json, diff --git a/crates/cdk-cln/src/lib.rs b/crates/cdk-cln/src/lib.rs index df0f51ba..29e09088 100644 --- a/crates/cdk-cln/src/lib.rs +++ b/crates/cdk-cln/src/lib.rs @@ -12,8 +12,8 @@ use std::time::Duration; use async_trait::async_trait; use cdk::amount::{amount_for_offer, to_unit, Amount}; use cdk::cdk_lightning::{ - self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, - PaymentQuoteResponse, Settings, + self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, CreateOfferResponse, MintLightning, + PayInvoiceResponse, PaymentQuoteResponse, Settings, }; use cdk::mint::types::PaymentRequest; use cdk::mint::FeeReserve; @@ -517,6 +517,17 @@ impl MintLightning for Cln { Ok(response) } + + /// Create bolt12 offer + async fn create_bolt12_offer( + &self, + _amount: Amount, + _unit: &CurrencyUnit, + _description: String, + _unix_expiry: u64, + ) -> Result { + todo!() + } } impl Cln { diff --git a/crates/cdk-fake-wallet/src/lib.rs b/crates/cdk-fake-wallet/src/lib.rs index caec61e1..3e880713 100644 --- a/crates/cdk-fake-wallet/src/lib.rs +++ b/crates/cdk-fake-wallet/src/lib.rs @@ -14,8 +14,8 @@ use bitcoin::hashes::{sha256, Hash}; use bitcoin::secp256k1::{Secp256k1, SecretKey}; use cdk::amount::{to_unit, Amount}; use cdk::cdk_lightning::{ - self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, - PaymentQuoteResponse, Settings, + self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, CreateOfferResponse, MintLightning, + PayInvoiceResponse, PaymentQuoteResponse, Settings, }; use cdk::mint; use cdk::mint::types::PaymentRequest; @@ -293,6 +293,17 @@ impl MintLightning for FakeWallet { ) -> Result { todo!() } + + /// Create bolt12 offer + async fn create_bolt12_offer( + &self, + _amount: Amount, + _unit: &CurrencyUnit, + _description: String, + _unix_expiry: u64, + ) -> Result { + todo!() + } } /// Create fake invoice diff --git a/crates/cdk-lnbits/src/lib.rs b/crates/cdk-lnbits/src/lib.rs index a3fb0ac6..ec9d283f 100644 --- a/crates/cdk-lnbits/src/lib.rs +++ b/crates/cdk-lnbits/src/lib.rs @@ -11,8 +11,8 @@ use async_trait::async_trait; use axum::Router; use cdk::amount::{to_unit, Amount, MSAT_IN_SAT}; use cdk::cdk_lightning::{ - self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, - PaymentQuoteResponse, Settings, + self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, CreateOfferResponse, MintLightning, + PayInvoiceResponse, PaymentQuoteResponse, Settings, }; use cdk::mint::types::PaymentRequest; use cdk::mint::FeeReserve; @@ -315,6 +315,17 @@ impl MintLightning for LNbits { ) -> Result { todo!() } + + /// Create bolt12 offer + async fn create_bolt12_offer( + &self, + _amount: Amount, + _unit: &CurrencyUnit, + _description: String, + _unix_expiry: u64, + ) -> Result { + todo!() + } } fn lnbits_to_melt_status(status: &str, pending: bool) -> MeltQuoteState { diff --git a/crates/cdk-lnd/src/lib.rs b/crates/cdk-lnd/src/lib.rs index 49b79e63..e9bc6bae 100644 --- a/crates/cdk-lnd/src/lib.rs +++ b/crates/cdk-lnd/src/lib.rs @@ -14,8 +14,8 @@ use anyhow::anyhow; use async_trait::async_trait; use cdk::amount::{to_unit, Amount, MSAT_IN_SAT}; use cdk::cdk_lightning::{ - self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, - PaymentQuoteResponse, Settings, + self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, CreateOfferResponse, MintLightning, + PayInvoiceResponse, PaymentQuoteResponse, Settings, }; use cdk::mint::types::PaymentRequest; use cdk::mint::FeeReserve; @@ -368,4 +368,15 @@ impl MintLightning for Lnd { ) -> Result { todo!() } + + /// Create bolt12 offer + async fn create_bolt12_offer( + &self, + _amount: Amount, + _unit: &CurrencyUnit, + _description: String, + _unix_expiry: u64, + ) -> Result { + todo!() + } } diff --git a/crates/cdk-phoenixd/src/lib.rs b/crates/cdk-phoenixd/src/lib.rs index da87a1a1..f2c4ea8b 100644 --- a/crates/cdk-phoenixd/src/lib.rs +++ b/crates/cdk-phoenixd/src/lib.rs @@ -12,8 +12,8 @@ use async_trait::async_trait; use axum::Router; use cdk::amount::{amount_for_offer, to_unit, Amount}; use cdk::cdk_lightning::{ - self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, - PaymentQuoteResponse, Settings, + self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, CreateOfferResponse, MintLightning, + PayInvoiceResponse, PaymentQuoteResponse, Settings, }; use cdk::mint::types::PaymentRequest; use cdk::mint::FeeReserve; @@ -373,4 +373,15 @@ impl MintLightning for Phoenixd { unit: CurrencyUnit::Sat, }) } + + /// Create bolt12 offer + async fn create_bolt12_offer( + &self, + _amount: Amount, + _unit: &CurrencyUnit, + _description: String, + _unix_expiry: u64, + ) -> Result { + todo!() + } } diff --git a/crates/cdk-strike/src/lib.rs b/crates/cdk-strike/src/lib.rs index 2b847818..940a5977 100644 --- a/crates/cdk-strike/src/lib.rs +++ b/crates/cdk-strike/src/lib.rs @@ -11,8 +11,8 @@ use async_trait::async_trait; use axum::Router; use cdk::amount::Amount; use cdk::cdk_lightning::{ - self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, - PaymentQuoteResponse, Settings, + self, Bolt12PaymentQuoteResponse, CreateInvoiceResponse, CreateOfferResponse, MintLightning, + PayInvoiceResponse, PaymentQuoteResponse, Settings, }; use cdk::nuts::{ CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt12Request, @@ -290,6 +290,17 @@ impl MintLightning for Strike { ) -> Result { todo!() } + + /// Create bolt12 offer + async fn create_bolt12_offer( + &self, + _amount: Amount, + _unit: &CurrencyUnit, + _description: String, + _unix_expiry: u64, + ) -> Result { + todo!() + } } impl Strike { diff --git a/crates/cdk/src/cdk_lightning/mod.rs b/crates/cdk/src/cdk_lightning/mod.rs index ef8d2f49..422fe00a 100644 --- a/crates/cdk/src/cdk_lightning/mod.rs +++ b/crates/cdk/src/cdk_lightning/mod.rs @@ -4,6 +4,7 @@ use std::pin::Pin; use async_trait::async_trait; use futures::Stream; +use lightning::offers::offer::Offer; use lightning_invoice::{Bolt11Invoice, ParseOrSemanticError}; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -117,10 +118,19 @@ pub trait MintLightning { amount: Option, max_fee_amount: Option, ) -> Result; + + /// Create bolt12 offer + async fn create_bolt12_offer( + &self, + amount: Amount, + unit: &CurrencyUnit, + description: String, + unix_expiry: u64, + ) -> Result; } /// Create invoice response -#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, Hash, PartialEq, Eq)] pub struct CreateInvoiceResponse { /// Id that is used to look up the invoice from the ln backend pub request_lookup_id: String, @@ -130,6 +140,17 @@ pub struct CreateInvoiceResponse { pub expiry: Option, } +/// Create offer response +#[derive(Debug, Clone, Hash, PartialEq, Eq)] +pub struct CreateOfferResponse { + /// Id that is used to look up the invoice from the ln backend + pub request_lookup_id: String, + /// Bolt11 payment request + pub request: Offer, + /// Unix Expiry of Invoice + pub expiry: Option, +} + /// Pay invoice response #[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)] pub struct PayInvoiceResponse { diff --git a/crates/cdk/src/mint/mint_nut04.rs b/crates/cdk/src/mint/mint_nut04.rs index 33e0c324..16e84f8a 100644 --- a/crates/cdk/src/mint/mint_nut04.rs +++ b/crates/cdk/src/mint/mint_nut04.rs @@ -68,7 +68,7 @@ impl Mint { let ln = self .ln - .get(&LnKey::new(unit, PaymentMethod::Bolt11)) + .get(&LnKey::new(unit, PaymentMethod::Bolt12)) .ok_or_else(|| { tracing::info!("Bolt11 mint request for unsupported unit"); @@ -83,7 +83,7 @@ impl Mint { } let create_invoice_response = ln - .create_invoice( + .create_bolt12_offer( amount, &unit, description.unwrap_or("".to_string()), @@ -105,7 +105,7 @@ impl Mint { ); tracing::debug!( - "New mint quote {} for {} {} with request id {}", + "New bolt12 mint quote {} for {} {} with request id {}", quote.id, amount, unit,