[][src]Enum bitcoin_cash_base::Opcode

pub enum Opcode {
    OP_0,
    OP_PUSHDATA1,
    OP_PUSHDATA2,
    OP_PUSHDATA4,
    OP_1NEGATE,
    OP_RESERVED,
    OP_1,
    OP_2,
    OP_3,
    OP_4,
    OP_5,
    OP_6,
    OP_7,
    OP_8,
    OP_9,
    OP_10,
    OP_11,
    OP_12,
    OP_13,
    OP_14,
    OP_15,
    OP_16,
    OP_NOP,
    OP_VER,
    OP_IF,
    OP_NOTIF,
    OP_VERIF,
    OP_VERNOTIF,
    OP_ELSE,
    OP_ENDIF,
    OP_VERIFY,
    OP_RETURN,
    OP_TOALTSTACK,
    OP_FROMALTSTACK,
    OP_2DROP,
    OP_2DUP,
    OP_3DUP,
    OP_2OVER,
    OP_2ROT,
    OP_2SWAP,
    OP_IFDUP,
    OP_DEPTH,
    OP_DROP,
    OP_DUP,
    OP_NIP,
    OP_OVER,
    OP_PICK,
    OP_ROLL,
    OP_ROT,
    OP_SWAP,
    OP_TUCK,
    OP_CAT,
    OP_SPLIT,
    OP_NUM2BIN,
    OP_BIN2NUM,
    OP_SIZE,
    OP_INVERT,
    OP_AND,
    OP_OR,
    OP_XOR,
    OP_EQUAL,
    OP_EQUALVERIFY,
    OP_RESERVED1,
    OP_RESERVED2,
    OP_1ADD,
    OP_1SUB,
    OP_2MUL,
    OP_2DIV,
    OP_NEGATE,
    OP_ABS,
    OP_NOT,
    OP_0NOTEQUAL,
    OP_ADD,
    OP_SUB,
    OP_MUL,
    OP_DIV,
    OP_MOD,
    OP_LSHIFT,
    OP_RSHIFT,
    OP_BOOLAND,
    OP_BOOLOR,
    OP_NUMEQUAL,
    OP_NUMEQUALVERIFY,
    OP_NUMNOTEQUAL,
    OP_LESSTHAN,
    OP_GREATERTHAN,
    OP_LESSTHANOREQUAL,
    OP_GREATERTHANOREQUAL,
    OP_MIN,
    OP_MAX,
    OP_WITHIN,
    OP_RIPEMD160,
    OP_SHA1,
    OP_SHA256,
    OP_HASH160,
    OP_HASH256,
    OP_CODESEPARATOR,
    OP_CHECKSIG,
    OP_CHECKSIGVERIFY,
    OP_CHECKMULTISIG,
    OP_CHECKMULTISIGVERIFY,
    OP_NOP1,
    OP_CHECKLOCKTIMEVERIFY,
    OP_CHECKSEQUENCEVERIFY,
    OP_NOP4,
    OP_NOP5,
    OP_NOP6,
    OP_NOP7,
    OP_NOP8,
    OP_NOP9,
    OP_NOP10,
    OP_CHECKDATASIG,
    OP_CHECKDATASIGVERIFY,
    OP_REVERSEBYTES,
    FIRST_UNDEFINED_OP_VALUE,
}

All opcodes which can be used in a Bitcoin Cash script.

Can be used in #[bitcoin_cash::script] functions.

Example

use bitcoin_cash::{Opcode::*, Address, ByteArray, Hashed};
struct Params {
  address: Address<'static>,
}
#[bitcoin_cash::script(P2pkhInputs)]
fn p2pkh_script(params: &Params, signature: ByteArray, public_key: ByteArray) {
  OP_DUP(public_key);
  let pkh = OP_HASH160(public_key);
  let address = { params.address.hash().as_slice() };
  OP_EQUALVERIFY(pkh, address);
  OP_CHECKSIG(signature, public_key);
}

Variants

OP_0
OP_0() -> Integer

Pushes the integer 0 onto the stack.

Usage:

let zero = OP_0;
let expected = 0;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(zero, expected);
OP_PUSHDATA1

Pushes the next byte number of bytes. Not to be used in #[bitcoin_cash::script] functions.

OP_PUSHDATA2

Pushes the next two byte number of bytes. Not to be used in #[bitcoin_cash::script] functions.

OP_PUSHDATA4

Pushes the next four byte number of bytes. Not to be used in #[bitcoin_cash::script] functions.

OP_1NEGATE
OP_1NEGATE() -> Integer

