refactor(cdk): defer BOLT12 invoice fetching to payment execution (#978)

* refactor(cdk): defer BOLT12 invoice fetching to payment execution

Move BOLT12 invoice generation from quote creation to payment time, make
request_lookup_id optional for offers, and simplify payment structures by
removing unnecessary boxing and intermediate storage of invoices.
This commit is contained in:
thesimplekid
2025-08-20 11:20:30 +01:00
committed by GitHub
parent 5c5075af71
commit ecdc78135d
17 changed files with 193 additions and 220 deletions

View File

@@ -229,7 +229,6 @@ impl MintPayment for PaymentProcessorClient {
offer: opts.offer.to_string(),
max_fee_amount: opts.max_fee_amount.map(Into::into),
timeout_secs: opts.timeout_secs,
invoice: opts.invoice,
melt_options: opts.melt_options.map(Into::into),
},
)),

View File

@@ -137,22 +137,11 @@ impl TryFrom<CreatePaymentResponse> for CreateIncomingPaymentResponse {
impl From<cdk_common::payment::PaymentQuoteResponse> for PaymentQuoteResponse {
fn from(value: cdk_common::payment::PaymentQuoteResponse) -> Self {
Self {
request_identifier: Some(value.request_lookup_id.into()),
request_identifier: value.request_lookup_id.map(|i| i.into()),
amount: value.amount.into(),
fee: value.fee.into(),
unit: value.unit.to_string(),
state: QuoteState::from(value.state).into(),
melt_options: value.options.map(|opt| match opt {
cdk_common::payment::PaymentQuoteOptions::Bolt12 { invoice } => {
PaymentQuoteOptions {
melt_options: Some(payment_quote_options::MeltOptions::Bolt12(
Bolt12Options {
invoice: invoice.map(String::from_utf8).and_then(|r| r.ok()),
},
)),
}
}
}),
}
}
}
@@ -160,26 +149,15 @@ impl From<cdk_common::payment::PaymentQuoteResponse> for PaymentQuoteResponse {
impl From<PaymentQuoteResponse> for cdk_common::payment::PaymentQuoteResponse {
fn from(value: PaymentQuoteResponse) -> Self {
let state_val = value.state();
let request_identifier = value
.request_identifier
.expect("request identifier required");
let request_identifier = value.request_identifier;
Self {
request_lookup_id: request_identifier
.try_into()
.expect("valid request identifier"),
.map(|i| i.try_into().expect("valid request identifier")),
amount: value.amount.into(),
fee: value.fee.into(),
unit: CurrencyUnit::from_str(&value.unit).unwrap_or_default(),
state: state_val.into(),
options: value.melt_options.map(|opt| match opt.melt_options {
Some(payment_quote_options::MeltOptions::Bolt12(bolt12)) => {
cdk_common::payment::PaymentQuoteOptions::Bolt12 {
invoice: bolt12.invoice.as_deref().map(str::as_bytes).map(Vec::from),
}
}
None => unreachable!(),
}),
}
}
}

View File

@@ -105,23 +105,13 @@ enum QuoteState {
ISSUED = 5;
}
message Bolt12Options {
optional string invoice = 1;
}
message PaymentQuoteOptions {
oneof melt_options {
Bolt12Options bolt12 = 1;
}
}
message PaymentQuoteResponse {
PaymentIdentifier request_identifier = 1;
uint64 amount = 2;
uint64 fee = 3;
QuoteState state = 4;
optional PaymentQuoteOptions melt_options = 5;
string unit = 6;
string unit = 5;
}
message Bolt11OutgoingPaymentOptions {
@@ -135,7 +125,6 @@ message Bolt12OutgoingPaymentOptions {
string offer = 1;
optional uint64 max_fee_amount = 2;
optional uint64 timeout_secs = 3;
optional bytes invoice = 4;
optional MeltOptions melt_options = 5;
}

View File

@@ -250,7 +250,6 @@ impl CdkPaymentProcessor for PaymentProcessorServer {
offer: Offer::from_str(&request.request).unwrap(),
max_fee_amount: None,
timeout_secs: None,
invoice: None,
melt_options: request.options.map(Into::into),
},
))
@@ -308,7 +307,6 @@ impl CdkPaymentProcessor for PaymentProcessorServer {
offer,
max_fee_amount: opts.max_fee_amount.map(Into::into),
timeout_secs: opts.timeout_secs,
invoice: opts.invoice,
melt_options: opts.melt_options.map(Into::into),
}),
);