Companion

object Companion

All of the Script Virtual Machine opcodes, and some helper functions to create opcodes, are defined in the companion object of this class.

Types

Link copied to clipboard
data class ParsedInst(val inst: ByteArray, val data: ByteArray?, val number: Long?)

Convert from an opcode into a assembly instruction

Properties

Link copied to clipboard
val ABS: OP

The input is made positive. Input: a b Output: out

Link copied to clipboard

Push the bytecode currently being evaluated, beginning after the last executed OP_CODESEPARATOR, to the stack1. For Pay-to-Script-Hash (P2SH) evaluations, this is the redeem bytecode of the Unspent Transaction Output (UTXO) being spent; for all other evaluations, this is the locking bytecode of the UTXO being spent. Input: nothing Output: number

Link copied to clipboard
val ADD: OP

a is added to b. Input: a b Output: out

Link copied to clipboard
val ADD1: OP

1 is added to the input. Input: **** Output: ****

Link copied to clipboard
val AND: OP

Boolean AND between each bit of the inputs Input: x1 x2 Output: out

Link copied to clipboard

Convert a byte string a to a big number, see detailed description (here)[https://spec.nexa.org/nexa/op_bin2bignum.md] Input: little-endian-sign-mag-byte-array Output: big number

Link copied to clipboard
val BIN2NUM: OP

Converts byte sequence x into a numeric value. Input: x Output: out

Link copied to clipboard
val BOOLAND: OP

If both a and b are not 0, the output is 1. Otherwise 0. Input: a b Output: true/false

Link copied to clipboard
val BOOLOR: OP

If a or b is not 0, the output is 1. Otherwise 0. Input: a b Output: true/false

Link copied to clipboard
val C0: OP

Pushes 0 onto the stack Input: nothing Output: 0

Link copied to clipboard
val C1: OP

Pushes 1 onto the stack Input: nothing Output: 1

Link copied to clipboard
val C10: OP

Pushes 10 onto the stack Input: nothing Output: 10

Link copied to clipboard
val C11: OP

Pushes 11 onto the stack Input: nothing Output: 11

Link copied to clipboard
val C12: OP

Pushes 12 onto the stack Input: nothing Output: 12

Link copied to clipboard
val C13: OP

Pushes 13 onto the stack Input: nothing Output: 13

Link copied to clipboard
val C14: OP

Pushes 14 onto the stack Input: nothing Output: 14

Link copied to clipboard
val C15: OP

Pushes 15 onto the stack Input: nothing Output: 15

Link copied to clipboard
val C16: OP

Pushes 16 onto the stack Input: nothing Output: 16

Link copied to clipboard
val C2: OP

Pushes 2 onto the stack Input: nothing Output: 2

Link copied to clipboard
val C3: OP

Pushes 3 onto the stack Input: nothing Output: 3

Link copied to clipboard
val C4: OP

Pushes 4 onto the stack Input: nothing Output: 4

Link copied to clipboard
val C5: OP

Pushes 5 onto the stack Input: nothing Output: 5

Link copied to clipboard
val C6: OP

Pushes 6 onto the stack Input: nothing Output: 6

Link copied to clipboard
val C7: OP

Pushes 7 onto the stack Input: nothing Output: 7

Link copied to clipboard
val C8: OP

Pushes 8 onto the stack Input: nothing Output: 8

Link copied to clipboard
val C9: OP

Pushes 9 onto the stack Input: nothing Output: 9

Link copied to clipboard
val CAT: OP

Concatenates two byte sequences Input: x1 x2 Output: out

Link copied to clipboard

Check if signature is valid for message and a public key. See checkdatasig Input: sig msg pubkey Output: true or false

Link copied to clipboard

Same as CHECKDATASIG, but runs VERIFY afterward. Input: sig msg pubkey Output: nothing or fail

Link copied to clipboard

Marks transaction as invalid if the top stack item is greater than the transaction’s nLockTime field, otherwise script evaluation continues as though an OP_NOP was executed. Transaction is also invalid if 1. the stack is empty; or 2. the top stack item is negative; or 3. the top stack item is greater than or equal to 500000000 while the transaction’s nLockTime field is less than 500000000, or vice versa; or 4. the input’s nSequence field is equal to 0xffffffff. The precise semantics are described in BIP65. Input: x Output: x or fail

Link copied to clipboard

Signatures are checked against public keys. Signatures must be placed in the unlocking script using the same order as their corresponding public keys were placed in the locking script or redeem script. If all signatures are valid, 1 is returned, 0 otherwise. All elements are removed from the stack. For more information on the execution of this opcode, see (Multisignature)https://spec.nexa.org/blockchain/cryptography/multisignature. Input: bitmap sig1 sig2 … #-of-sigs pub1 pub2 … #-of-pubkeys Output: true or false

Link copied to clipboard

Same as OP_CHECKMULTISIG, but OP_VERIFY is executed afterward. Input: bitmap sig1 sig2 … #-of-sigs pub1 pub2 … #-of-pubkeys Output: nothing or fail

Link copied to clipboard

Marks transaction as invalid if the relative lock time of the input (enforced by BIP68 with nSequence) is not equal to or longer than the value of the top stack item. The precise semantics are described in BIP112. Input: x Output: x or fail

Link copied to clipboard

Sig is a Schnorr signature concatenated to a sighash type specifier. The sighash for this input is calculated based on the sighash type. The validity of the Schnorr signature for this hash and public key is checked. If it is valid, 1 is returned, if it is empty, 0 is returned, otherwise the operation fails. Input: sig pubkey Output: true or false

Link copied to clipboard

Same as CHECKSIG, but VERIFY is executed afterward. Input: sig pubkey Output: nothing or fail

Link copied to clipboard
val CNEG1: OP

Pushes -1 onto the stack Input: nothing Output: -1

Link copied to clipboard

Makes OP_CHECK(MULTI)SIG(VERIFY) use the subset of the script of everything after the most recently-executed OP_CODESEPARATOR when computing the sighash. Input: nothing Output: nothing

Link copied to clipboard
val DEPTH: OP

Puts the number of stack items onto the stack. Input: nothing Output: stack size

Link copied to clipboard
val DIV: OP

a is divided by b. Input: a b Output: out

Link copied to clipboard
val DIV2: OP
Link copied to clipboard
val DROP: OP

Removes the top stack item. Input: x Output: nothing

Link copied to clipboard
val DROP2: OP

Removes the top two stack items. Input: x1 x2 Output: nothing

Link copied to clipboard
val DUP: OP

Duplicates the top stack item. Input: x Output: x x

Link copied to clipboard
val DUP2: OP

Duplicates the top two stack items. Input: x1 x2 Output: x1 x2 x1 x2

Link copied to clipboard
val DUP3: OP

Duplicates the top three stack items. Input: x1 x2 x3 Output: x1 x2 x3 x1 x2 x3

Link copied to clipboard
val ELSE: OP

IF statements [ELSE statements] ENDIF If the preceding OP_IF or OP_NOTIF or OP_ELSE was not executed then these statements are and if the preceding OP_IF or OP_NOTIF or OP_ELSE was executed then these statements are not.

Link copied to clipboard
val ENDIF: OP

IF statements [ELSE statements] ENDIF Ends an if/else block. All blocks must end, or the transaction is marked as invalid. An OP_ENDIF without OP_IF earlier is also invalid.

Link copied to clipboard
val EQUAL: OP

Returns 1 if the inputs are exactly equal, comparing bits, 0 otherwise. Input: x1 x2 Output: true or false

Link copied to clipboard

Same as OP_EQUAL, but runs OP_VERIFY afterward. Input: x1 x2 Output: nothing or fail

Link copied to clipboard
val EXEC: OP

OP_EXEC executes a subscript that is presented as data in a script, see (OP_EXEC description)[https://spec.nexa.org/nexa/op_exec] for more details Input: code param1…paramN N_Params M_Returns Output: M values

Link copied to clipboard

Puts the input onto the top of the main stack. Removes it from the alt stack. Input: (alt) x1 Output: x1

Link copied to clipboard

Returns 1 if a is greater than b, 0 otherwise. Input: a b Output: true/false

Link copied to clipboard

Returns 1 if a is greater than or equal to b, 0 otherwise. Input: a b Output: true/false

Link copied to clipboard
val HASH160: OP

Hashes input with SHA-256 and then with RIPEMD-160. Input: in Output: hash

Link copied to clipboard
val HASH256: OP

Hashes input twice with SHA-256. Input: in Output: hash

Link copied to clipboard
val IF: OP

IF statements [ELSE statements] ENDIF If the top stack value is not False, the statements are executed. The top stack value is removed.

Link copied to clipboard
val IFDUP: OP

If the top stack value is not 0, duplicate it. Input: x1 Output: x1 or x1 x1

Link copied to clipboard

Pop the top item from the stack as an input index (Script Number). Push the unlocking bytecode of the input at that index to the stack. Input: index Output: script

Link copied to clipboard

Push the index of the input being evaluated to the stack as a Script Number. Input: nothing Output: number

Link copied to clipboard

Pop the top item from the stack as an input index (Script Number). Push the sequence number of the input at that index to the stack as a Script Number. Input: index Output: number

Link copied to clipboard
val INVERT: OP
Link copied to clipboard
val LAST_DATAPUSH: Int = 75
Link copied to clipboard

Returns 1 if a is less than b, 0 otherwise. Input: a b Output: true/false

Link copied to clipboard

Returns 1 if a is less than or equal to b, 0 otherwise. Input: a b Output: true/false

Link copied to clipboard
val LSHIFT: OP

Shifts a left b bits, preserving sign. Input: a b Output: out

Link copied to clipboard
val MAX: OP

Returns the larger of a and b. Input: a b Output: out

Link copied to clipboard
val MIN: OP

Returns the smaller of a and b. Input: a b Output: out

Link copied to clipboard
val MOD: OP

Returns the remainder after a is divided by b. Input: a b Output: out

Link copied to clipboard
val MUL: OP

a is multiplied by b. Input: a b Output: out

Link copied to clipboard
val MUL2: OP
Link copied to clipboard
val NEGATE: OP

The sign of the input is flipped. Input: **** Output: ****

Link copied to clipboard
val NIP: OP

Removes the second-to-top stack item. Input: x1 x2 Output: x2

Link copied to clipboard
val NOP: OP

do nothing

Link copied to clipboard
val NOT: OP

If the input is 0 or 1, it is flipped. Otherwise the output will be 0. Input: in Output: true/false

Link copied to clipboard

Returns 0 if the input is 0. 1 otherwise. Input: in Output: true/false

Link copied to clipboard
val NOTIF: OP

NOTIF statements [ELSE statements] ENDIF If the top stack value is False, the statements are executed. The top stack value is removed.

Link copied to clipboard
val NUM2BIN: OP

Converts numeric value x1 into byte sequence of length n. This operator will handle also BigNum conversion Input: x1 n Output: out

Link copied to clipboard

Returns 1 if the numbers are equal, 0 otherwise. Input: a b Output: true/false

Link copied to clipboard

Same as OP_NUMEQUAL, but runs OP_VERIFY afterward. Input: a b Output: nothing or fail

Link copied to clipboard

Returns 1 if the numbers are not equal, 0 otherwise. Input: a b Output: true/false

Link copied to clipboard
val OR: OP

Boolean OR between each bit of the inputs. Input: x1 x2 Output: out

Link copied to clipboard

Pop the top item from the stack as an input index (Script Number). From that input, push the outpoint transaction hash - the hash of the transaction which created the Unspent Transaction Output (UTXO) which is being spent - to the stack in OP_HASH256 byte order. Input: index Output: hash

Link copied to clipboard

Pop the top item from the stack as an output index (Script Number). Push the locking bytecode of the output at that index to the stack. Input: index Output: script

Link copied to clipboard

Pop the top item from the stack as an output index (Script Number). Push the value (in satoshis) of the output at that index to the stack as a Script Number. Input: index Output: number

Link copied to clipboard
val OVER: OP

Copies the second-to-top stack item to the top. Input: x1 x2 Output: x1 x2 x1

Link copied to clipboard
val OVER2: OP

Copies the pair of items two spaces back in the stack to the front. Input: x1 x2 x3 x4 Output: x1 x2 x3 x4 x1 x2

Link copied to clipboard
val PICK: OP

The item n back in the stack is copied to the top. Input: xn … x2 x1 x0 n Output: xn … x2 x1 x0 xn

Link copied to clipboard
val PLACE: OP

Copies item count postion back in the stack. This opcode is the inverse of PICK Input: item n Output: nothing

Link copied to clipboard

Place information about the current transaction onto the stack, see detailed description (here)[https://spec.nexa.org/nexa/op_push_tx_state.md] Input: dataSpecifier Output: data

Link copied to clipboard

The next byte contains the number of bytes to be pushed onto the stack.

Link copied to clipboard

The next two little-endian bytes contain the number of bytes to be pushed onto the stack.

Link copied to clipboard

The next four bytes form a little-endian number of bytes to be pushed onto the stack in little endian order.

Link copied to clipboard

The FALSE or 0 opcode. Input: nothing, Output: 0

Link copied to clipboard

The TRUE or 1 opcode. Input: nothing, Output: **1

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val RETURN: OP

Marks the output as unspendable. A standard way of attaching extra data to transactions is to add a zero-value output with a scriptPubKey consisting of OP_RETURN followed by data. Output: fail

Link copied to clipboard
val REVERSE: OP

Reverses the order of the bytes in byte sequence x so that the first byte is now its last byte, the second is now its second-to-last, and so forth. Input: x Output: out

Link copied to clipboard

Hashes input with RIPEMD-160. Input: in Output: hash

Link copied to clipboard
val ROLL: OP

The item n back in the stack is moved to the top. Input: xn … x2 x1 x0 n Output: x(n-1) … x2 x1 x0 xn

Link copied to clipboard
val ROT: OP

The top three items on the stack are rotated in the direction such that the top item becomes the 2nd to top. Input: x1 x2 x3 Output: x2 x3 x1

Link copied to clipboard
val ROT2: OP

The fifth and sixth items back are moved to the top of the stack. Input: x1 x2 x3 x4 x5 x6 Output: x3 x4 x5 x6 x1 x2

Link copied to clipboard
val RSHIFT: OP

Shifts a right b bits, preserving sign. Input: a b Output: out

Link copied to clipboard
val SETBMD: OP

Sets d as the BigNum modulo divisor see detailed description (here)[https://spec.nexa.org/nexa/op_setbmd.md] Input: d Output: nothing

Link copied to clipboard
val SHA1: OP

Hashes input with SHA-1. Input: in Output: hash

Link copied to clipboard
val SHA256: OP

Hashes input with SHA-256. Input: in Output: hash

Link copied to clipboard
val SIZE: OP

Pushes the string length of the top element of the stack (without popping it). Input: x1 Output: x1 size

Link copied to clipboard
val SPLIT: OP

Splits byte sequence x at position n. x1 contains the byte sequence [0, n), and x2 contains [n, end). For example, ‘abc’ 0 OP_SPLIT produces the stack ‘’, ‘abc’. Input: x n Output: x1 x2

Link copied to clipboard
val SUB: OP

b is subtracted from a. Input: a b Output: out

Link copied to clipboard
val SUB1: OP

1 is subtracted from the input. Input: **** Output: ****

Link copied to clipboard
val SWAP: OP

The top two items on the stack are swapped. Input: x1 x2 Output: x2 x1

Link copied to clipboard
val SWAP2: OP

Swaps the top two pairs of items. Input: x1 x2 x3 x4 Output: x3 x4 x1 x2

Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard

Puts the input onto the top of the alt stack. Removes it from the main stack. Input: x1 Output: (alt) x1

Link copied to clipboard
val TUCK: OP

The item at the top of the stack is copied and inserted below the second-to-top item. Input: x1 x2 Output: x2 x1 x2

Link copied to clipboard

Push the count of inputs in the current transaction to the stack as a Script Number. Input: nothing Output: number

Link copied to clipboard

Push the locktime of the current transaction to the stack as a Script Number. Input: nothing Output: number

Link copied to clipboard

Push the count of outputs in the current transaction to the stack as a Script Number. Input: nothing Output: number

Link copied to clipboard

Push the version of the current transaction to the stack as a Script Number. Input: nothing Output: number

Link copied to clipboard

Pop the top item from the stack as an input index (Script Number). Push the full locking bytecode of the Unspent Transaction Output (UTXO) spent by that input to the stack. Input: index Output: script

Link copied to clipboard

Pop the top item from the stack as an input index (Script Number). Push the value (in satoshis) of the Unspent Transaction Output (UTXO) spent by that input to the stack as a Script Number. Input: index Output: number

Link copied to clipboard
val VER: OP

disabled

Link copied to clipboard
val VERIF: OP
Link copied to clipboard
val VERIFY: OP

Marks transaction as invalid if top stack value is not true. The top stack value is removed. Input: true/false, Output: nothing/fail

Link copied to clipboard
Link copied to clipboard
val XOR: OP

Boolean EXCLUSIVE OR between each bit of the inputs. Input: x1 x2 Output: out

Functions

Link copied to clipboard
fun compile(opcodes: List<OP>): ByteArray

compile list of opcodes into a binary program

Link copied to clipboard
fun decompile(scriptBytes: ByteArray): List<OP>

decompile a byte array (that is Nexa script) into a list of opcodes

Link copied to clipboard

If the opcode is a "direct numeric push" (opcodes OP1NEGATE -> OP16), return the value OP code represents as ByteArray. Otherwise, null.

Link copied to clipboard
fun groupAmount(value: Long): OP

Little endian serialization to 2, 4, or 8 bytes as defined in the Group Tokenization Consensus Specification

Link copied to clipboard

Return TRUE if the passed byte is a constant quantity pushdata opcode

Link copied to clipboard

Parse a byte array containing 1 instruction into an easy to use structure

Link copied to clipboard
fun push(data: ByteArray): OP
fun push(num: Int): OP
fun push(num: Long): OP
fun push(num: UInt): OP
fun push(num: ULong): OP

Create the appropriate operation to push the passed parameter to the stack

Link copied to clipboard

Convert from an opcode into a assembly instruction