]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Result from Bolt12Invoice::verify_using_payer_data
authorJeffrey Czyz <jkczyz@gmail.com>
Tue, 23 Jul 2024 21:09:14 +0000 (16:09 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Tue, 30 Jul 2024 21:06:24 +0000 (16:06 -0500)
Use a Result return type instead of a bool when verifying a
Bolt12Invoice. This way ignoring the result will produce a compiler
warning.

lightning/src/ln/channelmanager.rs
lightning/src/offers/invoice.rs
lightning/src/offers/invoice_request.rs
lightning/src/offers/refund.rs

index 28c40856f50cfa52ca299534d5695c8a69155116..10c010e8989c698a621a28954e8cb0e5828900f7 100644 (file)
@@ -4223,10 +4223,7 @@ where
                                invoice.verify_using_metadata(expanded_key, secp_ctx)
                        },
                        OffersContext::OutboundPayment { payment_id, nonce } => {
-                               invoice
-                                       .verify_using_payer_data(*payment_id, *nonce, expanded_key, secp_ctx)
-                                       .then(|| *payment_id)
-                                       .ok_or(())
+                               invoice.verify_using_payer_data(*payment_id, *nonce, expanded_key, secp_ctx)
                        },
                        _ => Err(()),
                }
index 7d482bc245e992ea2b7c0f9c813757e1151f0030..eee71995e321c0325b1e5baa3fe961d8ce47cabe 100644 (file)
@@ -790,12 +790,13 @@ impl Bolt12Invoice {
        /// sent through.
        pub fn verify_using_payer_data<T: secp256k1::Signing>(
                &self, payment_id: PaymentId, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1<T>
-       ) -> bool {
+       ) -> Result<PaymentId, ()> {
                let metadata = Metadata::payer_data(payment_id, nonce, key);
-               match self.contents.verify(TlvStream::new(&self.bytes), &metadata, key, secp_ctx) {
-                       Ok(extracted_payment_id) => payment_id == extracted_payment_id,
-                       Err(()) => false,
-               }
+               self.contents.verify(TlvStream::new(&self.bytes), &metadata, key, secp_ctx)
+                       .and_then(|extracted_payment_id| (payment_id == extracted_payment_id)
+                               .then(|| payment_id)
+                               .ok_or(())
+                       )
        }
 
        pub(crate) fn as_tlv_stream(&self) -> FullInvoiceTlvStreamRef {
index 4ad99645002271797d2a403eddcf31c3c5d4bcaf..4b340470226f546f15294f5b42db3f976c8c68be 100644 (file)
@@ -1417,7 +1417,9 @@ mod tests {
                        Ok(payment_id) => assert_eq!(payment_id, PaymentId([1; 32])),
                        Err(()) => panic!("verification failed"),
                }
-               assert!(!invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_err()
+               );
 
                // Fails verification with altered fields
                let (
@@ -1488,7 +1490,9 @@ mod tests {
                        .build().unwrap()
                        .sign(recipient_sign).unwrap();
                assert!(invoice.verify_using_metadata(&expanded_key, &secp_ctx).is_err());
-               assert!(invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_ok()
+               );
 
                // Fails verification with altered fields
                let (
@@ -1511,7 +1515,9 @@ mod tests {
                signature_tlv_stream.write(&mut encoded_invoice).unwrap();
 
                let invoice = Bolt12Invoice::try_from(encoded_invoice).unwrap();
-               assert!(!invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_err()
+               );
 
                // Fails verification with altered payer id
                let (
@@ -1534,7 +1540,9 @@ mod tests {
                signature_tlv_stream.write(&mut encoded_invoice).unwrap();
 
                let invoice = Bolt12Invoice::try_from(encoded_invoice).unwrap();
-               assert!(!invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_err()
+               );
        }
 
        #[test]
index 9cfa3147c63d698879a8f4cd72765b2c6a7f7c23..8547cfe993131e5f57130aa504914f67d7ad0e4c 100644 (file)
@@ -1049,7 +1049,9 @@ mod tests {
                        Ok(payment_id) => assert_eq!(payment_id, PaymentId([1; 32])),
                        Err(()) => panic!("verification failed"),
                }
-               assert!(!invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_err()
+               );
 
                let mut tlv_stream = refund.as_tlv_stream();
                tlv_stream.2.amount = Some(2000);
@@ -1111,7 +1113,9 @@ mod tests {
                        .build().unwrap()
                        .sign(recipient_sign).unwrap();
                assert!(invoice.verify_using_metadata(&expanded_key, &secp_ctx).is_err());
-               assert!(invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_ok()
+               );
 
                // Fails verification with altered fields
                let mut tlv_stream = refund.as_tlv_stream();
@@ -1125,7 +1129,9 @@ mod tests {
                        .unwrap()
                        .build().unwrap()
                        .sign(recipient_sign).unwrap();
-               assert!(!invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_err()
+               );
 
                // Fails verification with altered payer_id
                let mut tlv_stream = refund.as_tlv_stream();
@@ -1140,7 +1146,9 @@ mod tests {
                        .unwrap()
                        .build().unwrap()
                        .sign(recipient_sign).unwrap();
-               assert!(!invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx));
+               assert!(
+                       invoice.verify_using_payer_data(payment_id, nonce, &expanded_key, &secp_ctx).is_err()
+               );
        }
 
        #[test]