Skip to content

Commit

Permalink
feat: pay bolt12 (backends not impl)
Browse files Browse the repository at this point in the history
  • Loading branch information
thesimplekid committed Sep 28, 2024
1 parent 008c913 commit 4a83109
Show file tree
Hide file tree
Showing 17 changed files with 544 additions and 162 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 @@ -34,6 +34,8 @@ pub async fn create_mint_router(mint: Arc<Mint>) -> Result<Router> {
get(get_check_melt_bolt11_quote),
)
.route("/melt/bolt11", post(post_melt_bolt11))
.route("/melt/quote/bolt12", post(get_melt_bolt12_quote))
.route("/melt/bolt12", post(post_melt_bolt12))
.route("/checkstate", post(post_check))
.route("/info", get(get_mint_info))
.route("/restore", post(post_restore));
Expand Down
28 changes: 23 additions & 5 deletions crates/cdk-axum/src/router_handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use cdk::error::ErrorResponse;
use cdk::nuts::nut05::MeltBolt11Response;
use cdk::nuts::{
CheckStateRequest, CheckStateResponse, Id, KeysResponse, KeysetResponse, MeltBolt11Request,
MeltQuoteBolt11Request, MeltQuoteBolt11Response, MintBolt11Request, MintBolt11Response,
MintInfo, MintQuoteBolt11Request, MintQuoteBolt11Response, RestoreRequest, RestoreResponse,
SwapRequest, SwapResponse,
MeltBolt12Request, MeltQuoteBolt11Request, MeltQuoteBolt11Response, MeltQuoteBolt12Request,
MintBolt11Request, MintBolt11Response, MintInfo, MintQuoteBolt11Request,
MintQuoteBolt11Response, RestoreRequest, RestoreResponse, SwapRequest, SwapResponse,
};
use cdk::util::unix_time;

Expand Down Expand Up @@ -122,12 +122,30 @@ pub async fn post_melt_bolt11(
State(state): State<MintState>,
Json(payload): Json<MeltBolt11Request>,
) -> Result<Json<MeltBolt11Response>, Response> {
let res = state
let res = state.mint.melt(&payload).await.map_err(into_response)?;

Ok(Json(res.into()))
}

pub async fn get_melt_bolt12_quote(
State(state): State<MintState>,
Json(payload): Json<MeltQuoteBolt12Request>,
) -> Result<Json<MeltQuoteBolt11Response>, Response> {
let quote = state
.mint
.melt_bolt11(&payload)
.get_melt_bolt12_quote(&payload)
.await
.map_err(into_response)?;

Ok(Json(quote))
}

pub async fn post_melt_bolt12(
State(state): State<MintState>,
Json(payload): Json<MeltBolt12Request>,
) -> Result<Json<MeltQuoteBolt11Response>, Response> {
let res = state.mint.melt(&payload).await.map_err(into_response)?;

Ok(Json(res))
}

Expand Down
20 changes: 18 additions & 2 deletions crates/cdk-cln/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use cdk::cdk_lightning::{
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt12Request,
MeltQuoteState, MintMethodSettings, MintQuoteState,
};
use cdk::util::{hex, unix_time};
use cdk::{mint, Bolt11Invoice};
Expand Down Expand Up @@ -379,6 +379,22 @@ impl MintLightning for Cln {
}
}
}

async fn get_bolt12_payment_quote(
&self,
_melt_quote_request: &MeltQuoteBolt12Request,
) -> Result<PaymentQuoteResponse, Self::Err> {
todo!()
}

async fn pay_bolt12_offer(
&self,
_melt_quote: mint::MeltQuote,
_amount: Option<Amount>,
_max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}
}

impl Cln {
Expand Down
21 changes: 19 additions & 2 deletions crates/cdk-fake-wallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use cdk::cdk_lightning::{
use cdk::mint;
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt12Request,
MeltQuoteState, MintMethodSettings, MintQuoteState,
};
use cdk::util::unix_time;
use error::Error;
Expand Down Expand Up @@ -262,6 +262,23 @@ impl MintLightning for FakeWallet {
unit: self.get_settings().unit,
})
}

async fn get_bolt12_payment_quote(
&self,
_melt_quote_request: &MeltQuoteBolt12Request,
) -> Result<PaymentQuoteResponse, Self::Err> {
todo!()
}

/// Pay a bolt12 offer
async fn pay_bolt12_offer(
&self,
_melt_quote: mint::MeltQuote,
_amount: Option<Amount>,
_max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}
}

/// Create fake invoice
Expand Down
21 changes: 19 additions & 2 deletions crates/cdk-lnbits/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use cdk::cdk_lightning::{
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt12Request,
MeltQuoteState, MintMethodSettings, MintQuoteState,
};
use cdk::util::unix_time;
use cdk::{mint, Bolt11Invoice};
Expand Down Expand Up @@ -292,6 +292,23 @@ impl MintLightning for LNbits {

Ok(pay_response)
}

async fn get_bolt12_payment_quote(
&self,
_melt_quote_request: &MeltQuoteBolt12Request,
) -> Result<PaymentQuoteResponse, Self::Err> {
todo!()
}

/// Pay a bolt12 offer
async fn pay_bolt12_offer(
&self,
_melt_quote: mint::MeltQuote,
_amount: Option<Amount>,
_max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}
}

