Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 65 additions & 9 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,71 @@ interface Bolt11Invoice {
PublicKey recover_payee_pub_key();
};

[Enum]
interface OfferAmount {
Bitcoin(u64 amount_msats);
Currency(string iso4217_code, u64 amount);
};

[Traits=(Debug, Display, Eq)]
interface Offer {
[Throws=NodeError, Name=from_str]
constructor([ByRef] string offer_str);
OfferId id();
boolean is_expired();
string? description();
string? issuer();
OfferAmount? amount();
boolean is_valid_quantity(u64 quantity);
boolean expects_quantity();
boolean supports_chain(Network chain);
sequence<Network> chains();
sequence<u8>? metadata();
u64? absolute_expiry_seconds();
PublicKey? issuer_signing_pubkey();
};

[Traits=(Debug, Display, Eq)]
interface Refund {
[Throws=NodeError, Name=from_str]
constructor([ByRef] string refund_str);
string description();
u64? absolute_expiry_seconds();
boolean is_expired();
string? issuer();
sequence<u8> payer_metadata();
Network? chain();
u64 amount_msats();
u64? quantity();
PublicKey payer_signing_pubkey();
string? payer_note();
};

interface Bolt12Invoice {
[Throws=NodeError, Name=from_str]
constructor([ByRef] string invoice_str);
PaymentHash payment_hash();
u64 amount_msats();
OfferAmount? amount();
PublicKey signing_pubkey();
u64 created_at();
u64? absolute_expiry_seconds();
u64 relative_expiry();
boolean is_expired();
string? description();
string? issuer();
string? payer_note();
sequence<u8>? metadata();
u64? quantity();
sequence<u8> signable_hash();
PublicKey payer_signing_pubkey();
PublicKey? issuer_signing_pubkey();
sequence<u8> chain();
sequence<sequence<u8>>? offer_chains();
sequence<Address> fallback_addresses();
sequence<u8> encode();
};

[Custom]
typedef string Txid;

Expand All @@ -754,15 +819,6 @@ typedef string NodeId;
[Custom]
typedef string Address;

[Custom]
typedef string Offer;

[Custom]
typedef string Refund;

[Custom]
typedef string Bolt12Invoice;

[Custom]
typedef string OfferId;

Expand Down
47 changes: 47 additions & 0 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in

#[cfg(feature = "uniffi")]
mod types;

#[cfg(feature = "uniffi")]
pub use types::*;

#[cfg(feature = "uniffi")]
pub fn maybe_deref<T, R>(wrapped_type: &std::sync::Arc<T>) -> &R
where
T: AsRef<R>,
{
wrapped_type.as_ref().as_ref()
}

#[cfg(feature = "uniffi")]
pub fn maybe_try_convert_enum<T, R>(wrapped_type: &T) -> Result<R, crate::error::Error>
where
for<'a> R: TryFrom<&'a T, Error = crate::error::Error>,
{
R::try_from(wrapped_type)
}

#[cfg(feature = "uniffi")]
pub fn maybe_wrap<T>(ldk_type: impl Into<T>) -> std::sync::Arc<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is ldk_type impl Into<T> and not just T?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question! T is not actually the type that is passed to maybe_wrap. impl Into<T> means that the argument we pass to maybe_wrap can be any type that implements Into<T> to convert itself into T. In our case T would be the UniFFI type that is needed at the call site.

For example, we pass LDK native types (like lightning_invoice::Bolt11Invoice), which gets converted to their corresponding UniFFI wrapper type (like ffi::types::Bolt11Invoice) using .into() before being wrapped in an Arc.

std::sync::Arc::new(ldk_type.into())
}

#[cfg(not(feature = "uniffi"))]
pub fn maybe_deref<T>(value: &T) -> &T {
value
}

#[cfg(not(feature = "uniffi"))]
pub fn maybe_try_convert_enum<T>(value: &T) -> Result<&T, crate::error::Error> {
Ok(value)
}

#[cfg(not(feature = "uniffi"))]
pub fn maybe_wrap<T>(value: T) -> T {
value
}
Loading
Loading