]> Git Repo - VerusCoin.git/blob - src/script.h
Combine CCoinsViewCache's HaveCoins and const GetCoins into AccessCoins.
[VerusCoin.git] / src / script.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #ifndef H_BITCOIN_SCRIPT
7 #define H_BITCOIN_SCRIPT
8
9 #include "key.h"
10 #include "utilstrencodings.h"
11 #include "tinyformat.h"
12
13 #include <stdexcept>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17
18 #include <boost/variant.hpp>
19
20 class CKeyStore;
21 class CTransaction;
22 struct CMutableTransaction;
23
24 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
25 static const unsigned int MAX_OP_RETURN_RELAY = 40;      // bytes
26
27 class scriptnum_error : public std::runtime_error
28 {
29 public:
30     explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
31 };
32
33 class CScriptNum
34 {
35 // Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
36 // The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
37 // but results may overflow (and are valid as long as they are not used in a subsequent
38 // numeric operation). CScriptNum enforces those semantics by storing results as
39 // an int64 and allowing out-of-range values to be returned as a vector of bytes but
40 // throwing an exception if arithmetic is done or the result is interpreted as an integer.
41 public:
42
43     explicit CScriptNum(const int64_t& n)
44     {
45         m_value = n;
46     }
47
48     explicit CScriptNum(const std::vector<unsigned char>& vch)
49     {
50         if (vch.size() > nMaxNumSize)
51             throw scriptnum_error("CScriptNum(const std::vector<unsigned char>&) : overflow");
52         m_value = set_vch(vch);
53     }
54
55     inline bool operator==(const int64_t& rhs) const    { return m_value == rhs; }
56     inline bool operator!=(const int64_t& rhs) const    { return m_value != rhs; }
57     inline bool operator<=(const int64_t& rhs) const    { return m_value <= rhs; }
58     inline bool operator< (const int64_t& rhs) const    { return m_value <  rhs; }
59     inline bool operator>=(const int64_t& rhs) const    { return m_value >= rhs; }
60     inline bool operator> (const int64_t& rhs) const    { return m_value >  rhs; }
61
62     inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
63     inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
64     inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
65     inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
66     inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
67     inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
68
69     inline CScriptNum operator+(   const int64_t& rhs)    const { return CScriptNum(m_value + rhs);}
70     inline CScriptNum operator-(   const int64_t& rhs)    const { return CScriptNum(m_value - rhs);}
71     inline CScriptNum operator+(   const CScriptNum& rhs) const { return operator+(rhs.m_value);   }
72     inline CScriptNum operator-(   const CScriptNum& rhs) const { return operator-(rhs.m_value);   }
73
74     inline CScriptNum& operator+=( const CScriptNum& rhs)       { return operator+=(rhs.m_value);  }
75     inline CScriptNum& operator-=( const CScriptNum& rhs)       { return operator-=(rhs.m_value);  }
76
77     inline CScriptNum operator-()                         const
78     {
79         assert(m_value != std::numeric_limits<int64_t>::min());
80         return CScriptNum(-m_value);
81     }
82
83     inline CScriptNum& operator=( const int64_t& rhs)
84     {
85         m_value = rhs;
86         return *this;
87     }
88
89     inline CScriptNum& operator+=( const int64_t& rhs)
90     {
91         assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
92                            (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
93         m_value += rhs;
94         return *this;
95     }
96
97     inline CScriptNum& operator-=( const int64_t& rhs)
98     {
99         assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
100                            (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
101         m_value -= rhs;
102         return *this;
103     }
104
105     int getint() const
106     {
107         if (m_value > std::numeric_limits<int>::max())
108             return std::numeric_limits<int>::max();
109         else if (m_value < std::numeric_limits<int>::min())
110             return std::numeric_limits<int>::min();
111         return m_value;
112     }
113
114     std::vector<unsigned char> getvch() const
115     {
116         return serialize(m_value);
117     }
118
119     static std::vector<unsigned char> serialize(const int64_t& value)
120     {
121         if(value == 0)
122             return std::vector<unsigned char>();
123
124         std::vector<unsigned char> result;
125         const bool neg = value < 0;
126         uint64_t absvalue = neg ? -value : value;
127
128         while(absvalue)
129         {
130             result.push_back(absvalue & 0xff);
131             absvalue >>= 8;
132         }
133
134
135 //    - If the most significant byte is >= 0x80 and the value is positive, push a
136 //    new zero-byte to make the significant byte < 0x80 again.
137
138 //    - If the most significant byte is >= 0x80 and the value is negative, push a
139 //    new 0x80 byte that will be popped off when converting to an integral.
140
141 //    - If the most significant byte is < 0x80 and the value is negative, add
142 //    0x80 to it, since it will be subtracted and interpreted as a negative when
143 //    converting to an integral.
144
145         if (result.back() & 0x80)
146             result.push_back(neg ? 0x80 : 0);
147         else if (neg)
148             result.back() |= 0x80;
149
150         return result;
151     }
152
153     static const size_t nMaxNumSize = 4;
154
155 private:
156     static int64_t set_vch(const std::vector<unsigned char>& vch)
157     {
158       if (vch.empty())
159           return 0;
160
161       int64_t result = 0;
162       for (size_t i = 0; i != vch.size(); ++i)
163           result |= static_cast<int64_t>(vch[i]) << 8*i;
164
165       // If the input vector's most significant byte is 0x80, remove it from
166       // the result's msb and return a negative.
167       if (vch.back() & 0x80)
168           return -(result & ~(0x80ULL << (8 * (vch.size() - 1))));
169
170       return result;
171     }
172
173     int64_t m_value;
174 };
175
176 /** Signature hash types/flags */
177 enum
178 {
179     SIGHASH_ALL = 1,
180     SIGHASH_NONE = 2,
181     SIGHASH_SINGLE = 3,
182     SIGHASH_ANYONECANPAY = 0x80,
183 };
184
185 /** Script verification flags */
186 enum
187 {
188     SCRIPT_VERIFY_NONE      = 0,
189     SCRIPT_VERIFY_P2SH      = (1U << 0), // evaluate P2SH (BIP16) subscripts
190     SCRIPT_VERIFY_STRICTENC = (1U << 1), // enforce strict conformance to DER and SEC2 for signatures and pubkeys
191     SCRIPT_VERIFY_LOW_S     = (1U << 2), // enforce low S values (<n/2) in signatures (depends on STRICTENC)
192     SCRIPT_VERIFY_NOCACHE   = (1U << 3), // do not store results in signature cache (but do query it)
193     SCRIPT_VERIFY_NULLDUMMY = (1U << 4), // verify dummy stack item consumed by CHECKMULTISIG is of zero-length
194 };
195
196 /** IsMine() return codes */
197 enum isminetype
198 {
199     ISMINE_NO = 0,
200     ISMINE_WATCH_ONLY = 1,
201     ISMINE_SPENDABLE = 2,
202     ISMINE_ALL = ISMINE_WATCH_ONLY | ISMINE_SPENDABLE
203 };
204 /** used for bitflags of isminetype */
205 typedef uint8_t isminefilter;
206
207 // Mandatory script verification flags that all new blocks must comply with for
208 // them to be valid. (but old blocks may not comply with) Currently just P2SH,
209 // but in the future other flags may be added, such as a soft-fork to enforce
210 // strict DER encoding.
211 //
212 // Failing one of these tests may trigger a DoS ban - see CheckInputs() for
213 // details.
214 static const unsigned int MANDATORY_SCRIPT_VERIFY_FLAGS = SCRIPT_VERIFY_P2SH;
215
216 // Standard script verification flags that standard transactions will comply
217 // with. However scripts violating these flags may still be present in valid
218 // blocks and we must accept those blocks.
219 static const unsigned int STANDARD_SCRIPT_VERIFY_FLAGS = MANDATORY_SCRIPT_VERIFY_FLAGS |
220                                                          SCRIPT_VERIFY_STRICTENC |
221                                                          SCRIPT_VERIFY_NULLDUMMY;
222
223 // For convenience, standard but not mandatory verify flags.
224 static const unsigned int STANDARD_NOT_MANDATORY_VERIFY_FLAGS = STANDARD_SCRIPT_VERIFY_FLAGS & ~MANDATORY_SCRIPT_VERIFY_FLAGS;
225
226 enum txnouttype
227 {
228     TX_NONSTANDARD,
229     // 'standard' transaction types:
230     TX_PUBKEY,
231     TX_PUBKEYHASH,
232     TX_SCRIPTHASH,
233     TX_MULTISIG,
234     TX_NULL_DATA,
235 };
236
237 class CNoDestination {
238 public:
239     friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
240     friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
241 };
242
243 /** A txout script template with a specific destination. It is either:
244  *  * CNoDestination: no destination set
245  *  * CKeyID: TX_PUBKEYHASH destination
246  *  * CScriptID: TX_SCRIPTHASH destination
247  *  A CTxDestination is the internal data type encoded in a CBitcoinAddress
248  */
249 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
250
251 const char* GetTxnOutputType(txnouttype t);
252
253 /** Script opcodes */
254 enum opcodetype
255 {
256     // push value
257     OP_0 = 0x00,
258     OP_FALSE = OP_0,
259     OP_PUSHDATA1 = 0x4c,
260     OP_PUSHDATA2 = 0x4d,
261     OP_PUSHDATA4 = 0x4e,
262     OP_1NEGATE = 0x4f,
263     OP_RESERVED = 0x50,
264     OP_1 = 0x51,
265     OP_TRUE=OP_1,
266     OP_2 = 0x52,
267     OP_3 = 0x53,
268     OP_4 = 0x54,
269     OP_5 = 0x55,
270     OP_6 = 0x56,
271     OP_7 = 0x57,
272     OP_8 = 0x58,
273     OP_9 = 0x59,
274     OP_10 = 0x5a,
275     OP_11 = 0x5b,
276     OP_12 = 0x5c,
277     OP_13 = 0x5d,
278     OP_14 = 0x5e,
279     OP_15 = 0x5f,
280     OP_16 = 0x60,
281
282     // control
283     OP_NOP = 0x61,
284     OP_VER = 0x62,
285     OP_IF = 0x63,
286     OP_NOTIF = 0x64,
287     OP_VERIF = 0x65,
288     OP_VERNOTIF = 0x66,
289     OP_ELSE = 0x67,
290     OP_ENDIF = 0x68,
291     OP_VERIFY = 0x69,
292     OP_RETURN = 0x6a,
293
294     // stack ops
295     OP_TOALTSTACK = 0x6b,
296     OP_FROMALTSTACK = 0x6c,
297     OP_2DROP = 0x6d,
298     OP_2DUP = 0x6e,
299     OP_3DUP = 0x6f,
300     OP_2OVER = 0x70,
301     OP_2ROT = 0x71,
302     OP_2SWAP = 0x72,
303     OP_IFDUP = 0x73,
304     OP_DEPTH = 0x74,
305     OP_DROP = 0x75,
306     OP_DUP = 0x76,
307     OP_NIP = 0x77,
308     OP_OVER = 0x78,
309     OP_PICK = 0x79,
310     OP_ROLL = 0x7a,
311     OP_ROT = 0x7b,
312     OP_SWAP = 0x7c,
313     OP_TUCK = 0x7d,
314
315     // splice ops
316     OP_CAT = 0x7e,
317     OP_SUBSTR = 0x7f,
318     OP_LEFT = 0x80,
319     OP_RIGHT = 0x81,
320     OP_SIZE = 0x82,
321
322     // bit logic
323     OP_INVERT = 0x83,
324     OP_AND = 0x84,
325     OP_OR = 0x85,
326     OP_XOR = 0x86,
327     OP_EQUAL = 0x87,
328     OP_EQUALVERIFY = 0x88,
329     OP_RESERVED1 = 0x89,
330     OP_RESERVED2 = 0x8a,
331
332     // numeric
333     OP_1ADD = 0x8b,
334     OP_1SUB = 0x8c,
335     OP_2MUL = 0x8d,
336     OP_2DIV = 0x8e,
337     OP_NEGATE = 0x8f,
338     OP_ABS = 0x90,
339     OP_NOT = 0x91,
340     OP_0NOTEQUAL = 0x92,
341
342     OP_ADD = 0x93,
343     OP_SUB = 0x94,
344     OP_MUL = 0x95,
345     OP_DIV = 0x96,
346     OP_MOD = 0x97,
347     OP_LSHIFT = 0x98,
348     OP_RSHIFT = 0x99,
349
350     OP_BOOLAND = 0x9a,
351     OP_BOOLOR = 0x9b,
352     OP_NUMEQUAL = 0x9c,
353     OP_NUMEQUALVERIFY = 0x9d,
354     OP_NUMNOTEQUAL = 0x9e,
355     OP_LESSTHAN = 0x9f,
356     OP_GREATERTHAN = 0xa0,
357     OP_LESSTHANOREQUAL = 0xa1,
358     OP_GREATERTHANOREQUAL = 0xa2,
359     OP_MIN = 0xa3,
360     OP_MAX = 0xa4,
361
362     OP_WITHIN = 0xa5,
363
364     // crypto
365     OP_RIPEMD160 = 0xa6,
366     OP_SHA1 = 0xa7,
367     OP_SHA256 = 0xa8,
368     OP_HASH160 = 0xa9,
369     OP_HASH256 = 0xaa,
370     OP_CODESEPARATOR = 0xab,
371     OP_CHECKSIG = 0xac,
372     OP_CHECKSIGVERIFY = 0xad,
373     OP_CHECKMULTISIG = 0xae,
374     OP_CHECKMULTISIGVERIFY = 0xaf,
375
376     // expansion
377     OP_NOP1 = 0xb0,
378     OP_NOP2 = 0xb1,
379     OP_NOP3 = 0xb2,
380     OP_NOP4 = 0xb3,
381     OP_NOP5 = 0xb4,
382     OP_NOP6 = 0xb5,
383     OP_NOP7 = 0xb6,
384     OP_NOP8 = 0xb7,
385     OP_NOP9 = 0xb8,
386     OP_NOP10 = 0xb9,
387
388
389
390     // template matching params
391     OP_SMALLDATA = 0xf9,
392     OP_SMALLINTEGER = 0xfa,
393     OP_PUBKEYS = 0xfb,
394     OP_PUBKEYHASH = 0xfd,
395     OP_PUBKEY = 0xfe,
396
397     OP_INVALIDOPCODE = 0xff,
398 };
399
400 const char* GetOpName(opcodetype opcode);
401
402
403
404 inline std::string ValueString(const std::vector<unsigned char>& vch)
405 {
406     if (vch.size() <= 4)
407         return strprintf("%d", CScriptNum(vch).getint());
408     else
409         return HexStr(vch);
410 }
411
412 /** Serialized script, used inside transaction inputs and outputs */
413 class CScript : public std::vector<unsigned char>
414 {
415 protected:
416     CScript& push_int64(int64_t n)
417     {
418         if (n == -1 || (n >= 1 && n <= 16))
419         {
420             push_back(n + (OP_1 - 1));
421         }
422         else
423         {
424             *this << CScriptNum::serialize(n);
425         }
426         return *this;
427     }
428 public:
429     CScript() { }
430     CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
431     CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
432 #ifndef _MSC_VER
433     CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
434 #endif
435
436     CScript& operator+=(const CScript& b)
437     {
438         insert(end(), b.begin(), b.end());
439         return *this;
440     }
441
442     friend CScript operator+(const CScript& a, const CScript& b)
443     {
444         CScript ret = a;
445         ret += b;
446         return ret;
447     }
448
449
450     CScript(int64_t b)        { operator<<(b); }
451
452     explicit CScript(opcodetype b)     { operator<<(b); }
453     explicit CScript(const uint256& b) { operator<<(b); }
454     explicit CScript(const CScriptNum& b) { operator<<(b); }
455     explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
456
457
458     CScript& operator<<(int64_t b) { return push_int64(b); }
459
460     CScript& operator<<(opcodetype opcode)
461     {
462         if (opcode < 0 || opcode > 0xff)
463             throw std::runtime_error("CScript::operator<<() : invalid opcode");
464         insert(end(), (unsigned char)opcode);
465         return *this;
466     }
467
468     CScript& operator<<(const uint160& b)
469     {
470         insert(end(), sizeof(b));
471         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
472         return *this;
473     }
474
475     CScript& operator<<(const uint256& b)
476     {
477         insert(end(), sizeof(b));
478         insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
479         return *this;
480     }
481
482     CScript& operator<<(const CPubKey& key)
483     {
484         assert(key.size() < OP_PUSHDATA1);
485         insert(end(), (unsigned char)key.size());
486         insert(end(), key.begin(), key.end());
487         return *this;
488     }
489
490     CScript& operator<<(const CScriptNum& b)
491     {
492         *this << b.getvch();
493         return *this;
494     }
495
496     CScript& operator<<(const std::vector<unsigned char>& b)
497     {
498         if (b.size() < OP_PUSHDATA1)
499         {
500             insert(end(), (unsigned char)b.size());
501         }
502         else if (b.size() <= 0xff)
503         {
504             insert(end(), OP_PUSHDATA1);
505             insert(end(), (unsigned char)b.size());
506         }
507         else if (b.size() <= 0xffff)
508         {
509             insert(end(), OP_PUSHDATA2);
510             unsigned short nSize = b.size();
511             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
512         }
513         else
514         {
515             insert(end(), OP_PUSHDATA4);
516             unsigned int nSize = b.size();
517             insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
518         }
519         insert(end(), b.begin(), b.end());
520         return *this;
521     }
522
523     CScript& operator<<(const CScript& b)
524     {
525         // I'm not sure if this should push the script or concatenate scripts.
526         // If there's ever a use for pushing a script onto a script, delete this member fn
527         assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
528         return *this;
529     }
530
531
532     bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
533     {
534          // Wrapper so it can be called with either iterator or const_iterator
535          const_iterator pc2 = pc;
536          bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
537          pc = begin() + (pc2 - begin());
538          return fRet;
539     }
540
541     bool GetOp(iterator& pc, opcodetype& opcodeRet)
542     {
543          const_iterator pc2 = pc;
544          bool fRet = GetOp2(pc2, opcodeRet, NULL);
545          pc = begin() + (pc2 - begin());
546          return fRet;
547     }
548
549     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
550     {
551         return GetOp2(pc, opcodeRet, &vchRet);
552     }
553
554     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
555     {
556         return GetOp2(pc, opcodeRet, NULL);
557     }
558
559     bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
560     {
561         opcodeRet = OP_INVALIDOPCODE;
562         if (pvchRet)
563             pvchRet->clear();
564         if (pc >= end())
565             return false;
566
567         // Read instruction
568         if (end() - pc < 1)
569             return false;
570         unsigned int opcode = *pc++;
571
572         // Immediate operand
573         if (opcode <= OP_PUSHDATA4)
574         {
575             unsigned int nSize = 0;
576             if (opcode < OP_PUSHDATA1)
577             {
578                 nSize = opcode;
579             }
580             else if (opcode == OP_PUSHDATA1)
581             {
582                 if (end() - pc < 1)
583                     return false;
584                 nSize = *pc++;
585             }
586             else if (opcode == OP_PUSHDATA2)
587             {
588                 if (end() - pc < 2)
589                     return false;
590                 nSize = 0;
591                 memcpy(&nSize, &pc[0], 2);
592                 pc += 2;
593             }
594             else if (opcode == OP_PUSHDATA4)
595             {
596                 if (end() - pc < 4)
597                     return false;
598                 memcpy(&nSize, &pc[0], 4);
599                 pc += 4;
600             }
601             if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
602                 return false;
603             if (pvchRet)
604                 pvchRet->assign(pc, pc + nSize);
605             pc += nSize;
606         }
607
608         opcodeRet = (opcodetype)opcode;
609         return true;
610     }
611
612     // Encode/decode small integers:
613     static int DecodeOP_N(opcodetype opcode)
614     {
615         if (opcode == OP_0)
616             return 0;
617         assert(opcode >= OP_1 && opcode <= OP_16);
618         return (int)opcode - (int)(OP_1 - 1);
619     }
620     static opcodetype EncodeOP_N(int n)
621     {
622         assert(n >= 0 && n <= 16);
623         if (n == 0)
624             return OP_0;
625         return (opcodetype)(OP_1+n-1);
626     }
627
628     int FindAndDelete(const CScript& b)
629     {
630         int nFound = 0;
631         if (b.empty())
632             return nFound;
633         iterator pc = begin();
634         opcodetype opcode;
635         do
636         {
637             while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
638             {
639                 erase(pc, pc + b.size());
640                 ++nFound;
641             }
642         }
643         while (GetOp(pc, opcode));
644         return nFound;
645     }
646     int Find(opcodetype op) const
647     {
648         int nFound = 0;
649         opcodetype opcode;
650         for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
651             if (opcode == op)
652                 ++nFound;
653         return nFound;
654     }
655
656     // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
657     // as 20 sigops. With pay-to-script-hash, that changed:
658     // CHECKMULTISIGs serialized in scriptSigs are
659     // counted more accurately, assuming they are of the form
660     //  ... OP_N CHECKMULTISIG ...
661     unsigned int GetSigOpCount(bool fAccurate) const;
662
663     // Accurately count sigOps, including sigOps in
664     // pay-to-script-hash transactions:
665     unsigned int GetSigOpCount(const CScript& scriptSig) const;
666
667     bool IsPayToScriptHash() const;
668
669     // Called by IsStandardTx and P2SH VerifyScript (which makes it consensus-critical).
670     bool IsPushOnly() const;
671
672     // Called by IsStandardTx.
673     bool HasCanonicalPushes() const;
674
675     // Returns whether the script is guaranteed to fail at execution,
676     // regardless of the initial stack. This allows outputs to be pruned
677     // instantly when entering the UTXO set.
678     bool IsUnspendable() const
679     {
680         return (size() > 0 && *begin() == OP_RETURN);
681     }
682
683     void SetDestination(const CTxDestination& address);
684     void SetMultisig(int nRequired, const std::vector<CPubKey>& keys);
685
686     std::string ToString() const
687     {
688         std::string str;
689         opcodetype opcode;
690         std::vector<unsigned char> vch;
691         const_iterator pc = begin();
692         while (pc < end())
693         {
694             if (!str.empty())
695                 str += " ";
696             if (!GetOp(pc, opcode, vch))
697             {
698                 str += "[error]";
699                 return str;
700             }
701             if (0 <= opcode && opcode <= OP_PUSHDATA4)
702                 str += ValueString(vch);
703             else
704                 str += GetOpName(opcode);
705         }
706         return str;
707     }
708
709     CScriptID GetID() const
710     {
711         return CScriptID(Hash160(*this));
712     }
713
714     void clear()
715     {
716         // The default std::vector::clear() does not release memory.
717         std::vector<unsigned char>().swap(*this);
718     }
719 };
720
721 /** Compact serializer for scripts.
722  *
723  *  It detects common cases and encodes them much more efficiently.
724  *  3 special cases are defined:
725  *  * Pay to pubkey hash (encoded as 21 bytes)
726  *  * Pay to script hash (encoded as 21 bytes)
727  *  * Pay to pubkey starting with 0x02, 0x03 or 0x04 (encoded as 33 bytes)
728  *
729  *  Other scripts up to 121 bytes require 1 byte + script length. Above
730  *  that, scripts up to 16505 bytes require 2 bytes + script length.
731  */
732 class CScriptCompressor
733 {
734 private:
735     // make this static for now (there are only 6 special scripts defined)
736     // this can potentially be extended together with a new nVersion for
737     // transactions, in which case this value becomes dependent on nVersion
738     // and nHeight of the enclosing transaction.
739     static const unsigned int nSpecialScripts = 6;
740
741     CScript &script;
742 protected:
743     // These check for scripts for which a special case with a shorter encoding is defined.
744     // They are implemented separately from the CScript test, as these test for exact byte
745     // sequence correspondences, and are more strict. For example, IsToPubKey also verifies
746     // whether the public key is valid (as invalid ones cannot be represented in compressed
747     // form).
748     bool IsToKeyID(CKeyID &hash) const;
749     bool IsToScriptID(CScriptID &hash) const;
750     bool IsToPubKey(CPubKey &pubkey) const;
751
752     bool Compress(std::vector<unsigned char> &out) const;
753     unsigned int GetSpecialSize(unsigned int nSize) const;
754     bool Decompress(unsigned int nSize, const std::vector<unsigned char> &out);
755 public:
756     CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
757
758     unsigned int GetSerializeSize(int nType, int nVersion) const {
759         std::vector<unsigned char> compr;
760         if (Compress(compr))
761             return compr.size();
762         unsigned int nSize = script.size() + nSpecialScripts;
763         return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
764     }
765
766     template<typename Stream>
767     void Serialize(Stream &s, int nType, int nVersion) const {
768         std::vector<unsigned char> compr;
769         if (Compress(compr)) {
770             s << CFlatData(compr);
771             return;
772         }
773         unsigned int nSize = script.size() + nSpecialScripts;
774         s << VARINT(nSize);
775         s << CFlatData(script);
776     }
777
778     template<typename Stream>
779     void Unserialize(Stream &s, int nType, int nVersion) {
780         unsigned int nSize = 0;
781         s >> VARINT(nSize);
782         if (nSize < nSpecialScripts) {
783             std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
784             s >> REF(CFlatData(vch));
785             Decompress(nSize, vch);
786             return;
787         }
788         nSize -= nSpecialScripts;
789         script.resize(nSize);
790         s >> REF(CFlatData(script));
791     }
792 };
793
794 bool IsCanonicalPubKey(const std::vector<unsigned char> &vchPubKey, unsigned int flags);
795 bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int flags);
796
797 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
798 uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType);
799 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
800 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
801 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType);
802 isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
803 isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest);
804 void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys);
805 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
806 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
807 bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CMutableTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
808 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CMutableTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
809 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
810
811 // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
812 // combine them intelligently and return the result.
813 CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn, const CScript& scriptSig1, const CScript& scriptSig2);
814
815 #endif // H_BITCOIN_SCRIPT
This page took 0.068436 seconds and 4 git commands to generate.