Add prototype for EvalScript() to script.h
[novacoin.git] / src / script.h
1 // Copyright (c) 2009-2010 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 #ifndef H_BITCOIN_SCRIPT
5 #define H_BITCOIN_SCRIPT
6
7 #include "base58.h"
8 #include "keystore.h"
9
10 #include <string>
11 #include <vector>
12
13 #include <boost/foreach.hpp>
14
15 class CTransaction;
16
17 enum
18 {
19     SIGHASH_ALL = 1,
20     SIGHASH_NONE = 2,
21     SIGHASH_SINGLE = 3,
22     SIGHASH_ANYONECANPAY = 0x80,
23 };
24
25
26
27 enum opcodetype
28 {
29     // push value
30     OP_0=0,
31     OP_FALSE=OP_0,
32     OP_PUSHDATA1=76,
33     OP_PUSHDATA2,
34     OP_PUSHDATA4,
35     OP_1NEGATE,
36     OP_RESERVED,
37     OP_1,
38     OP_TRUE=OP_1,
39     OP_2,
40     OP_3,
41     OP_4,
42     OP_5,
43     OP_6,
44     OP_7,
45     OP_8,
46     OP_9,
47     OP_10,
48     OP_11,
49     OP_12,
50     OP_13,
51     OP_14,
52     OP_15,
53     OP_16,
54
55     // control
56     OP_NOP,
57     OP_VER,
58     OP_IF,
59     OP_NOTIF,
60     OP_VERIF,
61     OP_VERNOTIF,
62     OP_ELSE,
63     OP_ENDIF,
64     OP_VERIFY,
65     OP_RETURN,
66
67     // stack ops
68     OP_TOALTSTACK,
69     OP_FROMALTSTACK,
70     OP_2DROP,
71     OP_2DUP,
72     OP_3DUP,
73     OP_2OVER,
74     OP_2ROT,
75     OP_2SWAP,
76     OP_IFDUP,
77     OP_DEPTH,
78     OP_DROP,
79     OP_DUP,
80     OP_NIP,
81     OP_OVER,
82     OP_PICK,
83     OP_ROLL,
84     OP_ROT,
85     OP_SWAP,
86     OP_TUCK,
87
88     // splice ops
89     OP_CAT,
90     OP_SUBSTR,
91     OP_LEFT,
92     OP_RIGHT,
93     OP_SIZE,
94
95     // bit logic
96     OP_INVERT,
97     OP_AND,
98     OP_OR,
99     OP_XOR,
100     OP_EQUAL,
101     OP_EQUALVERIFY,
102     OP_RESERVED1,
103     OP_RESERVED2,
104
105     // numeric
106     OP_1ADD,
107     OP_1SUB,
108     OP_2MUL,
109     OP_2DIV,
110     OP_NEGATE,
111     OP_ABS,
112     OP_NOT,
113     OP_0NOTEQUAL,
114
115     OP_ADD,
116     OP_SUB,
117     OP_MUL,
118     OP_DIV,
119     OP_MOD,
120     OP_LSHIFT,
121     OP_RSHIFT,
122
123     OP_BOOLAND,
124     OP_BOOLOR,
125     OP_NUMEQUAL,
126     OP_NUMEQUALVERIFY,
127     OP_NUMNOTEQUAL,
128     OP_LESSTHAN,
129     OP_GREATERTHAN,
130     OP_LESSTHANOREQUAL,
131     OP_GREATERTHANOREQUAL,
132     OP_MIN,
133     OP_MAX,
134
135     OP_WITHIN,
136
137     // crypto
138     OP_RIPEMD160,
139     OP_SHA1,
140     OP_SHA256,
141     OP_HASH160,
142     OP_HASH256,
143     OP_CODESEPARATOR,
144     OP_CHECKSIG,
145     OP_CHECKSIGVERIFY,
146     OP_CHECKMULTISIG,
147     OP_CHECKMULTISIGVERIFY,
148
149     // expansion
150     OP_NOP1,
151     OP_NOP2,
152     OP_NOP3,
153     OP_NOP4,
154     OP_NOP5,
155     OP_NOP6,
156     OP_NOP7,
157     OP_NOP8,
158     OP_NOP9,
159     OP_NOP10,
160
161
162
163     // template matching params
164     OP_PUBKEYHASH = 0xfd,
165     OP_PUBKEY = 0xfe,
166
167     OP_INVALIDOPCODE = 0xff,
168 };
169
170
171
172
173
174
175
176
177 inline const char* GetOpName(opcodetype opcode)
178 {
179     switch (opcode)
180     {
181     // push value
182     case OP_0                      : return "0";
183     case OP_PUSHDATA1              : return "OP_PUSHDATA1";
184     case OP_PUSHDATA2              : return "OP_PUSHDATA2";
185     case OP_PUSHDATA4              : return "OP_PUSHDATA4";
186     case OP_1NEGATE                : return "-1";
187     case OP_RESERVED               : return "OP_RESERVED";
188     case OP_1                      : return "1";
189     case OP_2                      : return "2";
190     case OP_3                      : return "3";
191     case OP_4                      : return "4";
192     case OP_5                      : return "5";
193     case OP_6                      : return "6";
194     case OP_7                      : return "7";
195     case OP_8                      : return "8";
196     case OP_9                      : return "9";
197     case OP_10                     : return "10";
198     case OP_11                     : return "11";
199     case OP_12                     : return "12";
200     case OP_13                     : return "13";
201     case OP_14                     : return "14";
202     case OP_15                     : return "15";
203     case OP_16                     : return "16";
204
205     // control
206     case OP_NOP                    : return "OP_NOP";
207     case OP_VER                    : return "OP_VER";
208     case OP_IF                     : return "OP_IF";
209     case OP_NOTIF                  : return "OP_NOTIF";
210     case OP_VERIF                  : return "OP_VERIF";
211     case OP_VERNOTIF               : return "OP_VERNOTIF";
212     case OP_ELSE                   : return "OP_ELSE";
213     case OP_ENDIF                  : return "OP_ENDIF";
214     case OP_VERIFY                 : return "OP_VERIFY";
215     case OP_RETURN                 : return "OP_RETURN";
216
217     // stack ops
218     case OP_TOALTSTACK             : return "OP_TOALTSTACK";
219     case OP_FROMALTSTACK           : return "OP_FROMALTSTACK";
220     case OP_2DROP                  : return "OP_2DROP";
221     case OP_2DUP                   : return "OP_2DUP";
222     case OP_3DUP                   : return "OP_3DUP";
223     case OP_2OVER                  : return "OP_2OVER";
224     case OP_2ROT                   : return "OP_2ROT";
225     case OP_2SWAP                  : return "OP_2SWAP";
226     case OP_IFDUP                  : return "OP_IFDUP";
227     case OP_DEPTH                  : return "OP_DEPTH";
228     case OP_DROP                   : return "OP_DROP";
229     case OP_DUP                    : return "OP_DUP";
230     case OP_NIP                    : return "OP_NIP";
231     case OP_OVER                   : return "OP_OVER";
232     case OP_PICK                   : return "OP_PICK";
233     case OP_ROLL                   : return "OP_ROLL";
234     case OP_ROT                    : return "OP_ROT";
235     case OP_SWAP                   : return "OP_SWAP";
236     case OP_TUCK                   : return "OP_TUCK";
237
238     // splice ops
239     case OP_CAT                    : return "OP_CAT";
240     case OP_SUBSTR                 : return "OP_SUBSTR";
241     case OP_LEFT                   : return "OP_LEFT";
242     case OP_RIGHT                  : return "OP_RIGHT";
243     case OP_SIZE                   : return "OP_SIZE";
244
245     // bit logic
246     case OP_INVERT                 : return "OP_INVERT";
247     case OP_AND                    : return "OP_AND";
248     case OP_OR                     : return "OP_OR";
249     case OP_XOR                    : return "OP_XOR";
250     case OP_EQUAL                  : return "OP_EQUAL";
251     case OP_EQUALVERIFY            : return "OP_EQUALVERIFY";
252     case OP_RESERVED1              : return "OP_RESERVED1";
253     case OP_RESERVED2              : return "OP_RESERVED2";
254
255     // numeric
256     case OP_1ADD                   : return "OP_1ADD";
257     case OP_1SUB                   : return "OP_1SUB";
258     case OP_2MUL                   : return "OP_2MUL";
259     case OP_2DIV                   : return "OP_2DIV";
260     case OP_NEGATE                 : return "OP_NEGATE";
261     case OP_ABS                    : return "OP_ABS";
262     case OP_NOT                    : return "OP_NOT";
263     case OP_0NOTEQUAL              : return "OP_0NOTEQUAL";
264     case OP_ADD                    : return "OP_ADD";
265     case OP_SUB                    : return "OP_SUB";
266     case OP_MUL                    : return "OP_MUL";
267     case OP_DIV                    : return "OP_DIV";
268     case OP_MOD                    : return "OP_MOD";
269     case OP_LSHIFT                 : return "OP_LSHIFT";
270     case OP_RSHIFT                 : return "OP_RSHIFT";
271     case OP_BOOLAND                : return "OP_BOOLAND";
272     case OP_BOOLOR                 : return "OP_BOOLOR";
273     case OP_NUMEQUAL               : return "OP_NUMEQUAL";
274     case OP_NUMEQUALVERIFY         : return "OP_NUMEQUALVERIFY";
275     case OP_NUMNOTEQUAL            : return "OP_NUMNOTEQUAL";
276     case OP_LESSTHAN               : return "OP_LESSTHAN";
277     case OP_GREATERTHAN            : return "OP_GREATERTHAN";
278     case OP_LESSTHANOREQUAL        : return "OP_LESSTHANOREQUAL";
279     case OP_GREATERTHANOREQUAL     : return "OP_GREATERTHANOREQUAL";
280     case OP_MIN                    : return "OP_MIN";
281     case OP_MAX                    : return "OP_MAX";
282     case OP_WITHIN                 : return "OP_WITHIN";
283
284     // crypto
285     case OP_RIPEMD160              : return "OP_RIPEMD160";
286     case OP_SHA1                   : return "OP_SHA1";
287     case OP_SHA256                 : return "OP_SHA256";
288     case OP_HASH160                : return "OP_HASH160";
289     case OP_HASH256                : return "OP_HASH256";
290     case OP_CODESEPARATOR          : return "OP_CODESEPARATOR";
291     case OP_CHECKSIG               : return "OP_CHECKSIG";
292     case OP_CHECKSIGVERIFY         : return "OP_CHECKSIGVERIFY";
293     case OP_CHECKMULTISIG          : return "OP_CHECKMULTISIG";
294     case OP_CHECKMULTISIGVERIFY    : return "OP_CHECKMULTISIGVERIFY";
295
296     // expanson
297     case OP_NOP1                   : return "OP_NOP1";
298     case OP_NOP2                   : return "OP_NOP2";
299     case OP_NOP3                   : return "OP_NOP3";
300     case OP_NOP4                   : return "OP_NOP4";
301     case OP_NOP5                   : return "OP_NOP5";
302     case OP_NOP6                   : return "OP_NOP6";
303     case OP_NOP7                   : return "OP_NOP7";
304     case OP_NOP8                   : return "OP_NOP8";
305     case OP_NOP9                   : return "OP_NOP9";
306     case OP_NOP10                  : return "OP_NOP10";
307
308
309
310     // template matching params
311     case OP_PUBKEYHASH             : return "OP_PUBKEYHASH";
312     case OP_PUBKEY                 : return "OP_PUBKEY";
313
314     case OP_INVALIDOPCODE          : return "OP_INVALIDOPCODE";
315     default:
316         return "OP_UNKNOWN";
317     }
318 };
319
320
321
322
323 inline std::string ValueString(const std::vector<unsigned char>& vch)
324 {
325     if (vch.size() <= 4)
326         return strprintf("%d", CBigNum(vch).getint());
327     else
328         return HexStr(vch);
329 }
330
331 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
332 {
333     std::string str;
334     BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
335     {
336         if (!str.empty())
337             str += " ";
338         str += ValueString(vch);
339     }
340     return str;
341 }
342
343
344
345
346
347
348
349
350
351 class CScript : public std::vector<unsigned char>
352 {
353 protected:
354     CScript& push_int64(int64 n)
355     {
356         if (n == -1 || (n >= 1 && n <= 16))
357         {
358             push_back(n + (OP_1 - 1));
359         }
360         else
361         {
362             CBigNum bn(n);
363             *this << bn.getvch();
364         }
365         return *this;
366     }
367
368     CScript& push_uint64(uint64 n)
369     {
370         if (n >= 1 && n <= 16)
371         {
372             push_back(n + (OP_1 - 1));
373         }
374         else
375         {
376             CBigNum bn(n);
377             *this << bn.getvch();
378         }
379         return *this;
380     }
381
382 public:
383     CScript() { }
384     CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
385     CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
386 #ifndef _MSC_VER
387     CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
388 #endif
389
390     CScript& operator+=(const CScript& b)
391     {
392         insert(end(), b.begin(), b.end());
393         return *this;
394     }
395
396     friend CScript operator+(const CScript& a, const CScript& b)
397     {
398         CScript ret = a;
399         ret += b;
400         return ret;
401     }
402
403
404     explicit CScript(char b)           { operator<<(b); }
405     explicit CScript(short b)          { operator<<(b); }
406     explicit CScript(int b)            { operator<<(b); }
407     explicit CScript(long b)           { operator<<(b); }
408     explicit CScript(int64 b)          { operator<<(b); }
409     explicit CScript(unsigned char b)  { operator<<(b); }
410     explicit CScript(unsigned int b)   { operator<<(b); }
411     explicit CScript(unsigned short b) { operator<<(b); }
412     explicit CScript(unsigned long b)  { operator<<(b); }
413     explicit CScript(uint64 b)         { operator<<(b); }
414
415     explicit CScript(opcodetype b)     { operator<<(b); }
416     explicit CScript(const uint256& b) { operator<<(b); }
417     explicit CScript(const CBigNum& b) { operator<<(b); }
418     explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
419
420
421     CScript& operator<<(char b)           { return push_int64(b); }
422     CScript& operator<<(short b)          { return push_int64(b); }
423     CScript& operator<<(int b)            { return push_int64(b); }
424     CScript& operator<<(long b)           { return push_int64(b); }
425     CScript& operator<<(int64 b)          { return push_int64(b); }
426     CScript& operator<<(unsigned char b)  { return push_uint64(b); }
427     CScript& operator<<(unsigned int b)   { return push_uint64(b); }
428     CScript& operator<<(unsigned short b) { return push_uint64(b); }
429     CScript& operator<<(unsigned long b)  { return push_uint64(b); }
430     CScript& operator<<(uint64 b)         { return push_uint64(b); }
431
432     CScript& operator<<(opcodetype opcode)
433     {
434         if (opcode < 0 || opcode > 0xff)
435             throw std::runtime_error("CScript::operator<<() : invalid opcode");
436         insert(end(), (unsigned char)opcode);
437         return *this;
438     }
439
440     CScript& operator<<(const uint160& b)
441     {
442         insert(end(), sizeof(b));
443         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
444         return *this;
445     }
446
447     CScript& operator<<(const uint256& b)
448     {
449         insert(end(), sizeof(b));
450         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
451         return *this;
452     }
453
454     CScript& operator<<(const CBigNum& b)
455     {
456         *this << b.getvch();
457         return *this;
458     }
459
460     CScript& operator<<(const std::vector<unsigned char>& b)
461     {
462         if (b.size() < OP_PUSHDATA1)
463         {
464             insert(end(), (unsigned char)b.size());
465         }
466         else if (b.size() <= 0xff)
467         {
468             insert(end(), OP_PUSHDATA1);
469             insert(end(), (unsigned char)b.size());
470         }
471         else if (b.size() <= 0xffff)
472         {
473             insert(end(), OP_PUSHDATA2);
474             unsigned short nSize = b.size();
475             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
476         }
477         else
478         {
479             insert(end(), OP_PUSHDATA4);
480             unsigned int nSize = b.size();
481             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
482         }
483         insert(end(), b.begin(), b.end());
484         return *this;
485     }
486
487     CScript& operator<<(const CScript& b)
488     {
489         // I'm not sure if this should push the script or concatenate scripts.
490         // If there's ever a use for pushing a script onto a script, delete this member fn
491         assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
492         return *this;
493     }
494
495
496     bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
497     {
498          // Wrapper so it can be called with either iterator or const_iterator
499          const_iterator pc2 = pc;
500          bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
501          pc = begin() + (pc2 - begin());
502          return fRet;
503     }
504
505     bool GetOp(iterator& pc, opcodetype& opcodeRet)
506     {
507          const_iterator pc2 = pc;
508          bool fRet = GetOp2(pc2, opcodeRet, NULL);
509          pc = begin() + (pc2 - begin());
510          return fRet;
511     }
512
513     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
514     {
515         return GetOp2(pc, opcodeRet, &vchRet);
516     }
517
518     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
519     {
520         return GetOp2(pc, opcodeRet, NULL);
521     }
522
523     bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
524     {
525         opcodeRet = OP_INVALIDOPCODE;
526         if (pvchRet)
527             pvchRet->clear();
528         if (pc >= end())
529             return false;
530
531         // Read instruction
532         if (end() - pc < 1)
533             return false;
534         unsigned int opcode = *pc++;
535
536         // Immediate operand
537         if (opcode <= OP_PUSHDATA4)
538         {
539             unsigned int nSize;
540             if (opcode < OP_PUSHDATA1)
541             {
542                 nSize = opcode;
543             }
544             else if (opcode == OP_PUSHDATA1)
545             {
546                 if (end() - pc < 1)
547                     return false;
548                 nSize = *pc++;
549             }
550             else if (opcode == OP_PUSHDATA2)
551             {
552                 if (end() - pc < 2)
553                     return false;
554                 nSize = 0;
555                 memcpy(&nSize, &pc[0], 2);
556                 pc += 2;
557             }
558             else if (opcode == OP_PUSHDATA4)
559             {
560                 if (end() - pc < 4)
561                     return false;
562                 memcpy(&nSize, &pc[0], 4);
563                 pc += 4;
564             }
565             if (end() - pc < nSize)
566                 return false;
567             if (pvchRet)
568                 pvchRet->assign(pc, pc + nSize);
569             pc += nSize;
570         }
571
572         opcodeRet = (opcodetype)opcode;
573         return true;
574     }
575
576
577     void FindAndDelete(const CScript& b)
578     {
579         if (b.empty())
580             return;
581         iterator pc = begin();
582         opcodetype opcode;
583         do
584         {
585             while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
586                 erase(pc, pc + b.size());
587         }
588         while (GetOp(pc, opcode));
589     }
590
591
592     int GetSigOpCount() const
593     {
594         int n = 0;
595         const_iterator pc = begin();
596         while (pc < end())
597         {
598             opcodetype opcode;
599             if (!GetOp(pc, opcode))
600                 break;
601             if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
602                 n++;
603             else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
604                 n += 20;
605         }
606         return n;
607     }
608
609
610     bool IsPushOnly() const
611     {
612         if (size() > 200)
613             return false;
614         const_iterator pc = begin();
615         while (pc < end())
616         {
617             opcodetype opcode;
618             if (!GetOp(pc, opcode))
619                 return false;
620             if (opcode > OP_16)
621                 return false;
622         }
623         return true;
624     }
625
626
627     CBitcoinAddress GetBitcoinAddress() const
628     {
629         opcodetype opcode;
630         std::vector<unsigned char> vch;
631         CScript::const_iterator pc = begin();
632         if (!GetOp(pc, opcode, vch) || opcode != OP_DUP) return 0;
633         if (!GetOp(pc, opcode, vch) || opcode != OP_HASH160) return 0;
634         if (!GetOp(pc, opcode, vch) || vch.size() != sizeof(uint160)) return 0;
635         uint160 hash160 = uint160(vch);
636         if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
637         if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
638         if (pc != end()) return 0;
639         return CBitcoinAddress(hash160);
640     }
641
642     void SetBitcoinAddress(const CBitcoinAddress& address)
643     {
644         this->clear();
645         *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG;
646     }
647
648     void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
649     {
650         SetBitcoinAddress(CBitcoinAddress(vchPubKey));
651     }
652
653
654     void PrintHex() const
655     {
656         printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
657     }
658
659     std::string ToString() const
660     {
661         std::string str;
662         opcodetype opcode;
663         std::vector<unsigned char> vch;
664         const_iterator pc = begin();
665         while (pc < end())
666         {
667             if (!str.empty())
668                 str += " ";
669             if (!GetOp(pc, opcode, vch))
670             {
671                 str += "[error]";
672                 return str;
673             }
674             if (0 <= opcode && opcode <= OP_PUSHDATA4)
675                 str += ValueString(vch);
676             else
677                 str += GetOpName(opcode);
678         }
679         return str;
680     }
681
682     void print() const
683     {
684         printf("%s\n", ToString().c_str());
685     }
686 };
687
688
689
690
691
692
693
694 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
695
696 bool IsStandard(const CScript& scriptPubKey);
697 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
698 bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
699 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
700 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);
701
702 #endif