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