upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/src/script.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/script.h')
-rw-r--r--src/script.h633
1 files changed, 633 insertions, 0 deletions
diff --git a/src/script.h b/src/script.h
new file mode 100644
index 0000000..d36a317
--- /dev/null
+++ b/src/script.h
@@ -0,0 +1,633 @@
1// Copyright (c) 2009 Satoshi Nakamoto
2// Distributed under the MIT/X11 software license, see the accompanying
3// file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5class CTransaction;
6
7// Largest byte vector a script may push onto the stack. Without a ceiling,
8// script evaluation allocates whatever the transaction asks it to.
9static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
10
11// Bounds on what one script may do while being evaluated. The element cap
12// above limits how big a single item is; these limit how many there can be and
13// how much work producing them costs. Without them, a script pays for itself
14// once, in bytes, and then multiplies: 520 bytes per stack entry means a
15// script of N bytes made almost entirely of OP_DUP occupies roughly 520N in
16// memory, and every node that validates it pays.
17static const unsigned int MAX_OPS_PER_SCRIPT = 201;
18static const unsigned int MAX_STACK_SIZE = 1000;
19
20enum
21{
22 SIGHASH_ALL = 1,
23 SIGHASH_NONE = 2,
24 SIGHASH_SINGLE = 3,
25 SIGHASH_ANYONECANPAY = 0x80,
26};
27
28
29
30enum opcodetype
31{
32 // push value
33 OP_0=0,
34 OP_FALSE=OP_0,
35 OP_PUSHDATA1=76,
36 OP_PUSHDATA2,
37 OP_PUSHDATA4,
38 OP_1NEGATE,
39 OP_RESERVED,
40 OP_1,
41 OP_TRUE=OP_1,
42 OP_2,
43 OP_3,
44 OP_4,
45 OP_5,
46 OP_6,
47 OP_7,
48 OP_8,
49 OP_9,
50 OP_10,
51 OP_11,
52 OP_12,
53 OP_13,
54 OP_14,
55 OP_15,
56 OP_16,
57
58 // control
59 OP_NOP,
60 OP_VER,
61 OP_IF,
62 OP_NOTIF,
63 OP_VERIF,
64 OP_VERNOTIF,
65 OP_ELSE,
66 OP_ENDIF,
67 OP_VERIFY,
68 OP_RETURN,
69
70 // stack ops
71 OP_TOALTSTACK,
72 OP_FROMALTSTACK,
73 OP_2DROP,
74 OP_2DUP,
75 OP_3DUP,
76 OP_2OVER,
77 OP_2ROT,
78 OP_2SWAP,
79 OP_IFDUP,
80 OP_DEPTH,
81 OP_DROP,
82 OP_DUP,
83 OP_NIP,
84 OP_OVER,
85 OP_PICK,
86 OP_ROLL,
87 OP_ROT,
88 OP_SWAP,
89 OP_TUCK,
90
91 // splice ops
92 OP_CAT,
93 OP_SUBSTR,
94 OP_LEFT,
95 OP_RIGHT,
96 OP_SIZE,
97
98 // bit logic
99 OP_INVERT,
100 OP_AND,
101 OP_OR,
102 OP_XOR,
103 OP_EQUAL,
104 OP_EQUALVERIFY,
105 OP_RESERVED1,
106 OP_RESERVED2,
107
108 // numeric
109 OP_1ADD,
110 OP_1SUB,
111 OP_2MUL,
112 OP_2DIV,
113 OP_NEGATE,
114 OP_ABS,
115 OP_NOT,
116 OP_0NOTEQUAL,
117
118 OP_ADD,
119 OP_SUB,
120 OP_MUL,
121 OP_DIV,
122 OP_MOD,
123 OP_LSHIFT,
124 OP_RSHIFT,
125
126 OP_BOOLAND,
127 OP_BOOLOR,
128 OP_NUMEQUAL,
129 OP_NUMEQUALVERIFY,
130 OP_NUMNOTEQUAL,
131 OP_LESSTHAN,
132 OP_GREATERTHAN,
133 OP_LESSTHANOREQUAL,
134 OP_GREATERTHANOREQUAL,
135 OP_MIN,
136 OP_MAX,
137
138 OP_WITHIN,
139
140 // crypto
141 OP_RIPEMD160,
142 OP_SHA1,
143 OP_SHA256,
144 OP_HASH160,
145 OP_HASH256,
146 OP_CODESEPARATOR,
147 OP_CHECKSIG,
148 OP_CHECKSIGVERIFY,
149 OP_CHECKMULTISIG,
150 OP_CHECKMULTISIGVERIFY,
151
152
153 // multi-byte opcodes
154 OP_SINGLEBYTE_END = 0xF0,
155 OP_DOUBLEBYTE_BEGIN = 0xF000,
156
157 // template matching params
158 OP_PUBKEY,
159 OP_PUBKEYHASH,
160
161
162
163 OP_INVALIDOPCODE = 0xFFFF,
164};
165
166
167
168
169
170
171
172
173inline const char* GetOpName(opcodetype opcode)
174{
175 switch (opcode)
176 {
177 // push value
178 case OP_0 : return "0";
179 case OP_PUSHDATA1 : return "OP_PUSHDATA1";
180 case OP_PUSHDATA2 : return "OP_PUSHDATA2";
181 case OP_PUSHDATA4 : return "OP_PUSHDATA4";
182 case OP_1NEGATE : return "-1";
183 case OP_RESERVED : return "OP_RESERVED";
184 case OP_1 : return "1";
185 case OP_2 : return "2";
186 case OP_3 : return "3";
187 case OP_4 : return "4";
188 case OP_5 : return "5";
189 case OP_6 : return "6";
190 case OP_7 : return "7";
191 case OP_8 : return "8";
192 case OP_9 : return "9";
193 case OP_10 : return "10";
194 case OP_11 : return "11";
195 case OP_12 : return "12";
196 case OP_13 : return "13";
197 case OP_14 : return "14";
198 case OP_15 : return "15";
199 case OP_16 : return "16";
200
201 // control
202 case OP_NOP : return "OP_NOP";
203 case OP_VER : return "OP_VER";
204 case OP_IF : return "OP_IF";
205 case OP_NOTIF : return "OP_NOTIF";
206 case OP_VERIF : return "OP_VERIF";
207 case OP_VERNOTIF : return "OP_VERNOTIF";
208 case OP_ELSE : return "OP_ELSE";
209 case OP_ENDIF : return "OP_ENDIF";
210 case OP_VERIFY : return "OP_VERIFY";
211 case OP_RETURN : return "OP_RETURN";
212
213 // stack ops
214 case OP_TOALTSTACK : return "OP_TOALTSTACK";
215 case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
216 case OP_2DROP : return "OP_2DROP";
217 case OP_2DUP : return "OP_2DUP";
218 case OP_3DUP : return "OP_3DUP";
219 case OP_2OVER : return "OP_2OVER";
220 case OP_2ROT : return "OP_2ROT";
221 case OP_2SWAP : return "OP_2SWAP";
222 case OP_IFDUP : return "OP_IFDUP";
223 case OP_DEPTH : return "OP_DEPTH";
224 case OP_DROP : return "OP_DROP";
225 case OP_DUP : return "OP_DUP";
226 case OP_NIP : return "OP_NIP";
227 case OP_OVER : return "OP_OVER";
228 case OP_PICK : return "OP_PICK";
229 case OP_ROLL : return "OP_ROLL";
230 case OP_ROT : return "OP_ROT";
231 case OP_SWAP : return "OP_SWAP";
232 case OP_TUCK : return "OP_TUCK";
233
234 // splice ops
235 case OP_CAT : return "OP_CAT";
236 case OP_SUBSTR : return "OP_SUBSTR";
237 case OP_LEFT : return "OP_LEFT";
238 case OP_RIGHT : return "OP_RIGHT";
239 case OP_SIZE : return "OP_SIZE";
240
241 // bit logic
242 case OP_INVERT : return "OP_INVERT";
243 case OP_AND : return "OP_AND";
244 case OP_OR : return "OP_OR";
245 case OP_XOR : return "OP_XOR";
246 case OP_EQUAL : return "OP_EQUAL";
247 case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
248 case OP_RESERVED1 : return "OP_RESERVED1";
249 case OP_RESERVED2 : return "OP_RESERVED2";
250
251 // numeric
252 case OP_1ADD : return "OP_1ADD";
253 case OP_1SUB : return "OP_1SUB";
254 case OP_2MUL : return "OP_2MUL";
255 case OP_2DIV : return "OP_2DIV";
256 case OP_NEGATE : return "OP_NEGATE";
257 case OP_ABS : return "OP_ABS";
258 case OP_NOT : return "OP_NOT";
259 case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
260 case OP_ADD : return "OP_ADD";
261 case OP_SUB : return "OP_SUB";
262 case OP_MUL : return "OP_MUL";
263 case OP_DIV : return "OP_DIV";
264 case OP_MOD : return "OP_MOD";
265 case OP_LSHIFT : return "OP_LSHIFT";
266 case OP_RSHIFT : return "OP_RSHIFT";
267 case OP_BOOLAND : return "OP_BOOLAND";
268 case OP_BOOLOR : return "OP_BOOLOR";
269 case OP_NUMEQUAL : return "OP_NUMEQUAL";
270 case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
271 case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
272 case OP_LESSTHAN : return "OP_LESSTHAN";
273 case OP_GREATERTHAN : return "OP_GREATERTHAN";
274 case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
275 case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
276 case OP_MIN : return "OP_MIN";
277 case OP_MAX : return "OP_MAX";
278 case OP_WITHIN : return "OP_WITHIN";
279
280 // crypto
281 case OP_RIPEMD160 : return "OP_RIPEMD160";
282 case OP_SHA1 : return "OP_SHA1";
283 case OP_SHA256 : return "OP_SHA256";
284 case OP_HASH160 : return "OP_HASH160";
285 case OP_HASH256 : return "OP_HASH256";
286 case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
287 case OP_CHECKSIG : return "OP_CHECKSIG";
288 case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
289 case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
290 case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
291
292
293
294 // multi-byte opcodes
295 case OP_SINGLEBYTE_END : return "OP_SINGLEBYTE_END";
296 case OP_DOUBLEBYTE_BEGIN : return "OP_DOUBLEBYTE_BEGIN";
297 case OP_PUBKEY : return "OP_PUBKEY";
298 case OP_PUBKEYHASH : return "OP_PUBKEYHASH";
299
300
301
302 case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
303 default:
304 return "UNKNOWN_OPCODE";
305 }
306};
307
308
309
310
311inline string ValueString(const vector<unsigned char>& vch)
312{
313 if (vch.size() <= 4)
314 return strprintf("%d", CBigNum(vch).getint());
315 else
316 return HexNumStr(vch.begin(), vch.end());
317 //return string("(") + HexStr(vch.begin(), vch.end()) + string(")");
318}
319
320inline string StackString(const vector<vector<unsigned char> >& vStack)
321{
322 string str;
323 foreach(const vector<unsigned char>& vch, vStack)
324 {
325 if (!str.empty())
326 str += " ";
327 str += ValueString(vch);
328 }
329 return str;
330}
331
332
333
334
335
336
337
338
339
340class CScript : public vector<unsigned char>
341{
342protected:
343 CScript& push_int64(int64 n)
344 {
345 if (n == -1 || (n >= 1 && n <= 16))
346 {
347 push_back(n + (OP_1 - 1));
348 }
349 else
350 {
351 CBigNum bn(n);
352 *this << bn.getvch();
353 }
354 return (*this);
355 }
356
357 CScript& push_uint64(uint64 n)
358 {
359 if (n == -1 || (n >= 1 && n <= 16))
360 {
361 push_back(n + (OP_1 - 1));
362 }
363 else
364 {
365 CBigNum bn(n);
366 *this << bn.getvch();
367 }
368 return (*this);
369 }
370
371public:
372 CScript() { }
373 CScript(const CScript& b) : vector<unsigned char>(b.begin(), b.end()) { }
374 CScript(const_iterator pbegin, const_iterator pend) : vector<unsigned char>(pbegin, pend) { }
375#ifndef _MSC_VER
376 CScript(const unsigned char* pbegin, const unsigned char* pend) : vector<unsigned char>(pbegin, pend) { }
377#endif
378
379 CScript& operator+=(const CScript& b)
380 {
381 insert(end(), b.begin(), b.end());
382 return *this;
383 }
384
385 friend CScript operator+(const CScript& a, const CScript& b)
386 {
387 CScript ret = a;
388 ret += b;
389 return (ret);
390 }
391
392
393 explicit CScript(char b) { operator<<(b); }
394 explicit CScript(short b) { operator<<(b); }
395 explicit CScript(int b) { operator<<(b); }
396 explicit CScript(long b) { operator<<(b); }
397 explicit CScript(int64 b) { operator<<(b); }
398 explicit CScript(unsigned char b) { operator<<(b); }
399 explicit CScript(unsigned int b) { operator<<(b); }
400 explicit CScript(unsigned short b) { operator<<(b); }
401 explicit CScript(unsigned long b) { operator<<(b); }
402 explicit CScript(uint64 b) { operator<<(b); }
403
404 explicit CScript(opcodetype b) { operator<<(b); }
405 explicit CScript(const uint256& b) { operator<<(b); }
406 explicit CScript(const CBigNum& b) { operator<<(b); }
407 explicit CScript(const vector<unsigned char>& b) { operator<<(b); }
408
409
410 CScript& operator<<(char b) { return (push_int64(b)); }
411 CScript& operator<<(short b) { return (push_int64(b)); }
412 CScript& operator<<(int b) { return (push_int64(b)); }
413 CScript& operator<<(long b) { return (push_int64(b)); }
414 CScript& operator<<(int64 b) { return (push_int64(b)); }
415 CScript& operator<<(unsigned char b) { return (push_uint64(b)); }
416 CScript& operator<<(unsigned int b) { return (push_uint64(b)); }
417 CScript& operator<<(unsigned short b) { return (push_uint64(b)); }
418 CScript& operator<<(unsigned long b) { return (push_uint64(b)); }
419 CScript& operator<<(uint64 b) { return (push_uint64(b)); }
420
421 CScript& operator<<(opcodetype opcode)
422 {
423 if (opcode <= OP_SINGLEBYTE_END)
424 {
425 insert(end(), (unsigned char)opcode);
426 }
427 else
428 {
429 assert(opcode >= OP_DOUBLEBYTE_BEGIN);
430 insert(end(), (unsigned char)(opcode >> 8));
431 insert(end(), (unsigned char)(opcode & 0xFF));
432 }
433 return (*this);
434 }
435
436 CScript& operator<<(const uint160& b)
437 {
438 insert(end(), sizeof(b));
439 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
440 return (*this);
441 }
442
443 CScript& operator<<(const uint256& b)
444 {
445 insert(end(), sizeof(b));
446 insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
447 return (*this);
448 }
449
450 CScript& operator<<(const CBigNum& b)
451 {
452 *this << b.getvch();
453 return (*this);
454 }
455
456 CScript& operator<<(const vector<unsigned char>& b)
457 {
458 if (b.size() < OP_PUSHDATA1)
459 {
460 insert(end(), (unsigned char)b.size());
461 }
462 else if (b.size() <= 0xff)
463 {
464 insert(end(), OP_PUSHDATA1);
465 insert(end(), (unsigned char)b.size());
466 }
467 else
468 {
469 insert(end(), OP_PUSHDATA2);
470 unsigned short nSize = b.size();
471 insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
472 }
473 insert(end(), b.begin(), b.end());
474 return (*this);
475 }
476
477 CScript& operator<<(const CScript& b)
478 {
479 // I'm not sure if this should push the script or concatenate scripts.
480 // If there's ever a use for pushing a script onto a script, delete this member fn
481 assert(("warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate", false));
482 return (*this);
483 }
484
485
486 bool GetOp(iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet)
487 {
488 // This is why people hate C++
489 const_iterator pc2 = pc;
490 bool fRet = GetOp(pc2, opcodeRet, vchRet);
491 pc = begin() + (pc2 - begin());
492 return fRet;
493 }
494
495 bool GetOp(const_iterator& pc, opcodetype& opcodeRet, vector<unsigned char>& vchRet) const
496 {
497 opcodeRet = OP_INVALIDOPCODE;
498 vchRet.clear();
499 if (pc >= end())
500 return false;
501
502 // Read instruction
503 unsigned int opcode = *pc++;
504 if (opcode >= OP_SINGLEBYTE_END)
505 {
506 if (pc + 1 > end())
507 return false;
508 opcode <<= 8;
509 opcode |= *pc++;
510 }
511
512 // Immediate operand
513 if (opcode <= OP_PUSHDATA4)
514 {
515 unsigned int nSize = opcode;
516 if (opcode == OP_PUSHDATA1)
517 {
518 if (pc + 1 > end())
519 return false;
520 nSize = *pc++;
521 }
522 else if (opcode == OP_PUSHDATA2)
523 {
524 if (pc + 2 > end())
525 return false;
526 nSize = 0;
527 memcpy(&nSize, &pc[0], 2);
528 pc += 2;
529 }
530 else if (opcode == OP_PUSHDATA4)
531 {
532 if (pc + 4 > end())
533 return false;
534 memcpy(&nSize, &pc[0], 4);
535 pc += 4;
536 }
537 if (pc + nSize > end())
538 return false;
539 vchRet.assign(pc, pc + nSize);
540 pc += nSize;
541 }
542
543 opcodeRet = (opcodetype)opcode;
544 return true;
545 }
546
547
548 // Signature operations in this script. Each one costs every validating
549 // node an elliptic-curve verification, so blocks are capped on this count
550 // and not only on their size -- a byte of OP_CHECKSIG is far more
551 // expensive to check than a byte of anything else.
552 unsigned int GetSigOpCount() const
553 {
554 unsigned int n = 0;
555 const_iterator pc = begin();
556 opcodetype opcode;
557 vector<unsigned char> vchPushValue;
558 while (pc < end())
559 {
560 if (!GetOp(pc, opcode, vchPushValue))
561 break;
562 if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
563 n++;
564 else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
565 n += 20;
566 }
567 return n;
568 }
569
570
571 void FindAndDelete(const CScript& b)
572 {
573 iterator pc = begin();
574 opcodetype opcode;
575 vector<unsigned char> vchPushValue;
576 int count = 0;
577 do
578 {
579 while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
580 {
581 erase(pc, pc + b.size());
582 count++;
583 }
584 }
585 while (GetOp(pc, opcode, vchPushValue));
586 //printf("FindAndDeleted deleted %d items\n", count); /// debug
587 }
588
589
590 void PrintHex() const
591 {
592 printf("CScript(%s)\n", HexStr(begin(), end()).c_str());
593 }
594
595 string ToString() const
596 {
597 string str;
598 opcodetype opcode;
599 vector<unsigned char> vch;
600 const_iterator it = begin();
601 while (GetOp(it, opcode, vch))
602 {
603 if (!str.empty())
604 str += " ";
605 if (opcode <= OP_PUSHDATA4)
606 str += ValueString(vch);
607 else
608 str += GetOpName(opcode);
609 }
610 return str;
611 }
612
613 void print() const
614 {
615 printf("%s\n", ToString().c_str());
616 }
617};
618
619
620
621
622
623
624
625
626bool EvalScript(const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType=0,
627 vector<vector<unsigned char> >* pvStackRet=NULL);
628uint256 SignatureHash(CScript scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
629bool IsMine(const CScript& scriptPubKey);
630bool ExtractPubKey(const CScript& scriptPubKey, bool fMineOnly, vector<unsigned char>& vchPubKeyRet);
631bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret);
632bool SignSignature(const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
633bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);