Skip to content

Commit 9c802c2

Browse files
committed
Refactor payment_hash to return PaymentHash
This commit fixes the payment_hash function of Bolt11Invoice to return a PaymentHash type instead of a sha256 byte stream. Code and test files dependent on this function have also been modified to adhere to the updated changes.
1 parent f6b4f0c commit 9c802c2

File tree

5 files changed

+21
-21
lines changed

5 files changed

+21
-21
lines changed

lightning-invoice/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use core::time::Duration;
5454
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};
5555

5656
#[doc(no_inline)]
57-
pub use lightning_types::payment::PaymentSecret;
57+
pub use lightning_types::payment::{PaymentHash, PaymentSecret};
5858
#[doc(no_inline)]
5959
pub use lightning_types::routing::{RouteHint, RouteHintHop, RoutingFees};
6060
use lightning_types::string::UntrustedString;
@@ -1460,8 +1460,9 @@ impl Bolt11Invoice {
14601460
}
14611461

14621462
/// Returns the hash to which we will receive the preimage on completion of the payment
1463-
pub fn payment_hash(&self) -> &sha256::Hash {
1464-
&self.signed_invoice.payment_hash().expect("checked by constructor").0
1463+
pub fn payment_hash(&self) -> PaymentHash {
1464+
let hash = self.signed_invoice.payment_hash().expect("checked by constructor").0;
1465+
PaymentHash(hash.to_byte_array())
14651466
}
14661467

14671468
/// Return the description or a hash of it for longer ones
@@ -2339,7 +2340,7 @@ mod test {
23392340
sha256::Hash::from_slice(&[3; 32][..]).unwrap()
23402341
))
23412342
);
2342-
assert_eq!(invoice.payment_hash(), &sha256::Hash::from_slice(&[21; 32][..]).unwrap());
2343+
assert_eq!(invoice.payment_hash(), PaymentHash([21; 32]));
23432344
assert_eq!(invoice.payment_secret(), &PaymentSecret([42; 32]));
23442345

23452346
let mut expected_features = Bolt11InvoiceFeatures::empty();

