]>
Commit | Line | Data |
---|---|---|
cbd22a50 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
2d79bba3 | 3 | // Distributed under the MIT software license, see the accompanying |
cbd22a50 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
84738627 PJ |
6 | #ifndef BITCOIN_SCRIPT_SCRIPT_H |
7 | #define BITCOIN_SCRIPT_SCRIPT_H | |
cbd22a50 | 8 | |
5207f33f PK |
9 | #include "crypto/common.h" |
10 | ||
85c579e3 CF |
11 | #include <assert.h> |
12 | #include <climits> | |
13 | #include <limits> | |
cbd22a50 | 14 | #include <stdexcept> |
85c579e3 CF |
15 | #include <stdint.h> |
16 | #include <string.h> | |
3a757c52 | 17 | #include <string> |
85c579e3 | 18 | #include <vector> |
cbd22a50 | 19 | |
e980a26d | 20 | #define OPRETTYPE_TIMELOCK 1 |
21 | ||
cbd22a50 | 22 | static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes |
23 | ||
e625be68 SS |
24 | // Max size of pushdata in a CC sig in bytes |
25 | static const unsigned int MAX_SCRIPT_CRYPTOCONDITION_FULFILLMENT_SIZE = 2048; | |
26 | ||
4c6ea562 PW |
27 | // Maximum script length in bytes |
28 | static const int MAX_SCRIPT_SIZE = 10000; | |
29 | ||
6ec08db3 PT |
30 | // Threshold for nLockTime: below this value it is interpreted as block number, |
31 | // otherwise as UNIX timestamp. | |
32 | static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC | |
33 | ||
e9ca4280 CF |
34 | template <typename T> |
35 | std::vector<unsigned char> ToByteVector(const T& in) | |
36 | { | |
37 | return std::vector<unsigned char>(in.begin(), in.end()); | |
38 | } | |
39 | ||
cbd22a50 | 40 | /** Script opcodes */ |
41 | enum opcodetype | |
42 | { | |
43 | // push value | |
44 | OP_0 = 0x00, | |
45 | OP_FALSE = OP_0, | |
46 | OP_PUSHDATA1 = 0x4c, | |
47 | OP_PUSHDATA2 = 0x4d, | |
48 | OP_PUSHDATA4 = 0x4e, | |
49 | OP_1NEGATE = 0x4f, | |
50 | OP_RESERVED = 0x50, | |
51 | OP_1 = 0x51, | |
52 | OP_TRUE=OP_1, | |
53 | OP_2 = 0x52, | |
54 | OP_3 = 0x53, | |
55 | OP_4 = 0x54, | |
56 | OP_5 = 0x55, | |
57 | OP_6 = 0x56, | |
58 | OP_7 = 0x57, | |
59 | OP_8 = 0x58, | |
60 | OP_9 = 0x59, | |
61 | OP_10 = 0x5a, | |
62 | OP_11 = 0x5b, | |
63 | OP_12 = 0x5c, | |
64 | OP_13 = 0x5d, | |
65 | OP_14 = 0x5e, | |
66 | OP_15 = 0x5f, | |
67 | OP_16 = 0x60, | |
68 | ||
69 | // control | |
70 | OP_NOP = 0x61, | |
71 | OP_VER = 0x62, | |
72 | OP_IF = 0x63, | |
73 | OP_NOTIF = 0x64, | |
74 | OP_VERIF = 0x65, | |
75 | OP_VERNOTIF = 0x66, | |
76 | OP_ELSE = 0x67, | |
77 | OP_ENDIF = 0x68, | |
78 | OP_VERIFY = 0x69, | |
79 | OP_RETURN = 0x6a, | |
80 | ||
81 | // stack ops | |
82 | OP_TOALTSTACK = 0x6b, | |
83 | OP_FROMALTSTACK = 0x6c, | |
84 | OP_2DROP = 0x6d, | |
85 | OP_2DUP = 0x6e, | |
86 | OP_3DUP = 0x6f, | |
87 | OP_2OVER = 0x70, | |
88 | OP_2ROT = 0x71, | |
89 | OP_2SWAP = 0x72, | |
90 | OP_IFDUP = 0x73, | |
91 | OP_DEPTH = 0x74, | |
92 | OP_DROP = 0x75, | |
93 | OP_DUP = 0x76, | |
94 | OP_NIP = 0x77, | |
95 | OP_OVER = 0x78, | |
96 | OP_PICK = 0x79, | |
97 | OP_ROLL = 0x7a, | |
98 | OP_ROT = 0x7b, | |
99 | OP_SWAP = 0x7c, | |
100 | OP_TUCK = 0x7d, | |
101 | ||
102 | // splice ops | |
103 | OP_CAT = 0x7e, | |
104 | OP_SUBSTR = 0x7f, | |
105 | OP_LEFT = 0x80, | |
106 | OP_RIGHT = 0x81, | |
107 | OP_SIZE = 0x82, | |
108 | ||
109 | // bit logic | |
110 | OP_INVERT = 0x83, | |
111 | OP_AND = 0x84, | |
112 | OP_OR = 0x85, | |
113 | OP_XOR = 0x86, | |
114 | OP_EQUAL = 0x87, | |
115 | OP_EQUALVERIFY = 0x88, | |
116 | OP_RESERVED1 = 0x89, | |
117 | OP_RESERVED2 = 0x8a, | |
118 | ||
119 | // numeric | |
120 | OP_1ADD = 0x8b, | |
121 | OP_1SUB = 0x8c, | |
122 | OP_2MUL = 0x8d, | |
123 | OP_2DIV = 0x8e, | |
124 | OP_NEGATE = 0x8f, | |
125 | OP_ABS = 0x90, | |
126 | OP_NOT = 0x91, | |
127 | OP_0NOTEQUAL = 0x92, | |
128 | ||
129 | OP_ADD = 0x93, | |
130 | OP_SUB = 0x94, | |
131 | OP_MUL = 0x95, | |
132 | OP_DIV = 0x96, | |
133 | OP_MOD = 0x97, | |
134 | OP_LSHIFT = 0x98, | |
135 | OP_RSHIFT = 0x99, | |
136 | ||
137 | OP_BOOLAND = 0x9a, | |
138 | OP_BOOLOR = 0x9b, | |
139 | OP_NUMEQUAL = 0x9c, | |
140 | OP_NUMEQUALVERIFY = 0x9d, | |
141 | OP_NUMNOTEQUAL = 0x9e, | |
142 | OP_LESSTHAN = 0x9f, | |
143 | OP_GREATERTHAN = 0xa0, | |
144 | OP_LESSTHANOREQUAL = 0xa1, | |
145 | OP_GREATERTHANOREQUAL = 0xa2, | |
146 | OP_MIN = 0xa3, | |
147 | OP_MAX = 0xa4, | |
148 | ||
149 | OP_WITHIN = 0xa5, | |
150 | ||
151 | // crypto | |
152 | OP_RIPEMD160 = 0xa6, | |
153 | OP_SHA1 = 0xa7, | |
154 | OP_SHA256 = 0xa8, | |
155 | OP_HASH160 = 0xa9, | |
156 | OP_HASH256 = 0xaa, | |
157 | OP_CODESEPARATOR = 0xab, | |
158 | OP_CHECKSIG = 0xac, | |
159 | OP_CHECKSIGVERIFY = 0xad, | |
160 | OP_CHECKMULTISIG = 0xae, | |
161 | OP_CHECKMULTISIGVERIFY = 0xaf, | |
07f83521 SS |
162 | OP_CHECKCRYPTOCONDITION = 0xcc, |
163 | OP_CHECKCRYPTOCONDITIONVERIFY = 0xcd, | |
cbd22a50 | 164 | |
165 | // expansion | |
166 | OP_NOP1 = 0xb0, | |
167 | OP_NOP2 = 0xb1, | |
4fa7a048 | 168 | OP_CHECKLOCKTIMEVERIFY = OP_NOP2, |
cbd22a50 | 169 | OP_NOP3 = 0xb2, |
170 | OP_NOP4 = 0xb3, | |
171 | OP_NOP5 = 0xb4, | |
172 | OP_NOP6 = 0xb5, | |
173 | OP_NOP7 = 0xb6, | |
174 | OP_NOP8 = 0xb7, | |
175 | OP_NOP9 = 0xb8, | |
176 | OP_NOP10 = 0xb9, | |
177 | ||
cbd22a50 | 178 | // template matching params |
179 | OP_SMALLDATA = 0xf9, | |
180 | OP_SMALLINTEGER = 0xfa, | |
181 | OP_PUBKEYS = 0xfb, | |
182 | OP_PUBKEYHASH = 0xfd, | |
183 | OP_PUBKEY = 0xfe, | |
a99ca25a | 184 | OP_CRYPTOCONDITION = 0xfc, |
cbd22a50 | 185 | |
186 | OP_INVALIDOPCODE = 0xff, | |
187 | }; | |
188 | ||
189 | const char* GetOpName(opcodetype opcode); | |
190 | ||
191 | class scriptnum_error : public std::runtime_error | |
192 | { | |
193 | public: | |
194 | explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {} | |
195 | }; | |
196 | ||
197 | class CScriptNum | |
198 | { | |
b9a36b15 MF |
199 | /** |
200 | * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers. | |
201 | * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1], | |
202 | * but results may overflow (and are valid as long as they are not used in a subsequent | |
203 | * numeric operation). CScriptNum enforces those semantics by storing results as | |
204 | * an int64 and allowing out-of-range values to be returned as a vector of bytes but | |
205 | * throwing an exception if arithmetic is done or the result is interpreted as an integer. | |
206 | */ | |
cbd22a50 | 207 | public: |
208 | ||
209 | explicit CScriptNum(const int64_t& n) | |
210 | { | |
211 | m_value = n; | |
212 | } | |
213 | ||
684636ba PT |
214 | static const size_t nDefaultMaxNumSize = 4; |
215 | ||
216 | explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal, | |
217 | const size_t nMaxNumSize = nDefaultMaxNumSize) | |
cbd22a50 | 218 | { |
698c6abb PW |
219 | if (vch.size() > nMaxNumSize) { |
220 | throw scriptnum_error("script number overflow"); | |
221 | } | |
6004e77b PT |
222 | if (fRequireMinimal && vch.size() > 0) { |
223 | // Check that the number is encoded with the minimum possible | |
224 | // number of bytes. | |
225 | // | |
226 | // If the most-significant-byte - excluding the sign bit - is zero | |
227 | // then we're not minimal. Note how this test also rejects the | |
228 | // negative-zero encoding, 0x80. | |
229 | if ((vch.back() & 0x7f) == 0) { | |
230 | // One exception: if there's more than one byte and the most | |
231 | // significant bit of the second-most-significant-byte is set | |
232 | // it would conflict with the sign bit. An example of this case | |
233 | // is +-255, which encode to 0xff00 and 0xff80 respectively. | |
234 | // (big-endian). | |
235 | if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) { | |
236 | throw scriptnum_error("non-minimally encoded script number"); | |
237 | } | |
238 | } | |
698c6abb | 239 | } |
cbd22a50 | 240 | m_value = set_vch(vch); |
241 | } | |
242 | ||
243 | inline bool operator==(const int64_t& rhs) const { return m_value == rhs; } | |
244 | inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; } | |
245 | inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; } | |
246 | inline bool operator< (const int64_t& rhs) const { return m_value < rhs; } | |
247 | inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; } | |
248 | inline bool operator> (const int64_t& rhs) const { return m_value > rhs; } | |
249 | ||
250 | inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); } | |
251 | inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); } | |
252 | inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); } | |
253 | inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); } | |
254 | inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); } | |
255 | inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); } | |
256 | ||
257 | inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);} | |
258 | inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);} | |
259 | inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); } | |
260 | inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); } | |
261 | ||
262 | inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); } | |
263 | inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); } | |
264 | ||
265 | inline CScriptNum operator-() const | |
266 | { | |
267 | assert(m_value != std::numeric_limits<int64_t>::min()); | |
268 | return CScriptNum(-m_value); | |
269 | } | |
270 | ||
271 | inline CScriptNum& operator=( const int64_t& rhs) | |
272 | { | |
273 | m_value = rhs; | |
274 | return *this; | |
275 | } | |
276 | ||
277 | inline CScriptNum& operator+=( const int64_t& rhs) | |
278 | { | |
279 | assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) || | |
280 | (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs)); | |
281 | m_value += rhs; | |
282 | return *this; | |
283 | } | |
284 | ||
285 | inline CScriptNum& operator-=( const int64_t& rhs) | |
286 | { | |
287 | assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) || | |
288 | (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs)); | |
289 | m_value -= rhs; | |
290 | return *this; | |
291 | } | |
292 | ||
293 | int getint() const | |
294 | { | |
295 | if (m_value > std::numeric_limits<int>::max()) | |
296 | return std::numeric_limits<int>::max(); | |
297 | else if (m_value < std::numeric_limits<int>::min()) | |
298 | return std::numeric_limits<int>::min(); | |
299 | return m_value; | |
300 | } | |
301 | ||
302 | std::vector<unsigned char> getvch() const | |
303 | { | |
304 | return serialize(m_value); | |
305 | } | |
306 | ||
307 | static std::vector<unsigned char> serialize(const int64_t& value) | |
308 | { | |
309 | if(value == 0) | |
310 | return std::vector<unsigned char>(); | |
311 | ||
312 | std::vector<unsigned char> result; | |
313 | const bool neg = value < 0; | |
314 | uint64_t absvalue = neg ? -value : value; | |
315 | ||
316 | while(absvalue) | |
317 | { | |
318 | result.push_back(absvalue & 0xff); | |
319 | absvalue >>= 8; | |
320 | } | |
321 | ||
322 | // - If the most significant byte is >= 0x80 and the value is positive, push a | |
323 | // new zero-byte to make the significant byte < 0x80 again. | |
324 | ||
325 | // - If the most significant byte is >= 0x80 and the value is negative, push a | |
326 | // new 0x80 byte that will be popped off when converting to an integral. | |
327 | ||
328 | // - If the most significant byte is < 0x80 and the value is negative, add | |
329 | // 0x80 to it, since it will be subtracted and interpreted as a negative when | |
330 | // converting to an integral. | |
331 | ||
332 | if (result.back() & 0x80) | |
333 | result.push_back(neg ? 0x80 : 0); | |
334 | else if (neg) | |
335 | result.back() |= 0x80; | |
336 | ||
337 | return result; | |
338 | } | |
339 | ||
cbd22a50 | 340 | private: |
341 | static int64_t set_vch(const std::vector<unsigned char>& vch) | |
342 | { | |
343 | if (vch.empty()) | |
344 | return 0; | |
345 | ||
346 | int64_t result = 0; | |
347 | for (size_t i = 0; i != vch.size(); ++i) | |
348 | result |= static_cast<int64_t>(vch[i]) << 8*i; | |
349 | ||
350 | // If the input vector's most significant byte is 0x80, remove it from | |
351 | // the result's msb and return a negative. | |
352 | if (vch.back() & 0x80) | |
1e735048 | 353 | return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1))))); |
cbd22a50 | 354 | |
355 | return result; | |
356 | } | |
357 | ||
358 | int64_t m_value; | |
359 | }; | |
360 | ||
cbd22a50 | 361 | /** Serialized script, used inside transaction inputs and outputs */ |
362 | class CScript : public std::vector<unsigned char> | |
363 | { | |
364 | protected: | |
365 | CScript& push_int64(int64_t n) | |
366 | { | |
367 | if (n == -1 || (n >= 1 && n <= 16)) | |
368 | { | |
369 | push_back(n + (OP_1 - 1)); | |
370 | } | |
698c6abb PW |
371 | else if (n == 0) |
372 | { | |
373 | push_back(OP_0); | |
374 | } | |
cbd22a50 | 375 | else |
376 | { | |
377 | *this << CScriptNum::serialize(n); | |
378 | } | |
379 | return *this; | |
380 | } | |
381 | public: | |
382 | CScript() { } | |
383 | CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { } | |
384 | CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { } | |
cbd22a50 | 385 | CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { } |
cbd22a50 | 386 | |
387 | CScript& operator+=(const CScript& b) | |
388 | { | |
389 | insert(end(), b.begin(), b.end()); | |
390 | return *this; | |
391 | } | |
392 | ||
393 | friend CScript operator+(const CScript& a, const CScript& b) | |
394 | { | |
395 | CScript ret = a; | |
396 | ret += b; | |
397 | return ret; | |
398 | } | |
399 | ||
400 | CScript(int64_t b) { operator<<(b); } | |
401 | ||
402 | explicit CScript(opcodetype b) { operator<<(b); } | |
cbd22a50 | 403 | explicit CScript(const CScriptNum& b) { operator<<(b); } |
404 | explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); } | |
405 | ||
406 | ||
407 | CScript& operator<<(int64_t b) { return push_int64(b); } | |
408 | ||
409 | CScript& operator<<(opcodetype opcode) | |
410 | { | |
411 | if (opcode < 0 || opcode > 0xff) | |
5262fde0 | 412 | throw std::runtime_error("CScript::operator<<(): invalid opcode"); |
cbd22a50 | 413 | insert(end(), (unsigned char)opcode); |
414 | return *this; | |
415 | } | |
416 | ||
cbd22a50 | 417 | CScript& operator<<(const CScriptNum& b) |
418 | { | |
419 | *this << b.getvch(); | |
420 | return *this; | |
421 | } | |
422 | ||
423 | CScript& operator<<(const std::vector<unsigned char>& b) | |
424 | { | |
425 | if (b.size() < OP_PUSHDATA1) | |
426 | { | |
427 | insert(end(), (unsigned char)b.size()); | |
428 | } | |
429 | else if (b.size() <= 0xff) | |
430 | { | |
431 | insert(end(), OP_PUSHDATA1); | |
432 | insert(end(), (unsigned char)b.size()); | |
433 | } | |
434 | else if (b.size() <= 0xffff) | |
435 | { | |
436 | insert(end(), OP_PUSHDATA2); | |
4e853aa1 WL |
437 | uint8_t data[2]; |
438 | WriteLE16(data, b.size()); | |
439 | insert(end(), data, data + sizeof(data)); | |
cbd22a50 | 440 | } |
441 | else | |
442 | { | |
443 | insert(end(), OP_PUSHDATA4); | |
4e853aa1 WL |
444 | uint8_t data[4]; |
445 | WriteLE32(data, b.size()); | |
446 | insert(end(), data, data + sizeof(data)); | |
cbd22a50 | 447 | } |
448 | insert(end(), b.begin(), b.end()); | |
449 | return *this; | |
450 | } | |
451 | ||
452 | CScript& operator<<(const CScript& b) | |
453 | { | |
454 | // I'm not sure if this should push the script or concatenate scripts. | |
455 | // If there's ever a use for pushing a script onto a script, delete this member fn | |
456 | assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!"); | |
457 | return *this; | |
458 | } | |
459 | ||
460 | ||
461 | bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) | |
462 | { | |
463 | // Wrapper so it can be called with either iterator or const_iterator | |
464 | const_iterator pc2 = pc; | |
465 | bool fRet = GetOp2(pc2, opcodeRet, &vchRet); | |
466 | pc = begin() + (pc2 - begin()); | |
467 | return fRet; | |
468 | } | |
469 | ||
470 | bool GetOp(iterator& pc, opcodetype& opcodeRet) | |
471 | { | |
472 | const_iterator pc2 = pc; | |
473 | bool fRet = GetOp2(pc2, opcodeRet, NULL); | |
474 | pc = begin() + (pc2 - begin()); | |
475 | return fRet; | |
476 | } | |
477 | ||
478 | bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const | |
479 | { | |
480 | return GetOp2(pc, opcodeRet, &vchRet); | |
481 | } | |
482 | ||
483 | bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const | |
484 | { | |
485 | return GetOp2(pc, opcodeRet, NULL); | |
486 | } | |
487 | ||
488 | bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const | |
489 | { | |
490 | opcodeRet = OP_INVALIDOPCODE; | |
491 | if (pvchRet) | |
492 | pvchRet->clear(); | |
493 | if (pc >= end()) | |
494 | return false; | |
495 | ||
496 | // Read instruction | |
497 | if (end() - pc < 1) | |
498 | return false; | |
499 | unsigned int opcode = *pc++; | |
500 | ||
501 | // Immediate operand | |
502 | if (opcode <= OP_PUSHDATA4) | |
503 | { | |
504 | unsigned int nSize = 0; | |
505 | if (opcode < OP_PUSHDATA1) | |
506 | { | |
507 | nSize = opcode; | |
508 | } | |
509 | else if (opcode == OP_PUSHDATA1) | |
510 | { | |
511 | if (end() - pc < 1) | |
512 | return false; | |
513 | nSize = *pc++; | |
514 | } | |
515 | else if (opcode == OP_PUSHDATA2) | |
516 | { | |
517 | if (end() - pc < 2) | |
518 | return false; | |
4e853aa1 | 519 | nSize = ReadLE16(&pc[0]); |
cbd22a50 | 520 | pc += 2; |
521 | } | |
522 | else if (opcode == OP_PUSHDATA4) | |
523 | { | |
524 | if (end() - pc < 4) | |
525 | return false; | |
4e853aa1 | 526 | nSize = ReadLE32(&pc[0]); |
cbd22a50 | 527 | pc += 4; |
528 | } | |
529 | if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize) | |
530 | return false; | |
531 | if (pvchRet) | |
532 | pvchRet->assign(pc, pc + nSize); | |
533 | pc += nSize; | |
534 | } | |
535 | ||
536 | opcodeRet = (opcodetype)opcode; | |
537 | return true; | |
538 | } | |
539 | ||
b9a36b15 | 540 | /** Encode/decode small integers: */ |
cbd22a50 | 541 | static int DecodeOP_N(opcodetype opcode) |
542 | { | |
543 | if (opcode == OP_0) | |
544 | return 0; | |
545 | assert(opcode >= OP_1 && opcode <= OP_16); | |
546 | return (int)opcode - (int)(OP_1 - 1); | |
547 | } | |
548 | static opcodetype EncodeOP_N(int n) | |
549 | { | |
550 | assert(n >= 0 && n <= 16); | |
551 | if (n == 0) | |
552 | return OP_0; | |
553 | return (opcodetype)(OP_1+n-1); | |
554 | } | |
555 | ||
b9a36b15 MF |
556 | /** |
557 | * Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs | |
558 | * as 20 sigops. With pay-to-script-hash, that changed: | |
559 | * CHECKMULTISIGs serialized in scriptSigs are | |
560 | * counted more accurately, assuming they are of the form | |
561 | * ... OP_N CHECKMULTISIG ... | |
562 | */ | |
cbd22a50 | 563 | unsigned int GetSigOpCount(bool fAccurate) const; |
564 | ||
b9a36b15 MF |
565 | /** |
566 | * Accurately count sigOps, including sigOps in | |
567 | * pay-to-script-hash transactions: | |
568 | */ | |
cbd22a50 | 569 | unsigned int GetSigOpCount(const CScript& scriptSig) const; |
570 | ||
8b78a819 T |
571 | bool IsPayToPublicKeyHash() const; |
572 | ||
cbd22a50 | 573 | bool IsPayToScriptHash() const; |
a99ca25a | 574 | bool IsPayToCryptoCondition() const; |
2c8d8268 | 575 | bool MayAcceptCryptoCondition() const; |
cbd22a50 | 576 | |
b9a36b15 | 577 | /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */ |
cbd22a50 | 578 | bool IsPushOnly() const; |
579 | ||
e980a26d | 580 | /** if the front of the script has check lock time verify. this is a fairly simple check. |
581 | * accepts NULL as parameter if unlockTime is not needed. | |
582 | */ | |
583 | bool IsCheckLockTimeVerify(int64_t *unlockTime) const; | |
584 | ||
585 | bool IsCheckLockTimeVerify() const; | |
586 | ||
b9a36b15 MF |
587 | /** |
588 | * Returns whether the script is guaranteed to fail at execution, | |
589 | * regardless of the initial stack. This allows outputs to be pruned | |
590 | * instantly when entering the UTXO set. | |
591 | */ | |
cbd22a50 | 592 | bool IsUnspendable() const |
593 | { | |
6fc5764e | 594 | return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE); |
cbd22a50 | 595 | } |
596 | ||
db8eb54b | 597 | std::string ToString() const; |
cbd22a50 | 598 | void clear() |
599 | { | |
600 | // The default std::vector::clear() does not release memory. | |
601 | std::vector<unsigned char>().swap(*this); | |
602 | } | |
603 | }; | |
604 | ||
84738627 | 605 | #endif // BITCOIN_SCRIPT_SCRIPT_H |