Safe Haskell | None |
---|
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 ScriptOutput
- = PayPK { }
- | PayPKHash { }
- | PayMulSig {
- getOutputMulSigKeys :: ![PubKey]
- getOutputMulSigRequired :: !Int
- | PayScriptHash { }
- encodeOutput :: ScriptOutput -> Script
- decodeOutput :: Script -> Either String ScriptOutput
- isPayPK :: ScriptOutput -> Bool
- isPayPKHash :: ScriptOutput -> Bool
- isPayMulSig :: ScriptOutput -> Bool
- isPayScriptHash :: ScriptOutput -> Bool
- scriptAddr :: ScriptOutput -> Address
- sortMulSig :: ScriptOutput -> ScriptOutput
- data ScriptInput
- = SpendPK { }
- | SpendPKHash {
- getInputSig :: !TxSignature
- getInputKey :: !PubKey
- | SpendMulSig {
- getInputMulSigKeys :: ![TxSignature]
- getInputMulSigRequired :: !Int
- encodeInput :: ScriptInput -> Script
- decodeInput :: Script -> Either String ScriptInput
- isSpendPK :: ScriptInput -> Bool
- isSpendPKHash :: ScriptInput -> Bool
- isSpendMulSig :: ScriptInput -> Bool
- data ScriptHashInput = ScriptHashInput {}
- type RedeemScript = ScriptOutput
- encodeScriptHash :: ScriptHashInput -> Script
- decodeScriptHash :: Script -> Either String ScriptHashInput
- 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 {}
- encodeSig :: TxSignature -> ByteString
- decodeSig :: ByteString -> Either String TxSignature
- decodeCanonicalSig :: ByteString -> Either String TxSignature
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. |
Eq ScriptOutput | |
Show ScriptOutput |
encodeOutput :: ScriptOutput -> ScriptSource
Computes a Script
from a ScriptOutput
. The Script
is a list of
ScriptOp
can can be used to build a Tx
.
decodeOutput :: Script -> Either String ScriptOutputSource
Tries to decode a ScriptOutput
from a Script
. This can fail if the
script is not recognized as any of the standard output types.
isPayPK :: ScriptOutput -> BoolSource
Returns True if the script is a pay to public key output.
isPayPKHash :: ScriptOutput -> BoolSource
Returns True if the script is a pay to public key hash output.
isPayMulSig :: ScriptOutput -> BoolSource
Returns True if the script is a pay to multiple public keys output.
isPayScriptHash :: ScriptOutput -> BoolSource
Returns true if the script is a pay to script hash output.
scriptAddr :: ScriptOutput -> AddressSource
Computes a script address from a script output. This address can be used in a pay to script hash output.
sortMulSig :: ScriptOutput -> ScriptOutputSource
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 ScriptInput 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. |
|
Eq ScriptInput | |
Show ScriptInput |
encodeInput :: ScriptInput -> ScriptSource
Computes a Script
from a ScriptInput
. The Script
is a list of
ScriptOp
can can be used to build a Tx
.
decodeInput :: Script -> Either String ScriptInputSource
Decodes a ScriptInput
from a Script
. This function fails if the
script can not be parsed as a standard script input.
isSpendPK :: ScriptInput -> BoolSource
Returns True if the input script is spending a public key.
isSpendPKHash :: ScriptInput -> BoolSource
Returns True if the input script is spending a public key hash.
isSpendMulSig :: ScriptInput -> BoolSource
Returns True if the input script is spending a multisignature output.
ScriptHash Inputs
data ScriptHashInput Source
Data type describing an input script spending a pay to script hash output. To spend a script hash output, an input script must provide both a redeem script and a regular input script spending the redeem script.
ScriptHashInput | |
|
Eq ScriptHashInput | |
Show ScriptHashInput |
type RedeemScript = ScriptOutputSource
encodeScriptHash :: ScriptHashInput -> ScriptSource
Compute a Script
from a ScriptHashInput
. The Script
is a list of
ScriptOp
can can be used to build a Tx
.
decodeScriptHash :: Script -> Either String ScriptHashInputSource
Tries to decode a ScriptHashInput
from a Script
. This function fails
if the script can not be parsed as a script hash input.
Helpers
scriptRecipient :: Script -> Either String AddressSource
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 AddressSource
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 -> ScriptOpSource
Transforms integers [1 .. 16] to ScriptOp
[OP_1 .. OP_16]
scriptOpToInt :: ScriptOp -> Either String IntSource
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 -> ByteStringSource
Encodes a SigHash
to a 32 bit-long bytestring.
isSigSingle :: SigHash -> BoolSource
Returns True if the SigHash
has the value SigSingle.
isSigUnknown :: SigHash -> BoolSource
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
.
Eq TxSignature | |
Show TxSignature |
encodeSig :: TxSignature -> ByteStringSource
Serialize a TxSignature
to a ByteString.
decodeSig :: ByteString -> Either String TxSignatureSource
Decode a TxSignature
from a ByteString.
decodeCanonicalSig :: ByteString -> Either String TxSignatureSource
Decode a TxSignature
from a ByteString. This function will check if
the signature is canonical and fail if it is not.