Pushes the integer -1 onto the stack.

Usage:

// push -1
let minus_one = OP_1NEGATE;

let expected = -1;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(minus_one, expected);
OP_RESERVED

Reserved opcode. Fails script if in executed branch immediately.

OP_1
OP_1() -> Integer

Pushes the integer 1 onto the stack.

Usage:

// push 1
let one = OP_1;

let expected = 1;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(one, expected);
OP_2
OP_2() -> Integer

Pushes the integer 2 onto the stack.

Usage:

// push 2
let two = OP_2;

let expected = 2;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(two, expected);
OP_3
OP_3() -> Integer

Pushes the integer 3 onto the stack.

Usage:

// push 3
let three = OP_3;

let expected = 3;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(three, expected);
OP_4
OP_4() -> Integer

Pushes the integer 4 onto the stack.

Usage:

// push 4
let four = OP_4;

let expected = 4;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(four, expected);
OP_5
OP_5() -> Integer

Pushes the integer 5 onto the stack.

Usage:

// push 5
let five = OP_5;

let expected = 5;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(five, expected);
OP_6
OP_6() -> Integer

Pushes the integer 6 onto the stack.

Usage:

// push 6
let six = OP_6;

let expected = 6;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(six, expected);
OP_7
OP_7() -> Integer

Pushes the integer 7 onto the stack.

Usage:

// push 7
let seven = OP_7;

let expected = 7;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(seven, expected);
OP_8
OP_8() -> Integer

Pushes the integer 8 onto the stack.

Usage:

// push 8
let eight = OP_8;

let expected = 8;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(eight, expected);
OP_9
OP_9() -> Integer

Pushes the integer 9 onto the stack.

Usage:

// push 9
let nine = OP_9;

let expected = 9;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(nine, expected);
OP_10
OP_10() -> Integer

Pushes the integer 10 onto the stack.

Usage:

// push 10
let ten = OP_10;

let expected = 10;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(ten, expected);
OP_11
OP_11() -> Integer

Pushes the integer 11 onto the stack.

Usage:

// push 11
let eleven = OP_11;

let expected = 11;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(eleven, expected);
OP_12
OP_12() -> Integer

Pushes the integer 12 onto the stack.

Usage:

// push 12
let twelve = OP_12;

let expected = 12;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(twelve, expected);
OP_13
OP_13() -> Integer

Pushes the integer 13 onto the stack.

Usage:

// push 13
let thirteen = OP_13;

let expected = 13;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(thirteen, expected);
OP_14
OP_14() -> Integer

Pushes the integer 14 onto the stack.

Usage:

// push 14
let fourteen = OP_14;

let expected = 14;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(fourteen, expected);
OP_15
OP_15() -> Integer

Pushes the integer 15 onto the stack.

Usage:

// push 15
let fiveteen = OP_15;

let expected = 15;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(fiveteen, expected);
OP_16
OP_16() -> Integer

Pushes the integer 16 onto the stack.

Usage:

// push 16
let sixteen = OP_16;

let expected = 16;  // equivalent (and prefered) way to push numbers
OP_EQUALVERIFY(sixteen, expected);
OP_NOP
OP_NOP() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP();
OP_VER

Reserved opcode. Fails script if in executed branch immediately.

OP_IF
OP_IF(condition: bool) -> ()

Creates a branch (OP_IF/OP_ELSE/OP_ENDIF construct) that will only execute if condition is true and will execute the alternative branch otherwise.

Usage:

let condition = true;

// branch on condition
OP_IF(condition); {
    let result = 5;
} OP_ELSE; {
    let result = 6;
} OP_ENDIF;

let expected = 5;
OP_EQUALVERIFY(result, expected);
OP_NOTIF

Opposite of OP_NOTIF. Currently not supported in #[bitcoin_cash::script] functions.

OP_VERIF

Disabled opcode. Fails script immediately even if not in executed branch.

OP_VERNOTIF

Disabled opcode. Fails script immediately even if not in executed branch.

OP_ELSE
OP_ELSE() -> ()

Demarcates a branch (OP_IF/OP_ELSE/OP_ENDIF construct) that will only execute if condition is true and will execute the alternative branch otherwise.

Usage:

let condition = true;

// branch on condition
OP_IF(condition); {
    let result = 5;
} OP_ELSE; {
    let result = 6;
} OP_ENDIF;

let expected = 5;
OP_EQUALVERIFY(result, expected);
OP_ENDIF
OP_ENDIF() -> ()

