Copyright | No rights reserved |
---|---|
License | UNLICENSE |
Maintainer | xenog@protonmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
This module provides functions for parsing and evaluating bitcoin transaction scripts. Data types are provided for building and deconstructing all of the standard input and output script types.
Synopsis
- data ScriptInput
- data SimpleInput
- = SpendPK { }
- | SpendPKHash {
- getInputSig :: !TxSignature
- getInputKey :: !PubKeyI
- | SpendMulSig { }
- type RedeemScript = ScriptOutput
- encodeInput :: ScriptInput -> Script
- encodeInputBS :: ScriptInput -> ByteString
- decodeInput :: Network -> Script -> Either String ScriptInput
- decodeInputBS :: Network -> ByteString -> Either String ScriptInput
- sortMulSig :: ScriptOutput -> ScriptOutput
- scriptOpToInt :: ScriptOp -> Either String Int
- isSpendPK :: ScriptInput -> Bool
- isSpendPKHash :: ScriptInput -> Bool
- isSpendMulSig :: ScriptInput -> Bool
- isScriptHashInput :: ScriptInput -> Bool
- data SigHash
- data SigHashFlag
- sigHashAll :: SigHash
- sigHashNone :: SigHash
- sigHashSingle :: SigHash
- hasAnyoneCanPayFlag :: SigHash -> Bool
- hasForkIdFlag :: SigHash -> Bool
- setAnyoneCanPayFlag :: SigHash -> SigHash
- setForkIdFlag :: SigHash -> SigHash
- isSigHashAll :: SigHash -> Bool
- isSigHashNone :: SigHash -> Bool
- isSigHashSingle :: SigHash -> Bool
- isSigHashUnknown :: SigHash -> Bool
- sigHashAddForkId :: SigHash -> Word32 -> SigHash
- sigHashGetForkId :: SigHash -> Word32
- sigHashAddNetworkId :: Network -> SigHash -> SigHash
- txSigHash :: Network -> Tx -> Script -> Word64 -> Int -> SigHash -> Hash256
- txSigHashForkId :: Network -> Tx -> Script -> Word64 -> Int -> SigHash -> Hash256
- data TxSignature
- = TxSignature {
- txSignature :: !Sig
- txSignatureSigHash :: !SigHash
- | TxSignatureEmpty
- = TxSignature {
- encodeTxSig :: TxSignature -> ByteString
- decodeTxSig :: Network -> ByteString -> Either String TxSignature
- data ScriptOp
- = OP_PUSHDATA !ByteString !PushDataType
- | OP_0
- | OP_1NEGATE
- | OP_RESERVED
- | OP_1
- | OP_2
- | OP_3
- | OP_4
- | OP_5
- | OP_6
- | OP_7
- | OP_8
- | OP_9
- | OP_10
- | OP_11
- | OP_12
- | OP_13
- | OP_14
- | OP_15
- | OP_16
- | OP_NOP
- | OP_VER
- | OP_IF
- | OP_NOTIF
- | OP_VERIF
- | OP_VERNOTIF
- | OP_ELSE
- | OP_ENDIF
- | OP_VERIFY
- | OP_RETURN
- | OP_TOALTSTACK
- | OP_FROMALTSTACK
- | OP_IFDUP
- | OP_DEPTH
- | OP_DROP
- | OP_DUP
- | OP_NIP
- | OP_OVER
- | OP_PICK
- | OP_ROLL
- | OP_ROT
- | OP_SWAP
- | OP_TUCK
- | OP_2DROP
- | OP_2DUP
- | OP_3DUP
- | OP_2OVER
- | OP_2ROT
- | OP_2SWAP
- | OP_CAT
- | OP_SUBSTR
- | OP_LEFT
- | OP_RIGHT
- | OP_SIZE
- | OP_INVERT
- | OP_AND
- | OP_OR
- | OP_XOR
- | OP_EQUAL
- | OP_EQUALVERIFY
- | OP_RESERVED1
- | OP_RESERVED2
- | OP_1ADD
- | OP_1SUB
- | OP_2MUL
- | OP_2DIV
- | OP_NEGATE
- | OP_ABS
- | OP_NOT
- | OP_0NOTEQUAL
- | OP_ADD
- | OP_SUB
- | OP_MUL
- | OP_DIV
- | OP_MOD
- | OP_LSHIFT
- | OP_RSHIFT
- | OP_BOOLAND
- | OP_BOOLOR
- | OP_NUMEQUAL
- | OP_NUMEQUALVERIFY
- | OP_NUMNOTEQUAL
- | OP_LESSTHAN
- | OP_GREATERTHAN
- | OP_LESSTHANOREQUAL
- | OP_GREATERTHANOREQUAL
- | OP_MIN
- | OP_MAX
- | OP_WITHIN
- | OP_RIPEMD160
- | OP_SHA1
- | OP_SHA256
- | OP_HASH160
- | OP_HASH256
- | OP_CODESEPARATOR
- | OP_CHECKSIG
- | OP_CHECKSIGVERIFY
- | OP_CHECKMULTISIG
- | OP_CHECKMULTISIGVERIFY
- | OP_NOP1
- | OP_NOP2
- | OP_NOP3
- | OP_NOP4
- | OP_NOP5
- | OP_NOP6
- | OP_NOP7
- | OP_NOP8
- | OP_NOP9
- | OP_NOP10
- | OP_PUBKEYHASH
- | OP_PUBKEY
- | OP_INVALIDOPCODE !Word8
- newtype Script = Script {}
- data PushDataType
- data ScriptOutput
- = PayPK { }
- | PayPKHash {
- getOutputHash :: !Hash160
- | PayMulSig { }
- | PayScriptHash {
- getOutputHash :: !Hash160
- | PayWitnessPKHash {
- getOutputHash :: !Hash160
- | PayWitnessScriptHash {
- getScriptHash :: !Hash256
- | DataCarrier { }
- isPayPK :: ScriptOutput -> Bool
- isPayPKHash :: ScriptOutput -> Bool
- isPayMulSig :: ScriptOutput -> Bool
- isPayScriptHash :: ScriptOutput -> Bool
- isPayWitnessPKHash :: ScriptOutput -> Bool
- isPayWitnessScriptHash :: ScriptOutput -> Bool
- isDataCarrier :: ScriptOutput -> Bool
- encodeOutput :: ScriptOutput -> Script
- encodeOutputBS :: ScriptOutput -> ByteString
- decodeOutput :: Script -> Either String ScriptOutput
- decodeOutputBS :: ByteString -> Either String ScriptOutput
- isPushOp :: ScriptOp -> Bool
- opPushData :: ByteString -> ScriptOp
- intToScriptOp :: Int -> ScriptOp
- scriptOpToInt :: ScriptOp -> Either String Int
Documentation
data ScriptInput Source #
Standard input script high-level representation.
RegularInput | |
| |
ScriptHashInput | |
|
Instances
Eq ScriptInput Source # | |
Defined in Network.Haskoin.Script.Standard (==) :: ScriptInput -> ScriptInput -> Bool # (/=) :: ScriptInput -> ScriptInput -> Bool # | |
Show ScriptInput Source # | |
Defined in Network.Haskoin.Script.Standard showsPrec :: Int -> ScriptInput -> ShowS # show :: ScriptInput -> String # showList :: [ScriptInput] -> ShowS # |
data SimpleInput Source #
Data type describing standard transaction input scripts. Input scripts provide the signing data required to unlock the coins of the output they are trying to spend, except in pay-to-witness-public-key-hash and pay-to-script-hash transactions.
SpendPK | spend pay-to-public-key output |
| |
SpendPKHash | spend pay-to-public-key-hash output |
| |
SpendMulSig | spend multisig output |
|
Instances
Eq SimpleInput Source # | |
Defined in Network.Haskoin.Script.Standard (==) :: SimpleInput -> SimpleInput -> Bool # (/=) :: SimpleInput -> SimpleInput -> Bool # | |
Show SimpleInput Source # | |
Defined in Network.Haskoin.Script.Standard showsPrec :: Int -> SimpleInput -> ShowS # show :: SimpleInput -> String # showList :: [SimpleInput] -> ShowS # |
type RedeemScript = ScriptOutput Source #
A redeem script is the output script serialized into the spending input script. It must be included in inputs that spend pay-to-script-hash outputs.
encodeInput :: ScriptInput -> Script Source #
Encode a standard input into a script.
encodeInputBS :: ScriptInput -> ByteString Source #
Similar to encodeInput
but encodes directly to a serialized script
ByteString
.
decodeInput :: Network -> Script -> Either String ScriptInput Source #
Heuristic to decode a ScriptInput
from a Script
. This function fails if
the script can not be parsed as a standard script input.
decodeInputBS :: Network -> ByteString -> Either String ScriptInput Source #
Like decodeInput
but decodes directly from a serialized script
ByteString
.
sortMulSig :: ScriptOutput -> ScriptOutput Source #
Sort the public keys of a multisig output in ascending order by comparing their compressed serialized representations. Refer to BIP-67.
isSpendPK :: ScriptInput -> Bool Source #
Returns true if the input script is spending from a pay-to-public-key output.
isSpendPKHash :: ScriptInput -> Bool Source #
Returns true if the input script is spending from a pay-to-public-key-hash output.
isSpendMulSig :: ScriptInput -> Bool Source #
Returns true if the input script is spending a multisig output.
isScriptHashInput :: ScriptInput -> Bool Source #
Returns true if the input script is spending a pay-to-script-hash output.
Data type representing the different ways a transaction can be signed.
When producing a signature, a hash of the transaction is used as the message
to be signed. The SigHash
parameter controls which parts of the
transaction are used or ignored to produce the transaction hash. The idea is
that if some part of a transaction is not used to produce the transaction
hash, then you can change that part of the transaction after producing a
signature without invalidating that signature.
If the SIGHASH_ANYONECANPAY
flag is set (true), then only the current input
is signed. Otherwise, all of the inputs of a transaction are signed. The
default value for SIGHASH_ANYONECANPAY
is unset (false).
Instances
data SigHashFlag Source #
Constant representing a SIGHASH flag that controls what is being signed.
SIGHASH_ALL | sign all outputs |
SIGHASH_NONE | sign no outputs |
SIGHASH_SINGLE | sign the output index corresponding to the input |
SIGHASH_FORKID | replay protection for Bitcoin Cash transactions |
SIGHASH_ANYONECANPAY | new inputs can be added |
Instances
sigHashAll :: SigHash Source #
SIGHASH_ALL as a byte.
sigHashNone :: SigHash Source #
SIGHASH_NONE as a byte.
sigHashSingle :: SigHash Source #
SIGHASH_SINGLE as a byte.
hasAnyoneCanPayFlag :: SigHash -> Bool Source #
Is the SIGHASH_ANYONECANPAY flag set?
hasForkIdFlag :: SigHash -> Bool Source #
Is the SIGHASH_FORKID flag set?
setAnyoneCanPayFlag :: SigHash -> SigHash Source #
Set SIGHASH_ANYONECANPAY flag.
setForkIdFlag :: SigHash -> SigHash Source #
Set SIGHASH_FORKID flag.
isSigHashAll :: SigHash -> Bool Source #
Returns True
if the SigHash
has the value SIGHASH_ALL
.
isSigHashNone :: SigHash -> Bool Source #
Returns True
if the SigHash
has the value SIGHASH_NONE
.
isSigHashSingle :: SigHash -> Bool Source #
Returns True
if the SigHash
has the value SIGHASH_SINGLE
.
isSigHashUnknown :: SigHash -> Bool Source #
sigHashAddNetworkId :: Network -> SigHash -> SigHash Source #
Add fork id of a particular network to a SigHash
.
:: Network | |
-> Tx | transaction to sign |
-> Script | csript from output being spent |
-> Word64 | value of output being spent |
-> Int | index of input being signed |
-> SigHash | what to sign |
-> Hash256 | hash to be signed |
Computes the hash that will be used for signing a transaction.
:: Network | |
-> Tx | transaction to sign |
-> Script | script from output being spent |
-> Word64 | value of output being spent |
-> Int | index of input being signed |
-> SigHash | what to sign |
-> Hash256 | hash to be signed |
Compute the hash that will be used for signing a transaction. This
function is used when the SIGHASH_FORKID
flag is set.
data TxSignature Source #
Data type representing a signature together with a SigHash
. The SigHash
is serialized as one byte at the end of an ECDSA Sig
. All signatures in
transaction inputs are of type TxSignature
.
Instances
Eq TxSignature Source # | |
Defined in Network.Haskoin.Script.SigHash (==) :: TxSignature -> TxSignature -> Bool # (/=) :: TxSignature -> TxSignature -> Bool # | |
Show TxSignature Source # | |
Defined in Network.Haskoin.Script.SigHash showsPrec :: Int -> TxSignature -> ShowS # show :: TxSignature -> String # showList :: [TxSignature] -> ShowS # |
encodeTxSig :: TxSignature -> ByteString Source #
Serialize a TxSignature
.
decodeTxSig :: Network -> ByteString -> Either String TxSignature Source #
Deserialize a TxSignature
.
Data type representing an operator allowed inside a Script
.
Instances
Data type representing a transaction script. Scripts are defined as lists
of script operators ScriptOp
. Scripts are used to:
- Define the spending conditions in the output of a transaction.
- Provide signatures in the input of a transaction (except SegWit).
SigWit only: the segregated witness data structure, and not the input script, contains signatures and redeem script for pay-to-witness-script and pay-to-witness-public-key-hash transactions.
data PushDataType Source #
Data type representing the type of an OP_PUSHDATA opcode.
OPCODE | next opcode bytes is data to be pushed |
OPDATA1 | next byte contains number of bytes of data to be pushed |
OPDATA2 | next two bytes contains number of bytes to be pushed |
OPDATA4 | next four bytes contains the number of bytes to be pushed |
Instances
data ScriptOutput Source #
Data type describing standard transaction output scripts. Output scripts provide the conditions that must be fulfilled for someone to spend the funds in a transaction output.
PayPK | pay to public key |
PayPKHash | pay to public key hash |
| |
PayMulSig | multisig |
| |
PayScriptHash | pay to a script hash |
| |
PayWitnessPKHash | pay to witness public key hash |
| |
PayWitnessScriptHash | pay to witness script hash |
| |
DataCarrier | provably unspendable data carrier |
Instances
isPayPK :: ScriptOutput -> Bool Source #
Is script a pay-to-public-key output?
isPayPKHash :: ScriptOutput -> Bool Source #
Is script a pay-to-pub-key-hash output?
isPayMulSig :: ScriptOutput -> Bool Source #
Is script a pay-to-multi-sig output?
isPayScriptHash :: ScriptOutput -> Bool Source #
Is script a pay-to-script-hash output?
isPayWitnessPKHash :: ScriptOutput -> Bool Source #
Is script a pay-to-witness-pub-key-hash output?
isPayWitnessScriptHash :: ScriptOutput -> Bool Source #
Is script a pay-to-witness-script-hash output?
isDataCarrier :: ScriptOutput -> Bool Source #
Is script a data carrier output?
encodeOutput :: ScriptOutput -> Script Source #
Computes a Script
from a standard ScriptOutput
.
encodeOutputBS :: ScriptOutput -> ByteString Source #
Similar to encodeOutput
but encodes to a ByteString
decodeOutput :: Script -> Either String ScriptOutput Source #
Tries to decode a ScriptOutput
from a Script
. This can fail if the
script is not recognized as any of the standard output types.
decodeOutputBS :: ByteString -> Either String ScriptOutput Source #
Similar to decodeOutput
but decodes from a ByteString
.
opPushData :: ByteString -> ScriptOp Source #
Optimally encode data using one of the 4 types of data pushing opcodes.