Skip to content

Commit 820a829

Browse files
types: re-export LDK's PaidBolt12Invoice directly for non-uniffi
This patch removes the custom PaidBolt12Invoice type and its Writeable/Readable implementations for non-uniffi builds, instead re-exporting LDK's type directly. Problem: We were reimplementing serialization for PaidBolt12Invoice when LDK already provides it via impl_writeable_tlv_based_enum!. This adds unnecessary boilerplate and potential for bugs. Solution: For non-uniffi builds, simply re-export lightning::events::PaidBolt12Invoice. This uses LDK's existing serialization and avoids code duplication. For uniffi builds, we still need our own struct type due to UniFFI limitations with enum variant data containing Objects. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent f740fa8 commit 820a829

File tree

2 files changed

+10
-82
lines changed

2 files changed

+10
-82
lines changed

src/event.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,15 +1078,17 @@ where
10781078
);
10791079
});
10801080

1081-
// Convert LDK's PaidBolt12Invoice to our wrapped type.
1082-
let bolt12_invoice_wrapped = bolt12_invoice.map(PaidBolt12Invoice::from);
1081+
// For UniFFI builds, convert LDK's PaidBolt12Invoice to our wrapped type.
1082+
// For non-UniFFI builds, we use LDK's type directly.
1083+
#[cfg(feature = "uniffi")]
1084+
let bolt12_invoice = bolt12_invoice.map(PaidBolt12Invoice::from);
10831085

10841086
let event = Event::PaymentSuccessful {
10851087
payment_id: Some(payment_id),
10861088
payment_hash,
10871089
payment_preimage: Some(payment_preimage),
10881090
fee_paid_msat,
1089-
bolt12_invoice: bolt12_invoice_wrapped,
1091+
bolt12_invoice,
10901092
};
10911093

10921094
match self.event_queue.add_event(event).await {

src/types.rs

Lines changed: 5 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,11 @@ use bitcoin::secp256k1::PublicKey;
1414
use bitcoin::{OutPoint, ScriptBuf};
1515
use bitcoin_payment_instructions::onion_message_resolver::LDKOnionMessageDNSSECHrnResolver;
1616
use lightning::chain::chainmonitor;
17-
#[cfg(not(feature = "uniffi"))]
18-
use lightning::events::PaidBolt12Invoice as LdkPaidBolt12Invoice;
1917
use lightning::impl_writeable_tlv_based;
2018
use lightning::ln::channel_state::ChannelDetails as LdkChannelDetails;
21-
#[cfg(not(feature = "uniffi"))]
22-
use lightning::ln::msgs::DecodeError;
2319
use lightning::ln::msgs::{RoutingMessageHandler, SocketAddress};
2420
use lightning::ln::peer_handler::IgnoringMessageHandler;
2521
use lightning::ln::types::ChannelId;
26-
#[cfg(not(feature = "uniffi"))]
27-
use lightning::offers::invoice::Bolt12Invoice as LdkBolt12Invoice;
28-
#[cfg(not(feature = "uniffi"))]
29-
use lightning::offers::static_invoice::StaticInvoice as LdkStaticInvoice;
3022
use lightning::routing::gossip;
3123
use lightning::routing::router::DefaultRouter;
3224
use lightning::routing::scoring::{CombinedScorer, ProbabilisticScoringFeeParameters};
@@ -619,76 +611,10 @@ impl From<&(u64, Vec<u8>)> for CustomTlvRecord {
619611
}
620612

621613
// Re-export the invoice types. When UniFFI is enabled, we use the types from ffi::types
622-
// which have additional UniFFI-specific implementations. Otherwise, we use simpler wrappers.
623-
#[cfg(feature = "uniffi")]
624-
pub use crate::ffi::{Bolt12Invoice, PaidBolt12Invoice, StaticInvoice};
625-
614+
// which have additional UniFFI-specific implementations. Otherwise, we re-export LDK's
615+
// types directly to avoid unnecessary wrappers and serialization reimplementation.
626616
#[cfg(not(feature = "uniffi"))]
627-
mod invoice_types {
628-
use super::*;
629-
630-
/// Represents a BOLT12 invoice that was paid.
631-
///
632-
/// This is used in [`Event::PaymentSuccessful`] to provide proof of payment for BOLT12 payments.
633-
///
634-
/// [`Event::PaymentSuccessful`]: crate::Event::PaymentSuccessful
635-
#[derive(Debug, Clone, PartialEq, Eq)]
636-
pub enum PaidBolt12Invoice {
637-
/// A standard BOLT12 invoice.
638-
Bolt12Invoice(Arc<LdkBolt12Invoice>),
639-
/// A static invoice for async payments.
640-
StaticInvoice(Arc<LdkStaticInvoice>),
641-
}
642-
643-
impl From<LdkPaidBolt12Invoice> for PaidBolt12Invoice {
644-
fn from(ldk_invoice: LdkPaidBolt12Invoice) -> Self {
645-
match ldk_invoice {
646-
LdkPaidBolt12Invoice::Bolt12Invoice(invoice) => {
647-
PaidBolt12Invoice::Bolt12Invoice(Arc::new(invoice))
648-
},
649-
LdkPaidBolt12Invoice::StaticInvoice(invoice) => {
650-
PaidBolt12Invoice::StaticInvoice(Arc::new(invoice))
651-
},
652-
}
653-
}
654-
}
655-
656-
impl Writeable for PaidBolt12Invoice {
657-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
658-
match self {
659-
PaidBolt12Invoice::Bolt12Invoice(invoice) => {
660-
0u8.write(writer)?;
661-
invoice.encode().write(writer)?;
662-
},
663-
PaidBolt12Invoice::StaticInvoice(invoice) => {
664-
1u8.write(writer)?;
665-
invoice.encode().write(writer)?;
666-
},
667-
}
668-
Ok(())
669-
}
670-
}
671-
672-
impl Readable for PaidBolt12Invoice {
673-
fn read<R: lightning::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
674-
let tag: u8 = Readable::read(reader)?;
675-
let bytes: Vec<u8> = Readable::read(reader)?;
676-
match tag {
677-
0 => {
678-
let invoice =
679-
LdkBolt12Invoice::try_from(bytes).map_err(|_| DecodeError::InvalidValue)?;
680-
Ok(PaidBolt12Invoice::Bolt12Invoice(Arc::new(invoice)))
681-
},
682-
1 => {
683-
let invoice =
684-
LdkStaticInvoice::try_from(bytes).map_err(|_| DecodeError::InvalidValue)?;
685-
Ok(PaidBolt12Invoice::StaticInvoice(Arc::new(invoice)))
686-
},
687-
_ => Err(DecodeError::InvalidValue),
688-
}
689-
}
690-
}
691-
}
617+
pub use lightning::events::PaidBolt12Invoice;
692618

693-
#[cfg(not(feature = "uniffi"))]
694-
pub use invoice_types::*;
619+
#[cfg(feature = "uniffi")]
620+
pub use crate::ffi::{Bolt12Invoice, PaidBolt12Invoice, StaticInvoice};

0 commit comments

Comments
 (0)