commit e5243245d79bb8e3cbce0ec83e7c9f691395a1f5
parent 1353cecbe771b61d2d1dbee207fe0b14e929b389
Author: Jared Tobin <jared@jtobin.io>
Date: Sun, 25 Jan 2026 15:54:15 +0400
Refactor: extract doubleSha256 helper
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Diffstat:
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/lib/Lightning/Protocol/BOLT7/Hash.hs b/lib/Lightning/Protocol/BOLT7/Hash.hs
@@ -31,6 +31,11 @@ import qualified Crypto.Hash.SHA256 as SHA256
import Lightning.Protocol.BOLT7.CRC32C (crc32c)
import Lightning.Protocol.BOLT7.Types (signatureLen, chainHashLen)
+-- | Double SHA-256 hash (used for Lightning message signing).
+doubleSha256 :: ByteString -> ByteString
+doubleSha256 = SHA256.hash . SHA256.hash
+{-# INLINE doubleSha256 #-}
+
-- | Compute signature hash for channel_announcement.
--
-- The hash covers the message starting at byte offset 256, which is after
@@ -40,9 +45,9 @@ import Lightning.Protocol.BOLT7.Types (signatureLen, chainHashLen)
-- Returns the double-SHA256 hash (32 bytes).
channelAnnouncementHash :: ByteString -> ByteString
channelAnnouncementHash !msg =
- let offset = 4 * signatureLen -- 4 signatures * 64 bytes = 256
+ let offset = 4 * signatureLen
payload = BS.drop offset msg
- in SHA256.hash (SHA256.hash payload)
+ in doubleSha256 payload
{-# INLINE channelAnnouncementHash #-}
-- | Compute signature hash for node_announcement.
@@ -53,7 +58,7 @@ channelAnnouncementHash !msg =
nodeAnnouncementHash :: ByteString -> ByteString
nodeAnnouncementHash !msg =
let payload = BS.drop signatureLen msg
- in SHA256.hash (SHA256.hash payload)
+ in doubleSha256 payload
{-# INLINE nodeAnnouncementHash #-}
-- | Compute signature hash for channel_update.
@@ -64,7 +69,7 @@ nodeAnnouncementHash !msg =
channelUpdateHash :: ByteString -> ByteString
channelUpdateHash !msg =
let payload = BS.drop signatureLen msg
- in SHA256.hash (SHA256.hash payload)
+ in doubleSha256 payload
{-# INLINE channelUpdateHash #-}
-- | Compute checksum for channel_update.