Skip to content

Commit 56550d1

Browse files
committed
custom_records on PaymentReceived and PaymentClaimable variant of Event, TlvEntries custom udl type
1 parent e53e98b commit 56550d1

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

bindings/ldk_node.udl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ enum VssHeaderProviderError {
277277
interface Event {
278278
PaymentSuccessful(PaymentId? payment_id, PaymentHash payment_hash, PaymentPreimage? payment_preimage, u64? fee_paid_msat);
279279
PaymentFailed(PaymentId? payment_id, PaymentHash? payment_hash, PaymentFailureReason? reason);
280-
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat);
281-
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline);
280+
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat, TlvEntries? custom_records);
281+
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline, TlvEntries? custom_records);
282282
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
283283
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
284284
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);
@@ -620,3 +620,6 @@ typedef string UntrustedString;
620620

621621
[Custom]
622622
typedef string NodeAlias;
623+
624+
[Custom]
625+
typedef sequence<TlvEntry> TlvEntries;

src/event.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66
// accordance with one or both of these licenses.
77

8-
use crate::types::{DynStore, Sweeper, Wallet};
8+
use crate::types::{DynStore, Sweeper, TlvEntries, Wallet};
99

1010
use crate::{
1111
hex_utils, BumpTransactionEventHandler, ChannelManager, Config, Error, Graph, PeerInfo,
@@ -102,6 +102,8 @@ pub enum Event {
102102
payment_hash: PaymentHash,
103103
/// The value, in thousandths of a satoshi, that has been received.
104104
amount_msat: u64,
105+
/// Custom TLV records received on the payment
106+
custom_records: Option<TlvEntries>,
105107
},
106108
/// A payment for a previously-registered payment hash has been received.
107109
///
@@ -124,6 +126,8 @@ pub enum Event {
124126
/// The block height at which this payment will be failed back and will no longer be
125127
/// eligible for claiming.
126128
claim_deadline: Option<u32>,
129+
/// Custom TLV records attached to the payment
130+
custom_records: Option<TlvEntries>,
127131
},
128132
/// A channel has been created and is pending confirmation on-chain.
129133
ChannelPending {
@@ -180,6 +184,7 @@ impl_writeable_tlv_based_enum!(Event,
180184
(0, payment_hash, required),
181185
(1, payment_id, option),
182186
(2, amount_msat, required),
187+
(3, custom_records, option),
183188
},
184189
(3, ChannelReady) => {
185190
(0, channel_id, required),
@@ -204,6 +209,7 @@ impl_writeable_tlv_based_enum!(Event,
204209
(2, payment_id, required),
205210
(4, claimable_amount_msat, required),
206211
(6, claim_deadline, option),
212+
(8, custom_records, option),
207213
}
208214
);
209215

@@ -488,7 +494,7 @@ where
488494
via_channel_id: _,
489495
via_user_channel_id: _,
490496
claim_deadline,
491-
onion_fields: _,
497+
onion_fields,
492498
counterparty_skimmed_fee_msat,
493499
} => {
494500
let payment_id = PaymentId(payment_hash.0);
@@ -595,6 +601,14 @@ where
595601
payment_hash,
596602
claimable_amount_msat: amount_msat,
597603
claim_deadline,
604+
custom_records: onion_fields.map(|cf| {
605+
TlvEntries(
606+
cf.custom_tlvs()
607+
.into_iter()
608+
.map(|tlv| tlv.into())
609+
.collect(),
610+
)
611+
}),
598612
};
599613
match self.event_queue.add_event(event) {
600614
Ok(_) => return Ok(()),
@@ -745,7 +759,7 @@ where
745759
receiver_node_id: _,
746760
htlcs: _,
747761
sender_intended_total_msat: _,
748-
onion_fields: _,
762+
onion_fields,
749763
} => {
750764
let payment_id = PaymentId(payment_hash.0);
751765
log_info!(
@@ -821,6 +835,9 @@ where
821835
payment_id: Some(payment_id),
822836
payment_hash,
823837
amount_msat,
838+
custom_records: onion_fields.map(|cf| {
839+
TlvEntries(cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect())
840+
}),
824841
};
825842
match self.event_queue.add_event(event) {
826843
Ok(_) => return Ok(()),

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ use types::{
140140
Broadcaster, BumpTransactionEventHandler, ChainMonitor, ChannelManager, DynStore, Graph,
141141
KeysManager, OnionMessenger, PeerManager, Router, Scorer, Sweeper, Wallet,
142142
};
143-
pub use types::{ChannelDetails, PeerDetails, UserChannelId};
143+
pub use types::{ChannelDetails, PeerDetails, TlvEntry, UserChannelId};
144144

145145
use logger::{log_error, log_info, log_trace, FilesystemLogger, Logger};
146146

src/payment/spontaneous.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl SpontaneousPayment {
6262
self.send_inner(amount_msat, node_id, sending_parameters, None)
6363
}
6464

65-
/// Send a spontaneous payment including a custom TLV key and value.
65+
/// Send a spontaneous payment including a list of custom TLVs.
6666
pub fn send_with_custom_tlvs(
6767
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
6868
custom_tlvs: Vec<TlvEntry>,

src/types.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ pub struct PeerDetails {
350350
pub is_connected: bool,
351351
}
352352

353+
/// List of Custom TLV entries.
354+
#[derive(Debug, Clone, PartialEq, Eq)]
355+
pub struct TlvEntries(pub Vec<TlvEntry>);
356+
353357
/// Custom TLV entry.
354358
#[derive(Debug, Clone, PartialEq, Eq)]
355359
pub struct TlvEntry {
@@ -363,3 +367,32 @@ impl_writeable_tlv_based!(TlvEntry, {
363367
(0, r#type, required),
364368
(1, value, required),
365369
});
370+
371+
impl From<&(u64, Vec<u8>)> for TlvEntry {
372+
fn from(tlv: &(u64, Vec<u8>)) -> Self {
373+
TlvEntry { r#type: tlv.0, value: tlv.1.clone() }
374+
}
375+
}
376+
377+
impl Readable for TlvEntries {
378+
fn read<R: lightning::io::Read>(
379+
reader: &mut R,
380+
) -> Result<Self, lightning::ln::msgs::DecodeError> {
381+
let len: u16 = Readable::read(reader)?;
382+
let mut queue = Vec::with_capacity(len as usize);
383+
for _ in 0..len {
384+
queue.push(Readable::read(reader)?);
385+
}
386+
Ok(Self(queue))
387+
}
388+
}
389+
390+
impl Writeable for TlvEntries {
391+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
392+
(self.0.len() as u16).write(writer)?;
393+
for e in self.0.iter() {
394+
e.write(writer)?;
395+
}
396+
Ok(())
397+
}
398+
}

src/uniffi_types.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub use crate::config::{
1616
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
1717
pub use crate::payment::store::{LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus};
1818
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};
19+
pub use crate::types::{TlvEntries, TlvEntry};
1920

2021
pub use lightning::chain::channelmonitor::BalanceSource;
2122
pub use lightning::events::{ClosureReason, PaymentFailureReason};
@@ -343,3 +344,15 @@ impl UniffiCustomTypeConverter for NodeAlias {
343344
obj.to_string()
344345
}
345346
}
347+
348+
impl UniffiCustomTypeConverter for TlvEntries {
349+
type Builtin = Vec<TlvEntry>;
350+
351+
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
352+
Ok(TlvEntries(val))
353+
}
354+
355+
fn from_custom(obj: Self) -> Self::Builtin {
356+
obj.0
357+
}
358+
}

0 commit comments

Comments
 (0)