Begin doxygen-compatible comments
[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=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     // expansion
164     OP_NOP1,
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_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)           { operator<<(b); }
272     explicit CScript(short b)          { operator<<(b); }
273     explicit CScript(int b)            { operator<<(b); }
274     explicit CScript(long b)           { operator<<(b); }
275     explicit CScript(int64 b)          { operator<<(b); }
276     explicit CScript(unsigned char b)  { operator<<(b); }
277     explicit CScript(unsigned int b)   { operator<<(b); }
278     explicit CScript(unsigned short b) { operator<<(b); }
279     explicit CScript(unsigned long b)  { operator<<(b); }
280     explicit CScript(uint64 b)         { operator<<(b); }
281
282     explicit CScript(opcodetype b)     { operator<<(b); }
283     explicit CScript(const uint256& b) { operator<<(b); }
284     explicit CScript(const CBigNum& b) { operator<<(b); }
285     explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
286
287
288     CScript& operator<<(char b)           { return push_int64(b); }
289     CScript& operator<<(short b)          { return push_int64(b); }
290     CScript& operator<<(int b)            { return push_int64(b); }
291     CScript& operator<<(long b)           { return push_int64(b); }
292     CScript& operator<<(int64 b)          { return push_int64(b); }
293     CScript& operator<<(unsigned char b)  { return push_uint64(b); }
294     CScript& operator<<(unsigned int b)   { return push_uint64(b); }
295     CScript& operator<<(unsigned short b) { return push_uint64(b); }
296     CScript& operator<<(unsigned long b)  { return push_uint64(b); }
297     CScript& operator<<(uint64 b)         { return push_uint64(b); }
298
299     CScript& operator<<(opcodetype opcode)
300     {
301         if (opcode < 0 || opcode > 0xff)
302             throw std::runtime_error("CScript::operator<<() : invalid opcode");
303         insert(end(), (unsigned char)opcode);
304         return *this;
305     }
306
307     CScript& operator<<(const uint160& b)
308     {
309         insert(end(), sizeof(b));
310         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
311         return *this;
312     }
313
314     CScript& operator<<(const uint256& b)
315     {
316         insert(end(), sizeof(b));
317         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
318         return *this;
319     }
320
321     CScript& operator<<(const CBigNum& b)
322     {
323         *this << b.getvch();
324         return *this;
325     }
326
327     CScript& operator<<(const std::vector<unsigned char>& b)
328     {
329         if (b.size() < OP_PUSHDATA1)
330         {
331             insert(end(), (unsigned char)b.size());
332         }
333         else if (b.size() <= 0xff)
334         {
335             insert(end(), OP_PUSHDATA1);
336             insert(end(), (unsigned char)b.size());
337         }
338         else if (b.size() <= 0xffff)
339         {
340             insert(end(), OP_PUSHDATA2);
341             unsigned short nSize = b.size();
342             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
343         }
344         else
345         {
346             insert(end(), OP_PUSHDATA4);
347             unsigned int nSize = b.size();
348             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
349         }
350         insert(end(), b.begin(), b.end());
351         return *this;
352     }
353
354     CScript& operator<<(const CScript& b)
355     {
356         // I'm not sure if this should push the script or concatenate scripts.
357         // If there's ever a use for pushing a script onto a script, delete this member fn
358         assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
359         return *this;
360     }
361
362
363     bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
364     {
365          // Wrapper so it can be called with either iterator or const_iterator
366          const_iterator pc2 = pc;
367          bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
368          pc = begin() + (pc2 - begin());
369          return fRet;
370     }
371
372     bool GetOp(iterator& pc, opcodetype& opcodeRet)
373     {
374          const_iterator pc2 = pc;
375          bool fRet = GetOp2(pc2, opcodeRet, NULL);
376          pc = begin() + (pc2 - begin());
377          return fRet;
378     }
379
380     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
381     {
382         return GetOp2(pc, opcodeRet, &vchRet);
383     }
384
385     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
386     {
387         return GetOp2(pc, opcodeRet, NULL);
388     }
389
390     bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
391     {
392         opcodeRet = OP_INVALIDOPCODE;
393         if (pvchRet)
394             pvchRet->clear();
395         if (pc >= end())
396             return false;
397
398         // Read instruction
399         if (end() - pc < 1)
400             return false;
401         unsigned int opcode = *pc++;
402
403         // Immediate operand
404         if (opcode <= OP_PUSHDATA4)
405         {
406             unsigned int nSize;
407             if (opcode < OP_PUSHDATA1)
408             {
409                 nSize = opcode;
410             }
411             else if (opcode == OP_PUSHDATA1)
412             {
413                 if (end() - pc < 1)
414                     return false;
415                 nSize = *pc++;
416             }
417             else if (opcode == OP_PUSHDATA2)
418             {
419                 if (end() - pc < 2)
420                     return false;
421                 nSize = 0;
422                 memcpy(&nSize, &pc[0], 2);
423                 pc += 2;
424             }
425             else if (opcode == OP_PUSHDATA4)
426             {
427                 if (end() - pc < 4)
428                     return false;
429                 memcpy(&nSize, &pc[0], 4);
430                 pc += 4;
431             }
432             if (end() - pc < nSize)
433                 return false;
434             if (pvchRet)
435                 pvchRet->assign(pc, pc + nSize);
436             pc += nSize;
437         }
438
439         opcodeRet = (opcodetype)opcode;
440         return true;
441     }
442
443     // Encode/decode small integers:
444     static int DecodeOP_N(opcodetype opcode)
445     {
446         if (opcode == OP_0)
447             return 0;
448         assert(opcode >= OP_1 && opcode <= OP_16);
449         return (int)opcode - (int)(OP_1 - 1);
450     }
451     static opcodetype EncodeOP_N(int n)
452     {
453         assert(n >= 0 && n <= 16);
454         if (n == 0)
455             return OP_0;
456         return (opcodetype)(OP_1+n-1);
457     }
458
459     int FindAndDelete(const CScript& b)
460     {
461         int nFound = 0;
462         if (b.empty())
463             return nFound;
464         iterator pc = begin();
465         opcodetype opcode;
466         do
467         {
468             while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
469             {
470                 erase(pc, pc + b.size());
471                 ++nFound;
472             }
473         }
474         while (GetOp(pc, opcode));
475         return nFound;
476     }
477     int Find(opcodetype op) const
478     {
479         int nFound = 0;
480         opcodetype opcode;
481         for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
482             if (opcode == op)
483                 ++nFound;
484         return nFound;
485     }
486
487     // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
488     // as 20 sigops. With pay-to-script-hash, that changed:
489     // CHECKMULTISIGs serialized in scriptSigs are
490     // counted more accurately, assuming they are of the form
491     //  ... OP_N CHECKMULTISIG ...
492     int GetSigOpCount(bool fAccurate) const;
493
494     // Accurately count sigOps, including sigOps in
495     // pay-to-script-hash transactions:
496     int GetSigOpCount(const CScript& scriptSig) const;
497
498     bool IsPayToScriptHash() const;
499
500     // Called by CTransaction::IsStandard
501     bool IsPushOnly() const
502     {
503         const_iterator pc = begin();
504         while (pc < end())
505         {
506             opcodetype opcode;
507             if (!GetOp(pc, opcode))
508                 return false;
509             if (opcode > OP_16)
510                 return false;
511         }
512         return true;
513     }
514
515
516     void SetBitcoinAddress(const CBitcoinAddress& address);
517     void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
518     {
519         SetBitcoinAddress(CBitcoinAddress(vchPubKey));
520     }
521     void SetMultisig(int nRequired, const std::vector<CKey>& keys);
522     void SetPayToScriptHash(const CScript& subscript);
523
524
525     void PrintHex() const
526     {
527         printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
528     }
529
530     std::string ToString() const
531     {
532         std::string str;
533         opcodetype opcode;
534         std::vector<unsigned char> vch;
535         const_iterator pc = begin();
536         while (pc < end())
537         {
538             if (!str.empty())
539                 str += " ";
540             if (!GetOp(pc, opcode, vch))
541             {
542                 str += "[error]";
543                 return str;
544             }
545             if (0 <= opcode && opcode <= OP_PUSHDATA4)
546                 str += ValueString(vch);
547             else
548                 str += GetOpName(opcode);
549         }
550         return str;
551     }
552
553     void print() const
554     {
555         printf("%s\n", ToString().c_str());
556     }
557 };
558
559
560
561
562
563 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
564 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
565 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
566 bool IsStandard(const CScript& scriptPubKey);
567 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
568 bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
569 bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
570 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
571 bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
572
573 #endif