Anoncoin  0.9.4
P2P Digital Currency
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 // Copyright (c) 2013-2014 The Anoncoin Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef H_ANONCOIN_SCRIPT
8 #define H_ANONCOIN_SCRIPT
9 
10 #include "key.h"
11 #include "util.h"
12 
13 #include <stdexcept>
14 #include <stdint.h>
15 #include <string>
16 #include <vector>
17 
18 #include <boost/foreach.hpp>
19 #include <boost/variant.hpp>
20 
21 class CCoins;
22 class CKeyStore;
23 class CTransaction;
24 
25 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520; // bytes
26 static const unsigned int MAX_OP_RETURN_RELAY = 40; // bytes
27 
28 class scriptnum_error : public std::runtime_error
29 {
30 public:
31  explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
32 };
33 
35 {
36 // Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
37 // The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
38 // but results may overflow (and are valid as long as they are not used in a subsequent
39 // numeric operation). CScriptNum enforces those semantics by storing results as
40 // an int64 and allowing out-of-range values to be returned as a vector of bytes but
41 // throwing an exception if arithmetic is done or the result is interpreted as an integer.
42 public:
43 
44  explicit CScriptNum(const int64_t& n)
45  {
46  m_value = n;
47  }
48 
49  explicit CScriptNum(const std::vector<unsigned char>& vch)
50  {
51  if (vch.size() > nMaxNumSize)
52  throw scriptnum_error("CScriptNum(const std::vector<unsigned char>&) : overflow");
53  m_value = set_vch(vch);
54  }
55 
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  inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
62 
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  inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
69 
70  inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);}
71  inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);}
72  inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); }
73  inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); }
74 
75  inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); }
76  inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); }
77 
78  inline CScriptNum operator-() const
79  {
80  assert(m_value != std::numeric_limits<int64_t>::min());
81  return CScriptNum(-m_value);
82  }
83 
84  inline CScriptNum& operator=( const int64_t& rhs)
85  {
86  m_value = rhs;
87  return *this;
88  }
89 
90  inline CScriptNum& operator+=( const int64_t& rhs)
91  {
92  assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
93  (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
94  m_value += rhs;
95  return *this;
96  }
97 
98  inline CScriptNum& operator-=( const int64_t& rhs)
99  {
100  assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
101  (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
102  m_value -= rhs;
103  return *this;
104  }
105 
106  int getint() const
107  {
108  if (m_value > std::numeric_limits<int>::max())
109  return std::numeric_limits<int>::max();
110  else if (m_value < std::numeric_limits<int>::min())
111  return std::numeric_limits<int>::min();
112  return m_value;
113  }
114 
115  std::vector<unsigned char> getvch() const
116  {
117  return serialize(m_value);
118  }
119 
120  static std::vector<unsigned char> serialize(const int64_t& value)
121  {
122  if(value == 0)
123  return std::vector<unsigned char>();
124 
125  std::vector<unsigned char> result;
126  const bool neg = value < 0;
127  uint64_t absvalue = neg ? -value : value;
128 
129  while(absvalue)
130  {
131  result.push_back(absvalue & 0xff);
132  absvalue >>= 8;
133  }
134 
135 
136 // - If the most significant byte is >= 0x80 and the value is positive, push a
137 // new zero-byte to make the significant byte < 0x80 again.
138 
139 // - If the most significant byte is >= 0x80 and the value is negative, push a
140 // new 0x80 byte that will be popped off when converting to an integral.
141 
142 // - If the most significant byte is < 0x80 and the value is negative, add
143 // 0x80 to it, since it will be subtracted and interpreted as a negative when
144 // converting to an integral.
145 
146  if (result.back() & 0x80)
147  result.push_back(neg ? 0x80 : 0);
148  else if (neg)
149  result.back() |= 0x80;
150 
151  return result;
152  }
153 
154  static const size_t nMaxNumSize = 4;
155 
156 private:
157  static int64_t set_vch(const std::vector<unsigned char>& vch)
158  {
159  if (vch.empty())
160  return 0;
161 
162  int64_t result = 0;
163  for (size_t i = 0; i != vch.size(); ++i)
164  result |= static_cast<int64_t>(vch[i]) << 8*i;
165 
166  // If the input vector's most significant byte is 0x80, remove it from
167  // the result's msb and return a negative.
168  if (vch.back() & 0x80)
169  return -(result & ~(0x80 << (8 * (vch.size() - 1))));
170 
171  return result;
172  }
173 
174  int64_t m_value;
175 };
176 
178 enum
179 {
184 };
185 
187 enum
188 {
190  SCRIPT_VERIFY_P2SH = (1U << 0), // evaluate P2SH (BIP16) subscripts
191  SCRIPT_VERIFY_STRICTENC = (1U << 1), // enforce strict conformance to DER and SEC2 for signatures and pubkeys
192  SCRIPT_VERIFY_EVEN_S = (1U << 2), // enforce even S values in signatures (depends on STRICTENC)
193  SCRIPT_VERIFY_NOCACHE = (1U << 3), // do not store results in signature cache (but do query it)
194 };
195 
198 {
203 };
205 typedef uint8_t isminefilter;
206 
208 {
210  // 'standard' transaction types:
216 };
217 
219 public:
220  friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
221  friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
222 };
223 
230 typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
231 
232 const char* GetTxnOutputType(txnouttype t);
233 
236 {
237  // push value
238  OP_0 = 0x00,
240  OP_PUSHDATA1 = 0x4c,
241  OP_PUSHDATA2 = 0x4d,
242  OP_PUSHDATA4 = 0x4e,
243  OP_1NEGATE = 0x4f,
244  OP_RESERVED = 0x50,
245  OP_1 = 0x51,
247  OP_2 = 0x52,
248  OP_3 = 0x53,
249  OP_4 = 0x54,
250  OP_5 = 0x55,
251  OP_6 = 0x56,
252  OP_7 = 0x57,
253  OP_8 = 0x58,
254  OP_9 = 0x59,
255  OP_10 = 0x5a,
256  OP_11 = 0x5b,
257  OP_12 = 0x5c,
258  OP_13 = 0x5d,
259  OP_14 = 0x5e,
260  OP_15 = 0x5f,
261  OP_16 = 0x60,
262 
263  // control
264  OP_NOP = 0x61,
265  OP_VER = 0x62,
266  OP_IF = 0x63,
267  OP_NOTIF = 0x64,
268  OP_VERIF = 0x65,
269  OP_VERNOTIF = 0x66,
270  OP_ELSE = 0x67,
271  OP_ENDIF = 0x68,
272  OP_VERIFY = 0x69,
273  OP_RETURN = 0x6a,
274 
275  // stack ops
278  OP_2DROP = 0x6d,
279  OP_2DUP = 0x6e,
280  OP_3DUP = 0x6f,
281  OP_2OVER = 0x70,
282  OP_2ROT = 0x71,
283  OP_2SWAP = 0x72,
284  OP_IFDUP = 0x73,
285  OP_DEPTH = 0x74,
286  OP_DROP = 0x75,
287  OP_DUP = 0x76,
288  OP_NIP = 0x77,
289  OP_OVER = 0x78,
290  OP_PICK = 0x79,
291  OP_ROLL = 0x7a,
292  OP_ROT = 0x7b,
293  OP_SWAP = 0x7c,
294  OP_TUCK = 0x7d,
295 
296  // splice ops
297  OP_CAT = 0x7e,
298  OP_SUBSTR = 0x7f,
299  OP_LEFT = 0x80,
300  OP_RIGHT = 0x81,
301  OP_SIZE = 0x82,
302 
303  // bit logic
304  OP_INVERT = 0x83,
305  OP_AND = 0x84,
306  OP_OR = 0x85,
307  OP_XOR = 0x86,
308  OP_EQUAL = 0x87,
310  OP_RESERVED1 = 0x89,
311  OP_RESERVED2 = 0x8a,
312 
313  // numeric
314  OP_1ADD = 0x8b,
315  OP_1SUB = 0x8c,
316  OP_2MUL = 0x8d,
317  OP_2DIV = 0x8e,
318  OP_NEGATE = 0x8f,
319  OP_ABS = 0x90,
320  OP_NOT = 0x91,
321  OP_0NOTEQUAL = 0x92,
322 
323  OP_ADD = 0x93,
324  OP_SUB = 0x94,
325  OP_MUL = 0x95,
326  OP_DIV = 0x96,
327  OP_MOD = 0x97,
328  OP_LSHIFT = 0x98,
329  OP_RSHIFT = 0x99,
330 
331  OP_BOOLAND = 0x9a,
332  OP_BOOLOR = 0x9b,
333  OP_NUMEQUAL = 0x9c,
336  OP_LESSTHAN = 0x9f,
340  OP_MIN = 0xa3,
341  OP_MAX = 0xa4,
342 
343  OP_WITHIN = 0xa5,
344 
345  // crypto
346  OP_RIPEMD160 = 0xa6,
347  OP_SHA1 = 0xa7,
348  OP_SHA256 = 0xa8,
349  OP_HASH160 = 0xa9,
350  OP_HASH256 = 0xaa,
352  OP_CHECKSIG = 0xac,
356 
357  // expansion
358  OP_NOP1 = 0xb0,
359  OP_NOP2 = 0xb1,
360  OP_NOP3 = 0xb2,
361  OP_NOP4 = 0xb3,
362  OP_NOP5 = 0xb4,
363  OP_NOP6 = 0xb5,
364  OP_NOP7 = 0xb6,
365  OP_NOP8 = 0xb7,
366  OP_NOP9 = 0xb8,
367  OP_NOP10 = 0xb9,
368 
369 
370 
371  // template matching params
372  OP_SMALLDATA = 0xf9,
374  OP_PUBKEYS = 0xfb,
376  OP_PUBKEY = 0xfe,
377 
379 };
380 
381 const char* GetOpName(opcodetype opcode);
382 
383 
384 
385 inline std::string ValueString(const std::vector<unsigned char>& vch)
386 {
387  if (vch.size() <= 4)
388  return strprintf("%d", CScriptNum(vch).getint());
389  else
390  return HexStr(vch);
391 }
392 
393 inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
394 {
395  std::string str;
396  BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
397  {
398  if (!str.empty())
399  str += " ";
400  str += ValueString(vch);
401  }
402  return str;
403 }
404 
405 
406 
407 
408 
409 
410 
411 
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 
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 
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 
469  {
470  insert(end(), sizeof(b));
471  insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
472  return *this;
473  }
474 
476  {
477  insert(end(), sizeof(b));
478  insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
479  return *this;
480  }
481 
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 
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 
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 
687  void PrintHex() const
688  {
689  LogPrintf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
690  }
691 
692  std::string ToString() const
693  {
694  std::string str;
695  opcodetype opcode;
696  std::vector<unsigned char> vch;
697  const_iterator pc = begin();
698  while (pc < end())
699  {
700  if (!str.empty())
701  str += " ";
702  if (!GetOp(pc, opcode, vch))
703  {
704  str += "[error]";
705  return str;
706  }
707  if (0 <= opcode && opcode <= OP_PUSHDATA4)
708  str += ValueString(vch);
709  else
710  str += GetOpName(opcode);
711  }
712  return str;
713  }
714 
715  void print() const
716  {
717  LogPrintf("%s\n", ToString().c_str());
718  }
719 
720  CScriptID GetID() const
721  {
722  return CScriptID(Hash160(*this));
723  }
724 };
725 
738 {
739 private:
740  // make this static for now (there are only 6 special scripts defined)
741  // this can potentially be extended together with a new nVersion for
742  // transactions, in which case this value becomes dependent on nVersion
743  // and nHeight of the enclosing transaction.
744  static const unsigned int nSpecialScripts = 6;
745 
747 protected:
748  // These check for scripts for which a special case with a shorter encoding is defined.
749  // They are implemented separately from the CScript test, as these test for exact byte
750  // sequence correspondences, and are more strict. For example, IsToPubKey also verifies
751  // whether the public key is valid (as invalid ones cannot be represented in compressed
752  // form).
753  bool IsToKeyID(CKeyID &hash) const;
754  bool IsToScriptID(CScriptID &hash) const;
755  bool IsToPubKey(CPubKey &pubkey) const;
756 
757  bool Compress(std::vector<unsigned char> &out) const;
758  unsigned int GetSpecialSize(unsigned int nSize) const;
759  bool Decompress(unsigned int nSize, const std::vector<unsigned char> &out);
760 public:
761  CScriptCompressor(CScript &scriptIn) : script(scriptIn) { }
762 
763  unsigned int GetSerializeSize(int nType, int nVersion) const {
764  std::vector<unsigned char> compr;
765  if (Compress(compr))
766  return compr.size();
767  unsigned int nSize = script.size() + nSpecialScripts;
768  return script.size() + VARINT(nSize).GetSerializeSize(nType, nVersion);
769  }
770 
771  template<typename Stream>
772  void Serialize(Stream &s, int nType, int nVersion) const {
773  std::vector<unsigned char> compr;
774  if (Compress(compr)) {
775  s << CFlatData(&compr[0], &compr[compr.size()]);
776  return;
777  }
778  unsigned int nSize = script.size() + nSpecialScripts;
779  s << VARINT(nSize);
780  s << CFlatData(&script[0], &script[script.size()]);
781  }
782 
783  template<typename Stream>
784  void Unserialize(Stream &s, int nType, int nVersion) {
785  unsigned int nSize = 0;
786  s >> VARINT(nSize);
787  if (nSize < nSpecialScripts) {
788  std::vector<unsigned char> vch(GetSpecialSize(nSize), 0x00);
789  s >> REF(CFlatData(&vch[0], &vch[vch.size()]));
790  Decompress(nSize, vch);
791  return;
792  }
793  nSize -= nSpecialScripts;
794  script.resize(nSize);
795  s >> REF(CFlatData(&script[0], &script[script.size()]));
796  }
797 };
798 
799 bool IsCanonicalPubKey(const std::vector<unsigned char> &vchPubKey, unsigned int flags);
800 bool IsCanonicalSignature(const std::vector<unsigned char> &vchSig, unsigned int flags);
801 
802 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
803 bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
804 int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
805 bool IsStandard(const CScript& scriptPubKey, txnouttype& whichType);
806 isminetype IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
807 isminetype IsMine(const CKeyStore& keystore, const CTxDestination& dest);
808 void ExtractAffectedKeys(const CKeyStore &keystore, const CScript& scriptPubKey, std::vector<CKeyID> &vKeys);
809 bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
810 bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
811 bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
812 bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
813 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn, unsigned int flags, int nHashType);
814 
815 // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
816 // combine them intelligently and return the result.
817 CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn, const CScript& scriptSig1, const CScript& scriptSig2);
818 
819 #endif
Definition: script.h:320
Definition: script.h:249
Definition: script.h:305
bool IsToKeyID(CKeyID &hash) const
Definition: script.cpp:1960
#define VARINT(obj)
Definition: serialize.h:319
bool operator!=(const int64_t &rhs) const
Definition: script.h:57
static int64_t set_vch(const std::vector< unsigned char > &vch)
Definition: script.h:157
void print() const
Definition: script.h:715
Definition: script.h:288
static int DecodeOP_N(opcodetype opcode)
Definition: script.h:613
bool GetOp2(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > *pvchRet) const
Definition: script.h:559
bool operator>(const int64_t &rhs) const
Definition: script.h:61
CScriptNum operator-() const
Definition: script.h:78
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CTransaction &txTo, unsigned int nIn, unsigned int flags, int nHashType)
Definition: script.cpp:1608
Definition: script.h:341
CScript & operator<<(const uint160 &b)
Definition: script.h:468
bool IsCanonicalPubKey(const std::vector< unsigned char > &vchPubKey, unsigned int flags)
Definition: script.cpp:225
CScript(const CScript &b)
Definition: script.h:430
CScriptNum(const int64_t &n)
Definition: script.h:44
#define strprintf
Definition: tinyformat.h:1011
Definition: script.h:266
Definition: script.h:259
Definition: script.h:255
bool Compress(std::vector< unsigned char > &out) const
Definition: script.cpp:1996
bool IsPayToScriptHash() const
Definition: script.cpp:1866
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:298
CScriptNum & operator-=(const int64_t &rhs)
Definition: script.h:98
const char * GetTxnOutputType(txnouttype t)
Definition: script.cpp:66
Definition: script.h:319
unsigned int GetSpecialSize(unsigned int nSize) const
Definition: script.cpp:2027
Definition: script.h:247
Definition: script.h:326
Definition: script.h:253
unsigned int size() const
Definition: key.h:91
bool IsToPubKey(CPubKey &pubkey) const
Definition: script.cpp:1981
Definition: script.h:248
CScript & push_int64(int64_t n)
Definition: script.h:416
bool operator<(const int64_t &rhs) const
Definition: script.h:59
Compact serializer for scripts.
Definition: script.h:737
friend bool operator==(const CNoDestination &a, const CNoDestination &b)
Definition: script.h:220
void PrintHex() const
Definition: script.h:687
pruned version of CTransaction: only retains metadata and unspent transaction outputs ...
Definition: coins.h:69
uint160 Hash160(const T1 pbegin, const T1 pend)
Definition: hash.h:113
bool IsCanonicalSignature(const std::vector< unsigned char > &vchSig, unsigned int flags)
Definition: script.cpp:243
Definition: script.h:340
friend CScript operator+(const CScript &a, const CScript &b)
Definition: script.h:442
CScriptNum operator-(const CScriptNum &rhs) const
Definition: script.h:73
int ScriptSigArgsExpected(txnouttype t, const std::vector< std::vector< unsigned char > > &vSolutions)
Definition: script.cpp:1384
CScriptNum & operator=(const int64_t &rhs)
Definition: script.h:84
CScript(const std::vector< unsigned char > &b)
Definition: script.h:455
bool GetOp(const_iterator &pc, opcodetype &opcodeRet) const
Definition: script.h:554
Definition: script.h:261
static std::vector< unsigned char > serialize(const int64_t &value)
Definition: script.h:120
std::string ToString() const
Definition: script.h:692
Definition: script.h:257
CScriptNum operator+(const CScriptNum &rhs) const
Definition: script.h:72
const char * GetOpName(opcodetype opcode)
Definition: script.cpp:81
CScript(opcodetype b)
Definition: script.h:452
Definition: script.h:306
CScriptNum & operator+=(const CScriptNum &rhs)
Definition: script.h:75
void SetDestination(const CTxDestination &address)
Definition: script.cpp:1945
bool operator==(const CScriptNum &rhs) const
Definition: script.h:63
bool operator<=(const int64_t &rhs) const
Definition: script.h:58
isminetype
IsMine() return codes.
Definition: script.h:197
std::string ValueString(const std::vector< unsigned char > &vch)
Definition: script.h:385
void Serialize(Stream &s, int nType, int nVersion) const
Definition: script.h:772
CScript & operator<<(const CScriptNum &b)
Definition: script.h:490
Definition: script.h:250
int getint() const
Definition: script.h:106
CScript & operator<<(opcodetype opcode)
Definition: script.h:460
friend bool operator<(const CNoDestination &a, const CNoDestination &b)
Definition: script.h:221
Definition: script.h:327
opcodetype
Script opcodes.
Definition: script.h:235
CScript(const CScriptNum &b)
Definition: script.h:454
CScript & operator<<(const CScript &b)
Definition: script.h:523
#define LogPrintf(...)
Definition: util.h:118
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: script.h:763
Definition: script.h:297
CScriptCompressor(CScript &scriptIn)
Definition: script.h:761
unsigned int GetSigOpCount(bool fAccurate) const
Definition: script.cpp:1818
bool IsToScriptID(CScriptID &hash) const
Definition: script.cpp:1971
CScriptNum operator-(const int64_t &rhs) const
Definition: script.h:71
txnouttype
Definition: script.h:207
CScript(int64_t b)
Definition: script.h:450
An encapsulated public key.
Definition: key.h:43
Definition: script.h:252
CScript(const unsigned char *pbegin, const unsigned char *pend)
Definition: script.h:433
bool operator==(const int64_t &rhs) const
Definition: script.h:56
Definition: script.h:245
bool IsPushOnly() const
Definition: script.cpp:1875
Definition: script.h:264
int64_t m_value
Definition: script.h:174
bool operator>=(const int64_t &rhs) const
Definition: script.h:60
bool IsUnspendable() const
Definition: script.h:678
scriptnum_error(const std::string &str)
Definition: script.h:31
CScript()
Definition: script.h:429
CScript & script
Definition: script.h:746
Definition: script.h:324
bool GetOp(iterator &pc, opcodetype &opcodeRet)
Definition: script.h:541
bool Solver(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< std::vector< unsigned char > > &vSolutionsRet)
Definition: script.cpp:1187
Definition: script.h:292
uint8_t isminefilter
used for bitflags of isminetype
Definition: script.h:205
Definition: script.h:325
static const size_t nMaxNumSize
Definition: script.h:154
const unsigned char * begin() const
Definition: key.h:92
CScriptNum & operator-=(const CScriptNum &rhs)
Definition: script.h:76
Definition: script.h:254
256-bit unsigned integer
Definition: uint256.h:532
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType=SIGHASH_ALL)
Definition: script.cpp:1650
std::string StackString(const std::vector< std::vector< unsigned char > > &vStack)
Definition: script.h:393
static const unsigned int nSpecialScripts
Definition: script.h:744
CScript(const uint256 &b)
Definition: script.h:453
CScript(const_iterator pbegin, const_iterator pend)
Definition: script.h:431
static opcodetype EncodeOP_N(int n)
Definition: script.h:620
bool operator<=(const CScriptNum &rhs) const
Definition: script.h:65
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:413
bool Decompress(unsigned int nSize, const std::vector< unsigned char > &out)
Definition: script.cpp:2036
CScript & operator+=(const CScript &b)
Definition: script.h:436
void * memcpy(void *a, const void *b, size_t c)
A virtual base class for key stores.
Definition: keystore.h:28
void SetMultisig(int nRequired, const std::vector< CPubKey > &keys)
Definition: script.cpp:1950
isminetype IsMine(const CKeyStore &keystore, const CScript &scriptPubKey)
Definition: script.cpp:1457
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:27
CScriptNum operator+(const int64_t &rhs) const
Definition: script.h:70
std::vector< unsigned char > getvch() const
Definition: script.h:115
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
Definition: script.cpp:1513
CScriptID GetID() const
Definition: script.h:720
int FindAndDelete(const CScript &b)
Definition: script.h:628
Definition: script.h:251
CScript & operator<<(int64_t b)
Definition: script.h:458
160-bit unsigned integer
Definition: uint256.h:420
CScript CombineSignatures(CScript scriptPubKey, const CTransaction &txTo, unsigned int nIn, const CScript &scriptSig1, const CScript &scriptSig2)
Definition: script.cpp:1803
bool ExtractDestinations(const CScript &scriptPubKey, txnouttype &typeRet, std::vector< CTxDestination > &addressRet, int &nRequiredRet)
Definition: script.cpp:1539
CScriptNum & operator+=(const int64_t &rhs)
Definition: script.h:90
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: key.h:35
void ExtractAffectedKeys(const CKeyStore &keystore, const CScript &scriptPubKey, std::vector< CKeyID > &vKeys)
Definition: script.cpp:1604
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet) const
Definition: script.h:549
CScript & operator<<(const uint256 &b)
Definition: script.h:475
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: script.h:230
bool GetOp(iterator &pc, opcodetype &opcodeRet, std::vector< unsigned char > &vchRet)
Definition: script.h:532
Definition: script.h:258
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: core.h:179
bool operator!=(const CScriptNum &rhs) const
Definition: script.h:64
CScriptNum(const std::vector< unsigned char > &vch)
Definition: script.h:49
Definition: script.h:307
Definition: script.h:256
Definition: script.h:265
bool operator>=(const CScriptNum &rhs) const
Definition: script.h:67
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
Definition: util.h:256
Definition: script.h:238
T & REF(const T &val)
Definition: serialize.h:41
bool HasCanonicalPushes() const
Definition: script.cpp:1893
bool IsStandard(const CScript &scriptPubKey, txnouttype &whichType)
Definition: script.cpp:1405
CScript & operator<<(const CPubKey &key)
Definition: script.h:482
Definition: script.h:323
const unsigned char * end() const
Definition: key.h:93
Definition: script.h:260
Wrapper for serializing arrays and POD.
Definition: serialize.h:324
int Find(opcodetype op) const
Definition: script.h:646
Definition: script.h:287
void Unserialize(Stream &s, int nType, int nVersion)
Definition: script.h:784