Ends a branch (OP_IF/OP_ELSE/OP_ENDIF construct) that will only execute if condition is true and will execute the alternative branch otherwise.

Usage:

let condition = true;

// branch on condition
OP_IF(condition); {
    let result = 5;
} OP_ELSE; {
    let result = 6;
} OP_ENDIF;

let expected = 5;
OP_EQUALVERIFY(result, expected);
OP_VERIFY
OP_VERIFY(condition: bool) -> ()

Marks transaction as invalid if condition is not true.

Usage:

let condition = true;

// verify condition
OP_VERIFY(condition);
OP_RETURN

Fails execution of the script immediately. Used to attach data to transactions.

OP_TOALTSTACK
OP_TOALTSTACK<T>(item: T) -> ()

Moves item to the top of the altstack.

Usage:

let item = b"Bitcoin Cash";

// move item to altstack
OP_TOALTSTACK(item);

let expected = b"Bitcoin Cash";
OP_FROMALTSTACK(item);  // Note: retains name "item" on altstack
OP_EQUALVERIFY(expected, item);
OP_FROMALTSTACK
OP_FROMALTSTACK<T>(altitem: T) -> T

Moves the top item of the altstack to the main stack.

Usage:

let item = b"Bitcoin Cash";
OP_TOALTSTACK(item);
let expected = b"Bitcoin Cash";

// move item back to main stack
OP_FROMALTSTACK(item);  // Note: retains name "item" on main stack

OP_EQUALVERIFY(expected, item);
OP_2DROP
OP_2DROP<T, U>(a: T, b: U) -> ()

Drops the two top stack items.

a b ->

Usage:

let a = b"A";
let b = b"B";
let c = b"C";

// drop b and c
OP_2DROP(b, c);

let expected = b"A";
OP_EQUALVERIFY(a, expected);
OP_2DUP
OP_2DUP<T, U>(a: T, b: U) -> (T, U, T, U)

Duplicates the top two stack items.

a b -> a b a b

Usage:

let a = b"A";
let b = b"B";

// duplicate a and b
OP_2DUP(a, b);  // Note: duplicated items retain name

let expected = b"A";
OP_EQUALVERIFY(b, expected);
let expected = b"B";
OP_EQUALVERIFY(a, expected);
let expected = b"A";
OP_EQUALVERIFY(b, expected);
let expected = b"B";
OP_EQUALVERIFY(a, expected);
OP_3DUP
OP_3DUP<T, U, V>(a: T, b: U, c: V) -> (T, U, V, T, U, V)

Duplicates the top two three items.

a b c -> a b c a b c

Usage:

let a = b"A";
let b = b"B";
let c = b"C";

// duplicate a, b and c
OP_3DUP(a, b, c);  // Note: duplicated items retain name

