Types.hs (6811B)
1 {-# OPTIONS_HADDOCK prune #-} 2 {-# LANGUAGE BangPatterns #-} 3 {-# LANGUAGE DeriveGeneric #-} 4 {-# LANGUAGE DerivingStrategies #-} 5 {-# LANGUAGE GeneralizedNewtypeDeriving #-} 6 7 -- | 8 -- Module: Lightning.Protocol.BOLT2.Types 9 -- Copyright: (c) 2025 Jared Tobin 10 -- License: MIT 11 -- Maintainer: Jared Tobin <jared@ppad.tech> 12 -- 13 -- Core types for BOLT #2 peer protocol. 14 -- 15 -- This module provides newtypes for identifiers, amounts, hashes, and 16 -- keys used in the Lightning Network peer protocol. 17 18 module Lightning.Protocol.BOLT2.Types ( 19 -- * Identifiers (re-exported from BOLT1) 20 ChannelId(..) 21 , channelId 22 , unChannelId 23 24 -- * Amounts (re-exported from BOLT1) 25 , Satoshi(..) 26 , MilliSatoshi(..) 27 , satToMsat 28 , msatToSat 29 30 -- * Cryptographic types (re-exported from BOLT1) 31 , Signature(..) 32 , signature 33 , unSignature 34 , Point(..) 35 , point 36 , unPoint 37 , PaymentHash(..) 38 , paymentHash 39 , unPaymentHash 40 , PaymentPreimage(..) 41 , paymentPreimage 42 , unPaymentPreimage 43 , PerCommitmentSecret(..) 44 , perCommitmentSecret 45 , unPerCommitmentSecret 46 47 -- * Chain types (re-exported from BOLT1) 48 , ChainHash(..) 49 , chainHash 50 , unChainHash 51 , ShortChannelId(..) 52 , shortChannelId 53 , scidBlockHeight 54 , scidTxIndex 55 , scidOutputIndex 56 57 -- * Transaction types 58 , TxId(..) 59 , mkTxId 60 , OutPoint(..) 61 , ScriptPubKey 62 , scriptPubKey 63 , unScriptPubKey 64 65 -- * Protocol identifiers 66 , HtlcId 67 , htlcId 68 , unHtlcId 69 , SerialId 70 , serialId 71 , unSerialId 72 73 -- * Quiescence 74 , Initiator(..) 75 76 -- * Protocol types 77 , FeatureBits 78 , featureBits 79 , unFeatureBits 80 , OnionPacket 81 , onionPacket 82 , unOnionPacket 83 84 -- * Constants 85 , channelIdLen 86 , signatureLen 87 , pointLen 88 , chainHashLen 89 , shortChannelIdLen 90 , paymentHashLen 91 , paymentPreimageLen 92 , onionPacketLen 93 , perCommitmentSecretLen 94 ) where 95 96 import Bitcoin.Prim.Tx (TxId(..), mkTxId, OutPoint(..)) 97 import Control.DeepSeq (NFData) 98 import qualified Data.ByteString as BS 99 import Data.Word (Word64) 100 import GHC.Generics (Generic) 101 import Lightning.Protocol.BOLT1.Prim 102 ( ChannelId(..), channelId, unChannelId 103 , Satoshi(..), MilliSatoshi(..), satToMsat, msatToSat 104 , Signature(..), signature, unSignature 105 , Point(..), point, unPoint 106 , PaymentHash(..), paymentHash, unPaymentHash 107 , PaymentPreimage(..), paymentPreimage, unPaymentPreimage 108 , PerCommitmentSecret(..), perCommitmentSecret 109 , unPerCommitmentSecret 110 , ChainHash(..), chainHash, unChainHash 111 , ShortChannelId(..), shortChannelId 112 , scidBlockHeight, scidTxIndex, scidOutputIndex 113 ) 114 115 -- constants ------------------------------------------------------------------- 116 117 -- | Length of a channel_id in bytes (32). 118 channelIdLen :: Int 119 channelIdLen = 32 120 {-# INLINE channelIdLen #-} 121 122 -- | Length of a signature in bytes (64, compact format). 123 signatureLen :: Int 124 signatureLen = 64 125 {-# INLINE signatureLen #-} 126 127 -- | Length of a compressed secp256k1 public key in bytes (33). 128 pointLen :: Int 129 pointLen = 33 130 {-# INLINE pointLen #-} 131 132 -- | Length of a chain hash in bytes (32). 133 chainHashLen :: Int 134 chainHashLen = 32 135 {-# INLINE chainHashLen #-} 136 137 -- | Length of a short_channel_id in bytes (8). 138 shortChannelIdLen :: Int 139 shortChannelIdLen = 8 140 {-# INLINE shortChannelIdLen #-} 141 142 -- | Length of a payment hash in bytes (32). 143 paymentHashLen :: Int 144 paymentHashLen = 32 145 {-# INLINE paymentHashLen #-} 146 147 -- | Length of a payment preimage in bytes (32). 148 paymentPreimageLen :: Int 149 paymentPreimageLen = 32 150 {-# INLINE paymentPreimageLen #-} 151 152 -- | Length of an onion routing packet in bytes (1366). 153 onionPacketLen :: Int 154 onionPacketLen = 1366 155 {-# INLINE onionPacketLen #-} 156 157 -- | Length of a per-commitment secret in bytes (32). 158 perCommitmentSecretLen :: Int 159 perCommitmentSecretLen = 32 160 {-# INLINE perCommitmentSecretLen #-} 161 162 -- transaction types ----------------------------------------------------------- 163 164 -- | A script pubkey (output script). 165 newtype ScriptPubKey = ScriptPubKey BS.ByteString 166 deriving stock (Eq, Ord, Show, Generic) 167 deriving newtype NFData 168 169 -- | Construct a 'ScriptPubKey' from a 'BS.ByteString'. 170 scriptPubKey :: BS.ByteString -> ScriptPubKey 171 scriptPubKey = ScriptPubKey 172 {-# INLINE scriptPubKey #-} 173 174 -- | Extract the underlying 'BS.ByteString' from a 'ScriptPubKey'. 175 unScriptPubKey :: ScriptPubKey -> BS.ByteString 176 unScriptPubKey (ScriptPubKey bs) = bs 177 {-# INLINE unScriptPubKey #-} 178 179 -- protocol identifiers ------------------------------------------------------- 180 181 -- | An HTLC identifier, unique per channel per direction. 182 newtype HtlcId = HtlcId Word64 183 deriving stock (Eq, Ord, Show, Generic) 184 deriving newtype NFData 185 186 -- | Construct an 'HtlcId' from a 'Word64'. 187 htlcId :: Word64 -> HtlcId 188 htlcId = HtlcId 189 {-# INLINE htlcId #-} 190 191 -- | Extract the underlying 'Word64' from an 'HtlcId'. 192 unHtlcId :: HtlcId -> Word64 193 unHtlcId (HtlcId w) = w 194 {-# INLINE unHtlcId #-} 195 196 -- | A serial identifier for interactive transaction construction. 197 newtype SerialId = SerialId Word64 198 deriving stock (Eq, Ord, Show, Generic) 199 deriving newtype NFData 200 201 -- | Construct a 'SerialId' from a 'Word64'. 202 serialId :: Word64 -> SerialId 203 serialId = SerialId 204 {-# INLINE serialId #-} 205 206 -- | Extract the underlying 'Word64' from a 'SerialId'. 207 unSerialId :: SerialId -> Word64 208 unSerialId (SerialId w) = w 209 {-# INLINE unSerialId #-} 210 211 -- quiescence ----------------------------------------------------------------- 212 213 -- | Role in quiescence (STFU) protocol. 214 data Initiator 215 = IsInitiator -- ^ This node initiated quiescence. 216 | NotInitiator -- ^ This node did not initiate quiescence. 217 deriving stock (Eq, Ord, Show, Generic) 218 219 instance NFData Initiator 220 221 -- protocol types -------------------------------------------------------------- 222 223 -- | Feature bits (variable length). 224 newtype FeatureBits = FeatureBits BS.ByteString 225 deriving stock (Eq, Ord, Show, Generic) 226 deriving newtype NFData 227 228 -- | Construct 'FeatureBits' from a 'BS.ByteString'. 229 featureBits :: BS.ByteString -> FeatureBits 230 featureBits = FeatureBits 231 {-# INLINE featureBits #-} 232 233 -- | Extract the underlying 'BS.ByteString' from 'FeatureBits'. 234 unFeatureBits :: FeatureBits -> BS.ByteString 235 unFeatureBits (FeatureBits bs) = bs 236 {-# INLINE unFeatureBits #-} 237 238 -- | A 1366-byte onion routing packet. 239 newtype OnionPacket = OnionPacket BS.ByteString 240 deriving stock (Eq, Ord, Show, Generic) 241 deriving newtype NFData 242 243 -- | Construct an 'OnionPacket' from a 1366-byte 'BS.ByteString'. 244 -- 245 -- Returns 'Nothing' if the input is not exactly 1366 bytes. 246 onionPacket :: BS.ByteString -> Maybe OnionPacket 247 onionPacket !bs 248 | BS.length bs == onionPacketLen = Just $! OnionPacket bs 249 | otherwise = Nothing 250 {-# INLINABLE onionPacket #-} 251 252 -- | Extract the underlying 'BS.ByteString' from an 'OnionPacket'. 253 unOnionPacket :: OnionPacket -> BS.ByteString 254 unOnionPacket (OnionPacket bs) = bs 255 {-# INLINE unOnionPacket #-}