Skip to content

Commit d3f9a1e

Browse files
feat: Integrate Bolt11Invoice wrapper
- Update type aliases for Bolt11Invoice based on feature flags - Add convert_invoice and wrap_invoice functions for type conversion - Update payment code to work with the new wrapper type - Modify liquidity and unified_qr modules to handle wrapped invoices - Fix doctest to handle feature flag differences
1 parent 2128a58 commit d3f9a1e

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
//! controlled via commands such as [`start`], [`stop`], [`open_channel`], [`send`], etc.:
2424
//!
2525
//! ```no_run
26+
//! # #[cfg(not(feature = "uniffi"))]
27+
//! # {
2628
//! use ldk_node::Builder;
2729
//! use ldk_node::lightning_invoice::Bolt11Invoice;
2830
//! use ldk_node::lightning::ln::msgs::SocketAddress;
@@ -57,6 +59,7 @@
5759
//!
5860
//! node.stop().unwrap();
5961
//! }
62+
//! # }
6063
//! ```
6164
//!
6265
//! [`build`]: Builder::build

src/liquidity.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,15 +1308,18 @@ type PaymentInfo = lightning_liquidity::lsps1::msgs::PaymentInfo;
13081308
#[derive(Clone, Debug, PartialEq, Eq)]
13091309
pub struct PaymentInfo {
13101310
/// A Lightning payment using BOLT 11.
1311-
pub bolt11: Option<lightning_liquidity::lsps1::msgs::Bolt11PaymentInfo>,
1311+
pub bolt11: Option<crate::uniffi_types::Bolt11PaymentInfo>,
13121312
/// An onchain payment.
13131313
pub onchain: Option<OnchainPaymentInfo>,
13141314
}
13151315

13161316
#[cfg(feature = "uniffi")]
13171317
impl From<lightning_liquidity::lsps1::msgs::PaymentInfo> for PaymentInfo {
13181318
fn from(value: lightning_liquidity::lsps1::msgs::PaymentInfo) -> Self {
1319-
PaymentInfo { bolt11: value.bolt11, onchain: value.onchain.map(|o| o.into()) }
1319+
PaymentInfo {
1320+
bolt11: value.bolt11.map(|b| crate::uniffi_types::Bolt11PaymentInfo::from(b)),
1321+
onchain: value.onchain.map(|o| o.into()),
1322+
}
13201323
}
13211324
}
13221325

src/payment/bolt11.rs

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,37 @@ use lightning::routing::router::{PaymentParameters, RouteParameters};
3030

3131
use lightning_types::payment::{PaymentHash, PaymentPreimage};
3232

33-
use lightning_invoice::Bolt11Invoice;
33+
use lightning_invoice::Bolt11Invoice as LdkBolt11Invoice;
3434
use lightning_invoice::Bolt11InvoiceDescription as LdkBolt11InvoiceDescription;
3535

3636
use bitcoin::hashes::sha256::Hash as Sha256;
3737
use bitcoin::hashes::Hash;
3838

3939
use std::sync::{Arc, RwLock};
4040

