| 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_witnesses :: ![Witness]
- tx_locktime :: !Word32
- data TxIn = TxIn {}
- data TxOut = TxOut {}
- data OutPoint = OutPoint {}
- newtype Witness = Witness [ByteString]
- newtype TxId = TxId ByteString
- mkTxId :: ByteString -> Maybe 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
- put_word32_le :: Word32 -> Builder
- put_word64_le :: Word64 -> Builder
- put_compact :: Word64 -> Builder
- put_outpoint :: OutPoint -> Builder
- put_txout :: TxOut -> Builder
- to_strict :: Builder -> ByteString
Transaction Types
Complete transaction.
Bitcoin requires at least one input and one output, enforced here
via NonEmpty lists.
Constructors
| Tx | |
Fields
| |
Instances
Transaction input.
Constructors
| TxIn | |
Fields
| |
Instances
| Generic TxIn Source # | |||||
Defined in Bitcoin.Prim.Tx Associated Types
| |||||
| Show TxIn Source # | |||||
| Eq TxIn Source # | |||||
| type Rep TxIn Source # | |||||
Defined in Bitcoin.Prim.Tx type Rep TxIn = D1 ('MetaData "TxIn" "Bitcoin.Prim.Tx" "ppad-tx-0.1.0-55gH9IEpqyC9H1txhCYBkH" '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)))) | |||||
Transaction output.
Constructors
| TxOut | |
Fields
| |
Instances
| Generic TxOut Source # | |||||
Defined in Bitcoin.Prim.Tx Associated Types
| |||||
| Show TxOut Source # | |||||
| Eq TxOut Source # | |||||
| type Rep TxOut Source # | |||||
Defined in Bitcoin.Prim.Tx type Rep TxOut = D1 ('MetaData "TxOut" "Bitcoin.Prim.Tx" "ppad-tx-0.1.0-55gH9IEpqyC9H1txhCYBkH" '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
| Generic OutPoint Source # | |||||
Defined in Bitcoin.Prim.Tx Associated Types
| |||||
| Show OutPoint Source # | |||||
| Eq OutPoint Source # | |||||
| type Rep OutPoint Source # | |||||
Defined in Bitcoin.Prim.Tx type Rep OutPoint = D1 ('MetaData "OutPoint" "Bitcoin.Prim.Tx" "ppad-tx-0.1.0-55gH9IEpqyC9H1txhCYBkH" '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.
Constructors
| Witness [ByteString] |
Instances
| Generic Witness Source # | |||||
Defined in Bitcoin.Prim.Tx Associated Types
| |||||
| Show Witness Source # | |||||
| Eq Witness Source # | |||||
| type Rep Witness Source # | |||||
Defined in Bitcoin.Prim.Tx type Rep Witness = D1 ('MetaData "Witness" "Bitcoin.Prim.Tx" "ppad-tx-0.1.0-55gH9IEpqyC9H1txhCYBkH" 'True) (C1 ('MetaCons "Witness" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 [ByteString]))) | |||||
Transaction ID (32 bytes, little-endian double-SHA256).
Constructors
| TxId ByteString |
Instances
| Generic TxId Source # | |||||
Defined in Bitcoin.Prim.Tx Associated Types
| |||||
| Show TxId Source # | |||||
| Eq TxId Source # | |||||
| type Rep TxId Source # | |||||
Defined in Bitcoin.Prim.Tx type Rep TxId = D1 ('MetaData "TxId" "Bitcoin.Prim.Tx" "ppad-tx-0.1.0-55gH9IEpqyC9H1txhCYBkH" 'True) (C1 ('MetaCons "TxId" 'PrefixI 'False) (S1 ('MetaSel ('Nothing :: Maybe Symbol) 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 ByteString))) | |||||
mkTxId :: ByteString -> Maybe TxId Source #
Construct a TxId from a 32-byte ByteString.
Returns Nothing if the input is not exactly 32 bytes.
mkTxId (BS.replicate 32 0x00) == Just (TxId ...) mkTxId (BS.replicate 31 0x00) == Nothing
Serialisation
to_bytes :: Tx -> ByteString Source #
Serialise a transaction to bytes.
Uses segwit format if witnesses are present, legacy otherwise.
-- 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 legacy tx (no witnesses), same as to_bytes to_bytes_legacy legacyTx == to_bytes legacyTx -- for segwit tx, strips witnesses BS.length (to_bytes_legacy segwitTx) < BS.length (to_bytes segwitTx)
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.
-- Satoshi->Hal tx (block 170)
txid satoshiHalTx ==
TxId "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
Internal (for Sighash)
put_word32_le :: Word32 -> Builder Source #
Encode a Word32 as little-endian bytes.
put_word64_le :: Word64 -> Builder Source #
Encode a Word64 as little-endian bytes.
put_compact :: Word64 -> Builder Source #
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_outpoint :: OutPoint -> Builder Source #
Encode an OutPoint (txid + vout).
to_strict :: Builder -> ByteString Source #
Convert a Builder to a strict ByteString.