]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Add Sha256 HMAC (de)serialization
authorJeffrey Czyz <jkczyz@gmail.com>
Thu, 18 Jul 2024 16:42:57 +0000 (11:42 -0500)
committerJeffrey Czyz <jkczyz@gmail.com>
Wed, 14 Aug 2024 15:42:17 +0000 (10:42 -0500)
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

index 29c52f6a08b1aaeeabf9c7c9107dc77e3693ace6..5f203b74d97f9d32290d9bc31c61e2dea8745888 100644 (file)
@@ -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<Sha256> {
+       fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
+               w.write_all(&self[..])
+       }
+}
+
+impl Readable for Hmac<Sha256> {
+       fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
+               use bitcoin::hashes::Hash;
+
+               let buf: [u8; 32] = Readable::read(r)?;
+               Ok(Hmac::<Sha256>::from_byte_array(buf))
+       }
+}
+
 impl Writeable for Sha256dHash {
        fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
                w.write_all(&self[..])