41+
#[cfg(not(feature = "uniffi"))]
42+
type Bolt11Invoice = LdkBolt11Invoice;
43+
#[cfg(feature = "uniffi")]
44+
type Bolt11Invoice = Arc<crate::uniffi_types::Bolt11Invoice>;
45+
46+
#[cfg(not(feature = "uniffi"))]
47+
pub fn wrap_invoice(invoice: LdkBolt11Invoice) -> Bolt11Invoice {
48+
invoice
49+
}
50+
#[cfg(feature = "uniffi")]
51+
pub fn wrap_invoice(invoice: LdkBolt11Invoice) -> Bolt11Invoice {
52+
Arc::new(invoice.into())
53+
}
54+
55+
#[cfg(not(feature = "uniffi"))]
56+
pub fn convert_invoice(invoice: &Bolt11Invoice) -> &LdkBolt11Invoice {
57+
invoice
58+
}
59+
#[cfg(feature = "uniffi")]
60+
pub fn convert_invoice(invoice: &Bolt11Invoice) -> &LdkBolt11Invoice {
61+
&invoice.inner
62+
}
63+
4164
#[cfg(not(feature = "uniffi"))]
4265
type Bolt11InvoiceDescription = LdkBolt11InvoiceDescription;
4366
#[cfg(feature = "uniffi")]
@@ -101,6 +124,7 @@ impl Bolt11Payment {
101124
pub fn send(
102125
&self, invoice: &Bolt11Invoice, sending_parameters: Option<SendingParameters>,
103126
) -> Result<PaymentId, Error> {
127+
let invoice = convert_invoice(invoice);
104128
let rt_lock = self.runtime.read().unwrap();
105129
if rt_lock.is_none() {
106130
return Err(Error::NotRunning);
@@ -209,6 +233,7 @@ impl Bolt11Payment {
209233
&self, invoice: &Bolt11Invoice, amount_msat: u64,
210234
sending_parameters: Option<SendingParameters>,
211235
) -> Result<PaymentId, Error> {
236+
let invoice = convert_invoice(invoice);
212237
let rt_lock = self.runtime.read().unwrap();
213238
if rt_lock.is_none() {
214239
return Err(Error::NotRunning);
@@ -429,7 +454,8 @@ impl Bolt11Payment {
429454
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
430455
) -> Result<Bolt11Invoice, Error> {
431456
let description = maybe_convert_description!(description);
432-
self.receive_inner(Some(amount_msat), description, expiry_secs, None)
457+
let invoice = self.receive_inner(Some(amount_msat), description, expiry_secs, None)?;
458+
Ok(wrap_invoice(invoice))
433459
}
434460

435461
/// Returns a payable invoice that can be used to request a payment of the amount
@@ -451,7 +477,9 @@ impl Bolt11Payment {
451477
payment_hash: PaymentHash,
452478
) -> Result<Bolt11Invoice, Error> {
453479
let description = maybe_convert_description!(description);
454-
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))
480+
let invoice =
481+
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))?;
482+
Ok(wrap_invoice(invoice))
455483
}
456484

457485
/// Returns a payable invoice that can be used to request and receive a payment for which the
@@ -462,7 +490,8 @@ impl Bolt11Payment {
462490
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
463491
) -> Result<Bolt11Invoice, Error> {
464492
let description = maybe_convert_description!(description);
465-
self.receive_inner(None, description, expiry_secs, None)
493+
let invoice = self.receive_inner(None, description, expiry_secs, None)?;
494+
Ok(wrap_invoice(invoice))
466495
}
467496

468497
/// Returns a payable invoice that can be used to request a payment for the given payment hash
@@ -483,13 +512,14 @@ impl Bolt11Payment {
483512
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32, payment_hash: PaymentHash,
484513
) -> Result<Bolt11Invoice, Error> {
485514
let description = maybe_convert_description!(description);
486-
self.receive_inner(None, description, expiry_secs, Some(payment_hash))
515+
let invoice = self.receive_inner(None, description, expiry_secs, Some(payment_hash))?;
516+
Ok(wrap_invoice(invoice))
487517
}
488518

489519
pub(crate) fn receive_inner(
490520
&self, amount_msat: Option<u64>, invoice_description: &LdkBolt11InvoiceDescription,
491521
expiry_secs: u32, manual_claim_payment_hash: Option<PaymentHash>,
492-
) -> Result<Bolt11Invoice, Error> {
522+
) -> Result<LdkBolt11Invoice, Error> {
493523
let invoice = {
494524
let invoice_params = Bolt11InvoiceParameters {
495525
amount_msats: amount_msat,
@@ -559,13 +589,14 @@ impl Bolt11Payment {
559589
max_total_lsp_fee_limit_msat: Option<u64>,
560590
) -> Result<Bolt11Invoice, Error> {
561591
let description = maybe_convert_description!(description);
562-
self.receive_via_jit_channel_inner(
592+
let invoice = self.receive_via_jit_channel_inner(
563593
Some(amount_msat),
564594
description,
565595
expiry_secs,
566596
max_total_lsp_fee_limit_msat,
567597
None,
568-
)
598+
)?;
599+
Ok(wrap_invoice(invoice))
569600
}
570601

571602
/// Returns a payable invoice that can be used to request a variable amount payment (also known
@@ -584,20 +615,21 @@ impl Bolt11Payment {
584615
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
585616
) -> Result<Bolt11Invoice, Error> {
586617
let description = maybe_convert_description!(description);
587-
self.receive_via_jit_channel_inner(
618+
let invoice = self.receive_via_jit_channel_inner(
588619
None,
589620
description,
590621
expiry_secs,
591622
None,
592623
max_proportional_lsp_fee_limit_ppm_msat,
593-
)
624+
)?;
625+
Ok(wrap_invoice(invoice))
594626
}
595627

596628
fn receive_via_jit_channel_inner(
597629
&self, amount_msat: Option<u64>, description: &LdkBolt11InvoiceDescription,
598630
expiry_secs: u32, max_total_lsp_fee_limit_msat: Option<u64>,
599631
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
600-
) -> Result<Bolt11Invoice, Error> {
632+
) -> Result<LdkBolt11Invoice, Error> {
601633
let liquidity_source =
602634
self.liquidity_source.as_ref().ok_or(Error::LiquiditySourceUnavailable)?;
603635

@@ -697,6 +729,7 @@ impl Bolt11Payment {
697729
/// amount times [`Config::probing_liquidity_limit_multiplier`] won't be used to send
698730
/// pre-flight probes.
699731
pub fn send_probes(&self, invoice: &Bolt11Invoice) -> Result<(), Error> {
732+
let invoice = convert_invoice(invoice);
700733
let rt_lock = self.runtime.read().unwrap();
701734
if rt_lock.is_none() {
702735
return Err(Error::NotRunning);
@@ -729,6 +762,7 @@ impl Bolt11Payment {
729762
pub fn send_probes_using_amount(
730763
&self, invoice: &Bolt11Invoice, amount_msat: u64,
731764
) -> Result<(), Error> {
765+
let invoice = convert_invoice(invoice);
732766
let rt_lock = self.runtime.read().unwrap();
733767
if rt_lock.is_none() {
734768
return Err(Error::NotRunning);

src/payment/unified_qr.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! [BOLT 12]: https://github.com/lightning/bolts/blob/master/12-offer-encoding.md
1414
use crate::error::Error;
1515
use crate::logger::{log_error, LdkLogger, Logger};
16-
use crate::payment::{Bolt11Payment, Bolt12Payment, OnchainPayment};
16+
use crate::payment::{bolt11::wrap_invoice, Bolt11Payment, Bolt12Payment, OnchainPayment};
1717
use crate::Config;
1818

1919
use lightning::ln::channelmanager::PaymentId;
@@ -149,6 +149,7 @@ impl UnifiedQrPayment {
149149
}
150150

151151
if let Some(invoice) = uri_network_checked.extras.bolt11_invoice {
152+
let invoice = wrap_invoice(invoice);
152153
match self.bolt11_invoice.send(&invoice, None) {
153154
Ok(payment_id) => return Ok(QrPaymentResult::Bolt11 { payment_id }),
154155
Err(e) => log_error!(self.logger, "Failed to send BOLT11 invoice: {:?}. This is part of a unified QR code payment. Falling back to the on-chain transaction.", e),

0 commit comments

Comments
 (0)