Skip to content

Commit 1d0c656

Browse files
committed
Remove redundant utilites/tests from bolt11_payment.rs
1 parent 658eb7c commit 1d0c656

File tree

2 files changed

+4
-148
lines changed

2 files changed

+4
-148
lines changed

lightning/src/ln/bolt11_payment.rs

Lines changed: 4 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -7,162 +7,22 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
//! Convenient utilities for paying Lightning invoices.
11-
12-
use bitcoin::hashes::Hash;
13-
use lightning_invoice::Bolt11Invoice;
14-
15-
use crate::ln::channelmanager::RecipientOnionFields;
16-
use crate::routing::router::{PaymentParameters, RouteParameters};
17-
use crate::types::payment::PaymentHash;
18-
19-
/// Builds the necessary parameters to pay or pre-flight probe the given variable-amount
20-
/// (also known as 'zero-amount') [`Bolt11Invoice`] using
21-
/// [`ChannelManager::send_payment`] or [`ChannelManager::send_preflight_probes`].
22-
///
23-
/// Prior to paying, you must ensure that the [`Bolt11Invoice::payment_hash`] is unique and the
24-
/// same [`PaymentHash`] has never been paid before.
25-
///
26-
/// Will always succeed unless the invoice has an amount specified, in which case
27-
/// [`payment_parameters_from_invoice`] should be used.
28-
///
29-
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
30-
/// [`ChannelManager::send_preflight_probes`]: crate::ln::channelmanager::ChannelManager::send_preflight_probes
31-
pub fn payment_parameters_from_variable_amount_invoice(
32-
invoice: &Bolt11Invoice, amount_msat: u64,
33-
) -> Result<(PaymentHash, RecipientOnionFields, RouteParameters), ()> {
34-
if invoice.amount_milli_satoshis().is_some() {
35-
Err(())
36-
} else {
37-
Ok(params_from_invoice(invoice, amount_msat))
38-
}
39-
}
40-
41-
/// Builds the necessary parameters to pay or pre-flight probe the given [`Bolt11Invoice`] using
42-
/// [`ChannelManager::send_payment`] or [`ChannelManager::send_preflight_probes`].
43-
///
44-
/// Prior to paying, you must ensure that the [`Bolt11Invoice::payment_hash`] is unique and the
45-
/// same [`PaymentHash`] has never been paid before.
46-
///
47-
/// Will always succeed unless the invoice has no amount specified, in which case
48-
/// [`payment_parameters_from_variable_amount_invoice`] should be used.
49-
///
50-
/// [`ChannelManager::send_payment`]: crate::ln::channelmanager::ChannelManager::send_payment
51-
/// [`ChannelManager::send_preflight_probes`]: crate::ln::channelmanager::ChannelManager::send_preflight_probes
52-
pub fn payment_parameters_from_invoice(
53-
invoice: &Bolt11Invoice,
54-
) -> Result<(PaymentHash, RecipientOnionFields, RouteParameters), ()> {
55-
if let Some(amount_msat) = invoice.amount_milli_satoshis() {
56-
Ok(params_from_invoice(invoice, amount_msat))
57-
} else {
58-
Err(())
59-
}
60-
}
61-
62-
fn params_from_invoice(
63-
invoice: &Bolt11Invoice, amount_msat: u64,
64-
) -> (PaymentHash, RecipientOnionFields, RouteParameters) {
65-
let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array());
66-
67-
let mut recipient_onion = RecipientOnionFields::secret_only(*invoice.payment_secret());
68-
recipient_onion.payment_metadata = invoice.payment_metadata().map(|v| v.clone());
69-
70-
let mut payment_params = PaymentParameters::from_node_id(
71-
invoice.recover_payee_pub_key(),
72-
invoice.min_final_cltv_expiry_delta() as u32,
73-
)
74-
.with_route_hints(invoice.route_hints())
75-
.unwrap();
76-
if let Some(expiry) = invoice.expires_at() {
77-
payment_params = payment_params.with_expiry_time(expiry.as_secs());
78-
}
79-
if let Some(features) = invoice.features() {
80-
payment_params = payment_params.with_bolt11_features(features.clone()).unwrap();
81-
}
82-
83-
let route_params = RouteParameters::from_payment_params_and_value(payment_params, amount_msat);
84-
(payment_hash, recipient_onion, route_params)
85-
}
10+
//! Tests for verifying the correct end-to-end handling of BOLT11 payments, including metadata propagation.
8611
8712
#[cfg(test)]
8813
mod tests {
89-
use super::*;
9014
use crate::events::Event;
9115
use crate::ln::channelmanager::{PaymentId, Retry};
9216
use crate::ln::functional_test_utils::*;
9317
use crate::ln::msgs::ChannelMessageHandler;
9418
use crate::ln::outbound_payment::Bolt11PaymentError;
95-
use crate::routing::router::{Payee, RouteParametersConfig};
96-
use crate::types::payment::PaymentSecret;
19+
use crate::routing::router::RouteParametersConfig;
9720
use bitcoin::hashes::sha256::Hash as Sha256;
98-
use bitcoin::secp256k1::{PublicKey, Secp256k1, SecretKey};
21+
use bitcoin::hashes::Hash;
22+
use bitcoin::secp256k1::Secp256k1;
9923
use lightning_invoice::{Currency, InvoiceBuilder};
10024
use std::time::SystemTime;
10125

102-
#[test]
103-
fn invoice_test() {
104-
let payment_hash = Sha256::hash(&[0; 32]);
105-
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
106-
let secp_ctx = Secp256k1::new();
107-
let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
108-
109-
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
110-
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
111-
.description("test".into())
112-
.payment_hash(payment_hash)
113-
.payment_secret(PaymentSecret([0; 32]))
114-
.duration_since_epoch(timestamp)
115-
.min_final_cltv_expiry_delta(144)
116-
.amount_milli_satoshis(128)
117-
.build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &private_key))
118-
.unwrap();
119-
120-
assert!(payment_parameters_from_variable_amount_invoice(&invoice, 42).is_err());
121-
122-
let (hash, onion, params) = payment_parameters_from_invoice(&invoice).unwrap();
123-
assert_eq!(&hash.0[..], &payment_hash[..]);
124-
assert_eq!(onion.payment_secret, Some(PaymentSecret([0; 32])));
125-
assert_eq!(params.final_value_msat, 128);
126-
match params.payment_params.payee {
127-
Payee::Clear { node_id, .. } => {
128-
assert_eq!(node_id, public_key);
129-
},
130-
_ => panic!(),
131-
}
132-
}
133-
134-
#[test]
135-
fn zero_value_invoice_test() {
136-
let payment_hash = Sha256::hash(&[0; 32]);
137-
let private_key = SecretKey::from_slice(&[42; 32]).unwrap();
138-
let secp_ctx = Secp256k1::new();
139-
let public_key = PublicKey::from_secret_key(&secp_ctx, &private_key);
140-
141-
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap();
142-
let invoice = InvoiceBuilder::new(Currency::Bitcoin)
143-
.description("test".into())
144-
.payment_hash(payment_hash)
145-
.payment_secret(PaymentSecret([0; 32]))
146-
.duration_since_epoch(timestamp)
147-
.min_final_cltv_expiry_delta(144)
148-
.build_signed(|hash| secp_ctx.sign_ecdsa_recoverable(hash, &private_key))
149-
.unwrap();
150-
151-
assert!(payment_parameters_from_invoice(&invoice).is_err());
152-
153-
let (hash, onion, params) =
154-
payment_parameters_from_variable_amount_invoice(&invoice, 42).unwrap();
155-
assert_eq!(&hash.0[..], &payment_hash[..]);
156-
assert_eq!(onion.payment_secret, Some(PaymentSecret([0; 32])));
157-
assert_eq!(params.final_value_msat, 42);
158-
match params.payment_params.payee {
159-
Payee::Clear { node_id, .. } => {
160-
assert_eq!(node_id, public_key);
161-
},
162-
_ => panic!(),
163-
}
164-
}
165-
16626
#[test]
16727
fn payment_metadata_end_to_end_for_invoice_with_amount() {
16828
// Test that a payment metadata read from an invoice passed to `pay_invoice` makes it all

lightning/src/ln/channelmanager.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2041,9 +2041,6 @@ where
20412041
/// # }
20422042
/// ```
20432043
///
2044-
/// For paying an invoice, see the [`bolt11_payment`] module with convenience functions for use with
2045-
/// [`send_payment`].
2046-
///
20472044
/// ```
20482045
/// # use bitcoin::hashes::Hash;
20492046
/// # use lightning::events::{Event, EventsProvider};
@@ -2383,7 +2380,6 @@ where
23832380
/// [`create_bolt11_invoice`]: Self::create_bolt11_invoice
23842381
/// [`create_inbound_payment`]: Self::create_inbound_payment
23852382
/// [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
2386-
/// [`bolt11_payment`]: crate::ln::bolt11_payment
23872383
/// [`claim_funds`]: Self::claim_funds
23882384
/// [`send_payment`]: Self::send_payment
23892385
/// [`offers`]: crate::offers

0 commit comments

Comments
 (0)