@@ -24,6 +24,7 @@ use bitcoin::key::constants::SECRET_KEY_SIZE;
24
24
use bitcoin::network::Network;
25
25
26
26
use bitcoin::hashes::Hash;
27
+ use bitcoin::hashes::hmac::Hmac;
27
28
use bitcoin::hashes::sha256::Hash as Sha256;
28
29
use bitcoin::hash_types::{BlockHash, Txid};
29
30
@@ -413,6 +414,22 @@ pub struct PaymentId(pub [u8; Self::LENGTH]);
413
414
impl PaymentId {
414
415
/// Number of bytes in the id.
415
416
pub const LENGTH: usize = 32;
417
+
418
+ /// Constructs an HMAC to include in [`OffersContext::OutboundPayment`] for the payment id
419
+ /// along with the given [`Nonce`].
420
+ pub fn hmac_for_offer_payment(
421
+ &self, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
422
+ ) -> Hmac<Sha256> {
423
+ signer::hmac_for_payment_id(*self, nonce, expanded_key)
424
+ }
425
+
426
+ /// Authenticates the payment id using an HMAC and a [`Nonce`] taken from an
427
+ /// [`OffersContext::OutboundPayment`].
428
+ pub fn verify(
429
+ &self, hmac: Hmac<Sha256>, nonce: Nonce, expanded_key: &inbound_payment::ExpandedKey,
430
+ ) -> Result<(), ()> {
431
+ signer::verify_payment_id(*self, hmac, nonce, expanded_key)
432
+ }
416
433
}
417
434
418
435
impl Writeable for PaymentId {
@@ -9024,7 +9041,7 @@ where
9024
9041
};
9025
9042
let invoice_request = builder.build_and_sign()?;
9026
9043
9027
- let hmac = signer::hmac_for_payment_id( payment_id, nonce, expanded_key);
9044
+ let hmac = payment_id.hmac_for_offer_payment( nonce, expanded_key);
9028
9045
let context = OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) };
9029
9046
let reply_paths = self.create_blinded_paths(context)
9030
9047
.map_err(|_| Bolt12SemanticError::MissingPaths)?;
@@ -10900,7 +10917,7 @@ where
10900
10917
10901
10918
match context {
10902
10919
Some(OffersContext::OutboundPayment { payment_id, nonce, hmac: Some(hmac) }) => {
10903
- if signer::verify_payment_id(payment_id, hmac, nonce, expanded_key) {
10920
+ if let Ok(()) = payment_id.verify( hmac, nonce, expanded_key) {
10904
10921
self.abandon_payment_with_reason(
10905
10922
payment_id, PaymentFailureReason::InvoiceRequestRejected,
10906
10923
);
0 commit comments