Skip to content

Commit f31d768

Browse files
feat: Add Bolt12Invoice wrapper for FFI bindings
Implement Bolt12Invoice struct in uniffi_types to provide a wrapper around LDK's Bolt12Invoice for cross-language bindings. Modified payment handling in bolt12.rs to: - Support both native and FFI-compatible types via type aliasing - Implement conditional compilation for transparent FFI support - Update payment functions to handle wrapped types Added testing to verify that properties are preserved when wrapping/unwrapping between native and FFI types.
1 parent d7b2344 commit f31d768

File tree

3 files changed

+383
-24
lines changed

3 files changed

+383
-24
lines changed

bindings/ldk_node.udl

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,31 @@ interface Refund {
767767
string? payer_note();
768768
};
769769

770+
interface Bolt12Invoice {
771+
[Throws=NodeError, Name=from_str]
772+
constructor([ByRef] string invoice_str);
773+
PaymentHash payment_hash();
774+
u64 amount_msats();
775+
Amount? amount();
776+
PublicKey signing_pubkey();
777+
u64 created_at();
778+
u64? absolute_expiry_seconds();
779+
u64 relative_expiry();
780+
boolean is_expired();
781+
string? description();
782+
string? issuer();
783+
string? payer_note();
784+
sequence<u8>? metadata();
785+
u64? quantity();
786+
sequence<u8> signable_hash();
787+
PublicKey payer_signing_pubkey();
788+
PublicKey? issuer_signing_pubkey();
789+
sequence<u8> chain();
790+
sequence<sequence<u8>>? offer_chains();
791+
sequence<Address> fallback_addresses();
792+
sequence<u8> encode();
793+
};
794+
770795
[Custom]
771796
typedef string Txid;
772797

@@ -785,9 +810,6 @@ typedef string NodeId;
785810
[Custom]
786811
typedef string Address;
787812

788-
[Custom]
789-
typedef string Bolt12Invoice;
790-
791813
[Custom]
792814
typedef string OfferId;
793815

src/payment/bolt12.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::payment::store::{PaymentDetails, PaymentDirection, PaymentKind, Payme
1616
use crate::types::{ChannelManager, PaymentStore};
1717

1818
use lightning::ln::channelmanager::{PaymentId, Retry};
19-
use lightning::offers::invoice::Bolt12Invoice;
19+
use lightning::offers::invoice::Bolt12Invoice as LdkBolt12Invoice;
2020
use lightning::offers::offer::{Amount, Offer as LdkOffer, Quantity};
2121
use lightning::offers::parse::Bolt12SemanticError;
2222
use lightning::offers::refund::Refund as LdkRefund;
@@ -28,6 +28,20 @@ use std::num::NonZeroU64;
2828
use std::sync::{Arc, RwLock};
2929
use std::time::{Duration, SystemTime, UNIX_EPOCH};
3030

31+
#[cfg(not(feature = "uniffi"))]
32+
type Bolt12Invoice = LdkBolt12Invoice;
33+
#[cfg(feature = "uniffi")]
34+
type Bolt12Invoice = Arc<crate::uniffi_types::Bolt12Invoice>;
35+
36+
#[cfg(not(feature = "uniffi"))]
37+
fn maybe_wrap_bolt12_invoice(invoice: LdkBolt12Invoice) -> Bolt12Invoice {
38+
invoice
39+
}
40+
#[cfg(feature = "uniffi")]
41+
fn maybe_wrap_bolt12_invoice(invoice: LdkBolt12Invoice) -> Bolt12Invoice {
42+
Arc::new(invoice.into())
43+
}
44+
3145
#[cfg(not(feature = "uniffi"))]
3246
type Offer = LdkOffer;
3347
#[cfg(feature = "uniffi")]
@@ -376,6 +390,7 @@ impl Bolt12Payment {
376390
/// retrieve the refund).
377391
///
378392
/// [`Refund`]: lightning::offers::refund::Refund
393+
/// [`Bolt12Invoice`]: lightning::offers::invoice::Bolt12Invoice
379394
pub fn request_refund_payment(&self, refund: &Refund) -> Result<Bolt12Invoice, Error> {
380395
let refund = maybe_convert_refund(refund);
381396
let invoice = self.channel_manager.request_refund_payment(refund).map_err(|e| {
@@ -405,7 +420,7 @@ impl Bolt12Payment {
405420

406421
self.payment_store.insert(payment)?;
407422

408-
Ok(invoice)
423+
Ok(maybe_wrap_bolt12_invoice(invoice))
409424
}
410425

411426
/// Returns a [`Refund`] object that can be used to offer a refund payment of the amount given.

0 commit comments

Comments
 (0)