| Copyright | (c) 2025 Jared Tobin |
|---|---|
| License | MIT |
| Maintainer | Jared Tobin <jared@ppad.tech> |
| Safe Haskell | None |
| Language | Haskell2010 |
Bitcoin.Prim.Tx
Description
Minimal Bitcoin transaction primitives, including raw transaction types, serialisation to/from bytes, and txid computation.
Synopsis
- data Tx = Tx {
- tx_version :: !Word32
- tx_inputs :: !(NonEmpty TxIn)
- tx_outputs :: !(NonEmpty TxOut)
- tx_locktime :: !Word32
- data TxIn = TxIn {}
- data TxOut = TxOut {}
- data OutPoint = OutPoint {}
- newtype Witness = Witness [ByteString]
- data TxId
- mk_txid :: ByteString -> Maybe TxId
- un_txid :: TxId -> ByteString
- null_txid :: TxId
- to_bytes :: Tx -> ByteString
- from_bytes :: ByteString -> Maybe Tx
- to_bytes_legacy :: Tx -> ByteString
- to_base16 :: Tx -> ByteString
- from_base16 :: ByteString -> Maybe Tx
- txid :: Tx -> TxId
Transaction Types
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 | |
Fields
| |
Instances
Transaction input.
Constructors
| TxIn | |
Fields
| |
Instances
| NFData TxIn Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal | |||||
| Generic TxIn Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal Associated Types
| |||||
| Show TxIn Source # | |||||
| Eq TxIn Source # | |||||
| type Rep TxIn Source # | |||||
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)))) | |||||
Transaction output.
Constructors
| TxOut | |
Fields
| |
Instances
| NFData TxOut Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal | |||||
| Generic TxOut Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal Associated Types
| |||||
| Show TxOut Source # | |||||
| Eq TxOut Source # | |||||
| type Rep TxOut Source # | |||||
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))) | |||||
Transaction outpoint (txid + output index).
Instances
| NFData OutPoint Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal | |||||
| Generic OutPoint Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal Associated Types
| |||||
| Show OutPoint Source # | |||||
| Eq OutPoint Source # | |||||
| Ord OutPoint Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal | |||||
| type Rep OutPoint Source # | |||||
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))) | |||||
Witness stack for a single input (empty for a non-witness input).
Constructors
| Witness [ByteString] |
Instances
| NFData Witness Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal | |||||
| Generic Witness Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal Associated Types
| |||||
| Show Witness Source # | |||||
| Eq Witness Source # | |||||
| type Rep Witness Source # | |||||
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]))) | |||||
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
| NFData TxId Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal | |||||
| Generic TxId Source # | |||||
Defined in Bitcoin.Prim.Tx.Internal Associated Types
| |||||
| Show TxId Source # | |||||
| Eq TxId Source # | |||||
| Ord TxId Source # | |||||
| type Rep TxId Source # | |||||
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))) | |||||
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
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"