]>
Commit | Line | Data |
---|---|---|
0a61b0df | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
db0e8ccd | 2 | // Copyright (c) 2009-2013 The Bitcoin developers |
0a61b0df | 3 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
51ed9ec9 | 5 | |
223b6f1b WL |
6 | #ifndef H_BITCOIN_SCRIPT |
7 | #define H_BITCOIN_SCRIPT | |
8 | ||
51ed9ec9 BD |
9 | #include "key.h" |
10 | #include "util.h" | |
11 | ||
12 | #include <stdexcept> | |
13 | #include <stdint.h> | |
223b6f1b WL |
14 | #include <string> |
15 | #include <vector> | |
0a61b0df | 16 | |
437173f4 | 17 | #include <boost/foreach.hpp> |
10254401 PW |
18 | #include <boost/variant.hpp> |
19 | ||
450cbb09 | 20 | class CCoins; |
51ed9ec9 | 21 | class CKeyStore; |
0a61b0df | 22 | class CTransaction; |
a90689ff | 23 | struct CMutableTransaction; |
0a61b0df | 24 | |
192cc910 | 25 | static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes |
8175c790 | 26 | static const unsigned int MAX_OP_RETURN_RELAY = 40; // bytes |
192cc910 | 27 | |
48d8eb18 CF |
28 | class scriptnum_error : public std::runtime_error |
29 | { | |
30 | public: | |
31 | explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {} | |
32 | }; | |
33 | ||
34 | class CScriptNum | |
35 | { | |
36 | // Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers. | |
37 | // The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1], | |
38 | // but results may overflow (and are valid as long as they are not used in a subsequent | |
39 | // numeric operation). CScriptNum enforces those semantics by storing results as | |
40 | // an int64 and allowing out-of-range values to be returned as a vector of bytes but | |
41 | // throwing an exception if arithmetic is done or the result is interpreted as an integer. | |
42 | public: | |
43 | ||
44 | explicit CScriptNum(const int64_t& n) | |
45 | { | |
46 | m_value = n; | |
47 | } | |
48 | ||
49 | explicit CScriptNum(const std::vector<unsigned char>& vch) | |
50 | { | |
51 | if (vch.size() > nMaxNumSize) | |
52 | throw scriptnum_error("CScriptNum(const std::vector<unsigned char>&) : overflow"); | |
53 | m_value = set_vch(vch); | |
54 | } | |
55 | ||
56 | inline bool operator==(const int64_t& rhs) const { return m_value == rhs; } | |
57 | inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; } | |
58 | inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; } | |
59 | inline bool operator< (const int64_t& rhs) const { return m_value < rhs; } | |
60 | inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; } | |
61 | inline bool operator> (const int64_t& rhs) const { return m_value > rhs; } | |
62 | ||
63 | inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); } | |
64 | inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); } | |
65 | inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); } | |
66 | inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); } | |
67 | inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); } | |
68 | inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); } | |
69 | ||
70 | inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);} | |
71 | inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);} | |
72 | inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); } | |
73 | inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); } | |
74 | ||
75 | inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); } | |
76 | inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); } | |
77 | ||
78 | inline CScriptNum operator-() const | |
79 | { | |
80 | assert(m_value != std::numeric_limits<int64_t>::min()); | |
81 | return CScriptNum(-m_value); | |
82 | } | |
83 | ||
84 | inline CScriptNum& operator=( const int64_t& rhs) | |
85 | { | |
86 | m_value = rhs; | |
87 | return *this; | |
88 | } | |
89 | ||
90 | inline CScriptNum& operator+=( const int64_t& rhs) | |
91 | { | |
92 | assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) || | |
93 | (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs)); | |
94 | m_value += rhs; | |
95 | return *this; | |
96 | } | |
97 | ||
98 | inline CScriptNum& operator-=( const int64_t& rhs) | |
99 | { | |
100 | assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) || | |
101 | (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs)); | |
102 | m_value -= rhs; | |
103 | return *this; | |
104 | } | |
105 | ||
106 | int getint() const | |
107 | { | |
108 | if (m_value > std::numeric_limits<int>::max()) | |
109 | return std::numeric_limits<int>::max(); | |
110 | else if (m_value < std::numeric_limits<int>::min()) | |
111 | return std::numeric_limits<int>::min(); | |
112 | return m_value; | |
113 | } | |
114 | ||
115 | std::vector<unsigned char> getvch() const | |
116 | { | |
117 | return serialize(m_value); | |
118 | } | |
119 | ||
120 | static std::vector<unsigned char> serialize(const int64_t& value) | |
121 | { | |
122 | if(value == 0) | |
123 | return std::vector<unsigned char>(); | |
124 | ||
125 | std::vector<unsigned char> result; | |
126 | const bool neg = value < 0; | |
127 | uint64_t absvalue = neg ? -value : value; | |
128 | ||
129 | while(absvalue) | |
130 | { | |
131 | result.push_back(absvalue & 0xff); | |
132 | absvalue >>= 8; | |
133 | } | |
134 | ||
135 | ||
136 | // - If the most significant byte is >= 0x80 and the value is positive, push a | |
137 | // new zero-byte to make the significant byte < 0x80 again. | |
138 | ||
139 | // - If the most significant byte is >= 0x80 and the value is negative, push a | |
140 | // new 0x80 byte that will be popped off when converting to an integral. | |
141 | ||
142 | // - If the most significant byte is < 0x80 and the value is negative, add | |
143 | // 0x80 to it, since it will be subtracted and interpreted as a negative when | |
144 | // converting to an integral. | |
145 | ||
146 | if (result.back() & 0x80) | |
147 | result.push_back(neg ? 0x80 : 0); | |
148 | else if (neg) | |
149 | result.back() |= 0x80; | |
150 | ||
151 | return result; | |
152 | } | |
153 | ||
154 | static const size_t nMaxNumSize = 4; | |
155 | ||
156 | private: | |
157 | static int64_t set_vch(const std::vector<unsigned char>& vch) | |
158 | { | |
159 | if (vch.empty()) | |
160 | return 0; | |
161 | ||
162 | int64_t result = 0; | |
163 | for (size_t i = 0; i != vch.size(); ++i) | |
164 | result |= static_cast<int64_t>(vch[i]) << 8*i; | |
165 | ||
166 | // If the input vector's most significant byte is 0x80, remove it from | |
167 | // the result's msb and return a negative. | |
168 | if (vch.back() & 0x80) | |
6fd59ee8 | 169 | return -(result & ~(0x80ULL << (8 * (vch.size() - 1)))); |
48d8eb18 CF |
170 | |
171 | return result; | |
172 | } | |
173 | ||
174 | int64_t m_value; | |
175 | }; | |
176 | ||
6b8de05d | 177 | /** Signature hash types/flags */ |
0a61b0df | 178 | enum |
179 | { | |
180 | SIGHASH_ALL = 1, | |
181 | SIGHASH_NONE = 2, | |
182 | SIGHASH_SINGLE = 3, | |
183 | SIGHASH_ANYONECANPAY = 0x80, | |
184 | }; | |
185 | ||
99d0d0f3 PW |
186 | /** Script verification flags */ |
187 | enum | |
188 | { | |
189 | SCRIPT_VERIFY_NONE = 0, | |
a81cd968 PW |
190 | SCRIPT_VERIFY_P2SH = (1U << 0), // evaluate P2SH (BIP16) subscripts |
191 | SCRIPT_VERIFY_STRICTENC = (1U << 1), // enforce strict conformance to DER and SEC2 for signatures and pubkeys | |
6fd7ef2b | 192 | SCRIPT_VERIFY_LOW_S = (1U << 2), // enforce low S values (<n/2) in signatures (depends on STRICTENC) |
a81cd968 | 193 | SCRIPT_VERIFY_NOCACHE = (1U << 3), // do not store results in signature cache (but do query it) |
63801808 | 194 | SCRIPT_VERIFY_NULLDUMMY = (1U << 4), // verify dummy stack item consumed by CHECKMULTISIG is of zero-length |
99d0d0f3 | 195 | }; |
0a61b0df | 196 | |
c8988460 PW |
197 | /** IsMine() return codes */ |
198 | enum isminetype | |
199 | { | |
a3e192a3 J |
200 | ISMINE_NO = 0, |
201 | ISMINE_WATCH_ONLY = 1, | |
202 | ISMINE_SPENDABLE = 2, | |
203 | ISMINE_ALL = ISMINE_WATCH_ONLY | ISMINE_SPENDABLE | |
c8988460 | 204 | }; |
ffd40da3 J |
205 | /** used for bitflags of isminetype */ |
206 | typedef uint8_t isminefilter; | |
c8988460 | 207 | |
68f7d1d7 PT |
208 | // Mandatory script verification flags that all new blocks must comply with for |
209 | // them to be valid. (but old blocks may not comply with) Currently just P2SH, | |
210 | // but in the future other flags may be added, such as a soft-fork to enforce | |
211 | // strict DER encoding. | |
f80cffa2 PT |
212 | // |
213 | // Failing one of these tests may trigger a DoS ban - see CheckInputs() for | |
214 | // details. | |
68f7d1d7 PT |
215 | static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH; |
216 | ||
217 | // Standard script verification flags that standard transactions will comply | |
218 | // with. However scripts violating these flags may still be present in valid | |
219 | // blocks and we must accept those blocks. | |
220 | static const unsigned int STANDARD_SCRIPT_VERIFY_FLAGS = MANDATORY_SCRIPT_VERIFY_FLAGS | | |
63801808 PT |
221 | SCRIPT_VERIFY_STRICTENC | |
222 | SCRIPT_VERIFY_NULLDUMMY; | |
68f7d1d7 | 223 | |
f80cffa2 PT |
224 | // For convenience, standard but not mandatory verify flags. |
225 | static const unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS = STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS; | |
226 | ||
2a45a494 | 227 | enum txnouttype |
e679ec96 GA |
228 | { |
229 | TX_NONSTANDARD, | |
230 | // 'standard' transaction types: | |
231 | TX_PUBKEY, | |
232 | TX_PUBKEYHASH, | |
233 | TX_SCRIPTHASH, | |
234 | TX_MULTISIG, | |
a7934247 | 235 | TX_NULL_DATA, |
e679ec96 GA |
236 | }; |
237 | ||
10254401 PW |
238 | class CNoDestination { |
239 | public: | |
240 | friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; } | |
241 | friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; } | |
242 | }; | |
243 | ||
244 | /** A txout script template with a specific destination. It is either: | |
245 | * * CNoDestination: no destination set | |
246 | * * CKeyID: TX_PUBKEYHASH destination | |
247 | * * CScriptID: TX_SCRIPTHASH destination | |
248 | * A CTxDestination is the internal data type encoded in a CBitcoinAddress | |
249 | */ | |
250 | typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination; | |
251 | ||
2a45a494 | 252 | const char* GetTxnOutputType(txnouttype t); |
0a61b0df | 253 | |
6b8de05d | 254 | /** Script opcodes */ |
0a61b0df | 255 | enum opcodetype |
256 | { | |
257 | // push value | |
7be8b2ff WL |
258 | OP_0 = 0x00, |
259 | OP_FALSE = OP_0, | |
260 | OP_PUSHDATA1 = 0x4c, | |
261 | OP_PUSHDATA2 = 0x4d, | |
262 | OP_PUSHDATA4 = 0x4e, | |
263 | OP_1NEGATE = 0x4f, | |
264 | OP_RESERVED = 0x50, | |
265 | OP_1 = 0x51, | |
0a61b0df | 266 | OP_TRUE=OP_1, |
7be8b2ff WL |
267 | OP_2 = 0x52, |
268 | OP_3 = 0x53, | |
269 | OP_4 = 0x54, | |
270 | OP_5 = 0x55, | |
271 | OP_6 = 0x56, | |
272 | OP_7 = 0x57, | |
273 | OP_8 = 0x58, | |
274 | OP_9 = 0x59, | |
275 | OP_10 = 0x5a, | |
276 | OP_11 = 0x5b, | |
277 | OP_12 = 0x5c, | |
278 | OP_13 = 0x5d, | |
279 | OP_14 = 0x5e, | |
280 | OP_15 = 0x5f, | |
281 | OP_16 = 0x60, | |
0a61b0df | 282 | |
283 | // control | |
7be8b2ff WL |
284 | OP_NOP = 0x61, |
285 | OP_VER = 0x62, | |
286 | OP_IF = 0x63, | |
287 | OP_NOTIF = 0x64, | |
288 | OP_VERIF = 0x65, | |
289 | OP_VERNOTIF = 0x66, | |
290 | OP_ELSE = 0x67, | |
291 | OP_ENDIF = 0x68, | |
292 | OP_VERIFY = 0x69, | |
293 | OP_RETURN = 0x6a, | |
0a61b0df | 294 | |
295 | // stack ops | |
7be8b2ff WL |
296 | OP_TOALTSTACK = 0x6b, |
297 | OP_FROMALTSTACK = 0x6c, | |
298 | OP_2DROP = 0x6d, | |
299 | OP_2DUP = 0x6e, | |
300 | OP_3DUP = 0x6f, | |
301 | OP_2OVER = 0x70, | |
302 | OP_2ROT = 0x71, | |
303 | OP_2SWAP = 0x72, | |
304 | OP_IFDUP = 0x73, | |
305 | OP_DEPTH = 0x74, | |
306 | OP_DROP = 0x75, | |
307 | OP_DUP = 0x76, | |
308 | OP_NIP = 0x77, | |
309 | OP_OVER = 0x78, | |
310 | OP_PICK = 0x79, | |
311 | OP_ROLL = 0x7a, | |
312 | OP_ROT = 0x7b, | |
313 | OP_SWAP = 0x7c, | |
314 | OP_TUCK = 0x7d, | |
0a61b0df | 315 | |
316 | // splice ops | |
7be8b2ff WL |
317 | OP_CAT = 0x7e, |
318 | OP_SUBSTR = 0x7f, | |
319 | OP_LEFT = 0x80, | |
320 | OP_RIGHT = 0x81, | |
321 | OP_SIZE = 0x82, | |
0a61b0df | 322 | |
323 | // bit logic | |
7be8b2ff WL |
324 | OP_INVERT = 0x83, |
325 | OP_AND = 0x84, | |
326 | OP_OR = 0x85, | |
327 | OP_XOR = 0x86, | |
328 | OP_EQUAL = 0x87, | |
329 | OP_EQUALVERIFY = 0x88, | |
330 | OP_RESERVED1 = 0x89, | |
331 | OP_RESERVED2 = 0x8a, | |
0a61b0df | 332 | |
333 | // numeric | |
7be8b2ff WL |
334 | OP_1ADD = 0x8b, |
335 | OP_1SUB = 0x8c, | |
336 | OP_2MUL = 0x8d, | |
337 | OP_2DIV = 0x8e, | |
338 | OP_NEGATE = 0x8f, | |
339 | OP_ABS = 0x90, | |
340 | OP_NOT = 0x91, | |
341 | OP_0NOTEQUAL = 0x92, | |
342 | ||
343 | OP_ADD = 0x93, | |
344 | OP_SUB = 0x94, | |
345 | OP_MUL = 0x95, | |
346 | OP_DIV = 0x96, | |
347 | OP_MOD = 0x97, | |
348 | OP_LSHIFT = 0x98, | |
349 | OP_RSHIFT = 0x99, | |
350 | ||
351 | OP_BOOLAND = 0x9a, | |
352 | OP_BOOLOR = 0x9b, | |
353 | OP_NUMEQUAL = 0x9c, | |
354 | OP_NUMEQUALVERIFY = 0x9d, | |
355 | OP_NUMNOTEQUAL = 0x9e, | |
356 | OP_LESSTHAN = 0x9f, | |
357 | OP_GREATERTHAN = 0xa0, | |
358 | OP_LESSTHANOREQUAL = 0xa1, | |
359 | OP_GREATERTHANOREQUAL = 0xa2, | |
360 | OP_MIN = 0xa3, | |
361 | OP_MAX = 0xa4, | |
362 | ||
363 | OP_WITHIN = 0xa5, | |
0a61b0df | 364 | |
365 | // crypto | |
7be8b2ff WL |
366 | OP_RIPEMD160 = 0xa6, |
367 | OP_SHA1 = 0xa7, | |
368 | OP_SHA256 = 0xa8, | |
369 | OP_HASH160 = 0xa9, | |
370 | OP_HASH256 = 0xaa, | |
371 | OP_CODESEPARATOR = 0xab, | |
372 | OP_CHECKSIG = 0xac, | |
373 | OP_CHECKSIGVERIFY = 0xad, | |
374 | OP_CHECKMULTISIG = 0xae, | |
375 | OP_CHECKMULTISIGVERIFY = 0xaf, | |
0a61b0df | 376 | |
377 | // expansion | |
7be8b2ff WL |
378 | OP_NOP1 = 0xb0, |
379 | OP_NOP2 = 0xb1, | |
380 | OP_NOP3 = 0xb2, | |
381 | OP_NOP4 = 0xb3, | |
382 | OP_NOP5 = 0xb4, | |
383 | OP_NOP6 = 0xb5, | |
384 | OP_NOP7 = 0xb6, | |
385 | OP_NOP8 = 0xb7, | |
386 | OP_NOP9 = 0xb8, | |
387 | OP_NOP10 = 0xb9, | |
0a61b0df | 388 | |
389 | ||
390 | ||
0a61b0df | 391 | // template matching params |
a7934247 | 392 | OP_SMALLDATA = 0xf9, |
e679ec96 GA |
393 | OP_SMALLINTEGER = 0xfa, |
394 | OP_PUBKEYS = 0xfb, | |
f1e1fb4b | 395 | OP_PUBKEYHASH = 0xfd, |
396 | OP_PUBKEY = 0xfe, | |
0a61b0df | 397 | |
f1e1fb4b | 398 | OP_INVALIDOPCODE = 0xff, |
0a61b0df | 399 | }; |
400 | ||
e679ec96 | 401 | const char* GetOpName(opcodetype opcode); |
0a61b0df | 402 | |
403 | ||
404 | ||
223b6f1b | 405 | inline std::string ValueString(const std::vector<unsigned char>& vch) |
0a61b0df | 406 | { |
407 | if (vch.size() <= 4) | |
27bff74e | 408 | return strprintf("%d", CScriptNum(vch).getint()); |
0a61b0df | 409 | else |
f1e1fb4b | 410 | return HexStr(vch); |
0a61b0df | 411 | } |
412 | ||
223b6f1b | 413 | inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack) |
0a61b0df | 414 | { |
223b6f1b WL |
415 | std::string str; |
416 | BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack) | |
0a61b0df | 417 | { |
418 | if (!str.empty()) | |
419 | str += " "; | |
420 | str += ValueString(vch); | |
421 | } | |
422 | return str; | |
423 | } | |
424 | ||
425 | ||
426 | ||
427 | ||
428 | ||
429 | ||
430 | ||
431 | ||
6b8de05d | 432 | /** Serialized script, used inside transaction inputs and outputs */ |
223b6f1b | 433 | class CScript : public std::vector<unsigned char> |
0a61b0df | 434 | { |
435 | protected: | |
51ed9ec9 | 436 | CScript& push_int64(int64_t n) |
0a61b0df | 437 | { |
438 | if (n == -1 || (n >= 1 && n <= 16)) | |
439 | { | |
440 | push_back(n + (OP_1 - 1)); | |
441 | } | |
442 | else | |
443 | { | |
27bff74e | 444 | *this << CScriptNum::serialize(n); |
0a61b0df | 445 | } |
f1e1fb4b | 446 | return *this; |
0a61b0df | 447 | } |
0a61b0df | 448 | public: |
449 | CScript() { } | |
223b6f1b WL |
450 | CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { } |
451 | CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { } | |
0a61b0df | 452 | #ifndef _MSC_VER |
223b6f1b | 453 | CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { } |
0a61b0df | 454 | #endif |
455 | ||
456 | CScript& operator+=(const CScript& b) | |
457 | { | |
458 | insert(end(), b.begin(), b.end()); | |
459 | return *this; | |
460 | } | |
461 | ||
462 | friend CScript operator+(const CScript& a, const CScript& b) | |
463 | { | |
464 | CScript ret = a; | |
465 | ret += b; | |
f1e1fb4b | 466 | return ret; |
0a61b0df | 467 | } |
468 | ||
469 | ||
27bff74e | 470 | CScript(int64_t b) { operator<<(b); } |
0a61b0df | 471 | |
472 | explicit CScript(opcodetype b) { operator<<(b); } | |
473 | explicit CScript(const uint256& b) { operator<<(b); } | |
27bff74e | 474 | explicit CScript(const CScriptNum& b) { operator<<(b); } |
223b6f1b | 475 | explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); } |
0a61b0df | 476 | |
477 | ||
27bff74e | 478 | CScript& operator<<(int64_t b) { return push_int64(b); } |
0a61b0df | 479 | |
480 | CScript& operator<<(opcodetype opcode) | |
481 | { | |
f1e1fb4b | 482 | if (opcode < 0 || opcode > 0xff) |
223b6f1b | 483 | throw std::runtime_error("CScript::operator<<() : invalid opcode"); |
f1e1fb4b | 484 | insert(end(), (unsigned char)opcode); |
485 | return *this; | |
0a61b0df | 486 | } |
487 | ||
488 | CScript& operator<<(const uint160& b) | |
489 | { | |
490 | insert(end(), sizeof(b)); | |
491 | insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b)); | |
f1e1fb4b | 492 | return *this; |
0a61b0df | 493 | } |
494 | ||
495 | CScript& operator<<(const uint256& b) | |
496 | { | |
497 | insert(end(), sizeof(b)); | |
498 | insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b)); | |
f1e1fb4b | 499 | return *this; |
0a61b0df | 500 | } |
501 | ||
fd61d6f5 PW |
502 | CScript& operator<<(const CPubKey& key) |
503 | { | |
5d891489 PW |
504 | assert(key.size() < OP_PUSHDATA1); |
505 | insert(end(), (unsigned char)key.size()); | |
506 | insert(end(), key.begin(), key.end()); | |
507 | return *this; | |
fd61d6f5 PW |
508 | } |
509 | ||
27bff74e CF |
510 | CScript& operator<<(const CScriptNum& b) |
511 | { | |
512 | *this << b.getvch(); | |
513 | return *this; | |
514 | } | |
515 | ||
223b6f1b | 516 | CScript& operator<<(const std::vector<unsigned char>& b) |
0a61b0df | 517 | { |
518 | if (b.size() < OP_PUSHDATA1) | |
519 | { | |
520 | insert(end(), (unsigned char)b.size()); | |
521 | } | |
522 | else if (b.size() <= 0xff) | |
523 | { | |
524 | insert(end(), OP_PUSHDATA1); | |
525 | insert(end(), (unsigned char)b.size()); | |
526 | } | |
f1e1fb4b | 527 | else if (b.size() <= 0xffff) |
0a61b0df | 528 | { |
529 | insert(end(), OP_PUSHDATA2); | |
530 | unsigned short nSize = b.size(); | |
531 | insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize)); | |
532 | } | |
f1e1fb4b | 533 | else |
534 | { | |
535 | insert(end(), OP_PUSHDATA4); | |
536 | unsigned int nSize = b.size(); | |
537 | insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize)); | |
538 | } | |
0a61b0df | 539 | insert(end(), b.begin(), b.end()); |
f1e1fb4b | 540 | return *this; |
0a61b0df | 541 | } |
542 | ||
543 | CScript& operator<<(const CScript& b) | |
544 | { | |
545 | // I'm not sure if this should push the script or concatenate scripts. | |
546 | // If there's ever a use for pushing a script onto a script, delete this member fn | |
e6bc9c35 | 547 | assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!"); |
f1e1fb4b | 548 | return *this; |
0a61b0df | 549 | } |
550 | ||
551 | ||
223b6f1b | 552 | bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) |
0a61b0df | 553 | { |
554 | // Wrapper so it can be called with either iterator or const_iterator | |
555 | const_iterator pc2 = pc; | |
f1e1fb4b | 556 | bool fRet = GetOp2(pc2, opcodeRet, &vchRet); |
557 | pc = begin() + (pc2 - begin()); | |
558 | return fRet; | |
559 | } | |
560 | ||
561 | bool GetOp(iterator& pc, opcodetype& opcodeRet) | |
562 | { | |
563 | const_iterator pc2 = pc; | |
564 | bool fRet = GetOp2(pc2, opcodeRet, NULL); | |
0a61b0df | 565 | pc = begin() + (pc2 - begin()); |
566 | return fRet; | |
567 | } | |
568 | ||
223b6f1b | 569 | bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const |
f1e1fb4b | 570 | { |
571 | return GetOp2(pc, opcodeRet, &vchRet); | |
572 | } | |
573 | ||
574 | bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const | |
575 | { | |
576 | return GetOp2(pc, opcodeRet, NULL); | |
577 | } | |
578 | ||
223b6f1b | 579 | bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const |
0a61b0df | 580 | { |
581 | opcodeRet = OP_INVALIDOPCODE; | |
f1e1fb4b | 582 | if (pvchRet) |
583 | pvchRet->clear(); | |
0a61b0df | 584 | if (pc >= end()) |
585 | return false; | |
586 | ||
587 | // Read instruction | |
f1e1fb4b | 588 | if (end() - pc < 1) |
589 | return false; | |
0a61b0df | 590 | unsigned int opcode = *pc++; |
0a61b0df | 591 | |
592 | // Immediate operand | |
593 | if (opcode <= OP_PUSHDATA4) | |
594 | { | |
5f048816 | 595 | unsigned int nSize = 0; |
f1e1fb4b | 596 | if (opcode < OP_PUSHDATA1) |
597 | { | |
598 | nSize = opcode; | |
599 | } | |
600 | else if (opcode == OP_PUSHDATA1) | |
0a61b0df | 601 | { |
f1e1fb4b | 602 | if (end() - pc < 1) |
0a61b0df | 603 | return false; |
604 | nSize = *pc++; | |
605 | } | |
606 | else if (opcode == OP_PUSHDATA2) | |
607 | { | |
f1e1fb4b | 608 | if (end() - pc < 2) |
0a61b0df | 609 | return false; |
610 | nSize = 0; | |
611 | memcpy(&nSize, &pc[0], 2); | |
612 | pc += 2; | |
613 | } | |
614 | else if (opcode == OP_PUSHDATA4) | |
615 | { | |
f1e1fb4b | 616 | if (end() - pc < 4) |
0a61b0df | 617 | return false; |
618 | memcpy(&nSize, &pc[0], 4); | |
619 | pc += 4; | |
620 | } | |
467b7939 | 621 | if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize) |
0a61b0df | 622 | return false; |
f1e1fb4b | 623 | if (pvchRet) |
624 | pvchRet->assign(pc, pc + nSize); | |
0a61b0df | 625 | pc += nSize; |
626 | } | |
627 | ||
628 | opcodeRet = (opcodetype)opcode; | |
629 | return true; | |
630 | } | |
631 | ||
e679ec96 | 632 | // Encode/decode small integers: |
bf798734 GA |
633 | static int DecodeOP_N(opcodetype opcode) |
634 | { | |
635 | if (opcode == OP_0) | |
636 | return 0; | |
637 | assert(opcode >= OP_1 && opcode <= OP_16); | |
638 | return (int)opcode - (int)(OP_1 - 1); | |
639 | } | |
e679ec96 GA |
640 | static opcodetype EncodeOP_N(int n) |
641 | { | |
642 | assert(n >= 0 && n <= 16); | |
643 | if (n == 0) | |
644 | return OP_0; | |
645 | return (opcodetype)(OP_1+n-1); | |
646 | } | |
0a61b0df | 647 | |
e679ec96 | 648 | int FindAndDelete(const CScript& b) |
0a61b0df | 649 | { |
e679ec96 | 650 | int nFound = 0; |
f1e1fb4b | 651 | if (b.empty()) |
e679ec96 | 652 | return nFound; |
0a61b0df | 653 | iterator pc = begin(); |
654 | opcodetype opcode; | |
0a61b0df | 655 | do |
656 | { | |
1d8c7a95 | 657 | while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0) |
e679ec96 | 658 | { |
0a61b0df | 659 | erase(pc, pc + b.size()); |
e679ec96 GA |
660 | ++nFound; |
661 | } | |
0a61b0df | 662 | } |
f1e1fb4b | 663 | while (GetOp(pc, opcode)); |
e679ec96 GA |
664 | return nFound; |
665 | } | |
666 | int Find(opcodetype op) const | |
667 | { | |
668 | int nFound = 0; | |
669 | opcodetype opcode; | |
670 | for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);) | |
671 | if (opcode == op) | |
672 | ++nFound; | |
673 | return nFound; | |
f1e1fb4b | 674 | } |
675 | ||
922e8e29 GA |
676 | // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs |
677 | // as 20 sigops. With pay-to-script-hash, that changed: | |
678 | // CHECKMULTISIGs serialized in scriptSigs are | |
679 | // counted more accurately, assuming they are of the form | |
680 | // ... OP_N CHECKMULTISIG ... | |
7bd9c3a3 | 681 | unsigned int GetSigOpCount(bool fAccurate) const; |
922e8e29 GA |
682 | |
683 | // Accurately count sigOps, including sigOps in | |
684 | // pay-to-script-hash transactions: | |
7bd9c3a3 | 685 | unsigned int GetSigOpCount(const CScript& scriptSig) const; |
922e8e29 GA |
686 | |
687 | bool IsPayToScriptHash() const; | |
0a61b0df | 688 | |
87fe71e1 | 689 | // Called by IsStandardTx and P2SH VerifyScript (which makes it consensus-critical). |
9aea601b | 690 | bool IsPushOnly() const; |
a206a239 | 691 | |
87fe71e1 PW |
692 | // Called by IsStandardTx. |
693 | bool HasCanonicalPushes() const; | |
694 | ||
ec84e81e PW |
695 | // Returns whether the script is guaranteed to fail at execution, |
696 | // regardless of the initial stack. This allows outputs to be pruned | |
697 | // instantly when entering the UTXO set. | |
698 | bool IsUnspendable() const | |
699 | { | |
700 | return (size() > 0 && *begin() == OP_RETURN); | |
701 | } | |
a206a239 | 702 | |
10254401 | 703 | void SetDestination(const CTxDestination& address); |
dfa23b94 | 704 | void SetMultisig(int nRequired, const std::vector<CPubKey>& keys); |
0a61b0df | 705 | |
223b6f1b | 706 | std::string ToString() const |
0a61b0df | 707 | { |
223b6f1b | 708 | std::string str; |
0a61b0df | 709 | opcodetype opcode; |
223b6f1b | 710 | std::vector<unsigned char> vch; |
f1e1fb4b | 711 | const_iterator pc = begin(); |
712 | while (pc < end()) | |
0a61b0df | 713 | { |
714 | if (!str.empty()) | |
715 | str += " "; | |
f1e1fb4b | 716 | if (!GetOp(pc, opcode, vch)) |
717 | { | |
718 | str += "[error]"; | |
719 | return str; | |
720 | } | |
721 | if (0 <= opcode && opcode <= OP_PUSHDATA4) | |
0a61b0df | 722 | str += ValueString(vch); |
723 | else | |
724 | str += GetOpName(opcode); | |
725 | } | |
726 | return str; | |
727 | } | |
728 | ||
10254401 PW |
729 | CScriptID GetID() const |
730 | { | |
731 | return CScriptID(Hash160(*this)); | |
732 | } | |
0a61b0df | 733 | }; |
734 | ||
69fc8047 PW |
735 | /** Compact serializer for scripts. |
736 | * | |
737 | * It detects common cases and encodes them much more efficiently. | |
738 | * 3 special cases are defined: | |
739 | * * Pay to pubkey hash (encoded as 21 bytes) | |
740 | * * Pay to script hash (encoded as 21 bytes) | |
741 | * * Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes) | |
742 | * | |
743 | * Other scripts up to 121 bytes require 1 byte + script length. Above | |
744 | * that, scripts up to 16505 bytes require 2 bytes + script length. | |
745 | */ | |
746 | class CScriptCompressor | |
747 | { | |
748 | private: | |
749 | // make this static for now (there are only 6 special scripts defined) | |
750 | // this can potentially be extended together with a new nVersion for | |
751 | // transactions, in which case this value becomes dependent on nVersion | |
752 | // and nHeight of the enclosing transaction. | |
753 | static const unsigned int nSpecialScripts = 6; | |
754 | ||
755 | CScript &script; | |
756 | protected: | |
757 | // These check for scripts for which a special case with a shorter encoding is defined. | |
758 | // They are implemented separately from the CScript test, as these test for exact byte | |
759 | // sequence correspondences, and are more strict. For example, IsToPubKey also verifies | |
760 | // whether the public key is valid (as invalid ones cannot be represented in compressed | |
761 | // form). | |
762 | bool IsToKeyID(CKeyID &hash) const; | |
763 | bool IsToScriptID(CScriptID &hash) const; | |
dfa23b94 | 764 | bool IsToPubKey(CPubKey &pubkey) const; |
69fc8047 PW |
765 | |
766 | bool Compress(std::vector<unsigned char> &out) const; | |
767 | unsigned int GetSpecialSize(unsigned int nSize) const; | |
768 | bool Decompress(unsigned int nSize, const std::vector<unsigned char> &out); | |
769 | public: | |
770 | CScriptCompressor(CScript &scriptIn) : script(scriptIn) { } | |
771 | ||
772 | unsigned int GetSerializeSize(int nType, int nVersion) const { | |
773 | std::vector<unsigned char> compr; | |
774 | if (Compress(compr)) | |
775 | return compr.size(); | |
776 | unsigned int nSize = script.size() + nSpecialScripts; | |
777 | return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion); | |
778 | } | |
0a61b0df | 779 | |
69fc8047 PW |
780 | template<typename Stream> |
781 | void Serialize(Stream &s, int nType, int nVersion) const { | |
782 | std::vector<unsigned char> compr; | |
783 | if (Compress(compr)) { | |
fa126eff | 784 | s << CFlatData(compr); |
69fc8047 PW |
785 | return; |
786 | } | |
787 | unsigned int nSize = script.size() + nSpecialScripts; | |
788 | s << VARINT(nSize); | |
fa126eff | 789 | s << CFlatData(script); |
69fc8047 PW |
790 | } |
791 | ||
792 | template<typename Stream> | |
793 | void Unserialize(Stream &s, int nType, int nVersion) { | |
5f048816 | 794 | unsigned int nSize = 0; |
69fc8047 PW |
795 | s >> VARINT(nSize); |
796 | if (nSize < nSpecialScripts) { | |
797 | std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00); | |
fa126eff | 798 | s >> REF(CFlatData(vch)); |
69fc8047 PW |
799 | Decompress(nSize, vch); |
800 | return; | |
801 | } | |
802 | nSize -= nSpecialScripts; | |
803 | script.resize(nSize); | |
fa126eff | 804 | s >> REF(CFlatData(script)); |
69fc8047 PW |
805 | } |
806 | }; | |
0a61b0df | 807 | |
a81cd968 PW |
808 | bool IsCanonicalPubKey(const std::vector<unsigned char> &vchPubKey, unsigned int flags); |
809 | bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int flags); | |
0a61b0df | 810 | |
99d0d0f3 | 811 | bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType); |
f5745fa5 | 812 | uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType); |
2a45a494 | 813 | bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet); |
39f0d968 | 814 | int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions); |
a7934247 | 815 | bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType); |
c8988460 | 816 | isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); |
d5087d1b | 817 | isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest); |
acc775c5 | 818 | void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys); |
10254401 PW |
819 | bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet); |
820 | bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet); | |
4949004d PW |
821 | bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL); |
822 | bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL); | |
f1136200 | 823 | bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType); |
223b6f1b | 824 | |
a2709fad GA |
825 | // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders, |
826 | // combine them intelligently and return the result. | |
827 | CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn, const CScript& scriptSig1, const CScript& scriptSig2); | |
828 | ||
223b6f1b | 829 | #endif |