Skip to content

Commit 70d9d83

Browse files
committed
Migrate FFI wrapper objects to UniFFI proc-macro export
Migrate `Bolt11Invoice`, `Offer`, `HumanReadableName`, `Refund`, and `Bolt12Invoice` from UDL interface definitions to proc-macro derives (`uniffi::Object`) with `#[uniffi::export]` on impl blocks and `#[uniffi::constructor]` on constructors. For types with `[Traits=(Debug, Display, Eq)]`, use `#[uniffi::export(Debug, Display, Eq)]` on the struct. Replace UDL interface definitions with `typedef interface` references. Co-Authored-By: HAL 9000
1 parent f5e70c1 commit 70d9d83

File tree

2 files changed

+23
-87
lines changed

2 files changed

+23
-87
lines changed

bindings/ldk_node.udl

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -455,95 +455,17 @@ typedef enum AsyncPaymentsRole;
455455

456456
typedef dictionary RouteHintHop;
457457

458-
[Traits=(Debug, Display, Eq)]
459-
interface Bolt11Invoice {
460-
[Throws=NodeError, Name=from_str]
461-
constructor([ByRef] string invoice_str);
462-
sequence<u8> signable_hash();
463-
PaymentHash payment_hash();
464-
PaymentSecret payment_secret();
465-
u64? amount_milli_satoshis();
466-
u64 expiry_time_seconds();
467-
u64 seconds_since_epoch();
468-
u64 seconds_until_expiry();
469-
boolean is_expired();
470-
boolean would_expire(u64 at_time_seconds);
471-
Bolt11InvoiceDescription invoice_description();
472-
u64 min_final_cltv_expiry_delta();
473-
Network network();
474-
Currency currency();
475-
sequence<Address> fallback_addresses();
476-
sequence<sequence<RouteHintHop>> route_hints();
477-
PublicKey recover_payee_pub_key();
478-
};
458+
typedef interface Bolt11Invoice;
479459

480460
typedef enum OfferAmount;
481461

482-
[Traits=(Debug, Display, Eq)]
483-
interface Offer {
484-
[Throws=NodeError, Name=from_str]
485-
constructor([ByRef] string offer_str);
486-
OfferId id();
487-
boolean is_expired();
488-
string? offer_description();
489-
string? issuer();
490-
OfferAmount? amount();
491-
boolean is_valid_quantity(u64 quantity);
492-
boolean expects_quantity();
493-
boolean supports_chain(Network chain);
494-
sequence<Network> chains();
495-
sequence<u8>? metadata();
496-
u64? absolute_expiry_seconds();
497-
PublicKey? issuer_signing_pubkey();
498-
};
462+
typedef interface Offer;
499463

500-
interface HumanReadableName {
501-
[Throws=NodeError, Name=from_encoded]
502-
constructor([ByRef] string encoded);
503-
string user();
504-
string domain();
505-
};
464+
typedef interface HumanReadableName;
506465

507-
[Traits=(Debug, Display, Eq)]
508-
interface Refund {
509-
[Throws=NodeError, Name=from_str]
510-
constructor([ByRef] string refund_str);
511-
string refund_description();
512-
u64? absolute_expiry_seconds();
513-
boolean is_expired();
514-
string? issuer();
515-
sequence<u8> payer_metadata();
516-
Network? chain();
517-
u64 amount_msats();
518-
u64? quantity();
519-
PublicKey payer_signing_pubkey();
520-
string? payer_note();
521-
};
466+
typedef interface Refund;
522467

523-
interface Bolt12Invoice {
524-
[Throws=NodeError, Name=from_str]
525-
constructor([ByRef] string invoice_str);
526-
PaymentHash payment_hash();
527-
u64 amount_msats();
528-
OfferAmount? amount();
529-
PublicKey signing_pubkey();
530-
u64 created_at();
531-
u64? absolute_expiry_seconds();
532-
u64 relative_expiry();
533-
boolean is_expired();
534-
string? invoice_description();
535-
string? issuer();
536-
string? payer_note();
537-
sequence<u8>? metadata();
538-
u64? quantity();
539-
sequence<u8> signable_hash();
540-
PublicKey payer_signing_pubkey();
541-
PublicKey? issuer_signing_pubkey();
542-
sequence<u8> chain();
543-
sequence<sequence<u8>>? offer_chains();
544-
sequence<Address> fallback_addresses();
545-
sequence<u8> encode();
546-
};
468+
typedef interface Bolt12Invoice;
547469

