haskoin-script-0.0.1: Implementation of Bitcoin script parsing and evaluation

Safe HaskellNone

Network.Haskoin.Script

Contents

Description

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.

Synopsis

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.

Constructors

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 -> 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.

Constructors

SpendPK

Spend the coins of a PayPK output.

SpendPKHash

Spend the coins of a PayPKHash output.

SpendMulSig

Spend the coins of a PayMulSig output.

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.

Constructors

ScriptHashInput 

Fields

spendSHInput :: ScriptInput

Input script spending the redeem script

spendSHOutput :: RedeemScript

Redeem script

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

Decode ScriptOp [OP_1 .. OP_16] to integers [1 .. 16]. This functions fails for other values of ScriptOp

SigHash

For additional information on sighashes, see: http://en.bitcoin.it/wiki/OP_CHECKSIG

data SigHash Source

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.

Constructors

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.

Fields

anyoneCanPay :: Bool
 
SigNone

Sign none of the outputs of a transaction. This allows anyone to change any of the outputs of the transaction.

Fields

anyoneCanPay :: Bool
 
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.

Fields

anyoneCanPay :: Bool
 
SigUnknown

Unrecognized sighash types will decode to SigUnknown.

txSigHashSource

Arguments

:: 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.

isSigAll :: SigHash -> BoolSource

Returns True if the SigHash has the value SigAll.

isSigNone :: SigHash -> BoolSource

Returns True if the SigHash has the value SigNone.

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.

encodeSig :: TxSignature -> ByteStringSource

Serialize a TxSignature to 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.