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