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