util: Adds Into<PaymentHash> for PaymentPreimage
authorSergi Delgado Segura <sergi.delgado.s@gmail.com>
Thu, 29 Feb 2024 19:51:38 +0000 (14:51 -0500)
committerSergi Delgado Segura <sergi.delgado.s@gmail.com>
Thu, 29 Feb 2024 20:41:20 +0000 (15:41 -0500)
This seems like a useful interface to have for downstream users

lightning/src/ln/mod.rs

index e79f4bbd24d90d481735a7c602f64442a128d296..bf2e94caa03927184026a5ac4a1d7cfe3a3f6466 100644 (file)
@@ -85,6 +85,8 @@ mod offers_tests;
 
 pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
 
+use bitcoin::hashes::{sha256::Hash as Sha256, Hash};
+
 /// payment_hash type, use to cross-lock hop
 ///
 /// This is not exported to bindings users as we just use [u8; 32] directly
@@ -109,6 +111,13 @@ impl core::fmt::Display for PaymentPreimage {
        }
 }
 
+/// Converts a `PaymentPreimage` into a `PaymentHash` by hashing the preimage with SHA256.
+impl Into<PaymentHash> for PaymentPreimage {
+       fn into(self) -> PaymentHash {
+               PaymentHash(Sha256::hash(&self.0).to_byte_array())
+       }
+}
+
 /// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
 ///
 /// This is not exported to bindings users as we just use [u8; 32] directly