BSV-ex v0.4.1 BSV.Script.VM View Source

Pure Elixir Bitcoin Script VM.

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.

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.

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.

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

Link to this type

t()

View Source
t() :: %BSV.Script.VM{
  alt_stack: list(),
  error: nil | String.t(),
  if_stack: list(),
  op_return: list(),
  opts: map(),
  stack: list(),
  tx: BSV.Transaction.t() | nil,
  vin: integer() | nil
}

Link to this section Functions

Link to this function

eval(vm, chunk)

View Source
eval(t(), BSV.Script.t() | list()) :: {:ok, t()} | {:error, t()}

Evaluates the given script and returns a VM state.

Link to this function

eval!(vm, script)

View Source
eval!(t(), BSV.Script.t() | list()) :: t()

As eval/2 but returns the VM struct or raises an error.

Initiates a new VM struct

Link to this function

op_0notequal(vm)

View Source
op_0notequal(t()) :: t()

If the top stack element is not false it is made true. Otherwise it is made false.

Link to this function

op_1add(vm)

View Source
op_1add(t()) :: t()

Adds 1 onto the top stack element.

Link to this function

op_1sub(vm)

View Source
op_1sub(t()) :: t()

Subtracks 1 from the top stack element.

Link to this function

op_2div(vm)

View Source
op_2div(t()) :: t()

Divides the top stack element by 2. DISABLED

Link to this function

op_2drop(vm)

View Source
op_2drop(t()) :: t()

Removes the top two items from the stack.

Link to this function

op_2dup(vm)

View Source
op_2dup(t()) :: t()

Duplicates the top two items on the stack.

Link to this function

op_2mul(vm)

View Source
op_2mul(t()) :: t()

Multiplies the top stack element by 2. DISABLED

Link to this function

op_2over(vm)

View Source
op_2over(t()) :: t()

Copies two items two spaces back to the top of the stack.

Link to this function

op_2rot(vm)

View Source
op_2rot(t()) :: t()

Moves the 5th and 6th items to top of stack.

Link to this function

op_2swap(vm)

View Source
op_2swap(t()) :: t()

Swaps the top two pairs of items.

Link to this function

op_3dup(vm)

View Source
op_3dup(t()) :: t()

Duplicates the top three items on the stack.

Link to this function

op_abs(vm)

View Source
op_abs(t()) :: t()

Makes positive the numeric value of the top stack element.

Link to this function

op_add(vm)

View Source
op_add(t()) :: t()

The top two stack elements are added and replaced on the stack with the result.

Link to this function

op_and(vm)

View Source
op_and(t()) :: t()

Calculates the bitwise AND of the top two elements of the stack. Both elements must be the same length.

Link to this function

op_bin2num(vm)

View Source
op_bin2num(t()) :: t()

Converts the binary value into a numeric value.

Link to this function

op_booland(vm)

View Source
op_booland(t()) :: t()

Checks the top two stack elements are both truthy and replaces them with the boolean result.

Link to this function

op_boolor(vm)

View Source
op_boolor(t()) :: t()

Checks if either of the top two stack elements are truthy and replaces them with the boolean result.

Link to this function

op_cat(vm)

View Source
op_cat(t()) :: t()

Concatenates the top two stack items into one binary.

Link to this function

op_checkmultisig(vm)

View Source
op_checkmultisig(t()) :: t()

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.

Link to this function

op_checkmultisigverify(vm)

View Source
op_checkmultisigverify(t()) :: t()

Runs op_checkmultisig/1 and op_verify/1.

Link to this function

op_checksig(vm)

View Source
op_checksig(t()) :: t()

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.

Link to this function

op_checksigverify(vm)

View Source
op_checksigverify(t()) :: t()

Runs op_checksig/1 and op_verify/1.

Link to this function

op_depth(vm)

View Source
op_depth(t()) :: t()

Counts the stack lenth and puts the result on the top of the stack.

Link to this function

op_div(vm)

View Source
op_div(t()) :: t()

The top stack element is divided from the second, and both are replaced on the stack with the result.

Link to this function

op_drop(vm)

View Source
op_drop(t()) :: t()

Removes the top item from the stack.

Link to this function

op_dup(vm)

View Source
op_dup(t()) :: t()

Duplicates the top item on the stack.

Link to this function

op_else(vm)

View Source
op_else(t()) :: t()

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.

Link to this function

op_endif(vm)

View Source
op_endif(t()) :: t()

Ends the current IF/ELSE block. All blocks must end or the script is invalid.

Link to this function

op_equal(vm)

View Source
op_equal(t()) :: t()

Compares the equality of the top two stack items and replaces them with the boolean result.

Link to this function

op_equalverify(vm)

View Source
op_equalverify(t()) :: t()

Runs op_equal/1 and op_verify/1.

Link to this function

op_fromaltstack(vm)

View Source
op_fromaltstack(t()) :: t()

Removes the top of the alt stack and puts it into the stack.

Link to this function

op_greaterthan(vm)

View Source
op_greaterthan(t()) :: t()

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.

Link to this function

op_greaterthanorequal(vm)

View Source
op_greaterthanorequal(t()) :: t()

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.

Link to this function

op_hash160(vm)

View Source
op_hash160(t()) :: t()

The top element on the stack is hashed using SHA256 then RIPEMD160.

Link to this function

op_hash256(vm)

View Source
op_hash256(t()) :: t()

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.

