cbitcoin
 All Data Structures Files Functions Variables Typedefs
src/structures/CBObject/CBByteArray/CBScript/CBScript.h File Reference

Functions for bitcoin scripts which can be processed to determine if a transaction input is valid. A good resource is https://en.bitcoin.it/wiki/Script A CBScript object is an alias to a CBByteArray object. More...

#include "CBByteArray.h"
#include "CBDependencies.h"
#include <stdbool.h>

Go to the source code of this file.

Data Structures

struct  CBScriptStackItem
 Structure for a stack item. More...
struct  CBScriptStack
 Structure that holds byte data in a stack. More...

Typedefs

typedef CBByteArray CBScript

Functions

CBScriptCBNewScriptFromReference (CBByteArray *program, uint32_t offset, uint32_t len)
 Creates a new CBScript object.
CBScriptCBNewScriptOfSize (uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBScript object with a given size.
CBScriptCBNewScriptFromString (char *string, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBScript object from a string. The script text should follow the following Backus–Naur Form: /code <digit> = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" <hex_digits> ::= <digit> | <digit> <hex_digits> <hex> ::= "0x" <hex_digits> <operation_name> = "0" | "FALSE" | "1NEGATE" | "RESERVED" | "1" | "TRUE" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "10" | "11" | "12" | "13" | "14" | "15" | "16" | "NOP" | "VER" | "IF" | "NOTIF" | "VERIF" | "VERNOTIF" | "ELSE" | "ENDIF" | "VERIFY" | "RETURN" | "TOALTSTACK" | "FROMALTSTACK" | "2DROP" | "2DUP" | "3DUP" | "2OVER" | "2ROT" | "2SWAP" | "IFDUP" | "DEPTH" | "DROP" | "DUP" | "NIP" | "OVER" | "PICK" | "ROLL" | "ROT" | "SWAP" | "TUCK" | "CAT" | "SUBSTR" | "LEFT" | "RIGHT = 0x81" | "SIZE" | "INVERT" | "AND" | "OR" | "XOR" | "EQUAL" | "EQUALVERIFY" | "RESERVED1" | "RESERVED2" | "1ADD" | "1SUB" | "2MUL" | "2DIV" | "NEGATE" | "ABS" | "NOT" | "0NOTEQUAL" | "ADD" | "SUB" | "MUL" | "DIV" | "MOD" | "LSHIFT " | "RSHIFT" | "BOOLAND" | "BOOLOR" | "NUMEQUAL" | "NUMEQUALVERIFY" | "NUMNOTEQUAL" | "LESSTHAN" | "GREATERTHAN" | "LESSTHANOREQUAL" | "GREATERTHANOREQUAL" | "MIN" | "MAX" | "WITHIN" | "RIPEMD160" | "SHA1" | "SHA256" | "HASH160" | "HASH256" | "CODESEPARATOR" | "CHECKSIG" | "CHECKSIGVERIFY" | "CHECKMULTISIG" | "CHECKMULTISIGVERIFY" | "NOP1" | "NOP2" | "NOP3" | "NOP4" | "NOP5" | "NOP6" | "NOP7" | "NOP8" | "NOP9" | "NOP10" <operation> ::= "OP_" <operation_name> <lines> ::= "\n" | "\n" <lines> <seperator> ::= <lines> | " " <operation_or_hex> ::= <operation> | <hex> <seperated_operation_or_hex> ::= <operation_or_hex> <seperator> <script_body> ::= <seperated_operation_or_hex> | <seperated_operation_or_hex> <script_body> | "" <lines_or_none> ::= <lines> | "" <operation_or_hex_or_none> ::= <operation_or_hex> | "" <script> ::= <lines_or_none> <script_body> <operation_or_hex_or_none> /endcode Be warned that the scripts do not use two's compliment for hex values. To represent a negative number the far left digit should be 8 or higher. Adding 8 to the far left digit give the number a negative sign such that 0x81 = -1. Negative numbers should have an even number of digits. 0x8FF will not give -255, instead 0x80FF is needed.
CBScriptCBNewScriptWithData (uint8_t *data, uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBScript using data.
CBScriptCBNewScriptWithDataCopy (uint8_t *data, uint32_t size, void(*onErrorReceived)(CBError error, char *,...))
 Creates a new CBScript using data which is copied.
CBScriptCBGetScript (void *self)
 Gets a CBScript from another object. Use this to avoid casts.
bool CBInitScriptFromString (CBScript *self, char *string, void(*onErrorReceived)(CBError error, char *,...))
 Initialises a CBScript object from a string.
void CBFreeProcessScript (CBScript *self)
 Does the processing to free a CBScript object. Should be called by the children when freeing objects.
void CBFreeScriptStack (CBScriptStack stack)
 Frees a CBScriptStack.
CBScriptStack CBNewEmptyScriptStack (void)
 Returns a new empty stack.
CBScriptExecuteReturn CBScriptExecute (CBScript *self, CBScriptStack *stack, CBGetHashReturn(*getHashForSig)(void *, CBByteArray *, uint32_t, CBSignType, uint8_t *), void *transaction, uint32_t inputIndex, bool p2sh)
 Executes a bitcoin script.
uint32_t CBScriptGetSigOpCount (CBScript *self, bool inP2SH)
 Returns the number of sigops.
bool CBScriptIsP2SH (CBScript *self)
 Determines if a script object matches the P2SH template.
void CBSubScriptRemoveSignature (uint8_t *subScript, uint32_t *subScriptLen, CBScriptStackItem signature)
 Removes occurances of a signature from script data.
CBScriptStackItem CBScriptStackCopyItem (CBScriptStack *stack, uint8_t fromTop)
 Returns a copy of a stack item, "fromTop" from the top.
bool CBScriptStackEvalBool (CBScriptStack *stack)
 Evaluates the top stack item as a bool. False if 0 or -0.
int64_t CBScriptStackItemToInt64 (CBScriptStackItem item)
 Converts a CBScriptStackItem to an int64_t.
CBScriptStackItem CBScriptStackPopItem (CBScriptStack *stack)
 Removes the top item from the stack and returns it.
bool CBScriptStackPushItem (CBScriptStack *stack, CBScriptStackItem item)
 Push data onto the stack which is freed by the stack.
void CBScriptStackRemoveItem (CBScriptStack *stack)
 Removes top item from the stack.
CBScriptStackItem CBInt64ToScriptStackItem (CBScriptStackItem item, int64_t i)
 Converts a int64_t to a CBScriptStackItem.

Detailed Description

Functions for bitcoin scripts which can be processed to determine if a transaction input is valid. A good resource is https://en.bitcoin.it/wiki/Script A CBScript object is an alias to a CBByteArray object.


Function Documentation

void CBFreeProcessScript ( CBScript self)

Does the processing to free a CBScript object. Should be called by the children when freeing objects.

Parameters:
selfThe CBScript object to free.

Frees a CBScriptStack.

Parameters:
stackThe stack to free
CBScript* CBGetScript ( void *  self)

Gets a CBScript from another object. Use this to avoid casts.

Parameters:
selfThe object to obtain the CBScript from.
Returns:
The CBScript object.
bool CBInitScriptFromString ( CBScript self,
char *  string,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Initialises a CBScript object from a string.

See also:
CBNewScriptOfSize
Parameters:
selfThe CBScript object to initialise
paramsThe network parameters.
stringThe string to compile into a CBScript object.
onErrorReceivedThe CBEvents structure.
Returns:
true on success, false on failure.

Converts a int64_t to a CBScriptStackItem.

Parameters:
itemPass in a CBScriptStackItem for reallocating data.
iThe 64 bit signed integer.
Returns:
A CBScriptStackItem.

Returns a new empty stack.

Returns:
The new empty stack.
CBScript* CBNewScriptFromReference ( CBByteArray program,
uint32_t  offset,
uint32_t  len 
)

Creates a new CBScript object.

Returns:
A new CBScript object.
CBScript* CBNewScriptFromString ( char *  string,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBScript object from a string. The script text should follow the following Backus–Naur Form: /code <digit> = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" <hex_digits> ::= <digit> | <digit> <hex_digits> <hex> ::= "0x" <hex_digits> <operation_name> = "0" | "FALSE" | "1NEGATE" | "RESERVED" | "1" | "TRUE" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "10" | "11" | "12" | "13" | "14" | "15" | "16" | "NOP" | "VER" | "IF" | "NOTIF" | "VERIF" | "VERNOTIF" | "ELSE" | "ENDIF" | "VERIFY" | "RETURN" | "TOALTSTACK" | "FROMALTSTACK" | "2DROP" | "2DUP" | "3DUP" | "2OVER" | "2ROT" | "2SWAP" | "IFDUP" | "DEPTH" | "DROP" | "DUP" | "NIP" | "OVER" | "PICK" | "ROLL" | "ROT" | "SWAP" | "TUCK" | "CAT" | "SUBSTR" | "LEFT" | "RIGHT = 0x81" | "SIZE" | "INVERT" | "AND" | "OR" | "XOR" | "EQUAL" | "EQUALVERIFY" | "RESERVED1" | "RESERVED2" | "1ADD" | "1SUB" | "2MUL" | "2DIV" | "NEGATE" | "ABS" | "NOT" | "0NOTEQUAL" | "ADD" | "SUB" | "MUL" | "DIV" | "MOD" | "LSHIFT " | "RSHIFT" | "BOOLAND" | "BOOLOR" | "NUMEQUAL" | "NUMEQUALVERIFY" | "NUMNOTEQUAL" | "LESSTHAN" | "GREATERTHAN" | "LESSTHANOREQUAL" | "GREATERTHANOREQUAL" | "MIN" | "MAX" | "WITHIN" | "RIPEMD160" | "SHA1" | "SHA256" | "HASH160" | "HASH256" | "CODESEPARATOR" | "CHECKSIG" | "CHECKSIGVERIFY" | "CHECKMULTISIG" | "CHECKMULTISIGVERIFY" | "NOP1" | "NOP2" | "NOP3" | "NOP4" | "NOP5" | "NOP6" | "NOP7" | "NOP8" | "NOP9" | "NOP10" <operation> ::= "OP_" <operation_name> <lines> ::= "\n" | "\n" <lines> <seperator> ::= <lines> | " " <operation_or_hex> ::= <operation> | <hex> <seperated_operation_or_hex> ::= <operation_or_hex> <seperator> <script_body> ::= <seperated_operation_or_hex> | <seperated_operation_or_hex> <script_body> | "" <lines_or_none> ::= <lines> | "" <operation_or_hex_or_none> ::= <operation_or_hex> | "" <script> ::= <lines_or_none> <script_body> <operation_or_hex_or_none> /endcode Be warned that the scripts do not use two's compliment for hex values. To represent a negative number the far left digit should be 8 or higher. Adding 8 to the far left digit give the number a negative sign such that 0x81 = -1. Negative numbers should have an even number of digits. 0x8FF will not give -255, instead 0x80FF is needed.

Parameters:
paramsThe network parameters.
stringThe string to compile into a CBScript object.
onErrorReceivedThe CBEvents structure.
Returns:
A new CBScript object or NULL on failure.
CBScript* CBNewScriptOfSize ( uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBScript object with a given size.

Returns:
A new CBScript object.
CBScript* CBNewScriptWithData ( uint8_t *  data,
uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBScript using data.

Parameters:
dataThe data. This should be dynamically allocated. The new CBByteArray object will take care of it's memory management so do not free this data once passed into this constructor.
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
The new CBScript object.
CBScript* CBNewScriptWithDataCopy ( uint8_t *  data,
uint32_t  size,
void(*)(CBError error, char *,...)  onErrorReceived 
)

Creates a new CBScript using data which is copied.

Parameters:
dataThe data. This data is copied.
sizeSize in bytes for the new array.
onErrorReceivedCBEngine for errors.
Returns:
The new CBScript object.
CBScriptExecuteReturn CBScriptExecute ( CBScript self,
CBScriptStack stack,
CBGetHashReturn(*)(void *, CBByteArray *, uint32_t, CBSignType, uint8_t *)  getHashForSig,
void *  transaction,
uint32_t  inputIndex,
bool  p2sh 
)

Executes a bitcoin script.

Parameters:
selfThe CBScript object with the program
stackA pointer to the input stack for the program.
getHashForSigA pointer to the funtion to get the hash for checking the signature. Should take a CBTransaction object, input index, CBSignType and the CBDependencies object.
transactionTransaction for checking the signatures.
inputIndexThe index of the input for the signature.
p2shIf false, do not allow any P2SH matches.
Returns:
CB_SCRIPT_VALID is the program ended with true, CB_SCRIPT_INVALID on script failure or CB_SCRIPT_ERR if an error occured with the interpreter such as running of of memory.
uint32_t CBScriptGetSigOpCount ( CBScript self,
bool  inP2SH 
)

Returns the number of sigops.

Parameters:
selfThe CBScript object.
inP2SHtrue when getting sigops for a P2SH script. the number of sigops as used for validation.
bool CBScriptIsP2SH ( CBScript self)

Determines if a script object matches the P2SH template.

Parameters:
selfThe CBScript object. true if the script matches the P2SH template, false otherwise.
CBScriptStackItem CBScriptStackCopyItem ( CBScriptStack stack,
uint8_t  fromTop 
)

Returns a copy of a stack item, "fromTop" from the top.

Parameters:
stackA pointer to the stack.
fromTopNumber of items from the top to copy.
Returns:
A copy of the stack item which should be freed.

Evaluates the top stack item as a bool. False if 0 or -0.

Parameters:
stackThe stack.
Returns:
The boolean result.

Converts a CBScriptStackItem to an int64_t.

Parameters:
itemThe CBScriptStackItem to convert
Returns:
A 64 bit signed integer.

Removes the top item from the stack and returns it.

Parameters:
stackA pointer to the stack to pop the data.
Returns:
The top item. This must be freed.

Push data onto the stack which is freed by the stack.

Parameters:
stackA pointer to the stack to push data onto.
dataThe item to push on the stack.

Removes top item from the stack.

Parameters:
stackA pointer to the stack to remove the data.
void CBSubScriptRemoveSignature ( uint8_t *  subScript,
uint32_t *  subScriptLen,
CBScriptStackItem  signature 
)

Removes occurances of a signature from script data.

Parameters:
subScriptThe sub script to remove signatures from.
subScriptLenA pointer to the length of the sub script. The length will be modified to the new length.
signatureThe signature to be found and removed.