Bitcoin ABC 0.34.2
P2P Digital Currency
script.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <script/script.h>
7
8#include <crypto/hex_base.h>
9#include <hash.h>
10#include <script/script_flags.h>
11
12#include <algorithm>
13#include <string>
14
16
17std::string GetOpName(opcodetype opcode) {
18 switch (opcode) {
19 // push value
20 case OP_0:
21 return "0";
22 case OP_PUSHDATA1:
23 return "OP_PUSHDATA1";
24 case OP_PUSHDATA2:
25 return "OP_PUSHDATA2";
26 case OP_PUSHDATA4:
27 return "OP_PUSHDATA4";
28 case OP_1NEGATE:
29 return "-1";
30 case OP_RESERVED:
31 return "OP_RESERVED";
32 case OP_1:
33 return "1";
34 case OP_2:
35 return "2";
36 case OP_3:
37 return "3";
38 case OP_4:
39 return "4";
40 case OP_5:
41 return "5";
42 case OP_6:
43 return "6";
44 case OP_7:
45 return "7";
46 case OP_8:
47 return "8";
48 case OP_9:
49 return "9";
50 case OP_10:
51 return "10";
52 case OP_11:
53 return "11";
54 case OP_12:
55 return "12";
56 case OP_13:
57 return "13";
58 case OP_14:
59 return "14";
60 case OP_15:
61 return "15";
62 case OP_16:
63 return "16";
64
65 // control
66 case OP_NOP:
67 return "OP_NOP";
68 case OP_VER:
69 return "OP_VER";
70 case OP_IF:
71 return "OP_IF";
72 case OP_NOTIF:
73 return "OP_NOTIF";
74 case OP_VERIF:
75 return "OP_VERIF";
76 case OP_VERNOTIF:
77 return "OP_VERNOTIF";
78 case OP_ELSE:
79 return "OP_ELSE";
80 case OP_ENDIF:
81 return "OP_ENDIF";
82 case OP_VERIFY:
83 return "OP_VERIFY";
84 case OP_RETURN:
85 return "OP_RETURN";
86
87 // stack ops
88 case OP_TOALTSTACK:
89 return "OP_TOALTSTACK";
90 case OP_FROMALTSTACK:
91 return "OP_FROMALTSTACK";
92 case OP_2DROP:
93 return "OP_2DROP";
94 case OP_2DUP:
95 return "OP_2DUP";
96 case OP_3DUP:
97 return "OP_3DUP";
98 case OP_2OVER:
99 return "OP_2OVER";
100 case OP_2ROT:
101 return "OP_2ROT";
102 case OP_2SWAP:
103 return "OP_2SWAP";
104 case OP_IFDUP:
105 return "OP_IFDUP";
106 case OP_DEPTH:
107 return "OP_DEPTH";
108 case OP_DROP:
109 return "OP_DROP";
110 case OP_DUP:
111 return "OP_DUP";
112 case OP_NIP:
113 return "OP_NIP";
114 case OP_OVER:
115 return "OP_OVER";
116 case OP_PICK:
117 return "OP_PICK";
118 case OP_ROLL:
119 return "OP_ROLL";
120 case OP_ROT:
121 return "OP_ROT";
122 case OP_SWAP:
123 return "OP_SWAP";
124 case OP_TUCK:
125 return "OP_TUCK";
126
127 // splice ops
128 case OP_CAT:
129 return "OP_CAT";
130 case OP_SPLIT:
131 return "OP_SPLIT";
132 case OP_NUM2BIN:
133 return "OP_NUM2BIN";
134 case OP_BIN2NUM:
135 return "OP_BIN2NUM";
136 case OP_SIZE:
137 return "OP_SIZE";
138
139 // bit logic
140 case OP_INVERT:
141 return "OP_INVERT";
142 case OP_AND:
143 return "OP_AND";
144 case OP_OR:
145 return "OP_OR";
146 case OP_XOR:
147 return "OP_XOR";
148 case OP_EQUAL:
149 return "OP_EQUAL";
150 case OP_EQUALVERIFY:
151 return "OP_EQUALVERIFY";
152 case OP_RESERVED1:
153 return "OP_RESERVED1";
154 case OP_RESERVED2:
155 return "OP_RESERVED2";
156
157 // numeric
158 case OP_1ADD:
159 return "OP_1ADD";
160 case OP_1SUB:
161 return "OP_1SUB";
162 case OP_2MUL:
163 return "OP_2MUL";
164 case OP_2DIV:
165 return "OP_2DIV";
166 case OP_NEGATE:
167 return "OP_NEGATE";
168 case OP_ABS:
169 return "OP_ABS";
170 case OP_NOT:
171 return "OP_NOT";
172 case OP_0NOTEQUAL:
173 return "OP_0NOTEQUAL";
174 case OP_ADD:
175 return "OP_ADD";
176 case OP_SUB:
177 return "OP_SUB";
178 case OP_MUL:
179 return "OP_MUL";
180 case OP_DIV:
181 return "OP_DIV";
182 case OP_MOD:
183 return "OP_MOD";
184 case OP_LSHIFT:
185 return "OP_LSHIFT";
186 case OP_RSHIFT:
187 return "OP_RSHIFT";
188 case OP_BOOLAND:
189 return "OP_BOOLAND";
190 case OP_BOOLOR:
191 return "OP_BOOLOR";
192 case OP_NUMEQUAL:
193 return "OP_NUMEQUAL";
195 return "OP_NUMEQUALVERIFY";
196 case OP_NUMNOTEQUAL:
197 return "OP_NUMNOTEQUAL";
198 case OP_LESSTHAN:
199 return "OP_LESSTHAN";
200 case OP_GREATERTHAN:
201 return "OP_GREATERTHAN";
203 return "OP_LESSTHANOREQUAL";
205 return "OP_GREATERTHANOREQUAL";
206 case OP_MIN:
207 return "OP_MIN";
208 case OP_MAX:
209 return "OP_MAX";
210 case OP_WITHIN:
211 return "OP_WITHIN";
212
213 // crypto
214 case OP_RIPEMD160:
215 return "OP_RIPEMD160";
216 case OP_SHA1:
217 return "OP_SHA1";
218 case OP_SHA256:
219 return "OP_SHA256";
220 case OP_HASH160:
221 return "OP_HASH160";
222 case OP_HASH256:
223 return "OP_HASH256";
224 case OP_CODESEPARATOR:
225 return "OP_CODESEPARATOR";
226 case OP_CHECKSIG:
227 return "OP_CHECKSIG";
229 return "OP_CHECKSIGVERIFY";
230 case OP_CHECKMULTISIG:
231 return "OP_CHECKMULTISIG";
233 return "OP_CHECKMULTISIGVERIFY";
234 case OP_CHECKDATASIG:
235 return "OP_CHECKDATASIG";
237 return "OP_CHECKDATASIGVERIFY";
238 case OP_REVERSEBYTES:
239 return "OP_REVERSEBYTES";
240
241 // expansion
242 case OP_NOP1:
243 return "OP_NOP1";
245 return "OP_CHECKLOCKTIMEVERIFY";
247 return "OP_CHECKSEQUENCEVERIFY";
248 case OP_NOP4:
249 return "OP_NOP4";
250 case OP_NOP5:
251 return "OP_NOP5";
252 case OP_NOP6:
253 return "OP_NOP6";
254 case OP_NOP7:
255 return "OP_NOP7";
256 case OP_NOP8:
257 return "OP_NOP8";
258 case OP_NOP9:
259 return "OP_NOP9";
260 case OP_NOP10:
261 return "OP_NOP10";
262
263 case OP_INVALIDOPCODE:
264 return "OP_INVALIDOPCODE";
265
266 default:
267 return "OP_UNKNOWN";
268 }
269}
270
271bool CheckMinimalPush(const std::vector<uint8_t> &data, opcodetype opcode) {
272 // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
273 assert(0 <= opcode && opcode <= OP_PUSHDATA4);
274 if (data.size() == 0) {
275 // Should have used OP_0.
276 return opcode == OP_0;
277 }
278 if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
279 // Should have used OP_1 .. OP_16.
280 return false;
281 }
282 if (data.size() == 1 && data[0] == 0x81) {
283 // Should have used OP_1NEGATE.
284 return false;
285 }
286 if (data.size() <= 75) {
287 // Must have used a direct push (opcode indicating number of bytes
288 // pushed + those bytes).
289 return opcode == data.size();
290 }
291 if (data.size() <= 255) {
292 // Must have used OP_PUSHDATA.
293 return opcode == OP_PUSHDATA1;
294 }
295 if (data.size() <= 65535) {
296 // Must have used OP_PUSHDATA2.
297 return opcode == OP_PUSHDATA2;
298 }
299 return true;
300}
301
302bool CScriptNum::IsMinimallyEncoded(const std::vector<uint8_t> &vch,
303 const size_t nMaxNumSize) {
304 if (vch.size() > nMaxNumSize) {
305 return false;
306 }
307
308 if (vch.size() > 0) {
309 // Check that the number is encoded with the minimum possible number
310 // of bytes.
311 //
312 // If the most-significant-byte - excluding the sign bit - is zero
313 // then we're not minimal. Note how this test also rejects the
314 // negative-zero encoding, 0x80.
315 if ((vch.back() & 0x7f) == 0) {
316 // One exception: if there's more than one byte and the most
317 // significant bit of the second-most-significant-byte is set it
318 // would conflict with the sign bit. An example of this case is
319 // +-255, which encode to 0xff00 and 0xff80 respectively.
320 // (big-endian).
321 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
322 return false;
323 }
324 }
325 }
326
327 return true;
328}
329
330bool CScriptNum::MinimallyEncode(std::vector<uint8_t> &data) {
331 if (data.size() == 0) {
332 return false;
333 }
334
335 // If the last byte is not 0x00 or 0x80, we are minimally encoded.
336 uint8_t last = data.back();
337 if (last & 0x7f) {
338 return false;
339 }
340
341 // If the script is one byte long, then we have a zero, which encodes as an
342 // empty array.
343 if (data.size() == 1) {
344 data = {};
345 return true;
346 }
347
348 // If the next byte has it sign bit set, then we are minimaly encoded.
349 if (data[data.size() - 2] & 0x80) {
350 return false;
351 }
352
353 // We are not minimally encoded, we need to figure out how much to trim.
354 for (size_t i = data.size() - 1; i > 0; i--) {
355 // We found a non zero byte, time to encode.
356 if (data[i - 1] != 0) {
357 if (data[i - 1] & 0x80) {
358 // We found a byte with it sign bit set so we need one more
359 // byte.
360 data[i++] = last;
361 } else {
362 // the sign bit is clear, we can use it.
363 data[i - 1] |= last;
364 }
365
366 data.resize(i);
367 return true;
368 }
369 }
370
371 // If we the whole thing is zeros, then we have a zero.
372 data = {};
373 return true;
374}
375
377 // Extra-fast test for pay-to-script-hash CScripts:
378 return (this->size() == 23 && (*this)[0] == OP_HASH160 &&
379 (*this)[1] == 0x14 && (*this)[22] == OP_EQUAL);
380}
381
382// A witness program is any valid CScript that consists of a 1-byte push opcode
383// followed by a data push between 2 and 40 bytes.
385 std::vector<uint8_t> &program) const {
386 if (this->size() < 4 || this->size() > 42) {
387 return false;
388 }
389 if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
390 return false;
391 }
392 if (size_t((*this)[1] + 2) == this->size()) {
393 version = DecodeOP_N((opcodetype)(*this)[0]);
394 program = std::vector<uint8_t>(this->begin() + 2, this->end());
395 return true;
396 }
397 return false;
398}
399
400// Wrapper returning only the predicate
402 int version;
403 std::vector<uint8_t> program;
404 return IsWitnessProgram(version, program);
405}
406
408 while (pc < end()) {
409 opcodetype opcode;
410 if (!GetOp(pc, opcode)) {
411 return false;
412 }
413
414 // Note that IsPushOnly() *does* consider OP_RESERVED to be a push-type
415 // opcode, however execution of OP_RESERVED fails, so it's not relevant
416 // to P2SH/BIP62 as the scriptSig would fail prior to the P2SH special
417 // validation code being executed.
418 if (opcode > OP_16) {
419 return false;
420 }
421 }
422 return true;
423}
424
426 return this->IsPushOnly(begin());
427}
428
431 std::vector<uint8_t> *pvchRet) {
432 opcodeRet = OP_INVALIDOPCODE;
433 if (pvchRet) {
434 pvchRet->clear();
435 }
436 if (pc >= end) {
437 return false;
438 }
439
440 // Read instruction
441 if (end - pc < 1) {
442 return false;
443 }
444
445 uint32_t opcode = *pc++;
446
447 // Immediate operand
448 if (opcode <= OP_PUSHDATA4) {
449 uint32_t nSize = 0;
450 if (opcode < OP_PUSHDATA1) {
451 nSize = opcode;
452 } else if (opcode == OP_PUSHDATA1) {
453 if (end - pc < 1) {
454 return false;
455 }
456 nSize = *pc++;
457 } else if (opcode == OP_PUSHDATA2) {
458 if (end - pc < 2) {
459 return false;
460 }
461 nSize = ReadLE16(&pc[0]);
462 pc += 2;
463 } else if (opcode == OP_PUSHDATA4) {
464 if (end - pc < 4) {
465 return false;
466 }
467 nSize = ReadLE32(&pc[0]);
468 pc += 4;
469 }
470 if (end - pc < 0 || uint32_t(end - pc) < nSize) {
471 return false;
472 }
473 if (pvchRet) {
474 pvchRet->assign(pc, pc + nSize);
475 }
476 pc += nSize;
477 }
478
479 opcodeRet = static_cast<opcodetype>(opcode);
480 return true;
481}
482
485 while (it < end()) {
486 opcodetype opcode;
487 std::vector<uint8_t> item;
488 if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE ||
489 item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
490 return false;
491 }
492 }
493 return true;
494}
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:434
bool IsPayToScriptHash() const
Definition: script.cpp:376
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition: script.h:515
bool IsPushOnly() const
Definition: script.cpp:425
bool IsWitnessProgram() const
Definition: script.cpp:401
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Definition: script.cpp:483
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
Definition: script.h:505
CScriptID()
Definition: script.h:566
static bool IsMinimallyEncoded(const std::vector< uint8_t > &vch, const size_t nMaxNumSize)
Definition: script.cpp:302
static bool MinimallyEncode(std::vector< uint8_t > &data)
Definition: script.cpp:330
size_type size() const
Definition: prevector.h:398
iterator begin()
Definition: prevector.h:402
iterator end()
Definition: prevector.h:404
static uint16_t ReadLE16(const uint8_t *ptr)
Definition: common.h:13
static uint32_t ReadLE32(const uint8_t *ptr)
Definition: common.h:19
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
std::string GetOpName(opcodetype opcode)
Definition: script.cpp:17
bool CheckMinimalPush(const std::vector< uint8_t > &data, opcodetype opcode)
Check whether the given stack element data would be minimally pushed using the given opcode.
Definition: script.cpp:271
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< uint8_t > *pvchRet)
Definition: script.cpp:429
static const unsigned int MAX_OPCODE
Definition: script.h:206
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:27
opcodetype
Script opcodes.
Definition: script.h:53
@ OP_NUMNOTEQUAL
Definition: script.h:152
@ OP_RESERVED1
Definition: script.h:127
@ OP_SPLIT
Definition: script.h:115
@ OP_2
Definition: script.h:64
@ OP_SHA256
Definition: script.h:165
@ OP_PUSHDATA4
Definition: script.h:59
@ OP_NOP5
Definition: script.h:181
@ OP_CHECKDATASIG
Definition: script.h:189
@ OP_BOOLAND
Definition: script.h:148
@ OP_CHECKMULTISIG
Definition: script.h:171
@ OP_NEGATE
Definition: script.h:135
@ OP_IF
Definition: script.h:83
@ OP_13
Definition: script.h:75
@ OP_ROT
Definition: script.h:109
@ OP_SWAP
Definition: script.h:110
@ OP_1NEGATE
Definition: script.h:60
@ OP_VERNOTIF
Definition: script.h:86
@ OP_CHECKSIG
Definition: script.h:169
@ OP_CHECKLOCKTIMEVERIFY
Definition: script.h:176
@ OP_LESSTHAN
Definition: script.h:153
@ OP_16
Definition: script.h:78
@ OP_14
Definition: script.h:76
@ OP_CHECKDATASIGVERIFY
Definition: script.h:190
@ OP_NOP10
Definition: script.h:186
@ OP_2DIV
Definition: script.h:134
@ OP_INVALIDOPCODE
Definition: script.h:202
@ OP_NOT
Definition: script.h:137
@ OP_EQUAL
Definition: script.h:125
@ OP_NUMEQUAL
Definition: script.h:150
@ OP_MOD
Definition: script.h:144
@ OP_NOTIF
Definition: script.h:84
@ OP_4
Definition: script.h:66
@ OP_10
Definition: script.h:72
@ OP_SIZE
Definition: script.h:118
@ OP_3DUP
Definition: script.h:97
@ OP_ENDIF
Definition: script.h:88
@ OP_NOP1
Definition: script.h:175
@ OP_DUP
Definition: script.h:104
@ OP_GREATERTHAN
Definition: script.h:154
@ OP_NOP
Definition: script.h:81
@ OP_NUM2BIN
Definition: script.h:116
@ OP_VERIF
Definition: script.h:85
@ OP_TOALTSTACK
Definition: script.h:93
@ OP_CODESEPARATOR
Definition: script.h:168
@ OP_RIPEMD160
Definition: script.h:163
@ OP_MIN
Definition: script.h:157
@ OP_HASH256
Definition: script.h:167
@ OP_MAX
Definition: script.h:158
@ OP_1SUB
Definition: script.h:132
@ OP_FROMALTSTACK
Definition: script.h:94
@ OP_SUB
Definition: script.h:141
@ OP_NUMEQUALVERIFY
Definition: script.h:151
@ OP_OVER
Definition: script.h:106
@ OP_NOP8
Definition: script.h:184
@ OP_DIV
Definition: script.h:143
@ OP_HASH160
Definition: script.h:166
@ OP_2DUP
Definition: script.h:96
@ OP_NIP
Definition: script.h:105
@ OP_2MUL
Definition: script.h:133
@ OP_NOP4
Definition: script.h:180
@ OP_1
Definition: script.h:62
@ OP_LESSTHANOREQUAL
Definition: script.h:155
@ OP_2DROP
Definition: script.h:95
@ OP_DEPTH
Definition: script.h:102
@ OP_NOP9
Definition: script.h:185
@ OP_BIN2NUM
Definition: script.h:117
@ OP_VER
Definition: script.h:82
@ OP_VERIFY
Definition: script.h:89
@ OP_RESERVED2
Definition: script.h:128
@ OP_12
Definition: script.h:74
@ OP_ADD
Definition: script.h:140
@ OP_CHECKMULTISIGVERIFY
Definition: script.h:172
@ OP_NOP7
Definition: script.h:183
@ OP_8
Definition: script.h:70
@ OP_BOOLOR
Definition: script.h:149
@ OP_XOR
Definition: script.h:124
@ OP_DROP
Definition: script.h:103
@ OP_MUL
Definition: script.h:142
@ OP_WITHIN
Definition: script.h:160
@ OP_ELSE
Definition: script.h:87
@ OP_15
Definition: script.h:77
@ OP_CHECKSIGVERIFY
Definition: script.h:170
@ OP_PUSHDATA1
Definition: script.h:57
@ OP_TUCK
Definition: script.h:111
@ OP_2OVER
Definition: script.h:98
@ OP_0NOTEQUAL
Definition: script.h:138
@ OP_9
Definition: script.h:71
@ OP_3
Definition: script.h:65
@ OP_11
Definition: script.h:73
@ OP_SHA1
Definition: script.h:164
@ OP_GREATERTHANOREQUAL
Definition: script.h:156
@ OP_RSHIFT
Definition: script.h:146
@ OP_2SWAP
Definition: script.h:100
@ OP_PUSHDATA2
Definition: script.h:58
@ OP_2ROT
Definition: script.h:99
@ OP_6
Definition: script.h:68
@ OP_INVERT
Definition: script.h:121
@ OP_0
Definition: script.h:55
@ OP_ABS
Definition: script.h:136
@ OP_LSHIFT
Definition: script.h:145
@ OP_RETURN
Definition: script.h:90
@ OP_IFDUP
Definition: script.h:101
@ OP_PICK
Definition: script.h:107
@ OP_AND
Definition: script.h:122
@ OP_EQUALVERIFY
Definition: script.h:126
@ OP_CAT
Definition: script.h:114
@ OP_REVERSEBYTES
Definition: script.h:193
@ OP_RESERVED
Definition: script.h:61
@ OP_1ADD
Definition: script.h:131
@ OP_7
Definition: script.h:69
@ OP_OR
Definition: script.h:123
@ OP_ROLL
Definition: script.h:108
@ OP_NOP6
Definition: script.h:182
@ OP_5
Definition: script.h:67
@ OP_CHECKSEQUENCEVERIFY
Definition: script.h:178
assert(!tx.IsCoinBase())