Skip to content

Commit e53e98b

Browse files
committed
vec of custom tlvs, better error logging and msg
1 parent 295e05a commit e53e98b

File tree

4 files changed

+36
-23
lines changed

4 files changed

+36
-23
lines changed

bindings/ldk_node.udl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ interface SpontaneousPayment {
152152
[Throws=NodeError]
153153
PaymentId send(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters);
154154
[Throws=NodeError]
155-
PaymentId send_with_custom_tlv(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, u64 custom_tlv_key, sequence<u8> custom_tlv_value);
155+
PaymentId send_with_custom_tlvs(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, sequence<TlvEntry> custom_tlvs);
156156
[Throws=NodeError]
157157
void send_probes(u64 amount_msat, PublicKey node_id);
158158
};
@@ -363,6 +363,11 @@ dictionary SendingParameters {
363363
u8? max_channel_saturation_power_of_half;
364364
};
365365

366+
dictionary TlvEntry {
367+
u64 type;
368+
sequence<u8> value;
369+
};
370+
366371
[Enum]
367372
interface MaxTotalRoutingFeeLimit {
368373
None ();

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub enum Error {
3434
RefundCreationFailed,
3535
/// Sending a payment has failed.
3636
PaymentSendingFailed,
37+
/// Construction of spontaneous payment with custom TLVs failed.
38+
InvalidCustomTlvs,
3739
/// Sending a payment probe has failed.
3840
ProbeSendingFailed,
3941
/// A channel could not be opened.
@@ -130,6 +132,7 @@ impl fmt::Display for Error {
130132
Self::OfferCreationFailed => write!(f, "Failed to create offer."),
131133
Self::RefundCreationFailed => write!(f, "Failed to create refund."),
132134
Self::PaymentSendingFailed => write!(f, "Failed to send the given payment."),
135+
Self::InvalidCustomTlvs => write!(f, "Failed to construct payment with custom TLVs."),
133136
Self::ProbeSendingFailed => write!(f, "Failed to send the given payment probe."),
134137
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
135138
Self::ChannelClosingFailed => write!(f, "Failed to close channel."),

src/payment/spontaneous.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::payment::store::{
1414
PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, PaymentStore,
1515
};
1616
use crate::payment::SendingParameters;
17-
use crate::types::{ChannelManager, KeysManager};
17+
use crate::types::{ChannelManager, KeysManager, TlvEntry};
1818

1919
use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry, RetryableSendFailure};
2020
use lightning::ln::{PaymentHash, PaymentPreimage};
@@ -63,22 +63,16 @@ impl SpontaneousPayment {
6363
}
6464

6565
/// Send a spontaneous payment including a custom TLV key and value.
66-
pub fn send_with_custom_tlv(
66+
pub fn send_with_custom_tlvs(
6767
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
68-
custom_tlv_type: u64, custom_tlv_value: Vec<u8>,
68+
custom_tlvs: Vec<TlvEntry>,
6969
) -> Result<PaymentId, Error> {
70-
self.send_inner(
71-
amount_msat,
72-
node_id,
73-
sending_parameters,
74-
Some((custom_tlv_type, custom_tlv_value)),
75-
)
70+
self.send_inner(amount_msat, node_id, sending_parameters, Some(custom_tlvs))
7671
}
7772

78-
/// Send a spontaneous aka. "keysend", payment.
7973
fn send_inner(
8074
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
81-
custom_tlv: Option<(u64, Vec<u8>)>,
75+
custom_tlvs: Option<Vec<TlvEntry>>,
8276
) -> Result<PaymentId, Error> {
8377
let rt_lock = self.runtime.read().unwrap();
8478
if rt_lock.is_none() {
@@ -118,17 +112,13 @@ impl SpontaneousPayment {
118112
.map(|s| route_params.payment_params.max_channel_saturation_power_of_half = s);
119113
};
120114

121-
let recipient_fields = match custom_tlv {
122-
Some((tlv_type, tlv_value)) => {
123-
if tlv_value.len() > 1300 {
124-
// The real limit is lower, but we can't know without the full route.
125-
// This is just an early check to avoid trying a payment that will fail.
126-
return Err(Error::PaymentSendingFailed);
127-
}
128-
RecipientOnionFields::spontaneous_empty()
129-
.with_custom_tlvs(vec![(tlv_type, tlv_value)])
130-
.map_err(|_| Error::PaymentSendingFailed)?
131-
},
115+
let recipient_fields = match custom_tlvs {
116+
Some(tlvs) => RecipientOnionFields::spontaneous_empty()
117+
.with_custom_tlvs(tlvs.into_iter().map(|tlv| (tlv.r#type, tlv.value)).collect())
118+
.map_err(|e| {
119+
log_error!(self.logger, "Failed to send payment with custom TLVs: {:?}", e);
120+
Error::InvalidCustomTlvs
121+
})?,
132122
None => RecipientOnionFields::spontaneous_empty(),
133123
};
134124

src/types.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::logger::FilesystemLogger;
1212
use crate::message_handler::NodeCustomMessageHandler;
1313

1414
use lightning::chain::chainmonitor;
15+
use lightning::impl_writeable_tlv_based;
1516
use lightning::ln::channel_state::ChannelDetails as LdkChannelDetails;
1617
use lightning::ln::msgs::RoutingMessageHandler;
1718
use lightning::ln::msgs::SocketAddress;
@@ -348,3 +349,17 @@ pub struct PeerDetails {
348349
/// Indicates whether we currently have an active connection with the peer.
349350
pub is_connected: bool,
350351
}
352+
353+
/// Custom TLV entry.
354+
#[derive(Debug, Clone, PartialEq, Eq)]
355+
pub struct TlvEntry {
356+
/// Type number.
357+
pub r#type: u64,
358+
/// Serialized value.
359+
pub value: Vec<u8>,
360+
}
361+
362+
impl_writeable_tlv_based!(TlvEntry, {
363+
(0, r#type, required),
364+
(1, value, required),
365+
});

0 commit comments

Comments
 (0)