Skip to content

Commit 8693504

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 62c1ac9 commit 8693504

File tree

3 files changed

+381
-24
lines changed

3 files changed

+381
-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
@@ -18,7 +18,7 @@ use crate::payment::store::{
1818
use crate::types::ChannelManager;
1919

2020
use lightning::ln::channelmanager::{PaymentId, Retry};
21-
use lightning::offers::invoice::Bolt12Invoice;
21+
use lightning::offers::invoice::Bolt12Invoice as LdkBolt12Invoice;
2222
use lightning::offers::offer::{Amount, Offer as LdkOffer, Quantity};
2323
use lightning::offers::parse::Bolt12SemanticError;
2424
use lightning::offers::refund::Refund as LdkRefund;
@@ -30,6 +30,20 @@ use std::num::NonZeroU64;
3030
use std::sync::{Arc, RwLock};
3131
use std::time::{Duration, SystemTime, UNIX_EPOCH};
3232

33+
#[cfg(not(feature = "uniffi"))]
34+
type Bolt12Invoice = LdkBolt12Invoice;
35+
#[cfg(feature = "uniffi")]
36+
type Bolt12Invoice = Arc<crate::uniffi_types::Bolt12Invoice>;
37+
38+
#[cfg(not(feature = "uniffi"))]
39+
fn maybe_wrap_bolt12_invoice(invoice: LdkBolt12Invoice) -> Bolt12Invoice {
40+
invoice
41+
}
42+
#[cfg(feature = "uniffi")]
43+
fn maybe_wrap_bolt12_invoice(invoice: LdkBolt12Invoice) -> Bolt12Invoice {
44+
Arc::new(invoice.into())
45+
}
46+
3347
#[cfg(not(feature = "uniffi"))]
3448
type Offer = LdkOffer;
3549
#[cfg(feature = "uniffi")]
@@ -378,6 +392,7 @@ impl Bolt12Payment {
378392
/// retrieve the refund).
379393
///
380394
/// [`Refund`]: lightning::offers::refund::Refund
395+
/// [`Bolt12Invoice`]: lightning::offers::invoice::Bolt12Invoice
381396
pub fn request_refund_payment(&self, refund: &Refund) -> Result<Bolt12Invoice, Error> {
382397
let refund = maybe_convert_refund(refund);
383398
let invoice = self.channel_manager.request_refund_payment(refund).map_err(|e| {
@@ -407,7 +422,7 @@ impl Bolt12Payment {
407422

408423
self.payment_store.insert(payment)?;
409424

410-
Ok(invoice)
425+
Ok(maybe_wrap_bolt12_invoice(invoice))
411426
}
412427

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

0 commit comments

Comments
 (0)