Skip to content

Commit fd89919

Browse files
Add Refund wrapper for FFI bindings
Implement Refund struct in ffi/types.rs to provide a wrapper around LDK's Refund 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 9cf9b48 commit fd89919

File tree

4 files changed

+345
-15
lines changed

4 files changed

+345
-15
lines changed

bindings/ldk_node.udl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,21 @@ interface Offer {
759759
PublicKey? issuer_signing_pubkey();
760760
};
761761

762+
interface Refund {
763+
[Throws=NodeError, Name=from_str]
764+
constructor([ByRef] string refund_str);
765+
string description();
766+
u64? absolute_expiry_seconds();
767+
boolean is_expired();
768+
string? issuer();
769+
sequence<u8> payer_metadata();
770+
Network? chain();
771+
u64 amount_msats();
772+
u64? quantity();
773+
PublicKey payer_signing_pubkey();
774+
string? payer_note();
775+
};
776+
762777
[Custom]
763778
typedef string Txid;
764779

@@ -777,9 +792,6 @@ typedef string NodeId;
777792
[Custom]
778793
typedef string Address;
779794

780-
[Custom]
781-
typedef string Refund;
782-
783795
[Custom]
784796
typedef string Bolt12Invoice;
785797

src/ffi/conversions.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// This file is Copyright its original authors, visible in version control history.
2+
//
3+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5+
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6+
// accordance with one or both of these licenses.
7+
8+
mod uniffi_impl {
9+
use std::sync::Arc;
10+
11+
use lightning::offers::{offer::Offer as LdkOffer, refund::Refund as LdkRefund};
12+
use lightning_invoice::{
13+
Bolt11Invoice as LdkBolt11Invoice, Bolt11InvoiceDescription as LdkBolt11InvoiceDescription,
14+
};
15+
16+
use crate::error::Error;
17+
use crate::ffi::{Bolt11Invoice, Bolt11InvoiceDescription, Offer, Refund};
18+
19+
pub trait UniffiType {
20+
type LdkType;
21+
22+
fn from_ldk(ldk_value: Self::LdkType) -> Self;
23+
}
24+
25+
pub trait UniffiConversionType: UniffiType {
26+
fn as_ldk(&self) -> Self::LdkType;
27+
}
28+
29+
pub trait UniffiFallibleConversionType: UniffiType {
30+
fn try_as_ldk(&self) -> Result<Self::LdkType, Error>;
31+
}
32+
33+
impl UniffiType for Bolt11InvoiceDescription {
34+
type LdkType = LdkBolt11InvoiceDescription;
35+
36+
fn from_ldk(ldk_value: Self::LdkType) -> Self {
37+
Bolt11InvoiceDescription::from(ldk_value)
38+
}
39+
}
40+
41+
impl UniffiFallibleConversionType for Bolt11InvoiceDescription {
42+
fn try_as_ldk(&self) -> Result<Self::LdkType, Error> {
43+
LdkBolt11InvoiceDescription::try_from(self)
44+
}
45+
}
46+
47+
impl UniffiType for Arc<Bolt11Invoice> {
48+
type LdkType = LdkBolt11Invoice;
49+
50+
fn from_ldk(ldk_value: Self::LdkType) -> Self {
51+
Arc::new(Bolt11Invoice { inner: ldk_value })
52+
}
53+
}
54+
55+
impl UniffiConversionType for Arc<Bolt11Invoice> {
56+
fn as_ldk(&self) -> Self::LdkType {
57+
self.inner.clone()
58+
}
59+
}
60+
61+
impl UniffiType for Arc<Offer> {
62+
type LdkType = LdkOffer;
63+
64+
fn from_ldk(ldk_value: Self::LdkType) -> Self {
65+
Arc::new(Offer { inner: ldk_value })
66+
}
67+
}
68+
69+
impl UniffiConversionType for Arc<Offer> {
70+
fn as_ldk(&self) -> Self::LdkType {
71+
self.inner.clone()
72+
}
73+
}
74+
75+
impl UniffiType for Arc<Refund> {
76+
type LdkType = LdkRefund;
77+
78+
fn from_ldk(ldk_value: Self::LdkType) -> Self {
79+
Arc::new(Refund { inner: ldk_value })
80+
}
81+
}
82+
83+
impl UniffiConversionType for Arc<Refund> {
84+
fn as_ldk(&self) -> Self::LdkType {
85+
self.inner.clone()
86+
}
87+
}
88+
89+
pub fn maybe_convert<T: UniffiConversionType>(value: &T) -> T::LdkType {
90+
value.as_ldk()
91+
}
92+
93+
pub fn maybe_try_convert<T: UniffiFallibleConversionType>(
94+
value: &T,
95+
) -> Result<T::LdkType, Error> {
96+
value.try_as_ldk()
97+
}
98+
99+
pub fn maybe_wrap<T: UniffiType>(ldk_value: T::LdkType) -> T {
100+
T::from_ldk(ldk_value)
101+
}
102+
}
103+
104+
pub use uniffi_impl::maybe_convert;
105+
pub use uniffi_impl::maybe_try_convert;
106+
pub use uniffi_impl::maybe_wrap;

0 commit comments

Comments
 (0)