From 8119fbfaf9fb72a083c7e20250766a1eb4e0183b Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Thu, 18 Jul 2024 11:42:57 -0500 Subject: [PATCH] Add Sha256 HMAC (de)serialization An HMAC needs to be included in OffersContext::OutboundPayment to authenticate the included PaymentId. Implement Readable and Writeable to allow for this. --- lightning/src/util/ser.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lightning/src/util/ser.rs b/lightning/src/util/ser.rs index 29c52f6a0..5f203b74d 100644 --- a/lightning/src/util/ser.rs +++ b/lightning/src/util/ser.rs @@ -33,7 +33,9 @@ use bitcoin::script::{self, ScriptBuf}; use bitcoin::transaction::{OutPoint, Transaction, TxOut}; use bitcoin::{consensus, Witness}; use bitcoin::consensus::Encodable; +use bitcoin::hashes::hmac::Hmac; use bitcoin::hashes::sha256d::Hash as Sha256dHash; +use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hash_types::{Txid, BlockHash}; use core::time::Duration; use crate::chain::ClaimId; @@ -1033,6 +1035,21 @@ impl Readable for PartialSignatureWithNonce { } } +impl Writeable for Hmac { + fn write(&self, w: &mut W) -> Result<(), io::Error> { + w.write_all(&self[..]) + } +} + +impl Readable for Hmac { + fn read(r: &mut R) -> Result { + use bitcoin::hashes::Hash; + + let buf: [u8; 32] = Readable::read(r)?; + Ok(Hmac::::from_byte_array(buf)) + } +} + impl Writeable for Sha256dHash { fn write(&self, w: &mut W) -> Result<(), io::Error> { w.write_all(&self[..]) -- 2.39.5