BSV.VM (BSV v2.1.0) View Source
Pure Elixir Bitcoin Script VM.
Bitcoin Script is a Forth-like scripting language used to define the locking mechanism for a transaction output.
Transaction outputs each contain a "locking script" which lock a number of satoshis. Transaction inputs contain an "unlocking script" which unlock the satoshis contained in a previous output. Both the unlocking script and previous locking script are concatenated in the following order:
unlocking_script <> locking_script
The entire script is evaluated and when if the end result contains a truthy value on the top of the stack, it is a valid script. Otherwise it is invalid. This is known as a predicate - an equation that evaluates to true or false.
Link to this section Summary
Functions
Evaluates the given script and returns a VM state.
As eval/2
but returns the VM struct or raises an error.
Initiates a new VM struct
If the top stack element is not false it is made true. Otherwise it is made false.
Adds 1 onto the top stack element.
Subtracks 1 from the top stack element.
Divides the top stack element by 2. DISABLED
Removes the top two items from the stack.
Duplicates the top two items on the stack.
Multiplies the top stack element by 2. DISABLED
Copies two items two spaces back to the top of the stack.
Moves the 5th and 6th items to top of stack.
Swaps the top two pairs of items.
Duplicates the top three items on the stack.
Makes positive the numeric value of the top stack element.
The top two stack elements are added and replaced on the stack with the result.
Calculates the bitwise AND of the top two elements of the stack. Both elements must be the same length.
Converts the binary value into a numeric value.
Checks the top two stack elements are both truthy and replaces them with the boolean result.
Checks if either of the top two stack elements are truthy and replaces them with the boolean result.
Concatenates the top two stack items into one binary.
The top stack element is the number of public keys. The next n elements are the public keys. The next element is the number of signatures and the next n elements are the signatures.
Verifies the signature in the second top stack element against the current transaction sighash using the pubkey on top of the stack. Replaces them with the boolean result.
Runs op_checksig/1
and op_verify/1
.
Counts the stack lenth and puts the result on the top of the stack.
The top stack element is divided from the second, and both are replaced on the stack with the result.
Removes the top item from the stack.
Duplicates the top item on the stack.
If the preceding OP_IF or OP_NOTIF was not executed, the the following statements are executed. If the preceding OP_IF or OP_NOTIF was executed, the following statements are not.
Ends the current IF/ELSE block. All blocks must end or the script is invalid.
Compares the equality of the top two stack items and replaces them with the boolean result.
Runs op_equal/1
and op_verify/1
.
Removes the top of the alt stack and puts it into the stack.
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is greater than the first.
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is greater than or equal to the first.
The top element on the stack is hashed using SHA256
then RIPEMD160
.
The top element on the stack is hashed using SHA256
then SHA256
again.
Removes the top of the stack. If the top value is truthy, statements between OP_IF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed.
Duplicates the top stack item if it is truthy.
Flips all bits on the top element of the stack.
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is less than the first.
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is less than or equal to the first.
The second from top element numeric value is bitshifted to the left by the number of bits in the top element numeric value. Both elements are replaced on the stack with the result.
Compares numeric value of the top two stack elements, and both are replaced with the greater value.
Compares numeric value of the top two stack elements, and both are replaced with the smaller value.
The top stack element is divided from the second, and both are replaced on the stack with the remainder.
The top two stack elements are multiplied and replaced on the stack with the result.
Negates the numeric value of the top stack element.
Removes the second to top item from the stack.
No op. Does nothing and returns the vm.
If the top stack element is false it is made true. Otherwise it is made false.
Removes the top of the stack. If the top value is false, statements between OP_NOTIF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed.
Converts the second from top stack item into a binary of the length given in the top stack item.
Compares the equality of the numeric value of the top two stack items and replaces them with the boolean result.
Runs op_numequal/1
and op_verify/1
.
Checks the numeric value of the top two stack items are not equal and replaces them with the boolean result.
Calculates the bitwise OR of the top two elements of the stack. Both elements must be the same length.
Copies the second to top stack item to the top.
Removes the top stack item and uses it as an index length, then copies the nth item on the stack to the top.
Generic pushdata. Pushes any given binary or integer to the stack.
Unused. Makes transaction invalid.
Returns the vm and no further statements are evaluated.
The top element on the stack is hashed using RIPEMD160
.
Removes the top stack item and uses it as an index length, then moves the nth item on the stack to the top.
Rotates the top three items on the stack, effictively moving the 3rd item to the top of the stack.
The second from top element numeric value is bitshifted to the right by the number of bits in the top element numeric value. Both elements are replaced on the stack with the result.
The top element on the stack is hashed using SHA1
.
The top element on the stack is hashed using SHA256
.
Pushes the byte length of the top element of the stack, without popping it.
Splits the second from top stack item by the index given in the top stack item.
The top stack element is subtracted from the second, and both are replaced on the stack with the result.
Rotates the top two items on the stack, effectively moving the 2nd item to the top of the stack.
Removes the top of the stack and puts it into the alt stack.
Copies the top item on the stack and inserts it before the second to top item.
Puts the version of the protocol under which this transaction will be evaluated onto the stack. DISABLED
Removes the top of the stack. If the top value is equal to the version of the protocol under which the transaction is evaluated, statements between OP_IF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed. DISABLED
Mmarks the script as invalid unless the stack is truthy. Removes the top of the stack.
Removes the top of the stack. If the top value is not equal to the version of the protocol under which the transaction is evaluated, statements between OP_IF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed. DISABLED
The top (max) and second (min) numeric values on the stack define a range and the third element is checked to see if it is within the range. All three elements are replaced on the stack with the boolean result.
Calculates the bitwise XOR of the top two elements of the stack. Both elements must be the same length.
Determines if the Script VM is truthy or falsey.
Link to this section Types
Specs
ctx() :: {BSV.Tx.t(), non_neg_integer(), BSV.TxOut.t()}
Transaction context.
A tuple containing a BSV.Tx.t/0
, BSV.TxIn.vin/0
and BSV.TxOut.t/0
.
If not present, then op_checksig/1
and op_checkmultisig/1
will raise
errors and the script will fail.
Specs
t() :: %BSV.VM{ alt_stack: list(), ctx: ctx() | nil, error: nil | String.t(), if_stack: list(), op_return: list(), opts: map(), stack: list() }
VM struct
Link to this section Functions
Specs
eval(t(), BSV.Script.t() | list()) :: {:ok, t()} | {:error, t()}
Evaluates the given script and returns a VM state.
Specs
eval!(t(), BSV.Script.t() | list()) :: t()
As eval/2
but returns the VM struct or raises an error.
Specs
Initiates a new VM struct
Specs
If the top stack element is not false it is made true. Otherwise it is made false.
Specs
Adds 1 onto the top stack element.
Specs
Subtracks 1 from the top stack element.
Specs
Divides the top stack element by 2. DISABLED
Specs
Removes the top two items from the stack.
Specs
Duplicates the top two items on the stack.
Specs
Multiplies the top stack element by 2. DISABLED
Specs
Copies two items two spaces back to the top of the stack.
Specs
Moves the 5th and 6th items to top of stack.
Specs
Swaps the top two pairs of items.
Specs
Duplicates the top three items on the stack.
Specs
Makes positive the numeric value of the top stack element.
Specs
The top two stack elements are added and replaced on the stack with the result.
Specs
Calculates the bitwise AND of the top two elements of the stack. Both elements must be the same length.
Specs
Converts the binary value into a numeric value.
Specs
Checks the top two stack elements are both truthy and replaces them with the boolean result.
Specs
Checks if either of the top two stack elements are truthy and replaces them with the boolean result.
Specs
Concatenates the top two stack items into one binary.
Specs
The top stack element is the number of public keys. The next n elements are the public keys. The next element is the number of signatures and the next n elements are the signatures.
Each signature is itereated over and each public key is checked if it verifies the signature against the current transactions sighash. Once a public key is checked it is not checked again, so signatures must be pushed onto the stack in the same order as the corresponding public keys.
Specs
Runs op_checkmultisig/1
and op_verify/1
.
Specs
Verifies the signature in the second top stack element against the current transaction sighash using the pubkey on top of the stack. Replaces them with the boolean result.
Specs
Runs op_checksig/1
and op_verify/1
.
Specs
Counts the stack lenth and puts the result on the top of the stack.
Specs
The top stack element is divided from the second, and both are replaced on the stack with the result.
Specs
Removes the top item from the stack.
Specs
Duplicates the top item on the stack.
Specs
If the preceding OP_IF or OP_NOTIF was not executed, the the following statements are executed. If the preceding OP_IF or OP_NOTIF was executed, the following statements are not.
Specs
Ends the current IF/ELSE block. All blocks must end or the script is invalid.
Specs
Compares the equality of the top two stack items and replaces them with the boolean result.
Specs
Runs op_equal/1
and op_verify/1
.
Specs
Removes the top of the alt stack and puts it into the stack.
Specs
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is greater than the first.
Specs
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is greater than or equal to the first.
Specs
The top element on the stack is hashed using SHA256
then RIPEMD160
.
Specs
The top element on the stack is hashed using SHA256
then SHA256
again.
Specs
Removes the top of the stack. If the top value is truthy, statements between OP_IF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed.
Specs
Duplicates the top stack item if it is truthy.
Specs
Flips all bits on the top element of the stack.
Specs
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is less than the first.
Specs
Compares numeric value of the top two stack elements, and both are replaced on the stack with the result. Is true if the second element is less than or equal to the first.
Specs
The second from top element numeric value is bitshifted to the left by the number of bits in the top element numeric value. Both elements are replaced on the stack with the result.
Specs
Compares numeric value of the top two stack elements, and both are replaced with the greater value.
Specs
Compares numeric value of the top two stack elements, and both are replaced with the smaller value.
Specs
The top stack element is divided from the second, and both are replaced on the stack with the remainder.
Specs
The top two stack elements are multiplied and replaced on the stack with the result.
Specs
Negates the numeric value of the top stack element.
Specs
Removes the second to top item from the stack.
Specs
No op. Does nothing and returns the vm.
Specs
If the top stack element is false it is made true. Otherwise it is made false.
Specs
Removes the top of the stack. If the top value is false, statements between OP_NOTIF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed.
Specs
Converts the second from top stack item into a binary of the length given in the top stack item.
Specs
Compares the equality of the numeric value of the top two stack items and replaces them with the boolean result.
Specs
Runs op_numequal/1
and op_verify/1
.
Specs
Checks the numeric value of the top two stack items are not equal and replaces them with the boolean result.
Specs
Calculates the bitwise OR of the top two elements of the stack. Both elements must be the same length.
Specs
Copies the second to top stack item to the top.
Specs
Removes the top stack item and uses it as an index length, then copies the nth item on the stack to the top.
Specs
Generic pushdata. Pushes any given binary or integer to the stack.
Specs
Unused. Makes transaction invalid.
Specs
Returns the vm and no further statements are evaluated.
Specs
The top element on the stack is hashed using RIPEMD160
.
Specs
Removes the top stack item and uses it as an index length, then moves the nth item on the stack to the top.
Specs
Rotates the top three items on the stack, effictively moving the 3rd item to the top of the stack.
Specs
The second from top element numeric value is bitshifted to the right by the number of bits in the top element numeric value. Both elements are replaced on the stack with the result.
Specs
The top element on the stack is hashed using SHA1
.
Specs
The top element on the stack is hashed using SHA256
.
Specs
Pushes the byte length of the top element of the stack, without popping it.
Specs
Splits the second from top stack item by the index given in the top stack item.
Specs
The top stack element is subtracted from the second, and both are replaced on the stack with the result.
Specs
Rotates the top two items on the stack, effectively moving the 2nd item to the top of the stack.
Specs
Removes the top of the stack and puts it into the alt stack.
Specs
Copies the top item on the stack and inserts it before the second to top item.
Specs
Puts the version of the protocol under which this transaction will be evaluated onto the stack. DISABLED
Specs
Removes the top of the stack. If the top value is equal to the version of the protocol under which the transaction is evaluated, statements between OP_IF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed. DISABLED
Specs
Mmarks the script as invalid unless the stack is truthy. Removes the top of the stack.
Specs
Removes the top of the stack. If the top value is not equal to the version of the protocol under which the transaction is evaluated, statements between OP_IF and OP_ELSE are executed. Otherwise statements between OP_ELSE and OP_ENDIF are executed. DISABLED
Specs
The top (max) and second (min) numeric values on the stack define a range and the third element is checked to see if it is within the range. All three elements are replaced on the stack with the boolean result.
Specs
Calculates the bitwise XOR of the top two elements of the stack. Both elements must be the same length.
Specs
Determines if the Script VM is truthy or falsey.