bsv_wasm/script/op_codes.rs
1use serde::{Deserialize, Serialize};
2use std::fmt;
3use strum_macros::EnumString;
4#[cfg(target_arch = "wasm32")]
5use wasm_bindgen::prelude::*;
6
7/**
8 * This entire page is borrowed from rust-sv (https://github.com/brentongunning/rust-sv/blob/master/src/script/op_codes.rs)
9 */
10
11// --------------------------------------------------------------------------------------------
12// Constants
13// --------------------------------------------------------------------------------------------
14
15#[cfg_attr(all(target_arch = "wasm32", feature = "wasm-bindgen-opcodes"), wasm_bindgen)]
16#[derive(Debug, Clone, Copy, FromPrimitive, ToPrimitive, EnumString, PartialEq, Eq, Serialize, Deserialize)]
17#[allow(non_camel_case_types)]
18pub enum OpCodes {
19 /// Pushes 0 onto the stack
20 OP_0 = 0,
21 /// Pushes 0 onto the stack
22 // OP_FALSE = 0,
23
24 /// The next byte sets the number of bytes to push onto the stack
25 OP_PUSHDATA1 = 76,
26 /// The next two bytes sets the number of bytes to push onto the stack
27 OP_PUSHDATA2 = 77,
28 /// The next four bytes sets the number of bytes to push onto the stack
29 OP_PUSHDATA4 = 78,
30
31 /// Pushes -1 onto the stack
32 OP_1NEGATE = 79,
33 /// Pushes 1 onto the stack
34 OP_1 = 81,
35 /// Pushes 1 onto the stack
36 // OP_TRUE = 81,
37
38 /// Pushes 2 onto the stack
39 OP_2 = 82,
40 /// Pushes 3 onto the stack
41 OP_3 = 83,
42 /// Pushes 4 onto the stack
43 OP_4 = 84,
44 /// Pushes 5 onto the stack
45 OP_5 = 85,
46 /// Pushes 6 onto the stack
47 OP_6 = 86,
48 /// Pushes 7 onto the stack
49 OP_7 = 87,
50 /// Pushes 8 onto the stack
51 OP_8 = 88,
52 /// Pushes 9 onto the stack
53 OP_9 = 89,
54 /// Pushes 10 onto the stack
55 OP_10 = 90,
56 /// Pushes 11 onto the stack
57 OP_11 = 91,
58 /// Pushes 12 onto the stack
59 OP_12 = 92,
60 /// Pushes 13 onto the stack
61 OP_13 = 93,
62 /// Pushes 14 onto the stack
63 OP_14 = 94,
64 /// Pushes 15 onto the stack
65 OP_15 = 95,
66 /// Pushes 16 onto the stack
67 OP_16 = 96,
68
69 // --------------------------------------------------------------------------------------------
70 // Flow Control
71 // --------------------------------------------------------------------------------------------
72 /// Does nothing
73 OP_NOP = 97,
74 /// If the top stack is true, statements are executed. Top stack value is removed.
75 OP_IF = 99,
76 /// If the top stack is false, statements are executed. Top stack value is removed.
77 OP_NOTIF = 100,
78 /// If the preceding OP_IF or OP_NOTIF statemetns were not executed, then statements are executed.
79 OP_ELSE = 103,
80 /// Ends an if-else block
81 OP_ENDIF = 104,
82 /// Marks a statement as invalid if the top stack value is false. Top stack value is removed.
83 OP_VERIFY = 105,
84 /// Marks a statements as invalid
85 OP_RETURN = 106,
86
87 // --------------------------------------------------------------------------------------------
88 // Stack
89 // --------------------------------------------------------------------------------------------
90 /// Moves the top item on the main stack to the alt stack
91 OP_TOALTSTACK = 107,
92 /// Moves the top item on the alt stack to the main stack
93 OP_FROMALTSTACK = 108,
94 /// Duplicates the top stack value if it is not zero
95 OP_IFDUP = 115,
96 /// Puts the number of stack items onto the stack
97 OP_DEPTH = 116,
98 /// Drops the top stack value
99 OP_DROP = 117,
100 /// Duplicates the top stack item
101 OP_DUP = 118,
102 /// Removes the second-to-top stack item
103 OP_NIP = 119,
104 /// Copies the second-to-top stack item to the top
105 OP_OVER = 120,
106 /// The item n back in the stack is copied to the top
107 OP_PICK = 121,
108 /// The item n back in the stack is moved to the top
109 OP_ROLL = 122,
110 /// The top three items on the stack are rotated to the left
111 OP_ROT = 123,
112 /// The top two items on the stack are swapped
113 OP_SWAP = 124,
114 /// The item at the top of the stack is copied and inserted before the second-to-top item
115 OP_TUCK = 125,
116 /// Removes the top two items from the stack
117 OP_2DROP = 109,
118 /// Duplicates the top two stack items
119 OP_2DUP = 110,
120 /// Duplicates the top three stack items
121 OP_3DUP = 111,
122 /// Copies the pair of items two spaces back to the front
123 OP_2OVER = 112,
124 /// The fifth and sixth items back are moved to the top of the stack
125 OP_2ROT = 113,
126 /// Swaps the top two pairs of items
127 OP_2SWAP = 114,
128
129 // --------------------------------------------------------------------------------------------
130 // Splice
131 // --------------------------------------------------------------------------------------------
132 /// Concatenates two byte sequences
133 OP_CAT = 126,
134 /// Splits the byte sequence at position n
135 OP_SPLIT = 127,
136 /// Pushes the byte sequence length of the top stack item without popping it
137 OP_SIZE = 130,
138
139 // --------------------------------------------------------------------------------------------
140 // Bitwise Logic
141 // --------------------------------------------------------------------------------------------
142 /// Flips all of the bits in the input
143 OP_INVERT = 131,
144 /// Boolean and between each bit in the inputs
145 OP_AND = 132,
146 /// Boolean or between each bit in the inputs
147 OP_OR = 133,
148 /// Boolean exclusive or between each bit in the inputs
149 OP_XOR = 134,
150 /// Returns 1 if the inputs are exactly equal, 0 otherwise
151 OP_EQUAL = 135,
152 /// Same as OP_EQUAL, but runs OP_VERIFY afterward
153 OP_EQUALVERIFY = 136,
154
155 // --------------------------------------------------------------------------------------------
156 // Arithmetic
157 // --------------------------------------------------------------------------------------------
158 /// Adds 1 to the input
159 OP_1ADD = 139,
160 /// Subtracts 1 from the input
161 OP_1SUB = 140,
162 /// The sign of the input is flipped
163 OP_NEGATE = 143,
164 /// The input is made positive
165 OP_ABS = 144,
166 /// If the input is 0 or 1, it is flipped. Otherwise, the output will be 0.
167 OP_NOT = 145,
168 /// Returns 0 if the input is 0. 1 otherwise.
169 OP_0NOTEQUAL = 146,
170 /// Adds a to b
171 OP_ADD = 147,
172 /// Subtracts b from a
173 OP_SUB = 148,
174 /// Multiplies a by b
175 OP_MUL = 149,
176 /// Divides a by b
177 OP_DIV = 150,
178 /// Returns the remainder after dividing a by b
179 OP_MOD = 151,
180 /// Shifts a left b bits, preserving sign
181 OP_LSHIFT = 152,
182 /// Shifts a right b bits, preserving sign
183 OP_RSHIFT = 153,
184 /// If both a and b are not empty, the output is 1. Otherwise, 0.
185 OP_BOOLAND = 154,
186 /// If a or b is not empty, the output is 1. Otherwise, 0.
187 OP_BOOLOR = 155,
188 /// Returns 1 if the numbers are equal. Otherwise, 0.
189 OP_NUMEQUAL = 156,
190 /// Same as OP_NUMEQUAL, but runs OP_VERIFY afterward
191 OP_NUMEQUALVERIFY = 157,
192 /// Returns 1 if the numbers are not equal. Otherwise, 0.
193 OP_NUMNOTEQUAL = 158,
194 /// Returns 1 if a is less than b. Otherwise, 0.
195 OP_LESSTHAN = 159,
196 /// Returns 1 if a is greater than b. Otherwise, 0.
197 OP_GREATERTHAN = 160,
198 /// Returns 1 if a is less than or equal to b. Otherwise, 0.
199 OP_LESSTHANOREQUAL = 161,
200 /// Returns 1 if a is greater than or equal to b. Otherwise, 0.
201 OP_GREATERTHANOREQUAL = 162,
202 /// Returns the smaller of a and b
203 OP_MIN = 163,
204 /// Returns the larger of a and b
205 OP_MAX = 164,
206 /// Returns 1 if x is within the specified range, left inclusive. Otherwise, 0.
207 OP_WITHIN = 165,
208 /// Converts numeric value a into a byte sequence of length b
209 OP_NUM2BIN = 128,
210 /// Converts byte sequence x into a numeric value
211 OP_BIN2NUM = 129,
212
213 // --------------------------------------------------------------------------------------------
214 // Cryptography
215 // --------------------------------------------------------------------------------------------
216 /// The input is hashed using RIPEMD-160
217 OP_RIPEMD160 = 166,
218 /// The input is hashed using SHA-1
219 OP_SHA1 = 167,
220 /// The input is hashed using SHA-256
221 OP_SHA256 = 168,
222 /// The input is hashed twice: first with SHA-256 and then with RIPEMD-160
223 OP_HASH160 = 169,
224 /// The input is hashed two times with SHA-256
225 OP_HASH256 = 170,
226 /// Marks the part of the script after which the signature will begin matching
227 OP_CODESEPARATOR = 171,
228 /// Puts 1 on the stack if the signature authorizes the public key and transaction hash. Otherwise 0.
229 OP_CHECKSIG = 172,
230 /// Same as OP_CHECKSIG, but OP_VERIFY is executed afterward
231 OP_CHECKSIGVERIFY = 173,
232 /// Puts 1 on the stack if m of n signatures authorize the public key and transaction hash. Otherwise 0.
233 OP_CHECKMULTISIG = 174,
234 /// Same as OP_CHECKMULTISIG, but OP_VERIFY is executed afterward
235 OP_CHECKMULTISIGVERIFY = 175,
236
237 // --------------------------------------------------------------------------------------------
238 // Locktime
239 // --------------------------------------------------------------------------------------------
240 /// Marks transaction as invalid if the top stack item is greater than the transaction's lock_time
241 OP_CHECKLOCKTIMEVERIFY = 177,
242 /// Marks transaction as invalid if the top stack item is less than the transaction's sequence used for relative lock time
243 OP_CHECKSEQUENCEVERIFY = 178,
244
245 // --------------------------------------------------------------------------------------------
246 // Pseudo-words
247 // --------------------------------------------------------------------------------------------
248 /// OP_DATA followed by a varint represents arbitrary data on chain. Used for matching Script Templates.
249 OP_DATA = 251,
250 /// Represents a secp256k1 signature
251 OP_SIG = 252,
252 /// Represents a public key hashed with OP_HASH160
253 OP_PUBKEYHASH = 253,
254 /// Represents a public key compatible with OP_CHECKSIG
255 OP_PUBKEY = 254,
256 /// Matches any opcode that is not yet assigned
257 OP_INVALIDOPCODE = 255,
258
259 // --------------------------------------------------------------------------------------------
260 // Reserved words
261 // --------------------------------------------------------------------------------------------
262 /// Transaction is invalid unless occuring in an unexecuted OP_IF branch
263 OP_RESERVED = 80,
264 /// Transaction is invalid unless occuring in an unexecuted OP_IF branch
265 OP_VER = 98,
266 /// Transaction is invalid even when occuring in an unexecuted OP_IF branch
267 OP_VERIF = 101,
268 /// Transaction is invalid even when occuring in an unexecuted OP_IF branch
269 OP_VERNOTIF = 102,
270 /// Transaction is invalid unless occuring in an unexecuted OP_IF branch
271 OP_RESERVED1 = 137,
272 /// Transaction is invalid unless occuring in an unexecuted OP_IF branch
273 OP_RESERVED2 = 138,
274 /// The word is ignored. Does not mark transaction as invalid.
275 OP_NOP1 = 176,
276 /// The word is ignored. Does not mark transaction as invalid.
277 OP_NOP4 = 179,
278 /// The word is ignored. Does not mark transaction as invalid.
279 OP_NOP5 = 180,
280 /// The word is ignored. Does not mark transaction as invalid.
281 OP_NOP6 = 181,
282 /// The word is ignored. Does not mark transaction as invalid.
283 OP_NOP7 = 182,
284 /// The word is ignored. Does not mark transaction as invalid.
285 OP_NOP8 = 183,
286 /// The word is ignored. Does not mark transaction as invalid.
287 OP_NOP9 = 184,
288 /// The word is ignored. Does not mark transaction as invalid.
289 OP_NOP10 = 185,
290
291 /// Words at or above this number are invalid
292 OP_INVALID_ABOVE = 186,
293
294 // --------------------------------------------------------------------------------------------
295 // Disabled words
296 // --------------------------------------------------------------------------------------------
297 /// The input is multiplied by 2
298 OP_2MUL = 141,
299 /// The input is divided by 2
300 OP_2DIV = 142,
301}
302
303impl fmt::Display for OpCodes {
304 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
305 write!(f, "{:?}", self)
306 // or, alternatively:
307 // fmt::Debug::fmt(self, f)
308 }
309}