ppad-tx-0.2.0: Minimal Bitcoin transaction primitives
Copyright(c) 2025 Jared Tobin
LicenseMIT
MaintainerJared Tobin <jared@ppad.tech>
Safe HaskellNone
LanguageHaskell2010

Bitcoin.Prim.Tx

Description

Minimal Bitcoin transaction primitives, including raw transaction types, serialisation to/from bytes, and txid computation.

Synopsis

Transaction Types

data Tx Source #

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.

Constructors

Tx 

Instances

Instances details
NFData Tx Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

rnf :: Tx -> () #

Generic Tx Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Associated Types

type Rep Tx 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep Tx = D1 ('MetaData "Tx" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "Tx" 'PrefixI 'True) ((S1 ('MetaSel ('Just "tx_version") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32) :*: S1 ('MetaSel ('Just "tx_inputs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (NonEmpty TxIn))) :*: (S1 ('MetaSel ('Just "tx_outputs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (NonEmpty TxOut)) :*: S1 ('MetaSel ('Just "tx_locktime") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32))))

Methods

from :: Tx -> Rep Tx x #

to :: Rep Tx x -> Tx #

Show Tx Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

showsPrec :: Int -> Tx -> ShowS #

show :: Tx -> String #

showList :: [Tx] -> ShowS #

Eq Tx Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

(==) :: Tx -> Tx -> Bool #

(/=) :: Tx -> Tx -> Bool #

type Rep Tx Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep Tx = D1 ('MetaData "Tx" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "Tx" 'PrefixI 'True) ((S1 ('MetaSel ('Just "tx_version") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32) :*: S1 ('MetaSel ('Just "tx_inputs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (NonEmpty TxIn))) :*: (S1 ('MetaSel ('Just "tx_outputs") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (NonEmpty TxOut)) :*: S1 ('MetaSel ('Just "tx_locktime") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32))))

data TxIn Source #

Transaction input.

Instances

Instances details
NFData TxIn Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

rnf :: TxIn -> () #

Generic TxIn Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Associated Types

type Rep TxIn 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep TxIn = D1 ('MetaData "TxIn" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "TxIn" 'PrefixI 'True) ((S1 ('MetaSel ('Just "txin_prevout") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 OutPoint) :*: S1 ('MetaSel ('Just "txin_script_sig") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString)) :*: (S1 ('MetaSel ('Just "txin_sequence") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32) :*: S1 ('MetaSel ('Just "txin_witness") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Witness))))

Methods

from :: TxIn -> Rep TxIn x #

to :: Rep TxIn x -> TxIn #

Show TxIn Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

showsPrec :: Int -> TxIn -> ShowS #

show :: TxIn -> String #

showList :: [TxIn] -> ShowS #

Eq TxIn Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

(==) :: TxIn -> TxIn -> Bool #

(/=) :: TxIn -> TxIn -> Bool #

type Rep TxIn Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep TxIn = D1 ('MetaData "TxIn" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "TxIn" 'PrefixI 'True) ((S1 ('MetaSel ('Just "txin_prevout") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 OutPoint) :*: S1 ('MetaSel ('Just "txin_script_sig") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString)) :*: (S1 ('MetaSel ('Just "txin_sequence") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32) :*: S1 ('MetaSel ('Just "txin_witness") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Witness))))

data TxOut Source #

Transaction output.

Constructors

TxOut 

Instances

Instances details
NFData TxOut Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

rnf :: TxOut -> () #

Generic TxOut Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Associated Types

type Rep TxOut 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep TxOut = D1 ('MetaData "TxOut" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "TxOut" 'PrefixI 'True) (S1 ('MetaSel ('Just "txout_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word64) :*: S1 ('MetaSel ('Just "txout_script_pubkey") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString)))

Methods

from :: TxOut -> Rep TxOut x #

to :: Rep TxOut x -> TxOut #

Show TxOut Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

showsPrec :: Int -> TxOut -> ShowS #

show :: TxOut -> String #

showList :: [TxOut] -> ShowS #

Eq TxOut Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

(==) :: TxOut -> TxOut -> Bool #

(/=) :: TxOut -> TxOut -> Bool #

type Rep TxOut Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep TxOut = D1 ('MetaData "TxOut" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "TxOut" 'PrefixI 'True) (S1 ('MetaSel ('Just "txout_value") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word64) :*: S1 ('MetaSel ('Just "txout_script_pubkey") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ByteString)))

data OutPoint Source #

Transaction outpoint (txid + output index).

Constructors

OutPoint 

Fields

Instances

Instances details
NFData OutPoint Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

rnf :: OutPoint -> () #

Generic OutPoint Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Associated Types

type Rep OutPoint 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep OutPoint = D1 ('MetaData "OutPoint" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "OutPoint" 'PrefixI 'True) (S1 ('MetaSel ('Just "op_txid") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 TxId) :*: S1 ('MetaSel ('Just "op_vout") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32)))

Methods

from :: OutPoint -> Rep OutPoint x #

to :: Rep OutPoint x -> OutPoint #

Show OutPoint Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Eq OutPoint Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Ord OutPoint Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep OutPoint Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep OutPoint = D1 ('MetaData "OutPoint" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'False) (C1 ('MetaCons "OutPoint" 'PrefixI 'True) (S1 ('MetaSel ('Just "op_txid") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 TxId) :*: S1 ('MetaSel ('Just "op_vout") 'SourceUnpack 'SourceStrict 'DecidedStrict) (Rec0 Word32)))

newtype Witness Source #

Witness stack for a single input (empty for a non-witness input).

Constructors

Witness [ByteString] 

Instances

Instances details
NFData Witness Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

rnf :: Witness -> () #

Generic Witness Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Associated Types

type Rep Witness 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep Witness = D1 ('MetaData "Witness" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'True) (C1 ('MetaCons "Witness" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [ByteString])))

Methods

from :: Witness -> Rep Witness x #

to :: Rep Witness x -> Witness #

Show Witness Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Eq Witness Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

(==) :: Witness -> Witness -> Bool #

(/=) :: Witness -> Witness -> Bool #

type Rep Witness Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep Witness = D1 ('MetaData "Witness" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'True) (C1 ('MetaCons "Witness" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [ByteString])))

data TxId Source #

Transaction ID: 32 bytes, the double-SHA256 of the transaction's legacy serialisation, in internal byte order (the reverse of the usual hex display).

Instances

Instances details
NFData TxId Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

rnf :: TxId -> () #

Generic TxId Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Associated Types

type Rep TxId 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep TxId = D1 ('MetaData "TxId" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'True) (C1 ('MetaCons "TxId" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString)))

Methods

from :: TxId -> Rep TxId x #

to :: Rep TxId x -> TxId #

Show TxId Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

showsPrec :: Int -> TxId -> ShowS #

show :: TxId -> String #

showList :: [TxId] -> ShowS #

Eq TxId Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

(==) :: TxId -> TxId -> Bool #

(/=) :: TxId -> TxId -> Bool #

Ord TxId Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

Methods

compare :: TxId -> TxId -> Ordering #

(<) :: TxId -> TxId -> Bool #

(<=) :: TxId -> TxId -> Bool #

(>) :: TxId -> TxId -> Bool #

(>=) :: TxId -> TxId -> Bool #

max :: TxId -> TxId -> TxId #

min :: TxId -> TxId -> TxId #

type Rep TxId Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Internal

type Rep TxId = D1 ('MetaData "TxId" "Bitcoin.Prim.Tx.Internal" "ppad-tx-0.2.0-JDyfU3sNiRYLsZQB8ASOVt" 'True) (C1 ('MetaCons "TxId" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString)))

mk_txid :: ByteString -> Maybe TxId Source #

Construct a TxId from 32 bytes in internal byte order (the reverse of the usual hex display).

Returns Nothing if the input is not exactly 32 bytes.

>>> fmap (BS.length . un_txid) (mk_txid (BS.replicate 32 0x00))
Just 32
>>> mk_txid (BS.replicate 31 0x00)
Nothing

un_txid :: TxId -> ByteString Source #

The 32 bytes of a TxId, in internal byte order.

null_txid :: TxId Source #

The all-zero TxId, referenced by coinbase inputs' outpoints.

Serialisation

to_bytes :: Tx -> ByteString Source #

Serialise a transaction to bytes.

Uses the segwit format if any input has a non-empty witness, and the legacy format otherwise (as Bitcoin Core does).

  -- round-trip
  from_bytes (to_bytes tx) == Just tx
  

from_bytes :: ByteString -> Maybe Tx Source #

Parse a transaction from bytes.

Automatically detects segwit vs legacy format by checking for marker byte 0x00 followed by flag 0x01 after the version field.

Returns Nothing on invalid or truncated input.

  -- round-trip
  from_bytes (to_bytes tx) == Just tx
  

to_bytes_legacy :: Tx -> ByteString Source #

Serialise a transaction to legacy format (no witness data).

Used for txid computation. Excludes witness data even if present.

  -- for a legacy tx (no witnesses), the same as to_bytes
  to_bytes_legacy legacy_tx == to_bytes legacy_tx

  -- for a segwit tx, strips the witnesses
  BS.length (to_bytes_legacy segwit_tx) < BS.length (to_bytes segwit_tx)
  

to_base16 :: Tx -> ByteString Source #

Serialise a transaction to base16 (hex).

  to_base16 tx = B16.encode (to_bytes tx)
  

from_base16 :: ByteString -> Maybe Tx Source #

Parse a transaction from base16 (hex).

  -- round-trip
  from_base16 (to_base16 tx) == Just tx
  

TxId

txid :: Tx -> TxId Source #

Compute the transaction ID (double SHA256 of legacy serialisation).

The txid is computed from the legacy serialisation, so segwit transactions have the same txid regardless of witness data. It is in internal byte order; reverse it for the usual hex display.

  -- Satoshi->Hal tx (block 170)
  B16.encode (BS.reverse (un_txid (txid satoshi_hal_tx)))
    == "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"