fn lnbits_to_melt_status(status: &str, pending: bool) -> MeltQuoteState {
Expand Down
21 changes: 19 additions & 2 deletions crates/cdk-lnd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use cdk::cdk_lightning::{
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt12Request,
MeltQuoteState, MintMethodSettings, MintQuoteState,
};
use cdk::util::{hex, unix_time};
use cdk::{mint, Bolt11Invoice};
Expand Down Expand Up @@ -347,4 +347,21 @@ impl MintLightning for Lnd {
// If the stream is exhausted without a final status
Err(Error::UnknownPaymentStatus.into())
}

async fn get_bolt12_payment_quote(
&self,
_melt_quote_request: &MeltQuoteBolt12Request,
) -> Result<PaymentQuoteResponse, Self::Err> {
todo!()
}

/// Pay a bolt12 offer
async fn pay_bolt12_offer(
&self,
_melt_quote: mint::MeltQuote,
_amount: Option<Amount>,
_max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}
}
6 changes: 1 addition & 5 deletions crates/cdk-mintd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -577,11 +577,7 @@ async fn check_pending_melt_quotes(
match pay_invoice_response.status {
MeltQuoteState::Paid => {
if let Err(err) = mint
.process_melt_request(
&melt_request,
pay_invoice_response.payment_preimage,
pay_invoice_response.total_spent,
)
.process_melt_request(&melt_request, pay_invoice_response.total_spent)
.await
{
tracing::error!(
Expand Down
20 changes: 18 additions & 2 deletions crates/cdk-phoenixd/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use cdk::cdk_lightning::{
};
use cdk::mint::FeeReserve;
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt12Request,
MeltQuoteState, MintMethodSettings, MintQuoteState,
};
use cdk::{mint, Bolt11Invoice};
use error::Error;
Expand Down Expand Up @@ -274,4 +274,20 @@ impl MintLightning for Phoenixd {

Ok(state)
}

async fn get_bolt12_payment_quote(
&self,
_melt_quote_request: &MeltQuoteBolt12Request,
) -> Result<PaymentQuoteResponse, Self::Err> {
todo!()
}

async fn pay_bolt12_offer(
&self,
_melt_quote: mint::MeltQuote,
_amount: Option<Amount>,
_max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}
}
20 changes: 18 additions & 2 deletions crates/cdk-strike/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use cdk::cdk_lightning::{
self, CreateInvoiceResponse, MintLightning, PayInvoiceResponse, PaymentQuoteResponse, Settings,
};
use cdk::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteBolt12Request,
MeltQuoteState, MintMethodSettings, MintQuoteState,
};
use cdk::util::unix_time;
use cdk::{mint, Bolt11Invoice};
Expand Down Expand Up @@ -273,6 +273,22 @@ impl MintLightning for Strike {

Ok(pay_invoice_response)
}

async fn get_bolt12_payment_quote(
&self,
_melt_quote_request: &MeltQuoteBolt12Request,
) -> Result<PaymentQuoteResponse, Self::Err> {
todo!()
}

async fn pay_bolt12_offer(
&self,
_melt_quote: mint::MeltQuote,
_amount: Option<Amount>,
_max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err> {
todo!()
}
}

impl Strike {
Expand Down
1 change: 1 addition & 0 deletions crates/cdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ bitcoin = { version= "0.32.2", features = ["base64", "serde", "rand", "rand-std"
ciborium = { version = "0.2.2", default-features = false, features = ["std"] }
cbor-diag = "0.1.12"
lightning-invoice = { version = "0.32.0", features = ["serde", "std"] }
lightning = { version = "0.0.124", deafult-features = false }
once_cell = "1.19"
regex = "1"
reqwest = { version = "0.12", default-features = false, features = [
Expand Down
15 changes: 15 additions & 0 deletions crates/cdk/src/cdk_lightning/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use lightning_invoice::{Bolt11Invoice, ParseOrSemanticError};
use serde::{Deserialize, Serialize};
use thiserror::Error;

use crate::nuts::nut18::MeltQuoteBolt12Request;
use crate::nuts::{
CurrencyUnit, MeltMethodSettings, MeltQuoteBolt11Request, MeltQuoteState, MintMethodSettings,
MintQuoteState,
Expand Down Expand Up @@ -96,6 +97,20 @@ pub trait MintLightning {
&self,
request_lookup_id: &str,
) -> Result<PayInvoiceResponse, Self::Err>;

/// Bolt12 Payment quote
async fn get_bolt12_payment_quote(
&self,
melt_quote_request: &MeltQuoteBolt12Request,
) -> Result<PaymentQuoteResponse, Self::Err>;

/// Pay a bolt12 offer
async fn pay_bolt12_offer(
&self,
melt_quote: mint::MeltQuote,
amount: Option<Amount>,
max_fee_amount: Option<Amount>,
) -> Result<PayInvoiceResponse, Self::Err>;
}

/// Create invoice response
Expand Down
3 changes: 3 additions & 0 deletions crates/cdk/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ pub enum Error {
/// NUT14 Error
#[error(transparent)]
NUT14(#[from] crate::nuts::nut14::Error),
/// NUT18 Error
#[error(transparent)]
NUT18(#[from] crate::nuts::nut18::Error),
/// Database Error
#[cfg(any(feature = "wallet", feature = "mint"))]
#[error(transparent)]
Expand Down
Loading

0 comments on commit 4a83109

Please sign in to comment.