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 16KB

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