Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
script.h
Go to the documentation of this file.
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 "util.h"
11 
12 #include <stdexcept>
13 #include <stdint.h>
14 #include <string>
15 #include <vector>
16 
17 #include <boost/foreach.hpp>
18 #include <boost/variant.hpp>
19 
20 class CCoins;
21 class CKeyStore;
22 class CTransaction;
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 
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 & ~(0x80 << (8 * (vch.size() - 1))));
169 
170  return result;
171  }
172 
173  int64_t m_value;
174 };
175 
177 enum
178 {
183 };
184 
186 enum
187 {
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_EVEN_S = (1U << 2), // enforce even S values in signatures (depends on STRICTENC)
192  SCRIPT_VERIFY_NOCACHE = (1U << 3), // do not store results in signature cache (but do query it)
193 };
194 
196 {
198  // 'standard' transaction types:
204 };
205 
207 public:
208  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
209  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
210 };
211 
218 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
219 
220 const char* GetTxnOutputType(txnouttype t);
221 
224 {
225  // push value
226  OP_0 = 0x00,
228  OP_PUSHDATA1 = 0x4c,
229  OP_PUSHDATA2 = 0x4d,
230  OP_PUSHDATA4 = 0x4e,
231  OP_1NEGATE = 0x4f,
232  OP_RESERVED = 0x50,
233  OP_1 = 0x51,
235  OP_2 = 0x52,
236  OP_3 = 0x53,
237  OP_4 = 0x54,
238  OP_5 = 0x55,
239  OP_6 = 0x56,
240  OP_7 = 0x57,
241  OP_8 = 0x58,
242  OP_9 = 0x59,
243  OP_10 = 0x5a,
244  OP_11 = 0x5b,
245  OP_12 = 0x5c,
246  OP_13 = 0x5d,
247  OP_14 = 0x5e,
248  OP_15 = 0x5f,
249  OP_16 = 0x60,
250 
251  // control
252  OP_NOP = 0x61,
253  OP_VER = 0x62,
254  OP_IF = 0x63,
255  OP_NOTIF = 0x64,
256  OP_VERIF = 0x65,
257  OP_VERNOTIF = 0x66,
258  OP_ELSE = 0x67,
259  OP_ENDIF = 0x68,
260  OP_VERIFY = 0x69,
261  OP_RETURN = 0x6a,
262 
263  // stack ops
266  OP_2DROP = 0x6d,
267  OP_2DUP = 0x6e,
268  OP_3DUP = 0x6f,
269  OP_2OVER = 0x70,
270  OP_2ROT = 0x71,
271  OP_2SWAP = 0x72,
272  OP_IFDUP = 0x73,
273  OP_DEPTH = 0x74,
274  OP_DROP = 0x75,
275  OP_DUP = 0x76,
276  OP_NIP = 0x77,
277  OP_OVER = 0x78,
278  OP_PICK = 0x79,
279  OP_ROLL = 0x7a,
280  OP_ROT = 0x7b,
281  OP_SWAP = 0x7c,
282  OP_TUCK = 0x7d,
283 
284  // splice ops
285  OP_CAT = 0x7e,
286  OP_SUBSTR = 0x7f,
287  OP_LEFT = 0x80,
288  OP_RIGHT = 0x81,
289  OP_SIZE = 0x82,
290 
291  // bit logic
292  OP_INVERT = 0x83,
293  OP_AND = 0x84,
294  OP_OR = 0x85,
295  OP_XOR = 0x86,
296  OP_EQUAL = 0x87,
298  OP_RESERVED1 = 0x89,
299  OP_RESERVED2 = 0x8a,
300 
301  // numeric
302  OP_1ADD = 0x8b,
303  OP_1SUB = 0x8c,
304  OP_2MUL = 0x8d,
305  OP_2DIV = 0x8e,
306  OP_NEGATE = 0x8f,
307  OP_ABS = 0x90,
308  OP_NOT = 0x91,
309  OP_0NOTEQUAL = 0x92,
310 
311  OP_ADD = 0x93,
312  OP_SUB = 0x94,
313  OP_MUL = 0x95,
314  OP_DIV = 0x96,
315  OP_MOD = 0x97,
316  OP_LSHIFT = 0x98,
317  OP_RSHIFT = 0x99,
318 
319  OP_BOOLAND = 0x9a,
320  OP_BOOLOR = 0x9b,
321  OP_NUMEQUAL = 0x9c,
324  OP_LESSTHAN = 0x9f,
328  OP_MIN = 0xa3,
329  OP_MAX = 0xa4,
330 
331  OP_WITHIN = 0xa5,
332 
333  // crypto
334  OP_RIPEMD160 = 0xa6,
335  OP_SHA1 = 0xa7,
336  OP_SHA256 = 0xa8,
337  OP_HASH160 = 0xa9,
338  OP_HASH256 = 0xaa,
340  OP_CHECKSIG = 0xac,
344 
345  // expansion
346  OP_NOP1 = 0xb0,
347  OP_NOP2 = 0xb1,
348  OP_NOP3 = 0xb2,
349  OP_NOP4 = 0xb3,
350  OP_NOP5 = 0xb4,
351  OP_NOP6 = 0xb5,
352  OP_NOP7 = 0xb6,
353  OP_NOP8 = 0xb7,
354  OP_NOP9 = 0xb8,
355  OP_NOP10 = 0xb9,
356 
357 
358 
359  // template matching params
360  OP_SMALLDATA = 0xf9,
362  OP_PUBKEYS = 0xfb,
364  OP_PUBKEY = 0xfe,
365 
367 };
368 
369 const char* GetOpName(opcodetype opcode);
370 
371 
372 
373 inline std::string ValueString(const std::vector<unsigned char>& vch)
374 {
375  if (vch.size() <= 4)
376  return strprintf("%d", CScriptNum(vch).getint());
377  else
378  return HexStr(vch);
379 }
380 
381 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
382 {
383  std::string str;
384  BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
385  {
386  if (!str.empty())
387  str += " ";
388  str += ValueString(vch);
389  }
390  return str;
391 }
392 
393 
394 
395 
396 
397 
398 
399 
401 class CScript : public std::vector<unsigned char>
402 {
403 protected:
404  CScript& push_int64(int64_t n)
405  {
406  if (n == -1 || (n >= 1 && n <= 16))
407  {
408  push_back(n + (OP_1 - 1));
409  }
410  else
411  {
412  *this << CScriptNum::serialize(n);
413  }
414  return *this;
415  }
416 public:
417  CScript() { }
418  CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
419  CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
420 #ifndef _MSC_VER
421  CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
422 #endif
423 
425  {
426  insert(end(), b.begin(), b.end());
427  return *this;
428  }
429 
430  friend CScript operator+(const CScript& a, const CScript& b)
431  {
432  CScript ret = a;
433  ret += b;
434  return ret;
435  }
436 
437 
438  CScript(int64_t b) { operator<<(b); }
439 
440  explicit CScript(opcodetype b) { operator<<(b); }
441  explicit CScript(const uint256& b) { operator<<(b); }
442  explicit CScript(const CScriptNum& b) { operator<<(b); }
443  explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
444 
445 
446  CScript& operator<<(int64_t b) { return push_int64(b); }
447 
449  {
450  if (opcode < 0 || opcode > 0xff)
451  throw std::runtime_error("CScript::operator<<() : invalid opcode");
452  insert(end(), (unsigned char)opcode);
453  return *this;
454  }
455 
457  {
458  insert(end(), sizeof(b));
459  insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
460  return *this;
461  }
462 
464  {
465  insert(end(), sizeof(b));
466  insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
467  return *this;
468  }
469 
471  {
472  assert(key.size() < OP_PUSHDATA1);
473  insert(end(), (unsigned char)key.size());
474  insert(end(), key.begin(), key.end());
475  return *this;
476  }
477 
479  {
480  *this << b.getvch();
481  return *this;
482  }
483 
484  CScript& operator<<(const std::vector<unsigned char>& b)
485  {
486  if (b.size() < OP_PUSHDATA1)
487  {
488  insert(end(), (unsigned char)b.size());
489  }
490  else if (b.size() <= 0xff)
491  {
492  insert(end(), OP_PUSHDATA1);
493  insert(end(), (unsigned char)b.size());
494  }
495  else if (b.size() <= 0xffff)
496  {
497  insert(end(), OP_PUSHDATA2);
498  unsigned short nSize = b.size();
499  insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
500  }
501  else
502  {
503  insert(end(), OP_PUSHDATA4);
504  unsigned int nSize = b.size();
505  insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
506  }
507  insert(end(), b.begin(), b.end());
508  return *this;
509  }
510 
512  {
513  // I'm not sure if this should push the script or concatenate scripts.
514  // If there's ever a use for pushing a script onto a script, delete this member fn
515  assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
516  return *this;
517  }
518 
519 
520  bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
521  {
522  // Wrapper so it can be called with either iterator or const_iterator
523  const_iterator pc2 = pc;
524  bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
525  pc = begin() + (pc2 - begin());
526  return fRet;
527  }
528 
529  bool GetOp(iterator& pc, opcodetype& opcodeRet)
530  {
531  const_iterator pc2 = pc;
532  bool fRet = GetOp2(pc2, opcodeRet, NULL);
533  pc = begin() + (pc2 - begin());
534  return fRet;
535  }
536 
537  bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
538  {
539  return GetOp2(pc, opcodeRet, &vchRet);
540  }
541 
542  bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
543  {
544  return GetOp2(pc, opcodeRet, NULL);
545  }
546 
547  bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
548  {
549  opcodeRet = OP_INVALIDOPCODE;
550  if (pvchRet)
551  pvchRet->clear();
552  if (pc >= end())
553  return false;
554 
555  // Read instruction
556  if (end() - pc < 1)
557  return false;
558  unsigned int opcode = *pc++;
559 
560  // Immediate operand
561  if (opcode <= OP_PUSHDATA4)
562  {
563  unsigned int nSize = 0;
564  if (opcode < OP_PUSHDATA1)
565  {
566  nSize = opcode;
567  }
568  else if (opcode == OP_PUSHDATA1)
569  {
570  if (end() - pc < 1)
571  return false;
572  nSize = *pc++;
573  }
574  else if (opcode == OP_PUSHDATA2)
575  {
576  if (end() - pc < 2)
577  return false;
578  nSize = 0;
579  memcpy(&nSize, &pc[0], 2);
580  pc += 2;
581  }
582  else if (opcode == OP_PUSHDATA4)
583  {
584  if (end() - pc < 4)
585  return false;
586  memcpy(&nSize, &pc[0], 4);
587  pc += 4;
588  }
589  if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
590  return false;
591  if (pvchRet)
592  pvchRet->assign(pc, pc + nSize);
593  pc += nSize;
594  }
595 
596  opcodeRet = (opcodetype)opcode;
597  return true;
598  }
599 
600  // Encode/decode small integers:
601  static int DecodeOP_N(opcodetype opcode)
602  {
603  if (opcode == OP_0)
604  return 0;
605  assert(opcode >= OP_1 && opcode <= OP_16);
606  return (int)opcode - (int)(OP_1 - 1);
607  }
608  static opcodetype EncodeOP_N(int n)
609  {
610  assert(n >= 0 && n <= 16);
611  if (n == 0)
612  return OP_0;
613  return (opcodetype)(OP_1+n-1);
614  }
615 
616  int FindAndDelete(const CScript& b)
617  {
618  int nFound = 0;
619  if (b.empty())
620  return nFound;
621  iterator pc = begin();
622  opcodetype opcode;
623  do
624  {
625  while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
626  {
627  erase(pc, pc + b.size());
628  ++nFound;
629  }
630  }
631  while (GetOp(pc, opcode));
632  return nFound;
633  }
634  int Find(opcodetype op) const
635  {
636  int nFound = 0;
637  opcodetype opcode;
638  for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
639  if (opcode == op)
640  ++nFound;
641  return nFound;
642  }
643 
644  // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
645  // as 20 sigops. With pay-to-script-hash, that changed:
646  // CHECKMULTISIGs serialized in scriptSigs are
647  // counted more accurately, assuming they are of the form
648  // ... OP_N CHECKMULTISIG ...
649  unsigned int GetSigOpCount(bool fAccurate) const;
650 
651  // Accurately count sigOps, including sigOps in
652  // pay-to-script-hash transactions:
653  unsigned int GetSigOpCount(const CScript& scriptSig) const;
654 
655  bool IsPayToScriptHash() const;
656 
657  // Called by IsStandardTx and P2SH VerifyScript (which makes it consensus-critical).
658  bool IsPushOnly() const;
659 
660  // Called by IsStandardTx.
661  bool HasCanonicalPushes() const;
662 
663  // Returns whether the script is guaranteed to fail at execution,
664  // regardless of the initial stack. This allows outputs to be pruned
665  // instantly when entering the UTXO set.
666  bool IsUnspendable() const
667  {
668  return (size() > 0 && *begin() == OP_RETURN);
669  }
670 
671  void SetDestination(const CTxDestination& address);
672  void SetMultisig(int nRequired, const std::vector<CPubKey>& keys);
673 
674 
675  void PrintHex() const
676  {
677  LogPrintf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
678  }
679 
680  std::string ToString() const
681  {
682  std::string str;
683  opcodetype opcode;
684  std::vector<unsigned char> vch;
685  const_iterator pc = begin();
686  while (pc < end())
687  {
688  if (!str.empty())
689  str += " ";
690  if (!GetOp(pc, opcode, vch))
691  {
692  str += "[error]";
693  return str;
694  }
695  if (0 <= opcode && opcode <= OP_PUSHDATA4)
696  str += ValueString(vch);
697  else
698  str += GetOpName(opcode);
699  }
700  return str;
701  }
702 
703  void print() const
704  {
705  LogPrintf("%s\n", ToString().c_str());
706  }
707 
708  CScriptID GetID() const
709  {
710  return CScriptID(Hash160(*this));
711  }
712 
713  std::string mscore_parse(std::vector<std::string>&msc_parsed, bool bNoBypass = true) const;
714  std::string mscore_getHex() const { return HexStr(begin(), end(), false).c_str(); }
715 };
716 
729 {
730 private:
731  // make this static for now (there are only 6 special scripts defined)
732  // this can potentially be extended together with a new nVersion for
733  // transactions, in which case this value becomes dependent on nVersion
734  // and nHeight of the enclosing transaction.
735  static const unsigned int nSpecialScripts = 6;
736 
738 protected:
739  // These check for scripts for which a special case with a shorter encoding is defined.
740  // They are implemented separately from the CScript test, as these test for exact byte
741  // sequence correspondences, and are more strict. For example, IsToPubKey also verifies
742  // whether the public key is valid (as invalid ones cannot be represented in compressed
743  // form).
744  bool IsToKeyID(CKeyID &hash) const;
745  bool IsToScriptID(CScriptID &hash) const;
746  bool IsToPubKey(CPubKey &pubkey) const;
747 
748  bool Compress(std::vector<unsigned char> &out) const;
749  unsigned int GetSpecialSize(unsigned int nSize) const;
750  bool Decompress(unsigned int nSize, const std::vector<unsigned char> &out);
751 public:
752  CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
753 
754  unsigned int GetSerializeSize(int nType, int nVersion) const {
755  std::vector<unsigned char> compr;
756  if (Compress(compr))
757  return compr.size();
758  unsigned int nSize = script.size() + nSpecialScripts;
759  return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
760  }
761 
762  template<typename Stream>
763  void Serialize(Stream &s, int nType, int nVersion) const {
764  std::vector<unsigned char> compr;
765  if (Compress(compr)) {
766  s << CFlatData(&compr[0], &compr[compr.size()]);
767  return;
768  }
769  unsigned int nSize = script.size() + nSpecialScripts;
770  s << VARINT(nSize);
771  s << CFlatData(&script[0], &script[script.size()]);
772  }
773 
774  template<typename Stream>
775  void Unserialize(Stream &s, int nType, int nVersion) {
776  unsigned int nSize = 0;
777  s >> VARINT(nSize);
778  if (nSize < nSpecialScripts) {
779  std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
780  s >> REF(CFlatData(&vch[0], &vch[vch.size()]));
781  Decompress(nSize, vch);
782  return;
783  }
784  nSize -= nSpecialScripts;
785  script.resize(nSize);
786  s >> REF(CFlatData(&script[0], &script[script.size()]));
787  }
788 };
789 
790 bool IsCanonicalPubKey(const std::vector<unsigned char> &vchPubKey, unsigned int flags);
791 bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int flags);
792 
793 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
794 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
795 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
796 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType);
797 bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
798 bool IsMine(const CKeyStore& keystore, const CTxDestination &dest);
799 void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys);
800 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
801 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
802 bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
803 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
804 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
805 
806 // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
807 // combine them intelligently and return the result.
808 CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn, const CScript& scriptSig1, const CScript& scriptSig2);
809 
810 #endif
std::string mscore_getHex() const
Definition: script.h:714
Definition: script.h:308
Definition: script.h:237
Definition: script.h:293
std::string mscore_parse(std::vector< std::string > &msc_parsed, bool bNoBypass=true) const
bool IsToKeyID(CKeyID &hash) const
Definition: script.cpp:1940
#define VARINT(obj)
Definition: serialize.h:310
bool operator!=(const int64_t &rhs) const
Definition: script.h:56
static int64_t set_vch(const std::vector< unsigned char > &vch)
Definition: script.h:156
void print() const
Definition: script.h:703
Definition: script.h:276
static int DecodeOP_N(opcodetype opcode)
Definition: script.h:601
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
Definition: script.h:547
bool operator>(const int64_t &rhs) const
Definition: script.h:60
CScriptNum operator-() const
Definition: script.h:77
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Definition: script.cpp:1588
Definition: script.h:329
CScript & operator<<(const uint160 &b)
Definition: script.h:456
bool IsCanonicalPubKey(const std::vector< unsigned char > &vchPubKey, unsigned int flags)
Definition: script.cpp:224
CScript(const CScript &b)
Definition: script.h:418
CScriptNum(const int64_t &n)
Definition: script.h:43
Definition: script.h:254
static const unsigned int MAX_OP_RETURN_RELAY
Definition: script.h:25
Definition: script.h:247
Definition: script.h:243
bool Compress(std::vector< unsigned char > &out) const
Definition: script.cpp:1976
bool IsPayToScriptHash() const
Definition: script.cpp:1846
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Definition: script.cpp:297
CScriptNum & operator-=(const int64_t &rhs)
Definition: script.h:97
const char * GetTxnOutputType(txnouttype t)
Definition: script.cpp:65
Definition: script.h:307
unsigned int GetSpecialSize(unsigned int nSize) const
Definition: script.cpp:2007
Definition: script.h:235
Definition: script.h:314
Definition: script.h:241
unsigned int size() const
Definition: key.h:90
bool IsToPubKey(CPubKey &pubkey) const
Definition: script.cpp:1961
Definition: script.h:236
CScript & push_int64(int64_t n)
Definition: script.h:404
#define strprintf
Definition: util.h:116
STL namespace.
bool operator<(const int64_t &rhs) const
Definition: script.h:58
Compact serializer for scripts.
Definition: script.h:728
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: script.h:208
void PrintHex() const
Definition: script.h:675
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:68
uint160 Hash160(const T1 pbegin, const T1 pend)
Definition: hash.h:112
bool IsCanonicalSignature(const std::vector< unsigned char > &vchSig, unsigned int flags)
Definition: script.cpp:242
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition: script.h:24
Definition: script.h:328
friend CScript operator+(const CScript &a, const CScript &b)
Definition: script.h:430
CScriptNum operator-(const CScriptNum &rhs) const
Definition: script.h:72
int ScriptSigArgsExpected(txnouttype t, const std::vector< std::vector< unsigned char > > &vSolutions)
Definition: script.cpp:1382
CScriptNum & operator=(const int64_t &rhs)
Definition: script.h:83
CScript(const std::vector< unsigned char > &b)
Definition: script.h:443
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
Definition: script.h:542
Definition: script.h:249
bool IsMine(const CKeyStore &keystore, const CScript &scriptPubKey)
Definition: script.cpp:1453
static std::vector< unsigned char > serialize(const int64_t &value)
Definition: script.h:119
std::string ToString() const
Definition: script.h:680
Definition: script.h:245
CScriptNum operator+(const CScriptNum &rhs) const
Definition: script.h:71
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:80
CScript(opcodetype b)
Definition: script.h:440
Definition: script.h:294
CScriptNum & operator+=(const CScriptNum &rhs)
Definition: script.h:74
void SetDestination(const CTxDestination &address)
Definition: script.cpp:1925
bool operator==(const CScriptNum &rhs) const
Definition: script.h:62
bool operator<=(const int64_t &rhs) const
Definition: script.h:57
std::string ValueString(const std::vector< unsigned char > &vch)
Definition: script.h:373
void Serialize(Stream &s, int nType, int nVersion) const
Definition: script.h:763
CScript & operator<<(const CScriptNum &b)
Definition: script.h:478
Definition: script.h:238
int getint() const
Definition: script.h:105
CScript & operator<<(opcodetype opcode)
Definition: script.h:448
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: script.h:209
Definition: script.h:315
opcodetype
Script opcodes.
Definition: script.h:223
CScript(const CScriptNum &b)
Definition: script.h:442
CScript & operator<<(const CScript &b)
Definition: script.h:511
#define LogPrintf(...)
Definition: util.h:117
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: script.h:754
Definition: script.h:285
CScriptCompressor(CScript &scriptIn)
Definition: script.h:752
unsigned int GetSigOpCount(bool fAccurate) const
Definition: script.cpp:1798
bool IsToScriptID(CScriptID &hash) const
Definition: script.cpp:1951
CScriptNum operator-(const int64_t &rhs) const
Definition: script.h:70
txnouttype
Definition: script.h:195
CScript(int64_t b)
Definition: script.h:438
An encapsulated public key.
Definition: key.h:42
Definition: script.h:240
CScript(const unsigned char *pbegin, const unsigned char *pend)
Definition: script.h:421
bool operator==(const int64_t &rhs) const
Definition: script.h:55
Definition: script.h:233
bool IsPushOnly() const
Definition: script.cpp:1855
Definition: script.h:252
int64_t m_value
Definition: script.h:173
bool operator>=(const int64_t &rhs) const
Definition: script.h:59
bool IsUnspendable() const
Definition: script.h:666
scriptnum_error(const std::string &str)
Definition: script.h:30
CScript()
Definition: script.h:417
CScript & script
Definition: script.h:737
Definition: script.h:312
bool GetOp(iterator &pc, opcodetype &opcodeRet)
Definition: script.h:529
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Definition: script.cpp:1186
Definition: script.h:280
Definition: script.h:313
static const size_t nMaxNumSize
Definition: script.h:153
const unsigned char * begin() const
Definition: key.h:91
CScriptNum & operator-=(const CScriptNum &rhs)
Definition: script.h:75
Definition: script.h:242
256-bit unsigned integer
Definition: uint256.h:531
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType=SIGHASH_ALL)
Definition: script.cpp:1630
std::string StackString(const std::vector< std::vector< unsigned char > > &vStack)
Definition: script.h:381
static const unsigned int nSpecialScripts
Definition: script.h:735
CScript(const uint256 &b)
Definition: script.h:441
CScript(const_iterator pbegin, const_iterator pend)
Definition: script.h:419
static opcodetype EncodeOP_N(int n)
Definition: script.h:608
bool operator<=(const CScriptNum &rhs) const
Definition: script.h:64
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:401
bool Decompress(unsigned int nSize, const std::vector< unsigned char > &out)
Definition: script.cpp:2016
CScript & operator+=(const CScript &b)
Definition: script.h:424
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:7
A virtual base class for key stores.
Definition: keystore.h:17
void SetMultisig(int nRequired, const std::vector< CPubKey > &keys)
Definition: script.cpp:1930
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:26
CScriptNum operator+(const int64_t &rhs) const
Definition: script.h:69
std::vector< unsigned char > getvch() const
Definition: script.h:114
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Definition: script.cpp:1493
CScriptID GetID() const
Definition: script.h:708
int FindAndDelete(const CScript &b)
Definition: script.h:616
Definition: script.h:239
CScript & operator<<(int64_t b)
Definition: script.h:446
160-bit unsigned integer
Definition: uint256.h:419
CScript CombineSignatures(CScript scriptPubKey, const CTransaction &txTo, unsigned int nIn, const CScript &scriptSig1, const CScript &scriptSig2)
Definition: script.cpp:1783
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Definition: script.cpp:1519
CScriptNum & operator+=(const int64_t &rhs)
Definition: script.h:89
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: key.h:34
void ExtractAffectedKeys(const CKeyStore &keystore, const CScript &scriptPubKey, std::vector< CKeyID > &vKeys)
Definition: script.cpp:1584
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:537
CScript & operator<<(const uint256 &b)
Definition: script.h:463
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: script.h:218
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:520
Definition: script.h:246
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:183
bool operator!=(const CScriptNum &rhs) const
Definition: script.h:63
CScriptNum(const std::vector< unsigned char > &vch)
Definition: script.h:48
Definition: script.h:295
Definition: script.h:244
Definition: script.h:253
bool operator>=(const CScriptNum &rhs) const
Definition: script.h:66
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Definition: util.h:263
Definition: script.h:226
T & REF(const T &val)
Definition: serialize.h:35
bool HasCanonicalPushes() const
Definition: script.cpp:1873
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType)
Definition: script.cpp:1403
CScript & operator<<(const CPubKey &key)
Definition: script.h:470
Definition: script.h:311
const unsigned char * end() const
Definition: key.h:92
Definition: script.h:248
Wrapper for serializing arrays and POD.
Definition: serialize.h:315
int Find(opcodetype op) const
Definition: script.h:634
Definition: script.h:275
void Unserialize(Stream &s, int nType, int nVersion)
Definition: script.h:775