{-# OPTIONS_HADDOCK hide #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE RecordWildCards #-}

-- |
-- Module: Bitcoin.Prim.Tx.Internal
-- Copyright: (c) 2025 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@ppad.tech>
--
-- Transaction types and serialisation builders, shared by the public
-- modules.

module Bitcoin.Prim.Tx.Internal (
    -- * Transaction types
    TxId(..)
  , OutPoint(..)
  , TxIn(..)
  , TxOut(..)
  , Witness(..)
  , Tx(..)

    -- * Builders
  , to_strict
  , put_word32_le
  , put_word64_le
  , put_compact
  , put_bytes
  , put_outpoint
  , put_txin
  , put_txout
  , put_witness
  ) where

import Control.DeepSeq (NFData(..))
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Lazy as BL
import Data.List.NonEmpty (NonEmpty(..))
import Data.Word (Word32, Word64)
import GHC.Generics (Generic)

-- types ----------------------------------------------------------------------

-- | Transaction ID: 32 bytes, the double-SHA256 of the transaction's
--   legacy serialisation, in internal byte order (the reverse of the
--   usual hex display).
newtype TxId = TxId BS.ByteString
  deriving (TxId -> TxId -> Bool
(TxId -> TxId -> Bool) -> (TxId -> TxId -> Bool) -> Eq TxId
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxId -> TxId -> Bool
== :: TxId -> TxId -> Bool
$c/= :: TxId -> TxId -> Bool
/= :: TxId -> TxId -> Bool
Eq, Eq TxId
Eq TxId =>
(TxId -> TxId -> Ordering)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> Bool)
-> (TxId -> TxId -> TxId)
-> (TxId -> TxId -> TxId)
-> Ord TxId
TxId -> TxId -> Bool
TxId -> TxId -> Ordering
TxId -> TxId -> TxId
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: TxId -> TxId -> Ordering
compare :: TxId -> TxId -> Ordering
$c< :: TxId -> TxId -> Bool
< :: TxId -> TxId -> Bool
$c<= :: TxId -> TxId -> Bool
<= :: TxId -> TxId -> Bool
$c> :: TxId -> TxId -> Bool
> :: TxId -> TxId -> Bool
$c>= :: TxId -> TxId -> Bool
>= :: TxId -> TxId -> Bool
$cmax :: TxId -> TxId -> TxId
max :: TxId -> TxId -> TxId
$cmin :: TxId -> TxId -> TxId
min :: TxId -> TxId -> TxId
Ord, Int -> TxId -> ShowS
[TxId] -> ShowS
TxId -> String
(Int -> TxId -> ShowS)
-> (TxId -> String) -> ([TxId] -> ShowS) -> Show TxId
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxId -> ShowS
showsPrec :: Int -> TxId -> ShowS
$cshow :: TxId -> String
show :: TxId -> String
$cshowList :: [TxId] -> ShowS
showList :: [TxId] -> ShowS
Show, (forall x. TxId -> Rep TxId x)
-> (forall x. Rep TxId x -> TxId) -> Generic TxId
forall x. Rep TxId x -> TxId
forall x. TxId -> Rep TxId x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxId -> Rep TxId x
from :: forall x. TxId -> Rep TxId x
$cto :: forall x. Rep TxId x -> TxId
to :: forall x. Rep TxId x -> TxId
Generic)

instance NFData TxId

