{-# OPTIONS_HADDOCK prune #-}
{-# LANGUAGE BangPatterns #-}
module Lightning.Protocol.BOLT7.Hash (
channelAnnouncementHash
, nodeAnnouncementHash
, channelUpdateHash
, channelUpdateChecksum
) where
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Word (Word32)
import qualified Crypto.Hash.SHA256 as SHA256
import Lightning.Protocol.BOLT7.CRC32C (crc32c)
import Lightning.Protocol.BOLT7.Types (signatureLen, chainHashLen)
doubleSha256 :: ByteString -> ByteString
doubleSha256 :: ByteString -> ByteString
doubleSha256 = ByteString -> ByteString
SHA256.hash (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> ByteString
SHA256.hash
{-# INLINE doubleSha256 #-}
channelAnnouncementHash :: ByteString -> ByteString
channelAnnouncementHash :: ByteString -> ByteString
channelAnnouncementHash !ByteString
msg =
let offset :: Int
offset = Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
signatureLen
payload :: ByteString
payload = Int -> ByteString -> ByteString
BS.drop Int
offset ByteString
msg
in ByteString -> ByteString
doubleSha256 ByteString
payload
{-# INLINE channelAnnouncementHash #-}
nodeAnnouncementHash :: ByteString -> ByteString
nodeAnnouncementHash :: ByteString -> ByteString
nodeAnnouncementHash !ByteString
msg =
let payload :: ByteString
payload = Int -> ByteString -> ByteString
BS.drop Int
signatureLen ByteString
msg
in ByteString -> ByteString
doubleSha256 ByteString
payload
{-# INLINE nodeAnnouncementHash #-}
channelUpdateHash :: ByteString -> ByteString
channelUpdateHash :: ByteString -> ByteString
channelUpdateHash !ByteString
msg =
let payload :: ByteString
payload = Int -> ByteString -> ByteString
BS.drop Int
signatureLen ByteString
msg
in ByteString -> ByteString
doubleSha256 ByteString
payload
{-# INLINE channelUpdateHash #-}
channelUpdateChecksum :: ByteString -> Word32
channelUpdateChecksum :: ByteString -> Word32
channelUpdateChecksum !ByteString
msg =
let
chainHash :: ByteString
chainHash = Int -> ByteString -> ByteString
BS.take Int
chainHashLen (Int -> ByteString -> ByteString
BS.drop Int
signatureLen ByteString
msg)
scid :: ByteString
scid = Int -> ByteString -> ByteString
BS.take Int
8 (Int -> ByteString -> ByteString
BS.drop (Int
signatureLen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
chainHashLen) ByteString
msg)
restOffset :: Int
restOffset = Int
signatureLen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
chainHashLen Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
8 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4
rest :: ByteString
rest = Int -> ByteString -> ByteString
BS.drop Int
restOffset ByteString
msg
checksumData :: ByteString
checksumData = [ByteString] -> ByteString
BS.concat [ByteString
chainHash, ByteString
scid, ByteString
rest]
in ByteString -> Word32
crc32c ByteString
checksumData
{-# INLINE channelUpdateChecksum #-}