Skip to content

Commit e203658

Browse files
feat: Integrate Bolt11Invoice wrapper
- Update type aliases for Bolt11Invoice based on feature flags - Add maybe_convert_invoice and maybe_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 - Skip doctest execution when uniffi feature is enabled to avoid conflicting Bolt11Invoice type assumptions
1 parent 6b65bdb commit e203658

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

src/lib.rs

+3
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

+5-2
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| b.into()),
1321+
onchain: value.onchain.map(|o| o.into()),
1322+
}
13201323
}
13211324
}
13221325

src/payment/bolt11.rs

+45-11
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 maybe_wrap_invoice(invoice: LdkBolt11Invoice) -> Bolt11Invoice {
48+
invoice
49+
}
50+
#[cfg(feature = "uniffi")]
51+
pub fn maybe_wrap_invoice(invoice: LdkBolt11Invoice) -> Bolt11Invoice {
52+
Arc::new(invoice.into())
53+
}
54+
55+
#[cfg(not(feature = "uniffi"))]
56+
pub fn maybe_convert_invoice(invoice: &Bolt11Invoice) -> &LdkBolt11Invoice {
57+
invoice
58+
}
59+
#[cfg(feature = "uniffi")]
60+
pub fn maybe_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 = maybe_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 = maybe_convert_invoice(invoice);
212237
let rt_lock = self.runtime.read().unwrap();
213238
if rt_lock.is_none() {
214239
return Err(Error::NotRunning);
@@ -441,7 +466,8 @@ impl Bolt11Payment {
441466
&self, amount_msat: u64, description: &Bolt11InvoiceDescription, expiry_secs: u32,
442467
) -> Result<Bolt11Invoice, Error> {
443468
let description = maybe_convert_description!(description);
444-
self.receive_inner(Some(amount_msat), description, expiry_secs, None)
469+
let invoice = self.receive_inner(Some(amount_msat), description, expiry_secs, None)?;
470+
Ok(maybe_wrap_invoice(invoice))
445471
}
446472

447473
/// Returns a payable invoice that can be used to request a payment of the amount
@@ -463,7 +489,9 @@ impl Bolt11Payment {
463489
payment_hash: PaymentHash,
464490
) -> Result<Bolt11Invoice, Error> {
465491
let description = maybe_convert_description!(description);
466-
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))
492+
let invoice =
493+
self.receive_inner(Some(amount_msat), description, expiry_secs, Some(payment_hash))?;
494+
Ok(maybe_wrap_invoice(invoice))
467495
}
468496

469497
/// Returns a payable invoice that can be used to request and receive a payment for which the
@@ -474,7 +502,8 @@ impl Bolt11Payment {
474502
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32,
475503
) -> Result<Bolt11Invoice, Error> {
476504
let description = maybe_convert_description!(description);
477-
self.receive_inner(None, description, expiry_secs, None)
505+
let invoice = self.receive_inner(None, description, expiry_secs, None)?;
506+
Ok(maybe_wrap_invoice(invoice))
478507
}
479508

480509
/// Returns a payable invoice that can be used to request a payment for the given payment hash
@@ -495,13 +524,14 @@ impl Bolt11Payment {
495524
&self, description: &Bolt11InvoiceDescription, expiry_secs: u32, payment_hash: PaymentHash,
496525
) -> Result<Bolt11Invoice, Error> {
497526
let description = maybe_convert_description!(description);
498-
self.receive_inner(None, description, expiry_secs, Some(payment_hash))
527+
let invoice = self.receive_inner(None, description, expiry_secs, Some(payment_hash))?;
528+
Ok(maybe_wrap_invoice(invoice))
499529
}
500530

501531
pub(crate) fn receive_inner(
502532
&self, amount_msat: Option<u64>, invoice_description: &LdkBolt11InvoiceDescription,
503533
expiry_secs: u32, manual_claim_payment_hash: Option<PaymentHash>,
504-
) -> Result<Bolt11Invoice, Error> {
534+
) -> Result<LdkBolt11Invoice, Error> {
505535
let invoice = {
506536
let invoice_params = Bolt11InvoiceParameters {
507537
amount_msats: amount_msat,
@@ -571,13 +601,14 @@ impl Bolt11Payment {
571601
max_total_lsp_fee_limit_msat: Option<u64>,
572602
) -> Result<Bolt11Invoice, Error> {
573603
let description = maybe_convert_description!(description);
574-
self.receive_via_jit_channel_inner(
604+
let invoice = self.receive_via_jit_channel_inner(
575605
Some(amount_msat),
576606
description,
577607
expiry_secs,
578608
max_total_lsp_fee_limit_msat,
579609
None,
580-
)
610+
)?;
611+
Ok(maybe_wrap_invoice(invoice))
581612
}
582613

583614
/// Returns a payable invoice that can be used to request a variable amount payment (also known
@@ -596,20 +627,21 @@ impl Bolt11Payment {
596627
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
597628
) -> Result<Bolt11Invoice, Error> {
598629
let description = maybe_convert_description!(description);
599-
self.receive_via_jit_channel_inner(
630+
let invoice = self.receive_via_jit_channel_inner(
600631
None,
601632
description,
602633
expiry_secs,
603634
None,
604635
max_proportional_lsp_fee_limit_ppm_msat,
605-
)
636+
)?;
637+
Ok(maybe_wrap_invoice(invoice))
606638
}
607639

608640
fn receive_via_jit_channel_inner(
609641
&self, amount_msat: Option<u64>, description: &LdkBolt11InvoiceDescription,
610642
expiry_secs: u32, max_total_lsp_fee_limit_msat: Option<u64>,
611643
max_proportional_lsp_fee_limit_ppm_msat: Option<u64>,
612-
) -> Result<Bolt11Invoice, Error> {
644+
) -> Result<LdkBolt11Invoice, Error> {
613645
let liquidity_source =
614646
self.liquidity_source.as_ref().ok_or(Error::LiquiditySourceUnavailable)?;
615647

@@ -709,6 +741,7 @@ impl Bolt11Payment {
709741
/// amount times [`Config::probing_liquidity_limit_multiplier`] won't be used to send
710742
/// pre-flight probes.
711743
pub fn send_probes(&self, invoice: &Bolt11Invoice) -> Result<(), Error> {
744+
let invoice = maybe_convert_invoice(invoice);
712745
let rt_lock = self.runtime.read().unwrap();
713746
if rt_lock.is_none() {
714747
return Err(Error::NotRunning);
@@ -741,6 +774,7 @@ impl Bolt11Payment {
741774
pub fn send_probes_using_amount(
742775
&self, invoice: &Bolt11Invoice, amount_msat: u64,
743776
) -> Result<(), Error> {
777+
let invoice = maybe_convert_invoice(invoice);
744778
let rt_lock = self.runtime.read().unwrap();
745779
if rt_lock.is_none() {
746780
return Err(Error::NotRunning);

src/payment/unified_qr.rs

+2-1
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::maybe_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 = maybe_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)