lightning-liquidity/tests/lsps2_integration_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,7 +1211,7 @@ fn client_trusts_lsp_end_to_end_test() {
12111211
.node
12121212
.pay_for_bolt11_invoice(
12131213
&invoice,
1214-
PaymentId(invoice.payment_hash().to_byte_array()),
1214+
PaymentId(invoice.payment_hash().0),
12151215
None,
12161216
Default::default(),
12171217
Retry::Attempts(3),
@@ -1684,7 +1684,7 @@ fn late_payment_forwarded_and_safe_after_force_close_does_not_broadcast() {
16841684
.node
16851685
.pay_for_bolt11_invoice(
16861686
&invoice,
1687-
PaymentId(invoice.payment_hash().to_byte_array()),
1687+
PaymentId(invoice.payment_hash().0),
16881688
None,
16891689
Default::default(),
16901690
Retry::Attempts(3),
@@ -1714,7 +1714,7 @@ fn late_payment_forwarded_and_safe_after_force_close_does_not_broadcast() {
17141714
*requested_next_hop_scid,
17151715
*intercept_id,
17161716
*expected_outbound_amount_msat,
1717-
PaymentHash(invoice.payment_hash().to_byte_array()),
1717+
invoice.payment_hash(),
17181718
)
17191719
.unwrap();
17201720
},
@@ -1875,7 +1875,7 @@ fn htlc_timeout_before_client_claim_results_in_handling_failed() {
18751875
.node
18761876
.pay_for_bolt11_invoice(
18771877
&invoice,
1878-
PaymentId(invoice.payment_hash().to_byte_array()),
1878+
PaymentId(invoice.payment_hash().0),
18791879
None,
18801880
Default::default(),
18811881
Retry::Attempts(3),
@@ -1905,7 +1905,7 @@ fn htlc_timeout_before_client_claim_results_in_handling_failed() {
19051905
*requested_next_hop_scid,
19061906
*intercept_id,
19071907
*expected_outbound_amount_msat,
1908-
PaymentHash(invoice.payment_hash().to_byte_array()),
1908+
invoice.payment_hash(),
19091909
)
19101910
.unwrap();
19111911
},
@@ -1984,7 +1984,7 @@ fn htlc_timeout_before_client_claim_results_in_handling_failed() {
19841984
match &client_events[0] {
19851985
Event::HTLCHandlingFailed { failure_type, .. } => match failure_type {
19861986
lightning::events::HTLCHandlingFailureType::Receive { payment_hash } => {
1987-
assert_eq!(*payment_hash, PaymentHash(invoice.payment_hash().to_byte_array()));
1987+
assert_eq!(*payment_hash, invoice.payment_hash());
19881988
},
19891989
_ => panic!("Unexpected failure_type: {:?}", failure_type),
19901990
},
@@ -2212,7 +2212,7 @@ fn client_trusts_lsp_partial_fee_does_not_trigger_broadcast() {
22122212
.node
22132213
.pay_for_bolt11_invoice(
22142214
&invoice,
2215-
PaymentId(invoice.payment_hash().to_byte_array()),
2215+
PaymentId(invoice.payment_hash().0),
22162216
None,
22172217
Default::default(),
22182218
Retry::Attempts(3),

lightning/src/ln/channelmanager.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2230,23 +2230,23 @@ where
22302230
/// match event {
22312231
/// Event::PaymentClaimable { payment_hash, purpose, .. } => match purpose {
22322232
/// PaymentPurpose::Bolt11InvoicePayment { payment_preimage: Some(payment_preimage), .. } => {
2233-
/// assert_eq!(payment_hash.0, invoice.payment_hash().as_ref());
2233+
/// assert_eq!(payment_hash, invoice.payment_hash());
22342234
/// println!("Claiming payment {}", payment_hash);
22352235
/// channel_manager.claim_funds(payment_preimage);
22362236
/// },
22372237
/// PaymentPurpose::Bolt11InvoicePayment { payment_preimage: None, .. } => {
22382238
/// println!("Unknown payment hash: {}", payment_hash);
22392239
/// },
22402240
/// PaymentPurpose::SpontaneousPayment(payment_preimage) => {
2241-
/// assert_ne!(payment_hash.0, invoice.payment_hash().as_ref());
2241+
/// assert_ne!(payment_hash, invoice.payment_hash());
22422242
/// println!("Claiming spontaneous payment {}", payment_hash);
22432243
/// channel_manager.claim_funds(payment_preimage);
22442244
/// },
22452245
/// // ...
22462246
/// # _ => {},
22472247
/// },
22482248
/// Event::PaymentClaimed { payment_hash, amount_msat, .. } => {
2249-
/// assert_eq!(payment_hash.0, invoice.payment_hash().as_ref());
2249+
/// assert_eq!(payment_hash, invoice.payment_hash());
22502250
/// println!("Claimed {} msats", amount_msat);
22512251
/// },
22522252
/// // ...
@@ -2271,7 +2271,7 @@ where
22712271
/// # ) {
22722272
/// # let channel_manager = channel_manager.get_cm();
22732273
/// # let payment_id = PaymentId([42; 32]);
2274-
/// # let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array());
2274+
/// # let payment_hash = invoice.payment_hash();
22752275
/// match channel_manager.pay_for_bolt11_invoice(
22762276
/// invoice, payment_id, None, route_params_config, retry
22772277
/// ) {

lightning/src/ln/invoice_utils.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ mod test {
627627
use crate::util::dyn_signer::{DynKeysInterface, DynPhantomKeysInterface};
628628
use crate::util::test_utils;
629629
use bitcoin::hashes::sha256::Hash as Sha256;
630-
use bitcoin::hashes::{sha256, Hash};
630+
use bitcoin::hashes::Hash;
631631
use bitcoin::network::Network;
632632
use core::time::Duration;
633633
use lightning_invoice::{
@@ -829,7 +829,7 @@ mod test {
829829
invoice.description(),
830830
Bolt11InvoiceDescriptionRef::Direct(&Description::new("test".to_string()).unwrap())
831831
);
832-
assert_eq!(invoice.payment_hash(), &sha256::Hash::from_slice(&payment_hash.0[..]).unwrap());
832+
assert_eq!(invoice.payment_hash(), payment_hash);
833833
}
834834

835835
#[cfg(not(feature = "std"))]
@@ -1257,8 +1257,7 @@ mod test {
12571257
Duration::from_secs(genesis_timestamp),
12581258
)
12591259
.unwrap();
1260-
let (payment_hash, payment_secret) =
1261-
(PaymentHash(invoice.payment_hash().to_byte_array()), *invoice.payment_secret());
1260+
let (payment_hash, payment_secret) = (invoice.payment_hash(), *invoice.payment_secret());
12621261
let payment_preimage = if user_generated_pmt_hash {
12631262
user_payment_preimage
12641263
} else {
@@ -1290,7 +1289,7 @@ mod test {
12901289
invoice.amount_milli_satoshis().unwrap(),
12911290
);
12921291

1293-
let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
1292+
let payment_hash = invoice.payment_hash();
12941293
let id = PaymentId(payment_hash.0);
12951294
let onion = RecipientOnionFields::secret_only(*invoice.payment_secret());
12961295
nodes[0].node.send_payment(payment_hash, onion, id, params, Retry::Attempts(0)).unwrap();

lightning/src/ln/outbound_payment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,7 @@ where
933933
IH: Fn() -> InFlightHtlcs,
934934
SP: Fn(SendAlongPathArgs) -> Result<(), APIError>,
935935
{
936-
let payment_hash = PaymentHash((*invoice.payment_hash()).to_byte_array());
936+
let payment_hash = invoice.payment_hash();
937937

938938
let amount = match (invoice.amount_milli_satoshis(), amount_msats) {
939939
(Some(amt), None) | (None, Some(amt)) => amt,

0 commit comments

Comments
 (0)