let expected = b"C";
OP_EQUALVERIFY(c, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"C";
OP_EQUALVERIFY(c, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
OP_2OVER
OP_2OVER<T, U, V, W>(a: T, b: U, c: V, d: W) -> (T, U, V, W, T, U)

Copies the pair of items two spaces back in the stack to the front.

a b _ _ -> a b _ _ a b

Usage:

let a = b"A";
let b = b"B";
let c = b"C";
let d = b"D";

// duplicate a and b
OP_2OVER(a, b, __, __);  // Note: duplicated items retain name

let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"D";
OP_EQUALVERIFY(d, expected);
let expected = b"C";
OP_EQUALVERIFY(c, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
OP_2ROT
OP_2ROT<T, U, V, W, X, Y>(a: T, b: U, c: V, d: W, e: X, f: Y) -> (V, W, X, Y, T, U)

The fifth and sixth items back are moved to the top of the stack.

a b _ _ _ _ -> _ _ _ _ a b

Usage:

let a = b"A";
let b = b"B";
let c = b"C";
let d = b"D";
let e = b"E";
let f = b"F";

// move a and b to the top
OP_2ROT(a, b, __, __, __, __);  // Note: moved items retain name

let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"F";
OP_EQUALVERIFY(f, expected);
let expected = b"E";
OP_EQUALVERIFY(e, expected);
let expected = b"D";
OP_EQUALVERIFY(d, expected);
let expected = b"C";
OP_EQUALVERIFY(c, expected);
OP_2SWAP
OP_2SWAP<T, U, V, W>(a: T, b: U, c: V, d: W) -> (V, W, T, U)

Swaps the top two pairs of items.

a b c d -> c d a b

Usage:

let a = b"A";
let b = b"B";
let c = b"C";
let d = b"D";

// swap a and b with c and d
OP_2SWAP(a, b, c, d);  // Note: moved items retain name

let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"D";
OP_EQUALVERIFY(d, expected);
let expected = b"C";
OP_EQUALVERIFY(c, expected);
OP_IFDUP

If the top stack value is true, duplicate it. Not to be used in #[bitcoin_cash::script] functions.

OP_DEPTH
OP_DEPTH() -> Integer

Returns the number of stack items.

Usage:

let a = b"A";
let b = b"B";
let c = b"C";

// number of items on the stack
let n = OP_DEPTH();

let expected = 3;
OP_EQUALVERIFY(n, expected);
OP_DROP
OP_DROP<T>(a: T) -> ()

Drops the top stack item.

a ->

Usage:

let a = b"A";
let b = b"B";

// drop b
OP_DROP(b);

let expected = b"A";
OP_EQUALVERIFY(a, expected);
OP_DUP
OP_DUP<T>(a: T) -> (T, T)

Duplicates the top stack item.

a -> a a

Usage:

let a = b"A";

// drop b and c
OP_DUP(a);  // Note: duplicated item retains name

let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
OP_NIP
OP_NIP<T, U>(a: T, b: U) -> T

Removes the second-to-top stack item.

a b -> b

Usage:

let a = b"A";
let b = b"B";

// drop a
OP_NIP(a, __);

let expected = b"B";
OP_EQUALVERIFY(b, expected);
OP_OVER
OP_OVER<T, U>(a: T, b: U) -> (T, U, T)

Copies the second-to-top stack item to the top of the stack.

a b -> a b a

Usage:

let a = b"A";
let b = b"B";

// copy a
OP_OVER(a, __);  // Note: duplicated item retains name

let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
OP_PICK
OP_PICK<T>(n: Integer) -> T

The item n back in the stack is copied to the top.

xₙ ... x₂ x₁ x₀ <n> -> xₙ ... x₂ x₁ x₀ xₙ

Usage:

let a = b"A";
let b = b"B";

// calculate depth of a
let depth_a = depth_of(a);
// copy a
OP_PICK(depth_a);  // Note: duplicated item retains name

let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
OP_ROLL
OP_ROLL<T>(n: Integer) -> T

The item n back in the stack is moved to the top.

xₙ ... x₂ x₁ x₀ <n> -> xₙ₋₁ ... x₂ x₁ x₀ xₙ

Usage:

let a = b"A";
let b = b"B";

// calculate depth of a
let depth_a = depth_of(a);
// move a to the top of the stack
OP_ROLL(depth_a);  // Note: moved item retains name

let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
OP_ROT
OP_ROT<T, U, V>(a: T, b: U, c: V) -> (U, V, T)

The third-to-top item is moved to the top.

a b c -> b c a

Usage:

let a = b"A";
let b = b"B";
let c = b"C";

// move a to the top
OP_ROT(a, __, __);

let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"C";
OP_EQUALVERIFY(c, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
OP_SWAP
OP_SWAP<T, U>(a: T, b: U) -> (U, T)

The top two items on the stack are swapped.

a b -> b a

Usage:

let a = b"A";
let b = b"B";

// swap a and b
OP_SWAP(a, b);

let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
OP_TUCK
OP_TUCK<T, U>(a: T, b: U) -> (U, T, U)

The item at the top of the stack is copied and inserted before the second-to-top item.

a b -> b a b

Usage:

let a = b"A";
let b = b"B";

// insert b before a
OP_TUCK(__, b);

let expected = b"B";
OP_EQUALVERIFY(b, expected);
let expected = b"A";
OP_EQUALVERIFY(a, expected);
let expected = b"B";
OP_EQUALVERIFY(b, expected);
OP_CAT
OP_CAT(left: ByteArray, right: ByteArray) -> ByteArray

Concatenates two byte sequences.

Usage:

let a = b"Bitcoin";
let b = b"Cash";

// concatenate a and b
let ab = OP_CAT(a, b);

let expected = b"BitcoinCash";
OP_EQUALVERIFY(ab, expected);
OP_SPLIT
OP_SPLIT(array: ByteArray, split_index: Integer) -> (ByteArray, ByteArray)

Split array at split_index.

Usage:

let array = b"BitcoinCash";
let split_index = 7;

// split array at index 7
let (a, b) = OP_SPLIT(array, split_index);

let expected = b"Cash";
OP_EQUALVERIFY(b, expected);
let expected = b"Bitcoin";
OP_EQUALVERIFY(a, expected);
OP_NUM2BIN
OP_NUM2BIN(num: Integer, n_bytes: Integer) -> ByteArray

Convert numinto a byte sequence of size n_bytes, taking account of the sign bit. The byte sequence produced uses little-endian encoding.

Usage:

let num = 0x1337;
let n_bytes = 2;

// encode num as 2 bytes
let num2le = OP_NUM2BIN(num, n_bytes);

let expected = b"\x37\x13";
OP_EQUALVERIFY(num2le, expected);
OP_BIN2NUM
OP_BIN2NUM(array: ByteArray) -> Integer

Convert array into a numeric value, including minimal encoding. array must encode the value in little-endian encoding.

Usage:

let num_encoded = b"\x37\x13";

// decode num_encoded
let num = OP_BIN2NUM(num_encoded);

let expected = 0x1337;
OP_EQUALVERIFY(num, expected);
OP_SIZE
OP_SIZE(array: ByteArray) -> (ByteArray, Integer)

Returns the byte length of array, retaining array.

Usage:

let array = b"BitcoinCash";

// calculate size of array
let (__, size) = OP_SIZE(array);

let expected = 11;
OP_EQUALVERIFY(size, expected);
let expected = b"BitcoinCash";
OP_EQUALVERIFY(array, expected);
OP_INVERT

Disabled opcode. Fails script immediately even if not in executed branch.

OP_AND
OP_AND(a: ByteArray, b: ByteArray) -> ByteArray

Boolean AND between each bit of a and b.

The two operands must be the same size.

Usage:

let a = b"\x0103";
let b = b"\x0302";

// calculate a & b
let bits = OP_AND(a, b);

let expected = b"\x0102";
OP_EQUALVERIFY(bits, expected);
OP_OR
OP_OR(a: ByteArray, b: ByteArray) -> ByteArray

Boolean OR between each bit of a and b.

The two operands must be the same size.

Usage:

let a = b"\x0103";
let b = b"\x0201";

// calculate a | b
let bits = OP_OR(a, b);

let expected = b"\x0203";
OP_EQUALVERIFY(bits, expected);
OP_XOR
OP_XOR(a: ByteArray, b: ByteArray) -> ByteArray

Boolean XOR between each bit of a and b.

The two operands must be the same size.

Usage:

let a = b"\x0103";
let b = b"\x0302";

// calculate a ^ b
let bits = OP_XOR(a, b);

let expected = b"\x0201";
OP_EQUALVERIFY(bits, expected);
OP_EQUAL
OP_EQUAL<T>(a: T, b: T) -> bool

Returns whether a == b.

Usage:

let a = b"BitcoinCash";
let b = b"BitcoinCash";

// check if a == b
let is_equal = OP_EQUAL(a, b);

OP_VERIFY(is_equal);
OP_EQUALVERIFY
OP_EQUALVERIFY<T>(a: T, b: T) -> ()

Verifies that a == b.

Usage:

let a = b"BitcoinCash";
let b = b"BitcoinCash";

// verify that a == b
OP_EQUALVERIFY(a, b);
OP_RESERVED1

Reserved opcode. Fails script if in executed branch immediately.

OP_RESERVED2

Reserved opcode. Fails script if in executed branch immediately.

OP_1ADD
OP_1ADD(a: Integer) -> Integer

Calculates a + 1.

Usage:

let a = 7;

// calculate a + 1
let result = OP_1ADD(a);

let expected = 8;
OP_EQUALVERIFY(result, expected);
OP_1SUB
OP_1SUB(a: Integer) -> Integer

Calculates a - 1.

Usage:

let a = 7;

// calculate a - 1
let result = OP_1SUB(a);

let expected = 6;
OP_EQUALVERIFY(result, expected);
OP_2MUL

Disabled opcode. Fails script immediately even if not in executed branch.

OP_2DIV

Disabled opcode. Fails script immediately even if not in executed branch.

OP_NEGATE
OP_NEGATE(a: Integer) -> Integer

Calculates -a.

Usage:

let a = 7;

// calculate -a
let result = OP_NEGATE(a);

let expected = -7;
OP_EQUALVERIFY(result, expected);
OP_ABS
OP_ABS(a: Integer) -> Integer

Calculates abs(a).

Usage:

let a = -7;

// calculate abs(a)
let result = OP_ABS(a);

let expected = 7;
OP_EQUALVERIFY(result, expected);
OP_NOT
OP_NOT(a: Integer) -> Integer

Calculates !a.

Usage:

let a = false;

// calculate !a
let result = OP_NOT(a);

OP_VERIFY(result);
OP_0NOTEQUAL
OP_0NOTEQUAL(a: Integer) -> bool

Calculates a != 0.

Usage:

let a = 7;

// calculate a != 0
let result = OP_0NOTEQUAL(a);

OP_VERIFY(result);
OP_ADD
OP_ADD(a: Integer, b: Integer) -> Integer

Calculates a + b.

Usage:

let a = 7;
let b = 3;

// calculate a + b
let result = OP_ADD(a, b);

let expected = 10;
OP_EQUALVERIFY(result, expected);
OP_SUB
OP_SUB(a: Integer, b: Integer) -> Integer

Calculates a - b.

Usage:

let a = 7;
let b = 3;

// calculate a - b
let result = OP_SUB(a, b);

let expected = 4;
OP_EQUALVERIFY(result, expected);
OP_MUL

Disabled opcode. Fails script immediately even if not in executed branch.

OP_DIV
OP_DIV(a: Integer, b: Integer) -> Integer

Calculates a / b, truncating the fraction part.

Usage:

let a = 7;
let b = 3;

// calculate a / b
let result = OP_DIV(a, b);

let expected = 2;
OP_EQUALVERIFY(result, expected);
OP_MOD
OP_MOD(a: Integer, b: Integer) -> Integer

Calculates a % b (a modulo b).

Usage:

let a = 7;
let b = 3;

// calculate a % b
let result = OP_MOD(a, b);

let expected = 1;
OP_EQUALVERIFY(result, expected);
OP_LSHIFT

Disabled opcode. Fails script immediately even if not in executed branch.

OP_RSHIFT

Disabled opcode. Fails script immediately even if not in executed branch.

OP_BOOLAND
OP_BOOLAND(a: bool, b: bool) -> bool

Calculates a && b, boolean AND (∧).

Usage:

let a = false;
let b = true;

// calculate a && b
let result = OP_BOOLAND(a, b);

let expected = false;
OP_EQUALVERIFY(result, expected);
OP_BOOLOR
OP_BOOLOR(a: bool, b: bool) -> bool

Calculates a || b, boolean OR (∨).

Usage:

let a = false;
let b = true;

// calculate a || b
let result = OP_BOOLOR(a, b);

OP_VERIFY(result);
OP_NUMEQUAL
OP_NUMEQUAL(a: Integer, b: Integer) -> bool

Calculates a == b.

Usage:

let a = 7;
let b = 7;

// check if a == b
let is_equal = OP_NUMEQUAL(a, b);

OP_VERIFY(is_equal);
OP_NUMEQUALVERIFY
OP_NUMEQUALVERIFY(a: Integer, b: Integer) -> ()

Verifies that a == b.

Usage:

let a = 7;
let b = 7;

// verify that a == b
OP_NUMEQUALVERIFY(a, b);
OP_NUMNOTEQUAL
OP_NUMNOTEQUAL(a: Integer, b: Integer) -> bool

Calculates a != b.

Usage:

let a = 7;
let b = 9;

// check if a == b
let is_not_equal = OP_NUMNOTEQUAL(a, b);

OP_VERIFY(is_not_equal);
OP_LESSTHAN
OP_LESSTHAN(a: Integer, b: Integer) -> bool

Calculates a < b.

Usage:

let a = 7;
let b = 9;

// check if a < b
let is_less = OP_LESSTHAN(a, b);

OP_VERIFY(is_less);
OP_GREATERTHAN
OP_GREATERTHAN(a: Integer, b: Integer) -> bool

Calculates a > b.

Usage:

let a = 11;
let b = 9;

// check if a > b
let is_greater = OP_GREATERTHAN(a, b);

OP_VERIFY(is_greater);
OP_LESSTHANOREQUAL
OP_LESSTHANOREQUAL(a: Integer, b: Integer) -> bool

Calculates a <= b.

Usage:

let a = 7;
let b = 7;

// check if a <= b
let is_at_most = OP_LESSTHANOREQUAL(a, b);

OP_VERIFY(is_at_most);
OP_GREATERTHANOREQUAL
OP_GREATERTHANOREQUAL(a: Integer, b: Integer) -> bool

Calculates a >= b.

Usage:

let a = 9;
let b = 9;

// check if a >= b
let is_at_least = OP_GREATERTHANOREQUAL(a, b);

OP_VERIFY(is_at_least);
OP_MIN
OP_MIN(a: Integer, b: Integer) -> bool

Calculates min(a, b), the lesser of a and b.

Usage:

let a = 3;
let b = 9;

// calculate min(a, b)
let result = OP_MIN(a, b);

let expected = 3;
OP_EQUALVERIFY(result, expected);
OP_MAX
OP_MAX(a: Integer, b: Integer) -> bool

Calculates max(a, b), the greater of a and b.

Usage:

let a = 3;
let b = 9;

// calculate max(a, b)
let result = OP_MAX(a, b);

let expected = 9;
OP_EQUALVERIFY(result, expected);
OP_WITHIN
OP_WITHIN(a: Integer, min: Integer, max: Integer) -> bool

Calculates a >= min && a <= max.

Usage:

let a = 3;
let min = 1;
let max = 9;

// calculate if a is within min and max
let result = OP_WITHIN(a, min, max);

OP_VERIFY(result);
OP_RIPEMD160
OP_RIPEMD160(array: ByteArray) -> ByteArray

Hashes array using RIPEMD-160.

Usage:

let array = b"BitcoinCash";

// calculate ripemd160(array)
let hash = OP_RIPEMD160(array);

let expected = hex!("0d2aa57463e5fac82f97f496ed98525fbec71c4c");
OP_EQUALVERIFY(hash, expected);
OP_SHA1
OP_SHA1(array: ByteArray) -> ByteArray

Hashes array using SHA-1.

Usage:

let array = b"BitcoinCash";

// calculates sha1(array)
let hash = OP_SHA1(array);

let expected = hex!("a7a1986ab925f4d8a81fc0da1352c780ad2f5fe1");
OP_EQUALVERIFY(hash, expected);
OP_SHA256
OP_SHA256(array: ByteArray) -> ByteArray

Hashes array using SHA-256.

Usage:

let array = b"BitcoinCash";

// calculates sha256(array)
let hash = OP_SHA256(array);

let expected = hex!("78e015aa460c0a5be71fe4618c72898200a45a20f9bd7048398971babc3b372b");
OP_EQUALVERIFY(hash, expected);
OP_HASH160
OP_HASH160(array: ByteArray) -> ByteArray

Hashes array using first SHA-256 and then RIPEMD-160.

Usage:

let array = b"BitcoinCash";

// calculates ripemd160(sha256(array))
let hash = OP_HASH160(array);

let expected = hex!("29e99ecb43b5a4c19aa2b05c7d6fc439bca5f023");
OP_EQUALVERIFY(hash, expected);
OP_HASH256
OP_HASH256(array: ByteArray) -> ByteArray

Hashes array twice using SHA-256.

Usage:

let array = b"BitcoinCash";

// calculates sha256(sha256(array))
let hash = OP_HASH256(array);

let expected = hex!("575d8ad02159b76cf2beda18c4ccb9bdb9a7ac894506d97e319c9e5c3096ca37");
OP_EQUALVERIFY(hash, expected);
OP_CODESEPARATOR
OP_CODESEPARATOR() -> ()

Makes OP_CHECK(MULTI)SIG(VERIFY) set scriptCode to everything after the most recently-executed OP_CODESEPARATOR when computing the sighash.

Usage:

let array = b"BitcoinCash";

// removes "BitcoinCash" from scriptCode
OP_CODESEPARATOR();
OP_CHECKSIG
OP_CHECKSIG(public_key: ByteArray, signature: ByteArray) -> bool

The last byte (=sighash type) of signature is removed. The sighash for this input is calculated based on the sighash type. The truncated signature used by OP_CHECKSIG must be a valid ECDSA or Schnorr signature for that hash and public_key. If it is valid, 1 is returned, if it is empty, 0 is returned, otherwise the operation fails.

Usage:

#[bitcoin_cash::script(P2PKInputs)]
fn p2pk(_: (), signature: ByteArray) {
  let public_key = hex!("0201961ef44067e870a9b1684041929caffad57eae6bbc79dd785320d53231f519");

  // check if `signature` signs current sighash for `public_key`
  let success = OP_CHECKSIG(signature, public_key);
}
OP_CHECKSIGVERIFY
OP_CHECKSIGVERIFY(public_key: ByteArray, signature: ByteArray) -> ()

The last byte (=sighash type) of signature is removed. The sighash for this input is calculated based on the sighash type. Verifies the truncated signature used by OP_CHECKSIGVERIFY is a valid ECDSA or Schnorr signature for that hash and public_key.

Usage:

#[bitcoin_cash::script(P2PKInputs)]
fn p2pk(_: (), signature: ByteArray) {
  let public_key = hex!("0201961ef44067e870a9b1684041929caffad57eae6bbc79dd785320d53231f519");

  // verify `signature` signs current sighash for `public_key`
  OP_CHECKSIGVERIFY(signature, public_key);
}
OP_CHECKMULTISIG

Performs a multisig check. Not to be used in #[bitcoin_cash::script] functions.

OP_CHECKMULTISIGVERIFY

Verifies a multisig check. Not to be used in #[bitcoin_cash::script] functions.

OP_NOP1
OP_NOP1() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP1();
OP_CHECKLOCKTIMEVERIFY
OP_CHECKLOCKTIMEVERIFY(locktime: Integer) -> Integer

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.

Usage:

let locktime = 400_000;

// verify locktime
OP_CHECKLOCKTIMEVERIFY(locktime);

let expected = 400_000;
OP_EQUALVERIFY(locktime, expected);
OP_CHECKSEQUENCEVERIFY
OP_CHECKSEQUENCEVERIFY(sequence: Integer) -> Integer

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.

Usage:

let sequence = 1000;

// verify input age
OP_CHECKSEQUENCEVERIFY(sequence);

let expected = 1000;
OP_EQUALVERIFY(sequence, expected);
OP_NOP4
OP_NOP4() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP4();
OP_NOP5
OP_NOP5() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP5();
OP_NOP6
OP_NOP6() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP6();
OP_NOP7
OP_NOP7() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP7();
OP_NOP8
OP_NOP8() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP8();
OP_NOP9
OP_NOP9() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP9();
OP_NOP10
OP_NOP10() -> ()

Does nothing.

Usage:

// do nothing
OP_NOP10();
OP_CHECKDATASIG
OP_CHECKDATASIG(signature: ByteArray, message: ByteArray, public_key: ByteArray) -> bool

Checks whether signature is a valid ECDSA or Schnorr signature for sha256(message) and public_key.

Usage:

let signature = [
    hex!("3045022100f560a6e928ec52e77801a3ea4cbfbe6d89d1fff77a8d2ed00c457af278ae54").as_ref(),
    hex!("01022015cc2b1c92b53cef6afd10e5e5fa33eb66b9d13d82d970a4a951b6e7f1903509").as_ref(),
].concat();
let message = b"BitcoinCash";
let public_key = hex!("0350be375c6e807988a5f575c9776e271e19a79f64681bcfe6a1affde9a5444496");

let is_valid_sig = OP_CHECKDATASIG(signature, message, public_key);
OP_CHECKDATASIGVERIFY
OP_CHECKDATASIGVERIFY(signature: ByteArray, message: ByteArray, public_key: ByteArray) -> ()

Verifies that signature is a valid ECDSA or Schnorr signature for sha256(message) and public_key.

Usage:

let signature = [
    hex!("3045022100f560a6e928ec52e77801a3ea4cbfbe6d89d1fff77a8d2ed00c457af278ae54").as_ref(),
    hex!("01022015cc2b1c92b53cef6afd10e5e5fa33eb66b9d13d82d970a4a951b6e7f1903509").as_ref(),
].concat();
let message = b"BitcoinCash";
let public_key = hex!("0350be375c6e807988a5f575c9776e271e19a79f64681bcfe6a1affde9a5444496");

OP_CHECKDATASIGVERIFY(signature, message, public_key);
OP_REVERSEBYTES
OP_REVERSEBYTES(array: ByteArray) -> ByteArray

Reverse array.

Usage:

let array = b"BitcoinCash";

// reverse array
let reversed = OP_REVERSEBYTES(array);

let expected = b"hsaCnioctiB";
OP_EQUALVERIFY(reversed, expected);
FIRST_UNDEFINED_OP_VALUE

The first op_code value after all defined opcodes

Methods

impl Opcode[src]

pub fn is_disabled(self) -> bool[src]

pub fn retains_input(self) -> bool[src]

pub fn behavior(self) -> OpcodeBehavior[src]

Trait Implementations

impl Clone for Opcode[src]

impl Copy for Opcode[src]

impl Debug for Opcode[src]

impl Eq for Opcode[src]

impl FromPrimitive for Opcode[src]

impl Hash for Opcode[src]

impl Ord for Opcode[src]

impl PartialEq<Opcode> for Opcode[src]

impl PartialOrd<Opcode> for Opcode[src]

impl StructuralEq for Opcode[src]

impl StructuralPartialEq for Opcode[src]

Auto Trait Implementations

impl RefUnwindSafe for Opcode

impl Send for Opcode

impl Sync for Opcode

impl Unpin for Opcode

impl UnwindSafe for Opcode

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.