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