haskoin-core-0.4.0: Implementation of the core Bitcoin protocol features.

Safe HaskellNone
LanguageHaskell98

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

Scripts

More informations on scripts is available here: http://en.bitcoin.it/wiki/Script

data Script Source #

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

Constructors

Script 

Fields

data ScriptOp Source #

Data type representing all of the operators allowed inside a Script.

data PushDataType Source #

Data type representing the type of an OP_PUSHDATA opcode.

Constructors

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.

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

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.

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

intToScriptOp :: Int -> ScriptOp Source #

Transforms integers [1 .. 16] to ScriptOp [OP_1 .. OP_16]

scriptOpToInt :: ScriptOp -> Either String Int Source #

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

SigNone

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

Fields

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

SigUnknown

Unrecognized sighash types will decode to SigUnknown.

txSigHash Source #

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 -> ByteString Source #

Encodes a SigHash to a 32 bit-long bytestring.

isSigAll :: SigHash -> Bool Source #

Returns True if the SigHash has the value SigAll.

isSigNone :: SigHash -> Bool Source #

Returns True if the SigHash has the value SigNone.

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.

Constructors

TxSignature 

encodeSig :: TxSignature -> ByteString Source #

Serialize a TxSignature to a ByteString.

decodeSig :: ByteString -> Either String TxSignature Source #

Decode a TxSignature from a ByteString.

Evaluation

verifySpend Source #

Arguments

:: Tx

The spending transaction

-> Int

The input index

-> Script

The output script we are spending

-> [Flag]

Evaluation flags

-> Bool 

Uses evalScript to check that the input script of a spending transaction satisfies the output script.

type SigCheck = [ScriptOp] -> TxSignature -> PubKey -> Bool Source #

Defines the type of function required by script evaluating functions to check transaction signatures.