bolt2

Lightning peer protocol, per BOLT #2 (docs.ppad.tech/bolt2).
git clone git://git.ppad.tech/bolt2.git
Log | Files | Refs | README | LICENSE

commit cee2509e0b105a80bea50c8900d72f6b63cd6931
parent c98ddf37bc5d8c76d5440e3ecff369b86e11bdec
Author: Jared Tobin <jared@jtobin.io>
Date:   Sat, 18 Apr 2026 20:19:19 +0800

lib: use ppad-tx TxId and OutPoint types

Replace bolt2's local TxId, txId, Outpoint definitions with
re-exports from Bitcoin.Prim.Tx. Add orphan NFData instances
for TxId and OutPoint (needed for benchmarks). Keep unTxId
and txIdLen as backward-compat helpers.

Update Codec.hs (txId -> mkTxId), tests, and benchmarks.

Diffstat:
MAGENTS.md | 3++-
MCLAUDE.md | 3++-
Mbench/Main.hs | 2+-
Mbench/Weight.hs | 2+-
Mlib/Lightning/Protocol/BOLT2/Codec.hs | 2+-
Mlib/Lightning/Protocol/BOLT2/Types.hs | 37++++++++-----------------------------
Mtest/Main.hs | 2+-
7 files changed, 16 insertions(+), 35 deletions(-)

diff --git a/AGENTS.md b/AGENTS.md @@ -32,7 +32,8 @@ Do not use stack. All dependency and build management via nix. ### ppad libraries (use freely) Use ppad libraries (github.com/ppad-tech, git.ppad.tech) liberally. -Current dependencies: ppad-bolt1 (for primitive encoding/decoding, TLV). +Current dependencies: ppad-bolt1 (for primitive encoding/decoding, TLV), +ppad-tx (for TxId and OutPoint types). ### External libraries diff --git a/CLAUDE.md b/CLAUDE.md @@ -32,7 +32,8 @@ Do not use stack. All dependency and build management via nix. ### ppad libraries (use freely) Use ppad libraries (github.com/ppad-tech, git.ppad.tech) liberally. -Current dependencies: ppad-bolt1 (for primitive encoding/decoding, TLV). +Current dependencies: ppad-bolt1 (for primitive encoding/decoding, TLV), +ppad-tx (for TxId and OutPoint types). ### External libraries diff --git a/bench/Main.hs b/bench/Main.hs @@ -53,7 +53,7 @@ testChainHash = case chainHash zeroBytes32 of -- | 32-byte txid. testTxId :: TxId -testTxId = case txId zeroBytes32 of +testTxId = case mkTxId zeroBytes32 of Just t -> t Nothing -> error "testTxId: invalid" {-# NOINLINE testTxId #-} diff --git a/bench/Weight.hs b/bench/Weight.hs @@ -59,7 +59,7 @@ testChainHash = case chainHash zeroBytes32 of -- | 32-byte txid. testTxId :: TxId -testTxId = case txId zeroBytes32 of +testTxId = case mkTxId zeroBytes32 of Just t -> t Nothing -> error "testTxId: invalid" {-# NOINLINE testTxId #-} diff --git a/lib/Lightning/Protocol/BOLT2/Codec.hs b/lib/Lightning/Protocol/BOLT2/Codec.hs @@ -195,7 +195,7 @@ decodeTxIdBytes decodeTxIdBytes !bs = do (raw, rest) <- maybe (Left DecodeInsufficientBytes) Right (decodeBytes txIdLen bs) - tid <- maybe (Left DecodeInvalidTxId) Right (txId raw) + tid <- maybe (Left DecodeInvalidTxId) Right (mkTxId raw) Right (tid, rest) {-# INLINE decodeTxIdBytes #-} diff --git a/lib/Lightning/Protocol/BOLT2/Types.hs b/lib/Lightning/Protocol/BOLT2/Types.hs @@ -1,3 +1,4 @@ +{-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_HADDOCK prune #-} {-# LANGUAGE BangPatterns #-} {-# LANGUAGE DeriveGeneric #-} @@ -45,10 +46,10 @@ module Lightning.Protocol.BOLT2.Types ( , unSecret -- * Transaction types - , TxId - , txId + , TxId(..) + , mkTxId , unTxId - , Outpoint(..) + , OutPoint(..) , ScriptPubKey , scriptPubKey , unScriptPubKey @@ -84,6 +85,7 @@ module Lightning.Protocol.BOLT2.Types ( , secretLen ) where +import Bitcoin.Prim.Tx (TxId(..), mkTxId, OutPoint(..)) import Control.DeepSeq (NFData) import Data.Bits (unsafeShiftL, unsafeShiftR, (.&.), (.|.)) import qualified Data.ByteString as BS @@ -323,38 +325,15 @@ unSecret (Secret bs) = bs -- transaction types ----------------------------------------------------------- --- | A 32-byte transaction identifier. --- --- The double-SHA256 hash of a serialized transaction. -newtype TxId = TxId BS.ByteString - deriving stock (Eq, Ord, Show, Generic) - deriving newtype NFData - --- | Construct a 'TxId' from a 32-byte 'BS.ByteString'. --- --- Returns 'Nothing' if the input is not exactly 32 bytes. -txId :: BS.ByteString -> Maybe TxId -txId !bs - | BS.length bs == txIdLen = Just $! TxId bs - | otherwise = Nothing -{-# INLINABLE txId #-} +-- orphan NFData instances for ppad-tx types +instance NFData TxId +instance NFData OutPoint -- | Extract the underlying 'BS.ByteString' from a 'TxId'. unTxId :: TxId -> BS.ByteString unTxId (TxId bs) = bs {-# INLINE unTxId #-} --- | A transaction outpoint (txid + output index). --- --- Identifies a specific output of a transaction. -data Outpoint = Outpoint - { outpointTxId :: {-# UNPACK #-} !TxId - , outpointVout :: {-# UNPACK #-} !Word32 - } - deriving stock (Eq, Ord, Show, Generic) - -instance NFData Outpoint - -- | A script pubkey (output script). -- -- Variable length; used in shutdown messages, closing transactions, etc. diff --git a/test/Main.hs b/test/Main.hs @@ -47,7 +47,7 @@ testSignature = fromJust $ signature (BS.replicate 64 0xcc) -- | Create a valid TxId (32 bytes). testTxId :: TxId -testTxId = fromJust $ txId (BS.replicate 32 0xdd) +testTxId = fromJust $ mkTxId (BS.replicate 32 0xdd) -- | Create a valid PaymentHash (32 bytes). testPaymentHash :: PaymentHash