e41e09b6b30e404873cf3ebb286d1e4d7a49190a
[novacoin.git] / src / script.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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 /** Signature hash types/flags */
19 enum
20 {
21     SIGHASH_ALL = 1,
22     SIGHASH_NONE = 2,
23     SIGHASH_SINGLE = 3,
24     SIGHASH_ANYONECANPAY = 0x80,
25 };
26
27
28 enum txnouttype
29 {
30     TX_NONSTANDARD,
31     // 'standard' transaction types:
32     TX_PUBKEY,
33     TX_PUBKEYHASH,
34     TX_SCRIPTHASH,
35     TX_MULTISIG,
36 };
37
38 const char* GetTxnOutputType(txnouttype t);
39
40 /** Script opcodes */
41 enum opcodetype
42 {
43     // push value
44     OP_0 = 0x00,
45     OP_FALSE = OP_0,
46     OP_PUSHDATA1 = 0x4c,
47     OP_PUSHDATA2 = 0x4d,
48     OP_PUSHDATA4 = 0x4e,
49     OP_1NEGATE = 0x4f,
50     OP_RESERVED = 0x50,
51     OP_1 = 0x51,
52     OP_TRUE=OP_1,
53     OP_2 = 0x52,
54     OP_3 = 0x53,
55     OP_4 = 0x54,
56     OP_5 = 0x55,
57     OP_6 = 0x56,
58     OP_7 = 0x57,
59     OP_8 = 0x58,
60     OP_9 = 0x59,
61     OP_10 = 0x5a,
62     OP_11 = 0x5b,
63     OP_12 = 0x5c,
64     OP_13 = 0x5d,
65     OP_14 = 0x5e,
66     OP_15 = 0x5f,
67     OP_16 = 0x60,
68
69     // control
70     OP_NOP = 0x61,
71     OP_VER = 0x62,
72     OP_IF = 0x63,
73     OP_NOTIF = 0x64,
74     OP_VERIF = 0x65,
75     OP_VERNOTIF = 0x66,
76     OP_ELSE = 0x67,
77     OP_ENDIF = 0x68,
78     OP_VERIFY = 0x69,
79     OP_RETURN = 0x6a,
80
81     // stack ops
82     OP_TOALTSTACK = 0x6b,
83     OP_FROMALTSTACK = 0x6c,
84     OP_2DROP = 0x6d,
85     OP_2DUP = 0x6e,
86     OP_3DUP = 0x6f,
87     OP_2OVER = 0x70,
88     OP_2ROT = 0x71,
89     OP_2SWAP = 0x72,
90     OP_IFDUP = 0x73,
91     OP_DEPTH = 0x74,
92     OP_DROP = 0x75,
93     OP_DUP = 0x76,
94     OP_NIP = 0x77,
95     OP_OVER = 0x78,
96     OP_PICK = 0x79,
97     OP_ROLL = 0x7a,
98     OP_ROT = 0x7b,
99     OP_SWAP = 0x7c,
100     OP_TUCK = 0x7d,
101
102     // splice ops
103     OP_CAT = 0x7e,
104     OP_SUBSTR = 0x7f,
105     OP_LEFT = 0x80,
106     OP_RIGHT = 0x81,
107     OP_SIZE = 0x82,
108
109     // bit logic
110     OP_INVERT = 0x83,
111     OP_AND = 0x84,
112     OP_OR = 0x85,
113     OP_XOR = 0x86,
114     OP_EQUAL = 0x87,
115     OP_EQUALVERIFY = 0x88,
116     OP_RESERVED1 = 0x89,
117     OP_RESERVED2 = 0x8a,
118
119     // numeric
120     OP_1ADD = 0x8b,
121     OP_1SUB = 0x8c,
122     OP_2MUL = 0x8d,
123     OP_2DIV = 0x8e,
124     OP_NEGATE = 0x8f,
125     OP_ABS = 0x90,
126     OP_NOT = 0x91,
127     OP_0NOTEQUAL = 0x92,
128
129     OP_ADD = 0x93,
130     OP_SUB = 0x94,
131     OP_MUL = 0x95,
132     OP_DIV = 0x96,
133     OP_MOD = 0x97,
134     OP_LSHIFT = 0x98,
135     OP_RSHIFT = 0x99,
136
137     OP_BOOLAND = 0x9a,
138     OP_BOOLOR = 0x9b,
139     OP_NUMEQUAL = 0x9c,
140     OP_NUMEQUALVERIFY = 0x9d,
141     OP_NUMNOTEQUAL = 0x9e,
142     OP_LESSTHAN = 0x9f,
143     OP_GREATERTHAN = 0xa0,
144     OP_LESSTHANOREQUAL = 0xa1,
145     OP_GREATERTHANOREQUAL = 0xa2,
146     OP_MIN = 0xa3,
147     OP_MAX = 0xa4,
148
149     OP_WITHIN = 0xa5,
150
151     // crypto
152     OP_RIPEMD160 = 0xa6,
153     OP_SHA1 = 0xa7,
154     OP_SHA256 = 0xa8,
155     OP_HASH160 = 0xa9,
156     OP_HASH256 = 0xaa,
157     OP_CODESEPARATOR = 0xab,
158     OP_CHECKSIG = 0xac,
159     OP_CHECKSIGVERIFY = 0xad,
160     OP_CHECKMULTISIG = 0xae,
161     OP_CHECKMULTISIGVERIFY = 0xaf,
162
163     // expansion
164     OP_NOP1 = 0xb0,
165     OP_NOP2 = 0xb1,
166     OP_NOP3 = 0xb2,
167     OP_NOP4 = 0xb3,
168     OP_NOP5 = 0xb4,
169     OP_NOP6 = 0xb5,
170     OP_NOP7 = 0xb6,
171     OP_NOP8 = 0xb7,
172     OP_NOP9 = 0xb8,
173     OP_NOP10 = 0xb9,
174
175
176
177     // template matching params
178     OP_SMALLINTEGER = 0xfa,
179     OP_PUBKEYS = 0xfb,
180     OP_PUBKEYHASH = 0xfd,
181     OP_PUBKEY = 0xfe,
182
183     OP_INVALIDOPCODE = 0xff,
184 };
185
186 const char* GetOpName(opcodetype opcode);
187
188
189
190 inline std::string ValueString(const std::vector<unsigned char>& vch)
191 {
192     if (vch.size() <= 4)
193         return strprintf("%d", CBigNum(vch).getint());
194     else
195         return HexStr(vch);
196 }
197
198 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
199 {
200     std::string str;
201     BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
202     {
203         if (!str.empty())
204             str += " ";
205         str += ValueString(vch);
206     }
207     return str;
208 }
209
210
211
212
213
214
215
216
217 /** Serialized script, used inside transaction inputs and outputs */
218 class CScript : public std::vector<unsigned char>
219 {
220 protected:
221     CScript& push_int64(int64 n)
222     {
223         if (n == -1 || (n >= 1 && n <= 16))
224         {
225             push_back(n + (OP_1 - 1));
226         }
227         else
228         {
229             CBigNum bn(n);
230             *this << bn.getvch();
231         }
232         return *this;
233     }
234
235     CScript& push_uint64(uint64 n)
236     {
237         if (n >= 1 && n <= 16)
238         {
239             push_back(n + (OP_1 - 1));
240         }
241         else
242         {
243             CBigNum bn(n);
244             *this << bn.getvch();
245         }
246         return *this;
247     }
248
249 public:
250     CScript() { }
251     CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
252     CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
253 #ifndef _MSC_VER
254     CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
255 #endif
256
257     CScript& operator+=(const CScript& b)
258     {
259         insert(end(), b.begin(), b.end());
260         return *this;
261     }
262
263     friend CScript operator+(const CScript& a, const CScript& b)
264     {
265         CScript ret = a;
266         ret += b;
267         return ret;
268     }
269
270
271     //explicit CScript(char b) is not portable.  Use 'signed char' or 'unsigned char'.
272     explicit CScript(signed 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) is not portable.  Use 'signed char' or 'unsigned char'.
290     CScript& operator<<(signed char b)    { return push_int64(b); }
291     CScript& operator<<(short b)          { return push_int64(b); }
292     CScript& operator<<(int b)            { return push_int64(b); }
293     CScript& operator<<(long b)           { return push_int64(b); }
294     CScript& operator<<(int64 b)          { return push_int64(b); }
295     CScript& operator<<(unsigned char b)  { return push_uint64(b); }
296     CScript& operator<<(unsigned int b)   { return push_uint64(b); }
297     CScript& operator<<(unsigned short b) { return push_uint64(b); }
298     CScript& operator<<(unsigned long b)  { return push_uint64(b); }
299     CScript& operator<<(uint64 b)         { return push_uint64(b); }
300
301     CScript& operator<<(opcodetype opcode)
302     {
303         if (opcode < 0 || opcode > 0xff)
304             throw std::runtime_error("CScript::operator<<() : invalid opcode");
305         insert(end(), (unsigned char)opcode);
306         return *this;
307     }
308
309     CScript& operator<<(const uint160& b)
310     {
311         insert(end(), sizeof(b));
312         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
313         return *this;
314     }
315
316     CScript& operator<<(const uint256& b)
317     {
318         insert(end(), sizeof(b));
319         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
320         return *this;
321     }
322
323     CScript& operator<<(const CBigNum& b)
324     {
325         *this << b.getvch();
326         return *this;
327     }
328
329     CScript& operator<<(const std::vector<unsigned char>& b)
330     {
331         if (b.size() < OP_PUSHDATA1)
332         {
333             insert(end(), (unsigned char)b.size());
334         }
335         else if (b.size() <= 0xff)
336         {
337             insert(end(), OP_PUSHDATA1);
338             insert(end(), (unsigned char)b.size());
339         }
340         else if (b.size() <= 0xffff)
341         {
342             insert(end(), OP_PUSHDATA2);
343             unsigned short nSize = b.size();
344             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
345         }
346         else
347         {
348             insert(end(), OP_PUSHDATA4);
349             unsigned int nSize = b.size();
350             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
351         }
352         insert(end(), b.begin(), b.end());
353         return *this;
354     }
355
356     CScript& operator<<(const CScript& b)
357     {
358         // I'm not sure if this should push the script or concatenate scripts.
359         // If there's ever a use for pushing a script onto a script, delete this member fn
360         assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
361         return *this;
362     }
363
364
365     bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
366     {
367          // Wrapper so it can be called with either iterator or const_iterator
368          const_iterator pc2 = pc;
369          bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
370          pc = begin() + (pc2 - begin());
371          return fRet;
372     }
373
374     bool GetOp(iterator& pc, opcodetype& opcodeRet)
375     {
376          const_iterator pc2 = pc;
377          bool fRet = GetOp2(pc2, opcodeRet, NULL);
378          pc = begin() + (pc2 - begin());
379          return fRet;
380     }
381
382     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
383     {
384         return GetOp2(pc, opcodeRet, &vchRet);
385     }
386
387     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
388     {
389         return GetOp2(pc, opcodeRet, NULL);
390     }
391
392     bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
393     {
394         opcodeRet = OP_INVALIDOPCODE;
395         if (pvchRet)
396             pvchRet->clear();
397         if (pc >= end())
398             return false;
399
400         // Read instruction
401         if (end() - pc < 1)
402             return false;
403         unsigned int opcode = *pc++;
404
405         // Immediate operand
406         if (opcode <= OP_PUSHDATA4)
407         {
408             unsigned int nSize;
409             if (opcode < OP_PUSHDATA1)
410             {
411                 nSize = opcode;
412             }
413             else if (opcode == OP_PUSHDATA1)
414             {
415                 if (end() - pc < 1)
416                     return false;
417                 nSize = *pc++;
418             }
419             else if (opcode == OP_PUSHDATA2)
420             {
421                 if (end() - pc < 2)
422                     return false;
423                 nSize = 0;
424                 memcpy(&nSize, &pc[0], 2);
425                 pc += 2;
426             }
427             else if (opcode == OP_PUSHDATA4)
428             {
429                 if (end() - pc < 4)
430                     return false;
431                 memcpy(&nSize, &pc[0], 4);
432                 pc += 4;
433             }
434             if (end() - pc < nSize)
435                 return false;
436             if (pvchRet)
437                 pvchRet->assign(pc, pc + nSize);
438             pc += nSize;
439         }
440
441         opcodeRet = (opcodetype)opcode;
442         return true;
443     }
444
445     // Encode/decode small integers:
446     static int DecodeOP_N(opcodetype opcode)
447     {
448         if (opcode == OP_0)
449             return 0;
450         assert(opcode >= OP_1 && opcode <= OP_16);
451         return (int)opcode - (int)(OP_1 - 1);
452     }
453     static opcodetype EncodeOP_N(int n)
454     {
455         assert(n >= 0 && n <= 16);
456         if (n == 0)
457             return OP_0;
458         return (opcodetype)(OP_1+n-1);
459     }
460
461     int FindAndDelete(const CScript& b)
462     {
463         int nFound = 0;
464         if (b.empty())
465             return nFound;
466         iterator pc = begin();
467         opcodetype opcode;
468         do
469         {
470             while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
471             {
472                 erase(pc, pc + b.size());
473                 ++nFound;
474             }
475         }
476         while (GetOp(pc, opcode));
477         return nFound;
478     }
479     int Find(opcodetype op) const
480     {
481         int nFound = 0;
482         opcodetype opcode;
483         for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
484             if (opcode == op)
485                 ++nFound;
486         return nFound;
487     }
488
489     // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
490     // as 20 sigops. With pay-to-script-hash, that changed:
491     // CHECKMULTISIGs serialized in scriptSigs are
492     // counted more accurately, assuming they are of the form
493     //  ... OP_N CHECKMULTISIG ...
494     unsigned int GetSigOpCount(bool fAccurate) const;
495
496     // Accurately count sigOps, including sigOps in
497     // pay-to-script-hash transactions:
498     unsigned int GetSigOpCount(const CScript& scriptSig) const;
499
500     bool IsPayToScriptHash() const;
501
502     // Called by CTransaction::IsStandard
503     bool IsPushOnly() const
504     {
505         const_iterator pc = begin();
506         while (pc < end())
507         {
508             opcodetype opcode;
509             if (!GetOp(pc, opcode))
510                 return false;
511             if (opcode > OP_16)
512                 return false;
513         }
514         return true;
515     }
516
517
518     void SetBitcoinAddress(const CBitcoinAddress& address);
519     void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
520     {
521         SetBitcoinAddress(CBitcoinAddress(vchPubKey));
522     }
523     void SetMultisig(int nRequired, const std::vector<CKey>& keys);
524     void SetPayToScriptHash(const CScript& subscript);
525
526
527     void PrintHex() const
528     {
529         printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
530     }
531
532     std::string ToString() const
533     {
534         std::string str;
535         opcodetype opcode;
536         std::vector<unsigned char> vch;
537         const_iterator pc = begin();
538         while (pc < end())
539         {
540             if (!str.empty())
541                 str += " ";
542             if (!GetOp(pc, opcode, vch))
543             {
544                 str += "[error]";
545                 return str;
546             }
547             if (0 <= opcode && opcode <= OP_PUSHDATA4)
548                 str += ValueString(vch);
549             else
550                 str += GetOpName(opcode);
551         }
552         return str;
553     }
554
555     void print() const
556     {
557         printf("%s\n", ToString().c_str());
558     }
559 };
560
561
562
563
564
565 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
566 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
567 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
568 bool IsStandard(const CScript& scriptPubKey);
569 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
570 bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
571 bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
572 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
573 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
574
575 #endif