Skip to content

Commit

Permalink
custom_records on PaymentReceived and PaymentClaimable variant of Eve…
Browse files Browse the repository at this point in the history
…nt, TlvEntries custom udl type
  • Loading branch information
Evanfeenstra committed Nov 30, 2024
1 parent e53e98b commit 56550d1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 7 deletions.
7 changes: 5 additions & 2 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ enum VssHeaderProviderError {
interface Event {
PaymentSuccessful(PaymentId? payment_id, PaymentHash payment_hash, PaymentPreimage? payment_preimage, u64? fee_paid_msat);
PaymentFailed(PaymentId? payment_id, PaymentHash? payment_hash, PaymentFailureReason? reason);
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat);
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline);
PaymentReceived(PaymentId? payment_id, PaymentHash payment_hash, u64 amount_msat, TlvEntries? custom_records);
PaymentClaimable(PaymentId payment_id, PaymentHash payment_hash, u64 claimable_amount_msat, u32? claim_deadline, TlvEntries? custom_records);
ChannelPending(ChannelId channel_id, UserChannelId user_channel_id, ChannelId former_temporary_channel_id, PublicKey counterparty_node_id, OutPoint funding_txo);
ChannelReady(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id);
ChannelClosed(ChannelId channel_id, UserChannelId user_channel_id, PublicKey? counterparty_node_id, ClosureReason? reason);
Expand Down Expand Up @@ -620,3 +620,6 @@ typedef string UntrustedString;

[Custom]
typedef string NodeAlias;

[Custom]
typedef sequence<TlvEntry> TlvEntries;
23 changes: 20 additions & 3 deletions src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.

use crate::types::{DynStore, Sweeper, Wallet};
use crate::types::{DynStore, Sweeper, TlvEntries, Wallet};

use crate::{
hex_utils, BumpTransactionEventHandler, ChannelManager, Config, Error, Graph, PeerInfo,
Expand Down Expand Up @@ -102,6 +102,8 @@ pub enum Event {
payment_hash: PaymentHash,
/// The value, in thousandths of a satoshi, that has been received.
amount_msat: u64,
/// Custom TLV records received on the payment
custom_records: Option<TlvEntries>,
},
/// A payment for a previously-registered payment hash has been received.
///
Expand All @@ -124,6 +126,8 @@ pub enum Event {
/// The block height at which this payment will be failed back and will no longer be
/// eligible for claiming.
claim_deadline: Option<u32>,
/// Custom TLV records attached to the payment
custom_records: Option<TlvEntries>,
},
/// A channel has been created and is pending confirmation on-chain.
ChannelPending {
Expand Down Expand Up @@ -180,6 +184,7 @@ impl_writeable_tlv_based_enum!(Event,
(0, payment_hash, required),
(1, payment_id, option),
(2, amount_msat, required),
(3, custom_records, option),
},
(3, ChannelReady) => {
(0, channel_id, required),
Expand All @@ -204,6 +209,7 @@ impl_writeable_tlv_based_enum!(Event,
(2, payment_id, required),
(4, claimable_amount_msat, required),
(6, claim_deadline, option),
(8, custom_records, option),
}
);

Expand Down Expand Up @@ -488,7 +494,7 @@ where
via_channel_id: _,
via_user_channel_id: _,
claim_deadline,
onion_fields: _,
onion_fields,
counterparty_skimmed_fee_msat,
} => {
let payment_id = PaymentId(payment_hash.0);
Expand Down Expand Up @@ -595,6 +601,14 @@ where
payment_hash,
claimable_amount_msat: amount_msat,
claim_deadline,
custom_records: onion_fields.map(|cf| {
TlvEntries(
cf.custom_tlvs()
.into_iter()
.map(|tlv| tlv.into())
.collect(),
)
}),
};
match self.event_queue.add_event(event) {
Ok(_) => return Ok(()),
Expand Down Expand Up @@ -745,7 +759,7 @@ where
receiver_node_id: _,
htlcs: _,
sender_intended_total_msat: _,
onion_fields: _,
onion_fields,
} => {
let payment_id = PaymentId(payment_hash.0);
log_info!(
Expand Down Expand Up @@ -821,6 +835,9 @@ where
payment_id: Some(payment_id),
payment_hash,
amount_msat,
custom_records: onion_fields.map(|cf| {
TlvEntries(cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect())
}),
};
match self.event_queue.add_event(event) {
Ok(_) => return Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ use types::{
Broadcaster, BumpTransactionEventHandler, ChainMonitor, ChannelManager, DynStore, Graph,
KeysManager, OnionMessenger, PeerManager, Router, Scorer, Sweeper, Wallet,
};
pub use types::{ChannelDetails, PeerDetails, UserChannelId};
pub use types::{ChannelDetails, PeerDetails, TlvEntry, UserChannelId};

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

Expand Down
2 changes: 1 addition & 1 deletion src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl SpontaneousPayment {
self.send_inner(amount_msat, node_id, sending_parameters, None)
}

/// Send a spontaneous payment including a custom TLV key and value.
/// Send a spontaneous payment including a list of custom TLVs.
pub fn send_with_custom_tlvs(
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
custom_tlvs: Vec<TlvEntry>,
Expand Down
33 changes: 33 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ pub struct PeerDetails {
pub is_connected: bool,
}

/// List of Custom TLV entries.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TlvEntries(pub Vec<TlvEntry>);

/// Custom TLV entry.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TlvEntry {
Expand All @@ -363,3 +367,32 @@ impl_writeable_tlv_based!(TlvEntry, {
(0, r#type, required),
(1, value, required),
});

impl From<&(u64, Vec<u8>)> for TlvEntry {
fn from(tlv: &(u64, Vec<u8>)) -> Self {
TlvEntry { r#type: tlv.0, value: tlv.1.clone() }
}
}

impl Readable for TlvEntries {
fn read<R: lightning::io::Read>(
reader: &mut R,
) -> Result<Self, lightning::ln::msgs::DecodeError> {
let len: u16 = Readable::read(reader)?;
let mut queue = Vec::with_capacity(len as usize);
for _ in 0..len {
queue.push(Readable::read(reader)?);
}
Ok(Self(queue))
}
}

impl Writeable for TlvEntries {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), lightning::io::Error> {
(self.0.len() as u16).write(writer)?;
for e in self.0.iter() {
e.write(writer)?;
}
Ok(())
}
}
13 changes: 13 additions & 0 deletions src/uniffi_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use crate::config::{
pub use crate::graph::{ChannelInfo, ChannelUpdateInfo, NodeAnnouncementInfo, NodeInfo};
pub use crate::payment::store::{LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus};
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};
pub use crate::types::{TlvEntries, TlvEntry};

pub use lightning::chain::channelmonitor::BalanceSource;
pub use lightning::events::{ClosureReason, PaymentFailureReason};
Expand Down Expand Up @@ -343,3 +344,15 @@ impl UniffiCustomTypeConverter for NodeAlias {
obj.to_string()
}
}

impl UniffiCustomTypeConverter for TlvEntries {
type Builtin = Vec<TlvEntry>;

fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
Ok(TlvEntries(val))
}

fn from_custom(obj: Self) -> Self::Builtin {
obj.0
}
}

0 comments on commit 56550d1

Please sign in to comment.