Skip to content

Commit a2116cc

Browse files
committed
refactor: move preimage from method parameter to SendingParameters
1 parent 89ee011 commit a2116cc

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ interface SpontaneousPayment {
213213
PaymentId send_with_custom_tlvs(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, sequence<CustomTlvRecord> custom_tlvs);
214214
[Throws=NodeError]
215215
void send_probes(u64 amount_msat, PublicKey node_id);
216-
[Throws=NodeError]
217-
PaymentId send_with_preimage(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, PaymentPreimage preimage);
218-
[Throws=NodeError]
219-
PaymentId send_with_preimage_and_custom_tlvs(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, sequence<CustomTlvRecord> custom_tlvs, PaymentPreimage preimage);
220216
};
221217

222218
interface OnchainPayment {
@@ -455,6 +451,7 @@ dictionary SendingParameters {
455451
u32? max_total_cltv_expiry_delta;
456452
u8? max_path_count;
457453
u8? max_channel_saturation_power_of_half;
454+
PaymentPreimage? preimage;
458455
};
459456

460457
dictionary CustomTlvRecord {

src/payment/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ mod unified_qr;
1616

1717
pub use bolt11::Bolt11Payment;
1818
pub use bolt12::Bolt12Payment;
19+
pub use lightning_types::payment::PaymentPreimage;
1920
pub use onchain::OnchainPayment;
2021
pub use spontaneous::SpontaneousPayment;
2122
pub use store::{
@@ -76,6 +77,8 @@ pub struct SendingParameters {
7677
///
7778
/// Default value: 2
7879
pub max_channel_saturation_power_of_half: Option<u8>,
80+
/// Custom preimage to use for the payment.
81+
pub preimage: Option<PaymentPreimage>,
7982
}
8083

8184
/// Represents the possible states of [`SendingParameters::max_total_routing_fee_msat`].

src/payment/spontaneous.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,29 @@ impl SpontaneousPayment {
5757
pub fn send(
5858
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
5959
) -> Result<PaymentId, Error> {
60-
self.send_inner(amount_msat, node_id, sending_parameters, None, None)
60+
self.send_inner(amount_msat, node_id, sending_parameters, None)
6161
}
6262

6363
/// Send a spontaneous payment including a list of custom TLVs.
6464
pub fn send_with_custom_tlvs(
6565
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
6666
custom_tlvs: Vec<CustomTlvRecord>,
6767
) -> Result<PaymentId, Error> {
68-
self.send_inner(amount_msat, node_id, sending_parameters, Some(custom_tlvs), None)
69-
}
70-
71-
/// Send a spontaneous with custom preimage
72-
pub fn send_with_preimage(
73-
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
74-
preimage: PaymentPreimage,
75-
) -> Result<PaymentId, Error> {
76-
self.send_inner(amount_msat, node_id, sending_parameters, None, Some(preimage))
77-
}
78-
79-
/// Send a spontaneous payment with custom preimage including a list of custom TLVs.
80-
pub fn send_with_preimage_and_custom_tlvs(
81-
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
82-
custom_tlvs: Vec<CustomTlvRecord>, preimage: PaymentPreimage,
83-
) -> Result<PaymentId, Error> {
84-
self.send_inner(amount_msat, node_id, sending_parameters, Some(custom_tlvs), Some(preimage))
68+
self.send_inner(amount_msat, node_id, sending_parameters, Some(custom_tlvs))
8569
}
8670

8771
fn send_inner(
8872
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
89-
custom_tlvs: Option<Vec<CustomTlvRecord>>, preimage: Option<PaymentPreimage>,
73+
custom_tlvs: Option<Vec<CustomTlvRecord>>,
9074
) -> Result<PaymentId, Error> {
9175
let rt_lock = self.runtime.read().unwrap();
9276
if rt_lock.is_none() {
9377
return Err(Error::NotRunning);
9478
}
9579

96-
let payment_preimage = preimage
80+
let payment_preimage = sending_parameters
81+
.as_ref()
82+
.and_then(|sp| sp.preimage)
9783
.unwrap_or_else(|| PaymentPreimage(self.keys_manager.get_secure_random_bytes()));
9884
let payment_hash = PaymentHash::from(payment_preimage);
9985
let payment_id = PaymentId(payment_hash.0);

tests/integration_tests_rust.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ use lightning::routing::gossip::{NodeAlias, NodeId};
2929
use lightning::util::persist::KVStore;
3030

3131
use lightning_invoice::{Bolt11InvoiceDescription, Description};
32-
33-
use ldk_node::CustomTlvRecord;
3432
use lightning_types::payment::PaymentPreimage;
3533

3634
use bitcoin::address::NetworkUnchecked;
@@ -209,6 +207,7 @@ fn multi_hop_sending() {
209207
max_total_cltv_expiry_delta: Some(1000),
210208
max_path_count: Some(10),
211209
max_channel_saturation_power_of_half: Some(2),
210+
preimage: None,
212211
};
213212

214213
let invoice_description =
@@ -1414,10 +1413,18 @@ fn spontaneous_send_with_custom_preimage() {
14141413
let custom_bytes = bytes.to_byte_array();
14151414
let custom_preimage = PaymentPreimage(custom_bytes);
14161415

1416+
let sending_parameters = SendingParameters {
1417+
max_total_routing_fee_msat: Some(Some(75_000).into()),
1418+
max_total_cltv_expiry_delta: Some(1000),
1419+
max_path_count: Some(10),
1420+
max_channel_saturation_power_of_half: Some(2),
1421+
preimage: Some(custom_preimage),
1422+
};
1423+
14171424
let amount_msat = 100_000;
14181425
let payment_id = node_a
14191426
.spontaneous_payment()
1420-
.send_with_preimage(amount_msat, node_b.node_id(), None, custom_preimage.clone())
1427+
.send(amount_msat, node_b.node_id(), Some(sending_parameters))
14211428
.unwrap();
14221429

14231430
// check payment status and verify stored preimage

0 commit comments

Comments
 (0)