From: Jeffrey Czyz Date: Tue, 23 Jul 2024 21:09:14 +0000 (-0500) Subject: Result from Bolt12Invoice::verify_using_payer_data X-Git-Tag: v0.0.124-beta~30^2~7 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=4a69f58ff69f2cfc36c1cfb24630c7b7f9d8177b;p=rust-lightning Result from Bolt12Invoice::verify_using_payer_data Use a Result return type instead of a bool when verifying a Bolt12Invoice. This way ignoring the result will produce a compiler warning. --- diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 28c40856f..10c010e89 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -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(()), } diff --git a/lightning/src/offers/invoice.rs b/lightning/src/offers/invoice.rs index 7d482bc24..eee71995e 100644 --- a/lightning/src/offers/invoice.rs +++ b/lightning/src/offers/invoice.rs @@ -790,12 +790,13 @@ impl Bolt12Invoice { /// sent through. pub fn verify_using_payer_data( &self, payment_id: PaymentId, nonce: Nonce, key: &ExpandedKey, secp_ctx: &Secp256k1 - ) -> bool { + ) -> Result { 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 { diff --git a/lightning/src/offers/invoice_request.rs b/lightning/src/offers/invoice_request.rs index 4ad996450..4b3404702 100644 --- a/lightning/src/offers/invoice_request.rs +++ b/lightning/src/offers/invoice_request.rs @@ -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] diff --git a/lightning/src/offers/refund.rs b/lightning/src/offers/refund.rs index 9cfa3147c..8547cfe99 100644 --- a/lightning/src/offers/refund.rs +++ b/lightning/src/offers/refund.rs @@ -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]