548470
[Custom]
549471
typedef string Txid;

src/ffi/types.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,15 @@ impl From<LdkAmount> for OfferAmount {
233233
/// [`InvoiceRequest`]: lightning::offers::invoice_request::InvoiceRequest
234234
/// [`Bolt12Invoice`]: lightning::offers::invoice::Bolt12Invoice
235235
/// [`Offer`]: lightning::offers::Offer:amount
236-
#[derive(Debug, Clone, PartialEq, Eq)]
236+
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
237+
#[uniffi::export(Debug, Display, Eq)]
237238
pub struct Offer {
238239
pub(crate) inner: LdkOffer,
239240
}
240241

242+
#[uniffi::export]
241243
impl Offer {
244+
#[uniffi::constructor]
242245
pub fn from_str(offer_str: &str) -> Result<Self, Error> {
243246
offer_str.parse()
244247
}
@@ -376,15 +379,18 @@ impl std::fmt::Display for Offer {
376379
/// This struct can also be used for LN-Address recipients.
377380
///
378381
/// [Homograph Attacks]: https://en.wikipedia.org/wiki/IDN_homograph_attack
382+
#[derive(uniffi::Object)]
379383
pub struct HumanReadableName {
380384
pub(crate) inner: LdkHumanReadableName,
381385
}
382386

387+
#[uniffi::export]
383388
impl HumanReadableName {
384389
/// Constructs a new [`HumanReadableName`] from the standard encoding - `user`@`domain`.
385390
///
386391
/// If `user` includes the standard BIP 353 ₿ prefix it is automatically removed as required by
387392
/// BIP 353.
393+
#[uniffi::constructor]
388394
pub fn from_encoded(encoded: &str) -> Result<Self, Error> {
389395
let hrn = match LdkHumanReadableName::from_encoded(encoded) {
390396
Ok(hrn) => Ok(hrn),
@@ -438,12 +444,15 @@ impl AsRef<LdkHumanReadableName> for HumanReadableName {
438444
///
439445
/// [`Bolt12Invoice`]: lightning::offers::invoice::Bolt12Invoice
440446
/// [`Offer`]: lightning::offers::offer::Offer
441-
#[derive(Debug, Clone, PartialEq, Eq)]
447+
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
448+
#[uniffi::export(Debug, Display, Eq)]
442449
pub struct Refund {
443450
pub(crate) inner: LdkRefund,
444451
}
445452

453+
#[uniffi::export]
446454
impl Refund {
455+
#[uniffi::constructor]
447456
pub fn from_str(refund_str: &str) -> Result<Self, Error> {
448457
refund_str.parse()
449458
}
@@ -550,12 +559,14 @@ impl std::fmt::Display for Refund {
550559
}
551560
}
552561

553-
#[derive(Debug, Clone, PartialEq, Eq)]
562+
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
554563
pub struct Bolt12Invoice {
555564
pub(crate) inner: LdkBolt12Invoice,
556565
}
557566

567+
#[uniffi::export]
558568
impl Bolt12Invoice {
569+
#[uniffi::constructor]
559570
pub fn from_str(invoice_str: &str) -> Result<Self, Error> {
560571
invoice_str.parse()
561572
}
@@ -1055,12 +1066,15 @@ impl From<lightning::routing::router::RouteHintHop> for RouteHintHop {
10551066
}
10561067

10571068
/// Represents a syntactically and semantically correct lightning BOLT11 invoice.
1058-
#[derive(Debug, Clone, PartialEq, Eq)]
1069+
#[derive(Debug, Clone, PartialEq, Eq, uniffi::Object)]
1070+
#[uniffi::export(Debug, Display, Eq)]
10591071
pub struct Bolt11Invoice {
10601072
pub(crate) inner: LdkBolt11Invoice,
10611073
}
10621074

1075+
#[uniffi::export]
10631076
impl Bolt11Invoice {
1077+
#[uniffi::constructor]
10641078
pub fn from_str(invoice_str: &str) -> Result<Self, Error> {
10651079
invoice_str.parse()
10661080
}

0 commit comments

Comments
 (0)