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