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