Safe Haskell | None |
---|---|
Language | Haskell98 |
This package 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.
- data Script = Script {}
- 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
- data PushDataType
- opPushData :: ByteString -> ScriptOp
- data ScriptOutput
- = PayPK { }
- | PayPKHash { }
- | PayMulSig { }
- | PayScriptHash { }
- encodeOutput :: ScriptOutput -> Script
- encodeOutputBS :: ScriptOutput -> ByteString
- decodeOutput :: Script -> Either String ScriptOutput
- decodeOutputBS :: ByteString -> Either String ScriptOutput
- isPayPK :: ScriptOutput -> Bool
- isPayPKHash :: ScriptOutput -> Bool
- isPayMulSig :: ScriptOutput -> Bool
- isPayScriptHash :: ScriptOutput -> Bool
- scriptAddr :: ScriptOutput -> Address
- sortMulSig :: ScriptOutput -> ScriptOutput
- data ScriptInput
- data SimpleInput
- = SpendPK { }
- | SpendPKHash {
- getInputSig :: !TxSignature
- getInputKey :: !PubKey
- | SpendMulSig { }
- type RedeemScript = ScriptOutput
- encodeInput :: ScriptInput -> Script
- encodeInputBS :: ScriptInput -> ByteString
- decodeInput :: Script -> Either String ScriptInput
- decodeInputBS :: ByteString -> Either String ScriptInput
- isSpendPK :: ScriptInput -> Bool
- isSpendPKHash :: ScriptInput -> Bool
- isSpendMulSig :: ScriptInput -> Bool
- isScriptHashInput :: ScriptInput -> Bool
- scriptRecipient :: Script -> Either String Address
- scriptSender :: Script -> Either String Address
- intToScriptOp :: Int -> ScriptOp
- scriptOpToInt :: ScriptOp -> Either String Int
- data SigHash
- = SigAll {
- anyoneCanPay :: !Bool
- | SigNone {
- anyoneCanPay :: !Bool
- | SigSingle {
- anyoneCanPay :: !Bool
- | SigUnknown {
- anyoneCanPay :: !Bool
- getSigCode :: !Word8
- = SigAll {
- txSigHash :: Tx -> Script -> Int -> SigHash -> Hash256
- encodeSigHash32 :: SigHash -> ByteString
- isSigAll :: SigHash -> Bool
- isSigNone :: SigHash -> Bool
- isSigSingle :: SigHash -> Bool
- isSigUnknown :: SigHash -> Bool
- data TxSignature = TxSignature {
- txSignature :: !Signature
- sigHashType :: !SigHash
- encodeSig :: TxSignature -> ByteString
- decodeSig :: ByteString -> Either String TxSignature
- decodeCanonicalSig :: ByteString -> Either String TxSignature
- evalScript :: Script -> Script -> SigCheck -> [Flag] -> Bool
- verifySpend :: Tx -> Int -> Script -> [Flag] -> Bool
- type SigCheck = [ScriptOp] -> TxSignature -> PubKey -> Bool
Scripts
More informations on scripts is available here: http://en.bitcoin.it/wiki/Script
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 the spending signatures in the input of a transaction
Data type representing all of the operators allowed inside a Script
.
data PushDataType Source
Data type representing the type of an OP_PUSHDATA opcode.
OPCODE | The next opcode bytes is data to be pushed onto the stack |
OPDATA1 | The next byte contains the number of bytes to be pushed onto the stack |
OPDATA2 | The next two bytes contains the number of bytes to be pushed onto the stack |
OPDATA4 | The next four bytes contains the number of bytes to be pushed onto the stack |
opPushData :: ByteString -> ScriptOp Source
Optimally encode data using one of the 4 types of data pushing opcodes
Script Parsing
Script Outputs
data ScriptOutput Source
Data type describing standard transaction output scripts. Output scripts provide the conditions that must be fulfilled for someone to spend the output coins.
PayPK | Pay to a public key. |
PayPKHash | Pay to a public key hash. |
PayMulSig | Pay to multiple public keys. |
| |
PayScriptHash | Pay to a script hash. |
encodeOutput :: ScriptOutput -> Script Source
Computes a Script
from a ScriptOutput
. The Script
is a list of
ScriptOp
can can be used to build a Tx
.
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
isPayPK :: ScriptOutput -> Bool Source
Returns True if the script is a pay to public key output.
isPayPKHash :: ScriptOutput -> Bool Source
Returns True if the script is a pay to public key hash output.
isPayMulSig :: ScriptOutput -> Bool Source
Returns True if the script is a pay to multiple public keys output.
isPayScriptHash :: ScriptOutput -> Bool Source
Returns true if the script is a pay to script hash output.
scriptAddr :: ScriptOutput -> Address Source
Computes a script address from a script output. This address can be used in a pay to script hash output.
sortMulSig :: ScriptOutput -> ScriptOutput Source
Sorts the public keys of a multisignature output in ascending order by comparing their serialized representations. This feature allows for easier multisignature account management as participants in a multisignature wallet will blindly agree on an ordering of the public keys without having to communicate.
Script Inputs
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.
SpendPK | Spend the coins of a PayPK output. |
SpendPKHash | Spend the coins of a PayPKHash output. |
| |
SpendMulSig | Spend the coins of a PayMulSig output. |
type RedeemScript = ScriptOutput Source
encodeInput :: ScriptInput -> Script Source
encodeInputBS :: ScriptInput -> ByteString Source
Similar to encodeInput
but encodes to a ByteString
decodeInput :: Script -> Either String ScriptInput Source
Decodes a ScriptInput
from a Script
. This function fails if the
script can not be parsed as a standard script input.
decodeInputBS :: ByteString -> Either String ScriptInput Source
Similar to decodeInput
but decodes from a ByteString
isSpendPK :: ScriptInput -> Bool Source
Returns True if the input script is spending a public key.
isSpendPKHash :: ScriptInput -> Bool Source
Returns True if the input script is spending a public key hash.
isSpendMulSig :: ScriptInput -> Bool Source
Returns True if the input script is spending a multisignature output.
Helpers
scriptRecipient :: Script -> Either String Address Source
Computes the recipient address of a script. This function fails if the script could not be decoded as a pay to public key hash or pay to script hash.
scriptSender :: Script -> Either String Address Source
Computes the sender address of a script. This function fails if the script could not be decoded as a spend public key hash or script hash input.
intToScriptOp :: Int -> ScriptOp Source
Transforms integers [1 .. 16] to ScriptOp
[OP_1 .. OP_16]
SigHash
For additional information on sighashes, see: http://en.bitcoin.it/wiki/OP_CHECKSIG
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 anyoneCanPay flag is True, then only the current input is signed. Otherwise, all of the inputs of a transaction are signed. The default value for anyoneCanPay is False.
SigAll | Sign all of the outputs of a transaction (This is the default value). Changing any of the outputs of the transaction will invalidate the signature. |
| |
SigNone | Sign none of the outputs of a transaction. This allows anyone to change any of the outputs of the transaction. |
| |
SigSingle | Sign only the output corresponding the the current transaction input. You care about your own output in the transaction but you don't care about any of the other outputs. |
| |
SigUnknown | Unrecognized sighash types will decode to SigUnknown. |
|
:: Tx | Transaction to sign. |
-> Script | Output script that is being spent. |
-> Int | Index of the input that is being signed. |
-> SigHash | What parts of the transaction should be signed. |
-> Hash256 | Result hash to be signed. |
Computes the hash that will be used for signing a transaction.
encodeSigHash32 :: SigHash -> ByteString Source
Encodes a SigHash
to a 32 bit-long bytestring.
isSigSingle :: SigHash -> Bool Source
Returns True if the SigHash
has the value SigSingle.
isSigUnknown :: SigHash -> Bool Source
Returns True if the SigHash
has the value SigUnknown.
data TxSignature Source
Data type representing a Signature
together with a SigHash
. The
SigHash
is serialized as one byte at the end of a regular ECDSA
Signature
. All signatures in transaction inputs are of type TxSignature
.
encodeSig :: TxSignature -> ByteString Source
Serialize a TxSignature
to a ByteString.
decodeSig :: ByteString -> Either String TxSignature Source
Decode a TxSignature
from a ByteString.