You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

script.h 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef STARWELS_SCRIPT_SCRIPT_H
  6. #define STARWELS_SCRIPT_SCRIPT_H
  7. #include "crypto/common.h"
  8. #include "prevector.h"
  9. #include "serialize.h"
  10. #include <assert.h>
  11. #include <climits>
  12. #include <limits>
  13. #include <stdexcept>
  14. #include <stdint.h>
  15. #include <string.h>
  16. #include <string>
  17. #include <vector>
  18. // Maximum number of bytes pushable to the stack
  19. static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
  20. // Maximum number of non-push operations per script
  21. static const int MAX_OPS_PER_SCRIPT = 201;
  22. // Maximum number of public keys per multisig
  23. static const int MAX_PUBKEYS_PER_MULTISIG = 20;
  24. // Maximum script length in bytes
  25. static const int MAX_SCRIPT_SIZE = 10000;
  26. // Maximum number of values on script interpreter stack
  27. static const int MAX_STACK_SIZE = 1000;
  28. // Threshold for nLockTime: below this value it is interpreted as block number,
  29. // otherwise as UNIX timestamp.
  30. static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov 5 00:53:20 1985 UTC
  31. template <typename T>
  32. std::vector<unsigned char> ToByteVector(const T& in)
  33. {
  34. return std::vector<unsigned char>(in.begin(), in.end());
  35. }
  36. /** Script opcodes */
  37. enum opcodetype
  38. {
  39. // push value
  40. OP_0 = 0x00,
  41. OP_FALSE = OP_0,
  42. OP_PUSHDATA1 = 0x4c,
  43. OP_PUSHDATA2 = 0x4d,
  44. OP_PUSHDATA4 = 0x4e,
  45. OP_1NEGATE = 0x4f,
  46. OP_RESERVED = 0x50,
  47. OP_1 = 0x51,
  48. OP_TRUE=OP_1,
  49. OP_2 = 0x52,
  50. OP_3 = 0x53,
  51. OP_4 = 0x54,
  52. OP_5 = 0x55,
  53. OP_6 = 0x56,
  54. OP_7 = 0x57,
  55. OP_8 = 0x58,
  56. OP_9 = 0x59,
  57. OP_10 = 0x5a,
  58. OP_11 = 0x5b,
  59. OP_12 = 0x5c,
  60. OP_13 = 0x5d,
  61. OP_14 = 0x5e,
  62. OP_15 = 0x5f,
  63. OP_16 = 0x60,
  64. // control
  65. OP_NOP = 0x61,
  66. OP_VER = 0x62,
  67. OP_IF = 0x63,
  68. OP_NOTIF = 0x64,
  69. OP_VERIF = 0x65,
  70. OP_VERNOTIF = 0x66,
  71. OP_ELSE = 0x67,
  72. OP_ENDIF = 0x68,
  73. OP_VERIFY = 0x69,
  74. OP_RETURN = 0x6a,
  75. // stack ops
  76. OP_TOALTSTACK = 0x6b,
  77. OP_FROMALTSTACK = 0x6c,
  78. OP_2DROP = 0x6d,
  79. OP_2DUP = 0x6e,
  80. OP_3DUP = 0x6f,
  81. OP_2OVER = 0x70,
  82. OP_2ROT = 0x71,
  83. OP_2SWAP = 0x72,
  84. OP_IFDUP = 0x73,
  85. OP_DEPTH = 0x74,
  86. OP_DROP = 0x75,
  87. OP_DUP = 0x76,
  88. OP_NIP = 0x77,
  89. OP_OVER = 0x78,
  90. OP_PICK = 0x79,
  91. OP_ROLL = 0x7a,
  92. OP_ROT = 0x7b,
  93. OP_SWAP = 0x7c,
  94. OP_TUCK = 0x7d,
  95. // splice ops
  96. OP_CAT = 0x7e,
  97. OP_SUBSTR = 0x7f,
  98. OP_LEFT = 0x80,
  99. OP_RIGHT = 0x81,
  100. OP_SIZE = 0x82,
  101. // bit logic
  102. OP_INVERT = 0x83,
  103. OP_AND = 0x84,
  104. OP_OR = 0x85,
  105. OP_XOR = 0x86,
  106. OP_EQUAL = 0x87,
  107. OP_EQUALVERIFY = 0x88,
  108. OP_RESERVED1 = 0x89,
  109. OP_RESERVED2 = 0x8a,
  110. // numeric
  111. OP_1ADD = 0x8b,
  112. OP_1SUB = 0x8c,
  113. OP_2MUL = 0x8d,
  114. OP_2DIV = 0x8e,
  115. OP_NEGATE = 0x8f,
  116. OP_ABS = 0x90,
  117. OP_NOT = 0x91,
  118. OP_0NOTEQUAL = 0x92,
  119. OP_ADD = 0x93,
  120. OP_SUB = 0x94,
  121. OP_MUL = 0x95,
  122. OP_DIV = 0x96,
  123. OP_MOD = 0x97,
  124. OP_LSHIFT = 0x98,
  125. OP_RSHIFT = 0x99,
  126. OP_BOOLAND = 0x9a,
  127. OP_BOOLOR = 0x9b,
  128. OP_NUMEQUAL = 0x9c,
  129. OP_NUMEQUALVERIFY = 0x9d,
  130. OP_NUMNOTEQUAL = 0x9e,
  131. OP_LESSTHAN = 0x9f,
  132. OP_GREATERTHAN = 0xa0,
  133. OP_LESSTHANOREQUAL = 0xa1,
  134. OP_GREATERTHANOREQUAL = 0xa2,
  135. OP_MIN = 0xa3,
  136. OP_MAX = 0xa4,
  137. OP_WITHIN = 0xa5,
  138. // crypto
  139. OP_RIPEMD160 = 0xa6,
  140. OP_SHA1 = 0xa7,
  141. OP_SHA256 = 0xa8,
  142. OP_HASH160 = 0xa9,
  143. OP_HASH256 = 0xaa,
  144. OP_CODESEPARATOR = 0xab,
  145. OP_CHECKSIG = 0xac,
  146. OP_CHECKSIGVERIFY = 0xad,
  147. OP_CHECKMULTISIG = 0xae,
  148. OP_CHECKMULTISIGVERIFY = 0xaf,
  149. // expansion
  150. OP_NOP1 = 0xb0,
  151. OP_CHECKLOCKTIMEVERIFY = 0xb1,
  152. OP_NOP2 = OP_CHECKLOCKTIMEVERIFY,
  153. OP_CHECKSEQUENCEVERIFY = 0xb2,
  154. OP_NOP3 = OP_CHECKSEQUENCEVERIFY,
  155. OP_NOP4 = 0xb3,
  156. OP_NOP5 = 0xb4,
  157. OP_NOP6 = 0xb5,
  158. OP_NOP7 = 0xb6,
  159. OP_NOP8 = 0xb7,
  160. OP_NOP9 = 0xb8,
  161. OP_NOP10 = 0xb9,
  162. // template matching params
  163. OP_SMALLINTEGER = 0xfa,
  164. OP_PUBKEYS = 0xfb,
  165. OP_PUBKEYHASH = 0xfd,
  166. OP_PUBKEY = 0xfe,
  167. OP_INVALIDOPCODE = 0xff,
  168. };
  169. // Maximum value that an opcode can be
  170. static const unsigned int MAX_OPCODE = OP_NOP10;
  171. const char* GetOpName(opcodetype opcode);
  172. class scriptnum_error : public std::runtime_error
  173. {
  174. public:
  175. explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
  176. };
  177. class CScriptNum
  178. {
  179. /**
  180. * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
  181. * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
  182. * but results may overflow (and are valid as long as they are not used in a subsequent
  183. * numeric operation). CScriptNum enforces those semantics by storing results as
  184. * an int64 and allowing out-of-range values to be returned as a vector of bytes but
  185. * throwing an exception if arithmetic is done or the result is interpreted as an integer.
  186. */
  187. public:
  188. explicit CScriptNum(const int64_t& n)
  189. {
  190. m_value = n;
  191. }
  192. static const size_t nDefaultMaxNumSize = 4;
  193. explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
  194. const size_t nMaxNumSize = nDefaultMaxNumSize)
  195. {
  196. if (vch.size() > nMaxNumSize) {
  197. throw scriptnum_error("script number overflow");
  198. }
  199. if (fRequireMinimal && vch.size() > 0) {
  200. // Check that the number is encoded with the minimum possible
  201. // number of bytes.
  202. //
  203. // If the most-significant-byte - excluding the sign bit - is zero
  204. // then we're not minimal. Note how this test also rejects the
  205. // negative-zero encoding, 0x80.
  206. if ((vch.back() & 0x7f) == 0) {
  207. // One exception: if there's more than one byte and the most
  208. // significant bit of the second-most-significant-byte is set
  209. // it would conflict with the sign bit. An example of this case
  210. // is +-255, which encode to 0xff00 and 0xff80 respectively.
  211. // (big-endian).
  212. if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
  213. throw scriptnum_error("non-minimally encoded script number");
  214. }
  215. }
  216. }
  217. m_value = set_vch(vch);
  218. }
  219. inline bool operator==(const int64_t& rhs) const { return m_value == rhs; }
  220. inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; }
  221. inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; }
  222. inline bool operator< (const int64_t& rhs) const { return m_value < rhs; }
  223. inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; }
  224. inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
  225. inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
  226. inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
  227. inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
  228. inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
  229. inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
  230. inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
  231. inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);}
  232. inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);}
  233. inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); }
  234. inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); }
  235. inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); }
  236. inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); }
  237. inline CScriptNum operator&( const int64_t& rhs) const { return CScriptNum(m_value & rhs);}
  238. inline CScriptNum operator&( const CScriptNum& rhs) const { return operator&(rhs.m_value); }
  239. inline CScriptNum& operator&=( const CScriptNum& rhs) { return operator&=(rhs.m_value); }
  240. inline CScriptNum operator-() const
  241. {
  242. assert(m_value != std::numeric_limits<int64_t>::min());
  243. return CScriptNum(-m_value);
  244. }
  245. inline CScriptNum& operator=( const int64_t& rhs)
  246. {
  247. m_value = rhs;
  248. return *this;
  249. }
  250. inline CScriptNum& operator+=( const int64_t& rhs)
  251. {
  252. assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
  253. (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
  254. m_value += rhs;
  255. return *this;
  256. }
  257. inline CScriptNum& operator-=( const int64_t& rhs)
  258. {
  259. assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
  260. (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
  261. m_value -= rhs;
  262. return *this;
  263. }
  264. inline CScriptNum& operator&=( const int64_t& rhs)
  265. {
  266. m_value &= rhs;
  267. return *this;
  268. }
  269. int getint() const
  270. {
  271. if (m_value > std::numeric_limits<int>::max())
  272. return std::numeric_limits<int>::max();
  273. else if (m_value < std::numeric_limits<int>::min())
  274. return std::numeric_limits<int>::min();
  275. return m_value;
  276. }
  277. std::vector<unsigned char> getvch() const
  278. {
  279. return serialize(m_value);
  280. }
  281. static std::vector<unsigned char> serialize(const int64_t& value)
  282. {
  283. if(value == 0)
  284. return std::vector<unsigned char>();
  285. std::vector<unsigned char> result;
  286. const bool neg = value < 0;
  287. uint64_t absvalue = neg ? -value : value;
  288. while(absvalue)
  289. {
  290. result.push_back(absvalue & 0xff);
  291. absvalue >>= 8;
  292. }
  293. // - If the most significant byte is >= 0x80 and the value is positive, push a
  294. // new zero-byte to make the significant byte < 0x80 again.
  295. // - If the most significant byte is >= 0x80 and the value is negative, push a
  296. // new 0x80 byte that will be popped off when converting to an integral.
  297. // - If the most significant byte is < 0x80 and the value is negative, add
  298. // 0x80 to it, since it will be subtracted and interpreted as a negative when
  299. // converting to an integral.
  300. if (result.back() & 0x80)
  301. result.push_back(neg ? 0x80 : 0);
  302. else if (neg)
  303. result.back() |= 0x80;
  304. return result;
  305. }
  306. private:
  307. static int64_t set_vch(const std::vector<unsigned char>& vch)
  308. {
  309. if (vch.empty())
  310. return 0;
  311. int64_t result = 0;
  312. for (size_t i = 0; i != vch.size(); ++i)
  313. result |= static_cast<int64_t>(vch[i]) << 8*i;
  314. // If the input vector's most significant byte is 0x80, remove it from
  315. // the result's msb and return a negative.
  316. if (vch.back() & 0x80)
  317. return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
  318. return result;
  319. }
  320. int64_t m_value;
  321. };
  322. typedef prevector<28, unsigned char> CScriptBase;
  323. /** Serialized script, used inside transaction inputs and outputs */
  324. class CScript : public CScriptBase
  325. {
  326. protected:
  327. CScript& push_int64(int64_t n)
  328. {
  329. if (n == -1 || (n >= 1 && n <= 16))
  330. {
  331. push_back(n + (OP_1 - 1));
  332. }
  333. else if (n == 0)
  334. {
  335. push_back(OP_0);
  336. }
  337. else
  338. {
  339. *this << CScriptNum::serialize(n);
  340. }
  341. return *this;
  342. }
  343. public:
  344. CScript() { }
  345. CScript(const_iterator pbegin, const_iterator pend) : CScriptBase(pbegin, pend) { }
  346. CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
  347. CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
  348. ADD_SERIALIZE_METHODS;
  349. template <typename Stream, typename Operation>
  350. inline void SerializationOp(Stream& s, Operation ser_action) {
  351. READWRITE(static_cast<CScriptBase&>(*this));
  352. }
  353. CScript& operator+=(const CScript& b)
  354. {
  355. insert(end(), b.begin(), b.end());
  356. return *this;
  357. }
  358. friend CScript operator+(const CScript& a, const CScript& b)
  359. {
  360. CScript ret = a;
  361. ret += b;
  362. return ret;
  363. }
  364. CScript(int64_t b) { operator<<(b); }
  365. explicit CScript(opcodetype b) { operator<<(b); }
  366. explicit CScript(const CScriptNum& b) { operator<<(b); }
  367. explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
  368. CScript& operator<<(int64_t b) { return push_int64(b); }
  369. CScript& operator<<(opcodetype opcode)
  370. {
  371. if (opcode < 0 || opcode > 0xff)
  372. throw std::runtime_error("CScript::operator<<(): invalid opcode");
  373. insert(end(), (unsigned char)opcode);
  374. return *this;
  375. }
  376. CScript& operator<<(const CScriptNum& b)
  377. {
  378. *this << b.getvch();
  379. return *this;
  380. }
  381. CScript& operator<<(const std::vector<unsigned char>& b)
  382. {
  383. if (b.size() < OP_PUSHDATA1)
  384. {
  385. insert(end(), (unsigned char)b.size());
  386. }
  387. else if (b.size() <= 0xff)
  388. {
  389. insert(end(), OP_PUSHDATA1);
  390. insert(end(), (unsigned char)b.size());
  391. }
  392. else if (b.size() <= 0xffff)
  393. {
  394. insert(end(), OP_PUSHDATA2);
  395. uint8_t _data[2];
  396. WriteLE16(_data, b.size());
  397. insert(end(), _data, _data + sizeof(_data));
  398. }
  399. else
  400. {
  401. insert(end(), OP_PUSHDATA4);
  402. uint8_t _data[4];
  403. WriteLE32(_data, b.size());
  404. insert(end(), _data, _data + sizeof(_data));
  405. }
  406. insert(end(), b.begin(), b.end());
  407. return *this;
  408. }
  409. CScript& operator<<(const CScript& b)
  410. {
  411. // I'm not sure if this should push the script or concatenate scripts.
  412. // If there's ever a use for pushing a script onto a script, delete this member fn
  413. assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
  414. return *this;
  415. }
  416. bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
  417. {
  418. // Wrapper so it can be called with either iterator or const_iterator
  419. const_iterator pc2 = pc;
  420. bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
  421. pc = begin() + (pc2 - begin());
  422. return fRet;
  423. }
  424. bool GetOp(iterator& pc, opcodetype& opcodeRet)
  425. {
  426. const_iterator pc2 = pc;
  427. bool fRet = GetOp2(pc2, opcodeRet, nullptr);
  428. pc = begin() + (pc2 - begin());
  429. return fRet;
  430. }
  431. bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
  432. {
  433. return GetOp2(pc, opcodeRet, &vchRet);
  434. }
  435. bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
  436. {
  437. return GetOp2(pc, opcodeRet, nullptr);
  438. }
  439. bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
  440. {
  441. opcodeRet = OP_INVALIDOPCODE;
  442. if (pvchRet)
  443. pvchRet->clear();
  444. if (pc >= end())
  445. return false;
  446. // Read instruction
  447. if (end() - pc < 1)
  448. return false;
  449. unsigned int opcode = *pc++;
  450. // Immediate operand
  451. if (opcode <= OP_PUSHDATA4)
  452. {
  453. unsigned int nSize = 0;
  454. if (opcode < OP_PUSHDATA1)
  455. {
  456. nSize = opcode;
  457. }
  458. else if (opcode == OP_PUSHDATA1)
  459. {
  460. if (end() - pc < 1)
  461. return false;
  462. nSize = *pc++;
  463. }
  464. else if (opcode == OP_PUSHDATA2)
  465. {
  466. if (end() - pc < 2)
  467. return false;
  468. nSize = ReadLE16(&pc[0]);
  469. pc += 2;
  470. }
  471. else if (opcode == OP_PUSHDATA4)
  472. {
  473. if (end() - pc < 4)
  474. return false;
  475. nSize = ReadLE32(&pc[0]);
  476. pc += 4;
  477. }
  478. if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
  479. return false;
  480. if (pvchRet)
  481. pvchRet->assign(pc, pc + nSize);
  482. pc += nSize;
  483. }
  484. opcodeRet = (opcodetype)opcode;
  485. return true;
  486. }
  487. /** Encode/decode small integers: */
  488. static int DecodeOP_N(opcodetype opcode)
  489. {
  490. if (opcode == OP_0)
  491. return 0;
  492. assert(opcode >= OP_1 && opcode <= OP_16);
  493. return (int)opcode - (int)(OP_1 - 1);
  494. }
  495. static opcodetype EncodeOP_N(int n)
  496. {
  497. assert(n >= 0 && n <= 16);
  498. if (n == 0)
  499. return OP_0;
  500. return (opcodetype)(OP_1+n-1);
  501. }
  502. int FindAndDelete(const CScript& b)
  503. {
  504. int nFound = 0;
  505. if (b.empty())
  506. return nFound;
  507. CScript result;
  508. iterator pc = begin(), pc2 = begin();
  509. opcodetype opcode;
  510. do
  511. {
  512. result.insert(result.end(), pc2, pc);
  513. while (static_cast<size_t>(end() - pc) >= b.size() && std::equal(b.begin(), b.end(), pc))
  514. {
  515. pc = pc + b.size();
  516. ++nFound;
  517. }
  518. pc2 = pc;
  519. }
  520. while (GetOp(pc, opcode));
  521. if (nFound > 0) {
  522. result.insert(result.end(), pc2, end());
  523. *this = result;
  524. }
  525. return nFound;
  526. }
  527. int Find(opcodetype op) const
  528. {
  529. int nFound = 0;
  530. opcodetype opcode;
  531. for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
  532. if (opcode == op)
  533. ++nFound;
  534. return nFound;
  535. }
  536. /**
  537. * Pre-version-0.6, Starwels always counted CHECKMULTISIGs
  538. * as 20 sigops. With pay-to-script-hash, that changed:
  539. * CHECKMULTISIGs serialized in scriptSigs are
  540. * counted more accurately, assuming they are of the form
  541. * ... OP_N CHECKMULTISIG ...
  542. */
  543. unsigned int GetSigOpCount(bool fAccurate) const;
  544. /**
  545. * Accurately count sigOps, including sigOps in
  546. * pay-to-script-hash transactions:
  547. */
  548. unsigned int GetSigOpCount(const CScript& scriptSig) const;
  549. bool IsPayToScriptHash() const;
  550. bool IsPayToWitnessScriptHash() const;
  551. bool IsWitnessProgram(int& version, std::vector<unsigned char>& program) const;
  552. /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
  553. bool IsPushOnly(const_iterator pc) const;
  554. bool IsPushOnly() const;
  555. /** Check if the script contains valid OP_CODES */
  556. bool HasValidOps() const;
  557. /**
  558. * Returns whether the script is guaranteed to fail at execution,
  559. * regardless of the initial stack. This allows outputs to be pruned
  560. * instantly when entering the UTXO set.
  561. */
  562. bool IsUnspendable() const
  563. {
  564. return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);
  565. }
  566. void clear()
  567. {
  568. // The default prevector::clear() does not release memory
  569. CScriptBase::clear();
  570. shrink_to_fit();
  571. }
  572. };
  573. struct CScriptWitness
  574. {
  575. // Note that this encodes the data elements being pushed, rather than
  576. // encoding them as a CScript that pushes them.
  577. std::vector<std::vector<unsigned char> > stack;
  578. // Some compilers complain without a default constructor
  579. CScriptWitness() { }
  580. bool IsNull() const { return stack.empty(); }
  581. void SetNull() { stack.clear(); stack.shrink_to_fit(); }
  582. std::string ToString() const;
  583. };
  584. class CReserveScript
  585. {
  586. public:
  587. CScript reserveScript;
  588. virtual void KeepScript() {}
  589. CReserveScript() {}
  590. virtual ~CReserveScript() {}
  591. };
  592. #endif // STARWELS_SCRIPT_SCRIPT_H