-- | Transaction outpoint (txid + output index).
data OutPoint = OutPoint
  { OutPoint -> TxId
op_txid  :: {-# UNPACK #-} !TxId
  , OutPoint -> Word32
op_vout  :: {-# UNPACK #-} !Word32
  } deriving (OutPoint -> OutPoint -> Bool
(OutPoint -> OutPoint -> Bool)
-> (OutPoint -> OutPoint -> Bool) -> Eq OutPoint
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: OutPoint -> OutPoint -> Bool
== :: OutPoint -> OutPoint -> Bool
$c/= :: OutPoint -> OutPoint -> Bool
/= :: OutPoint -> OutPoint -> Bool
Eq, Eq OutPoint
Eq OutPoint =>
(OutPoint -> OutPoint -> Ordering)
-> (OutPoint -> OutPoint -> Bool)
-> (OutPoint -> OutPoint -> Bool)
-> (OutPoint -> OutPoint -> Bool)
-> (OutPoint -> OutPoint -> Bool)
-> (OutPoint -> OutPoint -> OutPoint)
-> (OutPoint -> OutPoint -> OutPoint)
-> Ord OutPoint
OutPoint -> OutPoint -> Bool
OutPoint -> OutPoint -> Ordering
OutPoint -> OutPoint -> OutPoint
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: OutPoint -> OutPoint -> Ordering
compare :: OutPoint -> OutPoint -> Ordering
$c< :: OutPoint -> OutPoint -> Bool
< :: OutPoint -> OutPoint -> Bool
$c<= :: OutPoint -> OutPoint -> Bool
<= :: OutPoint -> OutPoint -> Bool
$c> :: OutPoint -> OutPoint -> Bool
> :: OutPoint -> OutPoint -> Bool
$c>= :: OutPoint -> OutPoint -> Bool
>= :: OutPoint -> OutPoint -> Bool
$cmax :: OutPoint -> OutPoint -> OutPoint
max :: OutPoint -> OutPoint -> OutPoint
$cmin :: OutPoint -> OutPoint -> OutPoint
min :: OutPoint -> OutPoint -> OutPoint
Ord, Int -> OutPoint -> ShowS
[OutPoint] -> ShowS
OutPoint -> String
(Int -> OutPoint -> ShowS)
-> (OutPoint -> String) -> ([OutPoint] -> ShowS) -> Show OutPoint
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> OutPoint -> ShowS
showsPrec :: Int -> OutPoint -> ShowS
$cshow :: OutPoint -> String
show :: OutPoint -> String
$cshowList :: [OutPoint] -> ShowS
showList :: [OutPoint] -> ShowS
Show, (forall x. OutPoint -> Rep OutPoint x)
-> (forall x. Rep OutPoint x -> OutPoint) -> Generic OutPoint
forall x. Rep OutPoint x -> OutPoint
forall x. OutPoint -> Rep OutPoint x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. OutPoint -> Rep OutPoint x
from :: forall x. OutPoint -> Rep OutPoint x
$cto :: forall x. Rep OutPoint x -> OutPoint
to :: forall x. Rep OutPoint x -> OutPoint
Generic)

instance NFData OutPoint

-- | Witness stack for a single input (empty for a non-witness input).
newtype Witness = Witness [BS.ByteString]
  deriving (Witness -> Witness -> Bool
(Witness -> Witness -> Bool)
-> (Witness -> Witness -> Bool) -> Eq Witness
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Witness -> Witness -> Bool
== :: Witness -> Witness -> Bool
$c/= :: Witness -> Witness -> Bool
/= :: Witness -> Witness -> Bool
Eq, Int -> Witness -> ShowS
[Witness] -> ShowS
Witness -> String
(Int -> Witness -> ShowS)
-> (Witness -> String) -> ([Witness] -> ShowS) -> Show Witness
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Witness -> ShowS
showsPrec :: Int -> Witness -> ShowS
$cshow :: Witness -> String
show :: Witness -> String
$cshowList :: [Witness] -> ShowS
showList :: [Witness] -> ShowS
Show, (forall x. Witness -> Rep Witness x)
-> (forall x. Rep Witness x -> Witness) -> Generic Witness
forall x. Rep Witness x -> Witness
forall x. Witness -> Rep Witness x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Witness -> Rep Witness x
from :: forall x. Witness -> Rep Witness x
$cto :: forall x. Rep Witness x -> Witness
to :: forall x. Rep Witness x -> Witness
Generic)

instance NFData Witness

-- | Transaction input.
data TxIn = TxIn
  { TxIn -> OutPoint
txin_prevout    :: {-# UNPACK #-} !OutPoint
  , TxIn -> ByteString
txin_script_sig :: !BS.ByteString
  , TxIn -> Word32
txin_sequence   :: {-# UNPACK #-} !Word32
  , TxIn -> Witness
txin_witness    :: !Witness
  } deriving (TxIn -> TxIn -> Bool
(TxIn -> TxIn -> Bool) -> (TxIn -> TxIn -> Bool) -> Eq TxIn
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxIn -> TxIn -> Bool
== :: TxIn -> TxIn -> Bool
$c/= :: TxIn -> TxIn -> Bool
/= :: TxIn -> TxIn -> Bool
Eq, Int -> TxIn -> ShowS
[TxIn] -> ShowS
TxIn -> String
(Int -> TxIn -> ShowS)
-> (TxIn -> String) -> ([TxIn] -> ShowS) -> Show TxIn
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxIn -> ShowS
showsPrec :: Int -> TxIn -> ShowS
$cshow :: TxIn -> String
show :: TxIn -> String
$cshowList :: [TxIn] -> ShowS
showList :: [TxIn] -> ShowS
Show, (forall x. TxIn -> Rep TxIn x)
-> (forall x. Rep TxIn x -> TxIn) -> Generic TxIn
forall x. Rep TxIn x -> TxIn
forall x. TxIn -> Rep TxIn x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxIn -> Rep TxIn x
from :: forall x. TxIn -> Rep TxIn x
$cto :: forall x. Rep TxIn x -> TxIn
to :: forall x. Rep TxIn x -> TxIn
Generic)

instance NFData TxIn

-- | Transaction output.
data TxOut = TxOut
  { TxOut -> Word64
txout_value         :: {-# UNPACK #-} !Word64  -- ^ satoshis
  , TxOut -> ByteString
txout_script_pubkey :: !BS.ByteString
  } deriving (TxOut -> TxOut -> Bool
(TxOut -> TxOut -> Bool) -> (TxOut -> TxOut -> Bool) -> Eq TxOut
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: TxOut -> TxOut -> Bool
== :: TxOut -> TxOut -> Bool
$c/= :: TxOut -> TxOut -> Bool
/= :: TxOut -> TxOut -> Bool
Eq, Int -> TxOut -> ShowS
[TxOut] -> ShowS
TxOut -> String
(Int -> TxOut -> ShowS)
-> (TxOut -> String) -> ([TxOut] -> ShowS) -> Show TxOut
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> TxOut -> ShowS
showsPrec :: Int -> TxOut -> ShowS
$cshow :: TxOut -> String
show :: TxOut -> String
$cshowList :: [TxOut] -> ShowS
showList :: [TxOut] -> ShowS
Show, (forall x. TxOut -> Rep TxOut x)
-> (forall x. Rep TxOut x -> TxOut) -> Generic TxOut
forall x. Rep TxOut x -> TxOut
forall x. TxOut -> Rep TxOut x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. TxOut -> Rep TxOut x
from :: forall x. TxOut -> Rep TxOut x
$cto :: forall x. Rep TxOut x -> TxOut
to :: forall x. Rep TxOut x -> TxOut
Generic)

instance NFData TxOut

-- | Complete transaction.
--
--   Bitcoin requires at least one input and one output, enforced here
--   via 'NonEmpty' lists. Each input carries its own witness, so a
--   transaction is serialised in the segwit format exactly when some
--   input's witness is non-empty.
data Tx = Tx
  { Tx -> Word32
tx_version   :: {-# UNPACK #-} !Word32
  , Tx -> NonEmpty TxIn
tx_inputs    :: !(NonEmpty TxIn)
  , Tx -> NonEmpty TxOut
tx_outputs   :: !(NonEmpty TxOut)
  , Tx -> Word32
tx_locktime  :: {-# UNPACK #-} !Word32
  } deriving (Tx -> Tx -> Bool
(Tx -> Tx -> Bool) -> (Tx -> Tx -> Bool) -> Eq Tx
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Tx -> Tx -> Bool
== :: Tx -> Tx -> Bool
$c/= :: Tx -> Tx -> Bool
/= :: Tx -> Tx -> Bool
Eq, Int -> Tx -> ShowS
[Tx] -> ShowS
Tx -> String
(Int -> Tx -> ShowS)
-> (Tx -> String) -> ([Tx] -> ShowS) -> Show Tx
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Tx -> ShowS
showsPrec :: Int -> Tx -> ShowS
$cshow :: Tx -> String
show :: Tx -> String
$cshowList :: [Tx] -> ShowS
showList :: [Tx] -> ShowS
Show, (forall x. Tx -> Rep Tx x)
-> (forall x. Rep Tx x -> Tx) -> Generic Tx
forall x. Rep Tx x -> Tx
forall x. Tx -> Rep Tx x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Tx -> Rep Tx x
from :: forall x. Tx -> Rep Tx x
$cto :: forall x. Rep Tx x -> Tx
to :: forall x. Rep Tx x -> Tx
Generic)

instance NFData Tx

-- builders -------------------------------------------------------------------

-- | Convert a Builder to a strict ByteString.
to_strict :: BSB.Builder -> BS.ByteString
to_strict :: Builder -> ByteString
to_strict = LazyByteString -> ByteString
BL.toStrict (LazyByteString -> ByteString)
-> (Builder -> LazyByteString) -> Builder -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> LazyByteString
BSB.toLazyByteString
{-# INLINE to_strict #-}

-- | Encode a Word32 as little-endian bytes.
put_word32_le :: Word32 -> BSB.Builder
put_word32_le :: Word32 -> Builder
put_word32_le = Word32 -> Builder
BSB.word32LE
{-# INLINE put_word32_le #-}

-- | Encode a Word64 as little-endian bytes.
put_word64_le :: Word64 -> BSB.Builder
put_word64_le :: Word64 -> Builder
put_word64_le = Word64 -> Builder
BSB.word64LE
{-# INLINE put_word64_le #-}

-- | Encode a Word64 as Bitcoin compactSize (varint).
--
--   Encoding:
--   - 0x00-0xfc: 1 byte (value itself)
--   - 0xfd-0xffff: 0xfd ++ 2 bytes LE
--   - 0x10000-0xffffffff: 0xfe ++ 4 bytes LE
--   - larger: 0xff ++ 8 bytes LE
put_compact :: Word64 -> BSB.Builder
put_compact :: Word64 -> Builder
put_compact !Word64
n
    | Word64
n Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xfc       = Word8 -> Builder
BSB.word8 (Word64 -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
    | Word64
n Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xffff     = Word8 -> Builder
BSB.word8 Word8
0xfd Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word16 -> Builder
BSB.word16LE (Word64 -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
    | Word64
n Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
<= Word64
0xffffffff = Word8 -> Builder
BSB.word8 Word8
0xfe Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word32 -> Builder
BSB.word32LE (Word64 -> Word32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word64
n)
    | Bool
otherwise       = Word8 -> Builder
BSB.word8 Word8
0xff Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word64 -> Builder
BSB.word64LE Word64
n
{-# INLINE put_compact #-}

-- | Encode compactSize-length-prefixed bytes (Bitcoin @ser_string@).
put_bytes :: BS.ByteString -> BSB.Builder
put_bytes :: ByteString -> Builder
put_bytes !ByteString
bs =
     Word64 -> Builder
put_compact (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString -> Int
BS.length ByteString
bs))
  Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
BSB.byteString ByteString
bs
{-# INLINE put_bytes #-}

-- | Encode an OutPoint (txid + vout).
put_outpoint :: OutPoint -> BSB.Builder
put_outpoint :: OutPoint -> Builder
put_outpoint OutPoint {Word32
TxId
op_txid :: OutPoint -> TxId
op_vout :: OutPoint -> Word32
op_txid :: TxId
op_vout :: Word32
..} =
    let !(TxId !ByteString
txid_bs) = TxId
op_txid
    in  ByteString -> Builder
BSB.byteString ByteString
txid_bs Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word32 -> Builder
put_word32_le Word32
op_vout
{-# INLINE put_outpoint #-}

-- | Encode a TxIn (without its witness, which is serialised
--   separately).
put_txin :: TxIn -> BSB.Builder
put_txin :: TxIn -> Builder
put_txin TxIn {Word32
ByteString
Witness
OutPoint
txin_prevout :: TxIn -> OutPoint
txin_script_sig :: TxIn -> ByteString
txin_sequence :: TxIn -> Word32
txin_witness :: TxIn -> Witness
txin_prevout :: OutPoint
txin_script_sig :: ByteString
txin_sequence :: Word32
txin_witness :: Witness
..} =
       OutPoint -> Builder
put_outpoint OutPoint
txin_prevout
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
put_bytes ByteString
txin_script_sig
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Word32 -> Builder
put_word32_le Word32
txin_sequence
{-# INLINE put_txin #-}

-- | Encode a TxOut.
put_txout :: TxOut -> BSB.Builder
put_txout :: TxOut -> Builder
put_txout TxOut {Word64
ByteString
txout_value :: TxOut -> Word64
txout_script_pubkey :: TxOut -> ByteString
txout_value :: Word64
txout_script_pubkey :: ByteString
..} =
       Word64 -> Builder
put_word64_le Word64
txout_value
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> ByteString -> Builder
put_bytes ByteString
txout_script_pubkey
{-# INLINE put_txout #-}

-- | Encode a Witness stack.
put_witness :: Witness -> BSB.Builder
put_witness :: Witness -> Builder
put_witness (Witness [ByteString]
items) =
       Word64 -> Builder
put_compact (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
fromIntegral ([ByteString] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [ByteString]
items))
    Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (ByteString -> Builder) -> [ByteString] -> Builder
forall m a. Monoid m => (a -> m) -> [a] -> m
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap ByteString -> Builder
put_bytes [ByteString]
items
{-# INLINE put_witness #-}