Skip to content

Commit

Permalink
feat: bolt12 mint endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Oct 3, 2024
1 parent 124dc2a commit f78fd32
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 16 deletions.
2 changes: 2 additions & 0 deletions crates/cdk-axum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub async fn create_mint_router(mint: Arc<Mint>) -> Result<Router> {
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",
Expand Down
29 changes: 29 additions & 0 deletions crates/cdk-axum/src/router_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ pub async fn get_mint_bolt11_quote(
Ok(Json(quote))
}

pub async fn get_mint_bolt12_quote(
State(state): State<MintState>,
Json(payload): Json<MintQuoteBolt11Request>,
) -> Result<Json<MintQuoteBolt11Response>, 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<MintState>,
Path(quote_id): Path<String>,
Expand Down Expand Up @@ -89,6 +102,22 @@ pub async fn post_mint_bolt11(
Ok(Json(res))
}

pub async fn post_mint_bolt12(
State(state): State<MintState>,
Json(payload): Json<MintBolt11Request>,
) -> Result<Json<MintBolt11Response>, 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<MintState>,
Json(payload): Json<MeltQuoteBolt11Request>,
Expand Down
15 changes: 13 additions & 2 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CreateOfferResponse, Self::Err> {
todo!()
}
}

impl Cln {
Expand Down
15 changes: 13 additions & 2 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -293,6 +293,17 @@ impl MintLightning for FakeWallet {
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}

/// Create bolt12 offer
async fn create_bolt12_offer(
&self,
_amount: Amount,
_unit: &CurrencyUnit,
_description: String,
_unix_expiry: u64,
) -> Result<CreateOfferResponse, Self::Err> {
todo!()
}
}

/// Create fake invoice
Expand Down
15 changes: 13 additions & 2 deletions crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -315,6 +315,17 @@ impl MintLightning for LNbits {
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}

/// Create bolt12 offer
async fn create_bolt12_offer(
&self,
_amount: Amount,
_unit: &CurrencyUnit,
_description: String,
_unix_expiry: u64,
) -> Result<CreateOfferResponse, Self::Err> {
todo!()
}
}

fn lnbits_to_melt_status(status: &str, pending: bool) -> MeltQuoteState {
Expand Down
15 changes: 13 additions & 2 deletions crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,4 +368,15 @@ impl MintLightning for Lnd {
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}

/// Create bolt12 offer
async fn create_bolt12_offer(
&self,
_amount: Amount,
_unit: &CurrencyUnit,
_description: String,
_unix_expiry: u64,
) -> Result<CreateOfferResponse, Self::Err> {
todo!()
}
}
15 changes: 13 additions & 2 deletions crates/cdk-phoenixd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<CreateOfferResponse, Self::Err> {
todo!()
}
}
15 changes: 13 additions & 2 deletions crates/cdk-strike/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -290,6 +290,17 @@ impl MintLightning for Strike {
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}

/// Create bolt12 offer
async fn create_bolt12_offer(
&self,
_amount: Amount,
_unit: &CurrencyUnit,
_description: String,
_unix_expiry: u64,
) -> Result<CreateOfferResponse, Self::Err> {
todo!()
}
}

impl Strike {
Expand Down
23 changes: 22 additions & 1 deletion crates/cdk/src/cdk_lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -117,10 +118,19 @@ pub trait MintLightning {
amount: Option<Amount>,
max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err>;

/// Create bolt12 offer
async fn create_bolt12_offer(
&self,
amount: Amount,
unit: &CurrencyUnit,
description: String,
unix_expiry: u64,
) -> Result<CreateOfferResponse, Self::Err>;
}

/// 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,
Expand All @@ -130,6 +140,17 @@ pub struct CreateInvoiceResponse {
pub expiry: Option<u64>,
}

/// 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<u64>,
}

/// Pay invoice response
#[derive(Debug, Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct PayInvoiceResponse {
Expand Down
6 changes: 3 additions & 3 deletions crates/cdk/src/mint/mint_nut04.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -83,7 +83,7 @@ impl Mint {
}

let create_invoice_response = ln
.create_invoice(
.create_bolt12_offer(
amount,
&unit,
description.unwrap_or("".to_string()),
Expand All @@ -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,
Expand Down

0 comments on commit f78fd32

Please sign in to comment.