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