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

Bitcoin.Prim.Tx.Sighash

Description

Sighash computation for legacy and BIP143 segwit transactions.

Synopsis

Sighash Types

data SighashType Source #

Sighash type flags.

Instances

Instances details
Generic SighashType Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Sighash

Associated Types

type Rep SighashType 
Instance details

Defined in Bitcoin.Prim.Tx.Sighash

type Rep SighashType = D1 ('MetaData "SighashType" "Bitcoin.Prim.Tx.Sighash" "ppad-tx-0.1.0-55gH9IEpqyC9H1txhCYBkH" 'False) ((C1 ('MetaCons "SIGHASH_ALL" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "SIGHASH_NONE" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "SIGHASH_SINGLE" 'PrefixI 'False) (U1 :: Type -> Type))) :+: (C1 ('MetaCons "SIGHASH_ALL_ANYONECANPAY" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "SIGHASH_NONE_ANYONECANPAY" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "SIGHASH_SINGLE_ANYONECANPAY" 'PrefixI 'False) (U1 :: Type -> Type))))
Show SighashType Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Sighash

Eq SighashType Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Sighash

type Rep SighashType Source # 
Instance details

Defined in Bitcoin.Prim.Tx.Sighash

type Rep SighashType = D1 ('MetaData "SighashType" "Bitcoin.Prim.Tx.Sighash" "ppad-tx-0.1.0-55gH9IEpqyC9H1txhCYBkH" 'False) ((C1 ('MetaCons "SIGHASH_ALL" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "SIGHASH_NONE" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "SIGHASH_SINGLE" 'PrefixI 'False) (U1 :: Type -> Type))) :+: (C1 ('MetaCons "SIGHASH_ALL_ANYONECANPAY" 'PrefixI 'False) (U1 :: Type -> Type) :+: (C1 ('MetaCons "SIGHASH_NONE_ANYONECANPAY" 'PrefixI 'False) (U1 :: Type -> Type) :+: C1 ('MetaCons "SIGHASH_SINGLE_ANYONECANPAY" 'PrefixI 'False) (U1 :: Type -> Type))))

Legacy Sighash

sighash_legacy Source #

Arguments

:: Tx 
-> Int

input index

-> ByteString

scriptPubKey being spent

-> SighashType 
-> ByteString

32-byte hash

Compute legacy sighash for P2PKH/P2SH inputs.

Modifies a copy of the transaction based on sighash flags, appends the sighash type as 4-byte little-endian, and double SHA256s.

  -- sign input 0 with SIGHASH_ALL
  let hash = sighash_legacy tx 0 scriptPubKey SIGHASH_ALL
  -- use hash with ECDSA signing
  

For SIGHASH_SINGLE with input index >= output count, returns the special "sighash single bug" value (0x01 followed by 31 zero bytes).

BIP143 Segwit Sighash

sighash_segwit Source #

Arguments

:: Tx 
-> Int

input index

-> ByteString

scriptCode

-> Word64

value being spent (satoshis)

-> SighashType 
-> Maybe ByteString

32-byte hash, or Nothing if index invalid

Compute BIP143 segwit sighash.

Required for signing segwit inputs (P2WPKH, P2WSH). Unlike legacy sighash, this commits to the value being spent, preventing fee manipulation attacks.

Returns Nothing if the input index is out of range.

  -- sign P2WPKH input 0
  let scriptCode = ...  -- P2WPKH scriptCode
  let hash = sighash_segwit tx 0 scriptCode inputValue SIGHASH_ALL
  -- use hash with ECDSA signing (after checking Just)