Skip to content

Commit 3b965dc

Browse files
committed
custom_records on PaymentReceived and PaymentClaimable variant of Event, TlvEntries custom udl type
1 parent e46e00d commit 3b965dc

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
PaymentForwarded(ChannelId prev_channel_id, ChannelId next_channel_id, UserChannelId? prev_user_channel_id, UserChannelId? next_user_channel_id, u64? total_fee_earned_msat, u64? skimmed_fee_msat, boolean claim_from_onchain_tx, u64? outbound_amount_forwarded_msat);
283283
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
284284
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
@@ -621,3 +621,6 @@ typedef string UntrustedString;
621621

622622
[Custom]
623623
typedef string NodeAlias;
624+
625+
[Custom]
626+
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 has been forwarded.
107109
PaymentForwarded {
@@ -168,6 +170,8 @@ pub enum Event {
168170
/// The block height at which this payment will be failed back and will no longer be
169171
/// eligible for claiming.
170172
claim_deadline: Option<u32>,
173+
/// Custom TLV records attached to the payment
174+
custom_records: Option<TlvEntries>,
171175
},
172176
/// A channel has been created and is pending confirmation on-chain.
173177
ChannelPending {
@@ -224,6 +228,7 @@ impl_writeable_tlv_based_enum!(Event,
224228
(0, payment_hash, required),
225229
(1, payment_id, option),
226230
(2, amount_msat, required),
231+
(3, custom_records, option),
227232
},
228233
(3, ChannelReady) => {
229234
(0, channel_id, required),
@@ -248,6 +253,7 @@ impl_writeable_tlv_based_enum!(Event,
248253
(2, payment_id, required),
249254
(4, claimable_amount_msat, required),
250255
(6, claim_deadline, option),
256+
(8, custom_records, option),
251257
},
252258
(7, PaymentForwarded) => {
253259
(0, prev_channel_id, required),
@@ -542,7 +548,7 @@ where
542548
via_channel_id: _,
543549
via_user_channel_id: _,
544550
claim_deadline,
545-
onion_fields: _,
551+
onion_fields,
546552
counterparty_skimmed_fee_msat,
547553
} => {
548554
let payment_id = PaymentId(payment_hash.0);
@@ -649,6 +655,14 @@ where
649655
payment_hash,
650656
claimable_amount_msat: amount_msat,
651657
claim_deadline,
658+
custom_records: onion_fields.map(|cf| {
659+
TlvEntries(
660+
cf.custom_tlvs()
661+
.into_iter()
662+
.map(|tlv| tlv.into())
663+
.collect(),
664+
)
665+
}),
652666
};
653667
match self.event_queue.add_event(event) {
654668
Ok(_) => return Ok(()),
@@ -799,7 +813,7 @@ where
799813
receiver_node_id: _,
800814
htlcs: _,
801815
sender_intended_total_msat: _,
802-
onion_fields: _,
816+
onion_fields,
803817
} => {
804818
let payment_id = PaymentId(payment_hash.0);
805819
log_info!(
@@ -875,6 +889,9 @@ where
875889
payment_id: Some(payment_id),
876890
payment_hash,
877891
amount_msat,
892+
custom_records: onion_fields.map(|cf| {
893+
TlvEntries(cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect())
894+
}),
878895
};
879896
match self.event_queue.add_event(event) {
880897
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)