script.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. // ECOin - Copyright (c) - 2014/2022 - GPLv3 - epsylon@riseup.net (https://03c8.net)
  2. #ifndef H_ECOIN_SCRIPT
  3. #define H_ECOIN_SCRIPT
  4. #include <string>
  5. #include <vector>
  6. #include <boost/foreach.hpp>
  7. #include <boost/variant.hpp>
  8. #include "keystore.h"
  9. #include "bignum.h"
  10. typedef std::vector<unsigned char> valtype;
  11. class CTransaction;
  12. /** Signature hash types/flags */
  13. enum
  14. {
  15. SIGHASH_ALL = 1,
  16. SIGHASH_NONE = 2,
  17. SIGHASH_SINGLE = 3,
  18. SIGHASH_ANYONECANPAY = 0x80,
  19. };
  20. enum txnouttype
  21. {
  22. TX_NONSTANDARD,
  23. // 'standard' transaction types:
  24. TX_PUBKEY,
  25. TX_PUBKEYHASH,
  26. TX_SCRIPTHASH,
  27. TX_MULTISIG,
  28. };
  29. class CNoDestination {
  30. public:
  31. friend bool operator==(const CNoDestination &a, const CNoDestination &b) { return true; }
  32. friend bool operator<(const CNoDestination &a, const CNoDestination &b) { return true; }
  33. };
  34. /** A txout script template with a specific destination. It is either:
  35. * * CNoDestination: no destination set
  36. * * CKeyID: TX_PUBKEYHASH destination
  37. * * CScriptID: TX_SCRIPTHASH destination
  38. * A CTxDestination is the internal data type encoded in a CecoinAddress
  39. */
  40. typedef boost::variant<CNoDestination, CKeyID, CScriptID> CTxDestination;
  41. const char* GetTxnOutputType(txnouttype t);
  42. /** Script opcodes */
  43. enum opcodetype
  44. {
  45. // push value
  46. OP_0 = 0x00,
  47. OP_FALSE = OP_0,
  48. OP_PUSHDATA1 = 0x4c,
  49. OP_PUSHDATA2 = 0x4d,
  50. OP_PUSHDATA4 = 0x4e,
  51. OP_1NEGATE = 0x4f,
  52. OP_RESERVED = 0x50,
  53. OP_1 = 0x51,
  54. OP_TRUE=OP_1,
  55. OP_2 = 0x52,
  56. OP_3 = 0x53,
  57. OP_4 = 0x54,
  58. OP_5 = 0x55,
  59. OP_6 = 0x56,
  60. OP_7 = 0x57,
  61. OP_8 = 0x58,
  62. OP_9 = 0x59,
  63. OP_10 = 0x5a,
  64. OP_11 = 0x5b,
  65. OP_12 = 0x5c,
  66. OP_13 = 0x5d,
  67. OP_14 = 0x5e,
  68. OP_15 = 0x5f,
  69. OP_16 = 0x60,
  70. // control
  71. OP_NOP = 0x61,
  72. OP_VER = 0x62,
  73. OP_IF = 0x63,
  74. OP_NOTIF = 0x64,
  75. OP_VERIF = 0x65,
  76. OP_VERNOTIF = 0x66,
  77. OP_ELSE = 0x67,
  78. OP_ENDIF = 0x68,
  79. OP_VERIFY = 0x69,
  80. OP_RETURN = 0x6a,
  81. // stack ops
  82. OP_TOALTSTACK = 0x6b,
  83. OP_FROMALTSTACK = 0x6c,
  84. OP_2DROP = 0x6d,
  85. OP_2DUP = 0x6e,
  86. OP_3DUP = 0x6f,
  87. OP_2OVER = 0x70,
  88. OP_2ROT = 0x71,
  89. OP_2SWAP = 0x72,
  90. OP_IFDUP = 0x73,
  91. OP_DEPTH = 0x74,
  92. OP_DROP = 0x75,
  93. OP_DUP = 0x76,
  94. OP_NIP = 0x77,
  95. OP_OVER = 0x78,
  96. OP_PICK = 0x79,
  97. OP_ROLL = 0x7a,
  98. OP_ROT = 0x7b,
  99. OP_SWAP = 0x7c,
  100. OP_TUCK = 0x7d,
  101. // splice ops
  102. OP_CAT = 0x7e,
  103. OP_SUBSTR = 0x7f,
  104. OP_LEFT = 0x80,
  105. OP_RIGHT = 0x81,
  106. OP_SIZE = 0x82,
  107. // bit logic
  108. OP_INVERT = 0x83,
  109. OP_AND = 0x84,
  110. OP_OR = 0x85,
  111. OP_XOR = 0x86,
  112. OP_EQUAL = 0x87,
  113. OP_EQUALVERIFY = 0x88,
  114. OP_RESERVED1 = 0x89,
  115. OP_RESERVED2 = 0x8a,
  116. // numeric
  117. OP_1ADD = 0x8b,
  118. OP_1SUB = 0x8c,
  119. OP_2MUL = 0x8d,
  120. OP_2DIV = 0x8e,
  121. OP_NEGATE = 0x8f,
  122. OP_ABS = 0x90,
  123. OP_NOT = 0x91,
  124. OP_0NOTEQUAL = 0x92,
  125. OP_ADD = 0x93,
  126. OP_SUB = 0x94,
  127. OP_MUL = 0x95,
  128. OP_DIV = 0x96,
  129. OP_MOD = 0x97,
  130. OP_LSHIFT = 0x98,
  131. OP_RSHIFT = 0x99,
  132. OP_BOOLAND = 0x9a,
  133. OP_BOOLOR = 0x9b,
  134. OP_NUMEQUAL = 0x9c,
  135. OP_NUMEQUALVERIFY = 0x9d,
  136. OP_NUMNOTEQUAL = 0x9e,
  137. OP_LESSTHAN = 0x9f,
  138. OP_GREATERTHAN = 0xa0,
  139. OP_LESSTHANOREQUAL = 0xa1,
  140. OP_GREATERTHANOREQUAL = 0xa2,
  141. OP_MIN = 0xa3,
  142. OP_MAX = 0xa4,
  143. OP_WITHIN = 0xa5,
  144. // crypto
  145. OP_RIPEMD160 = 0xa6,
  146. OP_SHA1 = 0xa7,
  147. OP_SHA256 = 0xa8,
  148. OP_HASH160 = 0xa9,
  149. OP_HASH256 = 0xaa,
  150. OP_CODESEPARATOR = 0xab,
  151. OP_CHECKSIG = 0xac,
  152. OP_CHECKSIGVERIFY = 0xad,
  153. OP_CHECKMULTISIG = 0xae,
  154. OP_CHECKMULTISIGVERIFY = 0xaf,
  155. // expansion
  156. OP_NOP1 = 0xb0,
  157. OP_NOP2 = 0xb1,
  158. OP_NOP3 = 0xb2,
  159. OP_NOP4 = 0xb3,
  160. OP_NOP5 = 0xb4,
  161. OP_NOP6 = 0xb5,
  162. OP_NOP7 = 0xb6,
  163. OP_NOP8 = 0xb7,
  164. OP_NOP9 = 0xb8,
  165. OP_NOP10 = 0xb9,
  166. // template matching params
  167. OP_SMALLINTEGER = 0xfa,
  168. OP_PUBKEYS = 0xfb,
  169. OP_PUBKEYHASH = 0xfd,
  170. OP_PUBKEY = 0xfe,
  171. OP_INVALIDOPCODE = 0xff,
  172. };
  173. const char* GetOpName(opcodetype opcode);
  174. inline std::string ValueString(const std::vector<unsigned char>& vch)
  175. {
  176. if (vch.size() <= 4)
  177. return strprintf("%d", CBigNum(vch).getint());
  178. else
  179. return HexStr(vch);
  180. }
  181. inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
  182. {
  183. std::string str;
  184. BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
  185. {
  186. if (!str.empty())
  187. str += " ";
  188. str += ValueString(vch);
  189. }
  190. return str;
  191. }
  192. class scriptnum_error : public std::runtime_error
  193. {
  194. public:
  195. explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
  196. };
  197. class CScriptNum
  198. {
  199. /**
  200. * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
  201. * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
  202. * but results may overflow (and are valid as long as they are not used in a subsequent
  203. * numeric operation). CScriptNum enforces those semantics by storing results as
  204. * an int64 and allowing out-of-range values to be returned as a vector of bytes but
  205. * throwing an exception if arithmetic is done or the result is interpreted as an integer.
  206. */
  207. public:
  208. explicit CScriptNum(const int64_t& n)
  209. {
  210. m_value = n;
  211. }
  212. static const size_t nDefaultMaxNumSize = 4;
  213. explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
  214. const size_t nMaxNumSize = nDefaultMaxNumSize)
  215. {
  216. if (vch.size() > nMaxNumSize) {
  217. throw scriptnum_error("script number overflow");
  218. }
  219. if (fRequireMinimal && vch.size() > 0) {
  220. // Check that the number is encoded with the minimum possible
  221. // number of bytes.
  222. //
  223. // If the most-significant-byte - excluding the sign bit - is zero
  224. // then we're not minimal. Note how this test also rejects the
  225. // negative-zero encoding, 0x80.
  226. if ((vch.back() & 0x7f) == 0) {
  227. // One exception: if there's more than one byte and the most
  228. // significant bit of the second-most-significant-byte is set
  229. // it would conflict with the sign bit. An example of this case
  230. // is +-255, which encode to 0xff00 and 0xff80 respectively.
  231. // (big-endian).
  232. if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
  233. throw scriptnum_error("non-minimally encoded script number");
  234. }
  235. }
  236. }
  237. m_value = set_vch(vch);
  238. }
  239. inline bool operator==(const int64_t& rhs) const { return m_value == rhs; }
  240. inline bool operator!=(const int64_t& rhs) const { return m_value != rhs; }
  241. inline bool operator<=(const int64_t& rhs) const { return m_value <= rhs; }
  242. inline bool operator< (const int64_t& rhs) const { return m_value < rhs; }
  243. inline bool operator>=(const int64_t& rhs) const { return m_value >= rhs; }
  244. inline bool operator> (const int64_t& rhs) const { return m_value > rhs; }
  245. inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
  246. inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
  247. inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
  248. inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
  249. inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
  250. inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
  251. inline CScriptNum operator+( const int64_t& rhs) const { return CScriptNum(m_value + rhs);}
  252. inline CScriptNum operator-( const int64_t& rhs) const { return CScriptNum(m_value - rhs);}
  253. inline CScriptNum operator+( const CScriptNum& rhs) const { return operator+(rhs.m_value); }
  254. inline CScriptNum operator-( const CScriptNum& rhs) const { return operator-(rhs.m_value); }
  255. inline CScriptNum& operator+=( const CScriptNum& rhs) { return operator+=(rhs.m_value); }
  256. inline CScriptNum& operator-=( const CScriptNum& rhs) { return operator-=(rhs.m_value); }
  257. inline CScriptNum operator&( const int64_t& rhs) const { return CScriptNum(m_value & rhs);}
  258. inline CScriptNum operator&( const CScriptNum& rhs) const { return operator&(rhs.m_value); }
  259. inline CScriptNum& operator&=( const CScriptNum& rhs) { return operator&=(rhs.m_value); }
  260. inline CScriptNum operator-() const
  261. {
  262. assert(m_value != std::numeric_limits<int64_t>::min());
  263. return CScriptNum(-m_value);
  264. }
  265. inline CScriptNum& operator=( const int64_t& rhs)
  266. {
  267. m_value = rhs;
  268. return *this;
  269. }
  270. inline CScriptNum& operator+=( const int64_t& rhs)
  271. {
  272. assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
  273. (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
  274. m_value += rhs;
  275. return *this;
  276. }
  277. inline CScriptNum& operator-=( const int64_t& rhs)
  278. {
  279. assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
  280. (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
  281. m_value -= rhs;
  282. return *this;
  283. }
  284. inline CScriptNum& operator&=( const int64_t& rhs)
  285. {
  286. m_value &= rhs;
  287. return *this;
  288. }
  289. int getint() const
  290. {
  291. if (m_value > std::numeric_limits<int>::max())
  292. return std::numeric_limits<int>::max();
  293. else if (m_value < std::numeric_limits<int>::min())
  294. return std::numeric_limits<int>::min();
  295. return m_value;
  296. }
  297. std::vector<unsigned char> getvch() const
  298. {
  299. return serialize(m_value);
  300. }
  301. static std::vector<unsigned char> serialize(const int64_t& value)
  302. {
  303. if(value == 0)
  304. return std::vector<unsigned char>();
  305. std::vector<unsigned char> result;
  306. const bool neg = value < 0;
  307. uint64_t absvalue = neg ? -value : value;
  308. while(absvalue)
  309. {
  310. result.push_back(absvalue & 0xff);
  311. absvalue >>= 8;
  312. }
  313. // - If the most significant byte is >= 0x80 and the value is positive, push a
  314. // new zero-byte to make the significant byte < 0x80 again.
  315. // - If the most significant byte is >= 0x80 and the value is negative, push a
  316. // new 0x80 byte that will be popped off when converting to an integral.
  317. // - If the most significant byte is < 0x80 and the value is negative, add
  318. // 0x80 to it, since it will be subtracted and interpreted as a negative when
  319. // converting to an integral.
  320. if (result.back() & 0x80)
  321. result.push_back(neg ? 0x80 : 0);
  322. else if (neg)
  323. result.back() |= 0x80;
  324. return result;
  325. }
  326. private:
  327. static int64_t set_vch(const std::vector<unsigned char>& vch)
  328. {
  329. if (vch.empty())
  330. return 0;
  331. int64_t result = 0;
  332. for (size_t i = 0; i != vch.size(); ++i)
  333. result |= static_cast<int64_t>(vch[i]) << 8*i;
  334. // If the input vector's most significant byte is 0x80, remove it from
  335. // the result's msb and return a negative.
  336. if (vch.back() & 0x80)
  337. return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
  338. return result;
  339. }
  340. int64_t m_value;
  341. };
  342. /** Serialized script, used inside transaction inputs and outputs */
  343. class CScript : public std::vector<unsigned char>
  344. {
  345. protected:
  346. CScript& push_int64(int64 n)
  347. {
  348. if (n == -1 || (n >= 1 && n <= 16))
  349. {
  350. push_back(n + (OP_1 - 1));
  351. }
  352. else
  353. {
  354. *this << CScriptNum::serialize(n);
  355. }
  356. return *this;
  357. }
  358. CScript& push_uint64(uint64 n)
  359. {
  360. if (n >= 1 && n <= 16)
  361. {
  362. push_back(n + (OP_1 - 1));
  363. }
  364. else
  365. {
  366. *this << CScriptNum::serialize(n);
  367. }
  368. return *this;
  369. }
  370. public:
  371. CScript() { }
  372. CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
  373. CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
  374. #ifndef _MSC_VER
  375. CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
  376. #endif
  377. CScript& operator=(const CScript& b)
  378. {
  379. assign(b.begin(), b.end());
  380. return *this;
  381. }
  382. CScript& operator+=(const CScript& b)
  383. {
  384. insert(end(), b.begin(), b.end());
  385. return *this;
  386. }
  387. friend CScript operator+(const CScript& a, const CScript& b)
  388. {
  389. CScript ret = a;
  390. ret += b;
  391. return ret;
  392. }
  393. //explicit CScript(char b) is not portable. Use 'signed char' or 'unsigned char'.
  394. explicit CScript(signed char b) { operator<<(b); }
  395. explicit CScript(short b) { operator<<(b); }
  396. explicit CScript(int b) { operator<<(b); }
  397. explicit CScript(long b) { operator<<(b); }
  398. explicit CScript(int64 b) { operator<<(b); }
  399. explicit CScript(unsigned char b) { operator<<(b); }
  400. explicit CScript(unsigned int b) { operator<<(b); }
  401. explicit CScript(unsigned short b) { operator<<(b); }
  402. explicit CScript(unsigned long b) { operator<<(b); }
  403. explicit CScript(uint64 b) { operator<<(b); }
  404. explicit CScript(opcodetype b) { operator<<(b); }
  405. explicit CScript(const uint256& b) { operator<<(b); }
  406. explicit CScript(const CBigNum& b) { operator<<(b); }
  407. explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
  408. //CScript& operator<<(char b) is not portable. Use 'signed char' or 'unsigned char'.
  409. CScript& operator<<(signed char b) { return push_int64(b); }
  410. CScript& operator<<(short b) { return push_int64(b); }
  411. CScript& operator<<(int b) { return push_int64(b); }
  412. CScript& operator<<(long b) { return push_int64(b); }
  413. CScript& operator<<(int64 b) { return push_int64(b); }
  414. CScript& operator<<(unsigned char b) { return push_uint64(b); }
  415. CScript& operator<<(unsigned int b) { return push_uint64(b); }
  416. CScript& operator<<(unsigned short b) { return push_uint64(b); }
  417. CScript& operator<<(unsigned long b) { return push_uint64(b); }
  418. CScript& operator<<(uint64 b) { return push_uint64(b); }
  419. CScript& operator<<(opcodetype opcode)
  420. {
  421. if (opcode < 0 || opcode > 0xff)
  422. throw std::runtime_error("CScript::operator<<() : invalid opcode");
  423. insert(end(), (unsigned char)opcode);
  424. return *this;
  425. }
  426. CScript& operator<<(const uint160& b)
  427. {
  428. insert(end(), sizeof(b));
  429. insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
  430. return *this;
  431. }
  432. CScript& operator<<(const uint256& b)
  433. {
  434. insert(end(), sizeof(b));
  435. insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
  436. return *this;
  437. }
  438. CScript& operator<<(const CPubKey& key)
  439. {
  440. std::vector<unsigned char> vchKey = key.Raw();
  441. return (*this) << vchKey;
  442. }
  443. CScript& operator<<(const CBigNum& b)
  444. {
  445. *this << b.getvch();
  446. return *this;
  447. }
  448. CScript& operator<<(const std::vector<unsigned char>& b)
  449. {
  450. if (b.size() < OP_PUSHDATA1)
  451. {
  452. insert(end(), (unsigned char)b.size());
  453. }
  454. else if (b.size() <= 0xff)
  455. {
  456. insert(end(), OP_PUSHDATA1);
  457. insert(end(), (unsigned char)b.size());
  458. }
  459. else if (b.size() <= 0xffff)
  460. {
  461. insert(end(), OP_PUSHDATA2);
  462. unsigned short nSize = b.size();
  463. insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
  464. }
  465. else
  466. {
  467. insert(end(), OP_PUSHDATA4);
  468. unsigned int nSize = b.size();
  469. insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
  470. }
  471. insert(end(), b.begin(), b.end());
  472. return *this;
  473. }
  474. CScript& operator<<(const CScript& b)
  475. {
  476. // I'm not sure if this should push the script or concatenate scripts.
  477. // If there's ever a use for pushing a script onto a script, delete this member fn
  478. assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
  479. return *this;
  480. }
  481. bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
  482. {
  483. // Wrapper so it can be called with either iterator or const_iterator
  484. const_iterator pc2 = pc;
  485. bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
  486. pc = begin() + (pc2 - begin());
  487. return fRet;
  488. }
  489. bool GetOp(iterator& pc, opcodetype& opcodeRet)
  490. {
  491. const_iterator pc2 = pc;
  492. bool fRet = GetOp2(pc2, opcodeRet, NULL);
  493. pc = begin() + (pc2 - begin());
  494. return fRet;
  495. }
  496. bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
  497. {
  498. return GetOp2(pc, opcodeRet, &vchRet);
  499. }
  500. bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
  501. {
  502. return GetOp2(pc, opcodeRet, NULL);
  503. }
  504. bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
  505. {
  506. opcodeRet = OP_INVALIDOPCODE;
  507. if (pvchRet)
  508. pvchRet->clear();
  509. if (pc >= end())
  510. return false;
  511. // Read instruction
  512. if (end() - pc < 1)
  513. return false;
  514. unsigned int opcode = *pc++;
  515. // Immediate operand
  516. if (opcode <= OP_PUSHDATA4)
  517. {
  518. unsigned int nSize;
  519. if (opcode < OP_PUSHDATA1)
  520. {
  521. nSize = opcode;
  522. }
  523. else if (opcode == OP_PUSHDATA1)
  524. {
  525. if (end() - pc < 1)
  526. return false;
  527. nSize = *pc++;
  528. }
  529. else if (opcode == OP_PUSHDATA2)
  530. {
  531. if (end() - pc < 2)
  532. return false;
  533. nSize = 0;
  534. memcpy(&nSize, &pc[0], 2);
  535. pc += 2;
  536. }
  537. else if (opcode == OP_PUSHDATA4)
  538. {
  539. if (end() - pc < 4)
  540. return false;
  541. memcpy(&nSize, &pc[0], 4);
  542. pc += 4;
  543. }
  544. if (end() - pc < 0 || (unsigned int)(end() - pc) < nSize)
  545. return false;
  546. if (pvchRet)
  547. pvchRet->assign(pc, pc + nSize);
  548. pc += nSize;
  549. }
  550. opcodeRet = (opcodetype)opcode;
  551. return true;
  552. }
  553. // Encode/decode small integers:
  554. static int DecodeOP_N(opcodetype opcode)
  555. {
  556. if (opcode == OP_0)
  557. return 0;
  558. assert(opcode >= OP_1 && opcode <= OP_16);
  559. return (int)opcode - (int)(OP_1 - 1);
  560. }
  561. static opcodetype EncodeOP_N(int n)
  562. {
  563. assert(n >= 0 && n <= 16);
  564. if (n == 0)
  565. return OP_0;
  566. return (opcodetype)(OP_1+n-1);
  567. }
  568. int FindAndDelete(const CScript& b)
  569. {
  570. int nFound = 0;
  571. if (b.empty())
  572. return nFound;
  573. iterator pc = begin();
  574. opcodetype opcode;
  575. do
  576. {
  577. while (end() - pc >= (long)b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
  578. {
  579. erase(pc, pc + b.size());
  580. ++nFound;
  581. }
  582. }
  583. while (GetOp(pc, opcode));
  584. return nFound;
  585. }
  586. int Find(opcodetype op) const
  587. {
  588. int nFound = 0;
  589. opcodetype opcode;
  590. for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
  591. if (opcode == op)
  592. ++nFound;
  593. return nFound;
  594. }
  595. // Pre-version-0.6, ecoin always counted CHECKMULTISIGs
  596. // as 20 sigops. With pay-to-script-hash, that changed:
  597. // CHECKMULTISIGs serialized in scriptSigs are
  598. // counted more accurately, assuming they are of the form
  599. // ... OP_N CHECKMULTISIG ...
  600. unsigned int GetSigOpCount(bool fAccurate) const;
  601. // Accurately count sigOps, including sigOps in
  602. // pay-to-script-hash transactions:
  603. unsigned int GetSigOpCount(const CScript& scriptSig) const;
  604. bool IsPayToScriptHash() const;
  605. // Called by CTransaction::IsStandard
  606. bool IsPushOnly() const
  607. {
  608. const_iterator pc = begin();
  609. while (pc < end())
  610. {
  611. opcodetype opcode;
  612. if (!GetOp(pc, opcode))
  613. return false;
  614. if (opcode > OP_16)
  615. return false;
  616. }
  617. return true;
  618. }
  619. void SetDestination(const CTxDestination& address);
  620. void SetMultisig(int nRequired, const std::vector<CKey>& keys);
  621. void PrintHex() const
  622. {
  623. printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
  624. }
  625. std::string ToString(bool fShort=false) const
  626. {
  627. std::string str;
  628. opcodetype opcode;
  629. std::vector<unsigned char> vch;
  630. const_iterator pc = begin();
  631. while (pc < end())
  632. {
  633. if (!str.empty())
  634. str += " ";
  635. if (!GetOp(pc, opcode, vch))
  636. {
  637. str += "[error]";
  638. return str;
  639. }
  640. if (0 <= opcode && opcode <= OP_PUSHDATA4)
  641. str += fShort? ValueString(vch).substr(0, 10) : ValueString(vch);
  642. else
  643. str += GetOpName(opcode);
  644. }
  645. return str;
  646. }
  647. void print() const
  648. {
  649. printf("%s\n", ToString().c_str());
  650. }
  651. CScriptID GetID() const
  652. {
  653. return CScriptID(Hash160(*this));
  654. }
  655. };
  656. bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
  657. bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
  658. int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
  659. bool IsStandard(const CScript& scriptPubKey);
  660. bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
  661. bool IsMine(const CKeyStore& keystore, const CTxDestination &dest);
  662. bool ExtractDestination(const CScript& scriptPubKey, CTxDestination& addressRet);
  663. bool ExtractDestinations(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CTxDestination>& addressRet, int& nRequiredRet);
  664. bool SignSignature(const CKeyStore& keystore, const CScript& fromPubKey, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
  665. bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
  666. bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CTransaction& txTo, unsigned int nIn,
  667. bool fValidatePayToScriptHash, int nHashType);
  668. bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
  669. // Given two sets of signatures for scriptPubKey, possibly with OP_0 placeholders,
  670. // combine them intelligently and return the result.
  671. CScript CombineSignatures(CScript scriptPubKey, const CTransaction& txTo, unsigned int nIn, const CScript& scriptSig1, const CScript& scriptSig2);
  672. #endif