Replace OP_EVAL (BIP 12) with Pay-to-script-hash (BIP 16).
[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     // expansion
162     OP_NOP1,
163     OP_NOP2,
164     OP_NOP3,
165     OP_NOP4,
166     OP_NOP5,
167     OP_NOP6,
168     OP_NOP7,
169     OP_NOP8,
170     OP_NOP9,
171     OP_NOP10,
172
173
174
175     // template matching params
176     OP_SMALLINTEGER = 0xfa,
177     OP_PUBKEYS = 0xfb,
178     OP_PUBKEYHASH = 0xfd,
179     OP_PUBKEY = 0xfe,
180
181     OP_INVALIDOPCODE = 0xff,
182 };
183
184 const char* GetOpName(opcodetype opcode);
185
186
187
188 inline std::string ValueString(const std::vector<unsigned char>& vch)
189 {
190     if (vch.size() <= 4)
191         return strprintf("%d", CBigNum(vch).getint());
192     else
193         return HexStr(vch);
194 }
195
196 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
197 {
198     std::string str;
199     BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
200     {
201         if (!str.empty())
202             str += " ";
203         str += ValueString(vch);
204     }
205     return str;
206 }
207
208
209
210
211
212
213
214
215
216 class CScript : public std::vector<unsigned char>
217 {
218 protected:
219     CScript& push_int64(int64 n)
220     {
221         if (n == -1 || (n >= 1 && n <= 16))
222         {
223             push_back(n + (OP_1 - 1));
224         }
225         else
226         {
227             CBigNum bn(n);
228             *this << bn.getvch();
229         }
230         return *this;
231     }
232
233     CScript& push_uint64(uint64 n)
234     {
235         if (n >= 1 && n <= 16)
236         {
237             push_back(n + (OP_1 - 1));
238         }
239         else
240         {
241             CBigNum bn(n);
242             *this << bn.getvch();
243         }
244         return *this;
245     }
246
247 public:
248     CScript() { }
249     CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
250     CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
251 #ifndef _MSC_VER
252     CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
253 #endif
254
255     CScript& operator+=(const CScript& b)
256     {
257         insert(end(), b.begin(), b.end());
258         return *this;
259     }
260
261     friend CScript operator+(const CScript& a, const CScript& b)
262     {
263         CScript ret = a;
264         ret += b;
265         return ret;
266     }
267
268
269     explicit CScript(char b)           { operator<<(b); }
270     explicit CScript(short b)          { operator<<(b); }
271     explicit CScript(int b)            { operator<<(b); }
272     explicit CScript(long b)           { operator<<(b); }
273     explicit CScript(int64 b)          { operator<<(b); }
274     explicit CScript(unsigned char b)  { operator<<(b); }
275     explicit CScript(unsigned int b)   { operator<<(b); }
276     explicit CScript(unsigned short b) { operator<<(b); }
277     explicit CScript(unsigned long b)  { operator<<(b); }
278     explicit CScript(uint64 b)         { operator<<(b); }
279
280     explicit CScript(opcodetype b)     { operator<<(b); }
281     explicit CScript(const uint256& b) { operator<<(b); }
282     explicit CScript(const CBigNum& b) { operator<<(b); }
283     explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
284
285
286     CScript& operator<<(char b)           { return push_int64(b); }
287     CScript& operator<<(short b)          { return push_int64(b); }
288     CScript& operator<<(int b)            { return push_int64(b); }
289     CScript& operator<<(long b)           { return push_int64(b); }
290     CScript& operator<<(int64 b)          { return push_int64(b); }
291     CScript& operator<<(unsigned char b)  { return push_uint64(b); }
292     CScript& operator<<(unsigned int b)   { return push_uint64(b); }
293     CScript& operator<<(unsigned short b) { return push_uint64(b); }
294     CScript& operator<<(unsigned long b)  { return push_uint64(b); }
295     CScript& operator<<(uint64 b)         { return push_uint64(b); }
296
297     CScript& operator<<(opcodetype opcode)
298     {
299         if (opcode < 0 || opcode > 0xff)
300             throw std::runtime_error("CScript::operator<<() : invalid opcode");
301         insert(end(), (unsigned char)opcode);
302         return *this;
303     }
304
305     CScript& operator<<(const uint160& b)
306     {
307         insert(end(), sizeof(b));
308         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
309         return *this;
310     }
311
312     CScript& operator<<(const uint256& 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 CBigNum& b)
320     {
321         *this << b.getvch();
322         return *this;
323     }
324
325     CScript& operator<<(const std::vector<unsigned char>& b)
326     {
327         if (b.size() < OP_PUSHDATA1)
328         {
329             insert(end(), (unsigned char)b.size());
330         }
331         else if (b.size() <= 0xff)
332         {
333             insert(end(), OP_PUSHDATA1);
334             insert(end(), (unsigned char)b.size());
335         }
336         else if (b.size() <= 0xffff)
337         {
338             insert(end(), OP_PUSHDATA2);
339             unsigned short nSize = b.size();
340             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
341         }
342         else
343         {
344             insert(end(), OP_PUSHDATA4);
345             unsigned int nSize = b.size();
346             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
347         }
348         insert(end(), b.begin(), b.end());
349         return *this;
350     }
351
352     CScript& operator<<(const CScript& b)
353     {
354         // I'm not sure if this should push the script or concatenate scripts.
355         // If there's ever a use for pushing a script onto a script, delete this member fn
356         assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
357         return *this;
358     }
359
360
361     bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
362     {
363          // Wrapper so it can be called with either iterator or const_iterator
364          const_iterator pc2 = pc;
365          bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
366          pc = begin() + (pc2 - begin());
367          return fRet;
368     }
369
370     bool GetOp(iterator& pc, opcodetype& opcodeRet)
371     {
372          const_iterator pc2 = pc;
373          bool fRet = GetOp2(pc2, opcodeRet, NULL);
374          pc = begin() + (pc2 - begin());
375          return fRet;
376     }
377
378     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
379     {
380         return GetOp2(pc, opcodeRet, &vchRet);
381     }
382
383     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
384     {
385         return GetOp2(pc, opcodeRet, NULL);
386     }
387
388     bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
389     {
390         opcodeRet = OP_INVALIDOPCODE;
391         if (pvchRet)
392             pvchRet->clear();
393         if (pc >= end())
394             return false;
395
396         // Read instruction
397         if (end() - pc < 1)
398             return false;
399         unsigned int opcode = *pc++;
400
401         // Immediate operand
402         if (opcode <= OP_PUSHDATA4)
403         {
404             unsigned int nSize;
405             if (opcode < OP_PUSHDATA1)
406             {
407                 nSize = opcode;
408             }
409             else if (opcode == OP_PUSHDATA1)
410             {
411                 if (end() - pc < 1)
412                     return false;
413                 nSize = *pc++;
414             }
415             else if (opcode == OP_PUSHDATA2)
416             {
417                 if (end() - pc < 2)
418                     return false;
419                 nSize = 0;
420                 memcpy(&nSize, &pc[0], 2);
421                 pc += 2;
422             }
423             else if (opcode == OP_PUSHDATA4)
424             {
425                 if (end() - pc < 4)
426                     return false;
427                 memcpy(&nSize, &pc[0], 4);
428                 pc += 4;
429             }
430             if (end() - pc < nSize)
431                 return false;
432             if (pvchRet)
433                 pvchRet->assign(pc, pc + nSize);
434             pc += nSize;
435         }
436
437         opcodeRet = (opcodetype)opcode;
438         return true;
439     }
440
441     // Encode/decode small integers:
442     static int DecodeOP_N(opcodetype opcode)
443     {
444         if (opcode == OP_0)
445             return 0;
446         assert(opcode >= OP_1 && opcode <= OP_16);
447         return (int)opcode - (int)(OP_1 - 1);
448     }
449     static opcodetype EncodeOP_N(int n)
450     {
451         assert(n >= 0 && n <= 16);
452         if (n == 0)
453             return OP_0;
454         return (opcodetype)(OP_1+n-1);
455     }
456
457     int FindAndDelete(const CScript& b)
458     {
459         int nFound = 0;
460         if (b.empty())
461             return nFound;
462         iterator pc = begin();
463         opcodetype opcode;
464         do
465         {
466             while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
467             {
468                 erase(pc, pc + b.size());
469                 ++nFound;
470             }
471         }
472         while (GetOp(pc, opcode));
473         return nFound;
474     }
475     int Find(opcodetype op) const
476     {
477         int nFound = 0;
478         opcodetype opcode;
479         for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
480             if (opcode == op)
481                 ++nFound;
482         return nFound;
483     }
484
485     // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
486     // as 20 sigops. With pay-to-script-hash, that changed:
487     // CHECKMULTISIGs serialized in scriptSigs are
488     // counted more accurately, assuming they are of the form
489     //  ... OP_N CHECKMULTISIG ...
490     int GetSigOpCount(bool fAccurate) const;
491
492     // Accurately count sigOps, including sigOps in
493     // pay-to-script-hash transactions:
494     int GetSigOpCount(const CScript& scriptSig) const;
495
496     bool IsPayToScriptHash() const;
497
498     // Called by CTransaction::IsStandard
499     bool IsPushOnly() const
500     {
501         const_iterator pc = begin();
502         while (pc < end())
503         {
504             opcodetype opcode;
505             if (!GetOp(pc, opcode))
506                 return false;
507             if (opcode > OP_16)
508                 return false;
509         }
510         return true;
511     }
512
513
514     void SetBitcoinAddress(const CBitcoinAddress& address);
515     void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
516     {
517         SetBitcoinAddress(CBitcoinAddress(vchPubKey));
518     }
519     void SetMultisig(int nRequired, const std::vector<CKey>& keys);
520     void SetPayToScriptHash(const CScript& subscript);
521
522
523     void PrintHex() const
524     {
525         printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
526     }
527
528     std::string ToString() const
529     {
530         std::string str;
531         opcodetype opcode;
532         std::vector<unsigned char> vch;
533         const_iterator pc = begin();
534         while (pc < end())
535         {
536             if (!str.empty())
537                 str += " ";
538             if (!GetOp(pc, opcode, vch))
539             {
540                 str += "[error]";
541                 return str;
542             }
543             if (0 <= opcode && opcode <= OP_PUSHDATA4)
544                 str += ValueString(vch);
545             else
546                 str += GetOpName(opcode);
547         }
548         return str;
549     }
550
551     void print() const
552     {
553         printf("%s\n", ToString().c_str());
554     }
555 };
556
557
558
559
560
561 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
562 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
563 bool IsStandard(const CScript& scriptPubKey);
564 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
565 bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
566 bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
567 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
568 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
569
570 #endif