Link to this function

op_ifdup(vm)

View Source
op_ifdup(t()) :: t()

Duplicates the top stack item if it is truthy.

Link to this function

op_invert(vm)

View Source
op_invert(t()) :: t()

Flips all bits on the top element of the stack.

Link to this function

op_lessthan(vm)

View Source
op_lessthan(t()) :: t()

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.

Link to this function

op_lessthanorequal(vm)

View Source
op_lessthanorequal(t()) :: t()

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.

Link to this function

op_lshift(vm)

View Source
op_lshift(t()) :: t()

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.

Link to this function

op_max(vm)

View Source
op_max(t()) :: t()

Compares numeric value of the top two stack elements, and both are replaced with the greater value.

Link to this function

op_min(vm)

View Source
op_min(t()) :: t()

Compares numeric value of the top two stack elements, and both are replaced with the smaller value.

Link to this function

op_mod(vm)

View Source
op_mod(t()) :: t()

The top stack element is divided from the second, and both are replaced on the stack with the remainder.

Link to this function

op_mul(vm)

View Source
op_mul(t()) :: t()

The top two stack elements are multiplied and replaced on the stack with the result.

Link to this function

op_negate(vm)

View Source
op_negate(t()) :: t()

Negates the numeric value of the top stack element.

Link to this function

op_nip(vm)

View Source
op_nip(t()) :: t()

Removes the second to top item from the stack.

Link to this function

op_nop(vm)

View Source
op_nop(t()) :: t()

No op. Does nothing and returns the vm.

Link to this function

op_not(vm)

View Source
op_not(t()) :: t()

If the top stack element is false it is made true. Otherwise it is made false.

Link to this function

op_notif(vm)

View Source
op_notif(t()) :: t()

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.

Link to this function

op_num2bin(vm)

View Source
op_num2bin(t()) :: t()

Converts the second from top stack item into a binary of the length given in the top stack item.

Link to this function

op_numequal(vm)

View Source
op_numequal(t()) :: t()

Compares the equality of the numeric value of the top two stack items and replaces them with the boolean result.

Link to this function

op_numequalverify(vm)

View Source
op_numequalverify(t()) :: t()

Runs op_numequal/1 and op_verify/1.

Link to this function

op_numnotequal(vm)

View Source
op_numnotequal(t()) :: t()

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.

Link to this function

op_over(vm)

View Source
op_over(t()) :: t()

Copies the second to top stack item to the top.

Link to this function

op_pick(vm)

View Source
op_pick(t()) :: t()

Removes the top stack item and uses it as an index length, then copies the nth item on the stack to the top.

Link to this function

op_pushdata(vm, data)

View Source
op_pushdata(t(), binary() | number()) :: t()

Generic pushdata. Pushes any given binary or integer to the stack.

Link to this function

op_reserved(vm)

View Source
op_reserved(t()) :: t()

Unused. Makes transaction invalid.

Link to this function

op_return(vm, chunks)

View Source
op_return(t(), list()) :: t()

Returns the vm and no further statements are evaluated.

Link to this function

op_ripemd160(vm)

View Source
op_ripemd160(t()) :: t()

The top element on the stack is hashed using RIPEMD160.

Link to this function

op_roll(vm)

View Source
op_roll(t()) :: t()

Removes the top stack item and uses it as an index length, then moves the nth item on the stack to the top.

Link to this function

op_rot(vm)

View Source
op_rot(t()) :: t()

Rotates the top three items on the stack, effictively moving the 3rd item to the top of the stack.

Link to this function

op_rshift(vm)

View Source
op_rshift(t()) :: t()

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.

Link to this function

op_sha1(vm)

View Source
op_sha1(t()) :: t()

The top element on the stack is hashed using SHA1.

Link to this function

op_sha256(vm)

View Source
op_sha256(t()) :: t()

The top element on the stack is hashed using SHA256.

Link to this function

op_size(vm)

View Source
op_size(t()) :: t()

Pushes the byte length of the top element of the stack, without popping it.

Link to this function

op_split(vm)

View Source
op_split(t()) :: t()

Splits the second from top stack item by the index given in the top stack item.

Link to this function

op_sub(vm)

View Source
op_sub(t()) :: t()

The top stack element is subtracted from the second, and both are replaced on the stack with the result.

Link to this function

op_swap(vm)

View Source
op_swap(t()) :: t()

Rotates the top two items on the stack, effectively moving the 2nd item to the top of the stack.

Link to this function

op_toaltstack(vm)

View Source
op_toaltstack(t()) :: t()

Removes the top of the stack and puts it into the alt stack.

Link to this function

op_tuck(vm)

View Source
op_tuck(t()) :: t()

Copies the top item on the stack and inserts it before the second to top item.

Link to this function

op_ver(vm)

View Source
op_ver(t()) :: t()

Puts the version of the protocol under which this transaction will be evaluated onto the stack. DISABLED

Link to this function

op_verif(vm)

View Source
op_verif(t()) :: t()

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

Link to this function

op_verify(vm)

View Source
op_verify(t()) :: t()

Mmarks the script as invalid unless the stack is truthy. Removes the top of the stack.

Link to this function

op_vernotif(vm)

View Source
op_vernotif(t()) :: t()

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

Link to this function

op_within(vm)

View Source
op_within(t()) :: t()

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.

Link to this function

op_xor(vm)

View Source
op_xor(t()) :: t()

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.