PageRenderTime 108ms CodeModel.GetById 49ms RepoModel.GetById 2ms app.codeStats 0ms

/src/script/script.cpp

https://github.com/bitcoin/bitcoin
C++ | 341 lines | 284 code | 30 blank | 27 comment | 75 complexity | 11704d7d16ba3d6b849de24f6540c207 MD5 | raw file
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2020 The Bitcoin Core developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include <script/script.h>
  6. #include <util/strencodings.h>
  7. #include <string>
  8. std::string GetOpName(opcodetype opcode)
  9. {
  10. switch (opcode)
  11. {
  12. // push value
  13. case OP_0 : return "0";
  14. case OP_PUSHDATA1 : return "OP_PUSHDATA1";
  15. case OP_PUSHDATA2 : return "OP_PUSHDATA2";
  16. case OP_PUSHDATA4 : return "OP_PUSHDATA4";
  17. case OP_1NEGATE : return "-1";
  18. case OP_RESERVED : return "OP_RESERVED";
  19. case OP_1 : return "1";
  20. case OP_2 : return "2";
  21. case OP_3 : return "3";
  22. case OP_4 : return "4";
  23. case OP_5 : return "5";
  24. case OP_6 : return "6";
  25. case OP_7 : return "7";
  26. case OP_8 : return "8";
  27. case OP_9 : return "9";
  28. case OP_10 : return "10";
  29. case OP_11 : return "11";
  30. case OP_12 : return "12";
  31. case OP_13 : return "13";
  32. case OP_14 : return "14";
  33. case OP_15 : return "15";
  34. case OP_16 : return "16";
  35. // control
  36. case OP_NOP : return "OP_NOP";
  37. case OP_VER : return "OP_VER";
  38. case OP_IF : return "OP_IF";
  39. case OP_NOTIF : return "OP_NOTIF";
  40. case OP_VERIF : return "OP_VERIF";
  41. case OP_VERNOTIF : return "OP_VERNOTIF";
  42. case OP_ELSE : return "OP_ELSE";
  43. case OP_ENDIF : return "OP_ENDIF";
  44. case OP_VERIFY : return "OP_VERIFY";
  45. case OP_RETURN : return "OP_RETURN";
  46. // stack ops
  47. case OP_TOALTSTACK : return "OP_TOALTSTACK";
  48. case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
  49. case OP_2DROP : return "OP_2DROP";
  50. case OP_2DUP : return "OP_2DUP";
  51. case OP_3DUP : return "OP_3DUP";
  52. case OP_2OVER : return "OP_2OVER";
  53. case OP_2ROT : return "OP_2ROT";
  54. case OP_2SWAP : return "OP_2SWAP";
  55. case OP_IFDUP : return "OP_IFDUP";
  56. case OP_DEPTH : return "OP_DEPTH";
  57. case OP_DROP : return "OP_DROP";
  58. case OP_DUP : return "OP_DUP";
  59. case OP_NIP : return "OP_NIP";
  60. case OP_OVER : return "OP_OVER";
  61. case OP_PICK : return "OP_PICK";
  62. case OP_ROLL : return "OP_ROLL";
  63. case OP_ROT : return "OP_ROT";
  64. case OP_SWAP : return "OP_SWAP";
  65. case OP_TUCK : return "OP_TUCK";
  66. // splice ops
  67. case OP_CAT : return "OP_CAT";
  68. case OP_SUBSTR : return "OP_SUBSTR";
  69. case OP_LEFT : return "OP_LEFT";
  70. case OP_RIGHT : return "OP_RIGHT";
  71. case OP_SIZE : return "OP_SIZE";
  72. // bit logic
  73. case OP_INVERT : return "OP_INVERT";
  74. case OP_AND : return "OP_AND";
  75. case OP_OR : return "OP_OR";
  76. case OP_XOR : return "OP_XOR";
  77. case OP_EQUAL : return "OP_EQUAL";
  78. case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
  79. case OP_RESERVED1 : return "OP_RESERVED1";
  80. case OP_RESERVED2 : return "OP_RESERVED2";
  81. // numeric
  82. case OP_1ADD : return "OP_1ADD";
  83. case OP_1SUB : return "OP_1SUB";
  84. case OP_2MUL : return "OP_2MUL";
  85. case OP_2DIV : return "OP_2DIV";
  86. case OP_NEGATE : return "OP_NEGATE";
  87. case OP_ABS : return "OP_ABS";
  88. case OP_NOT : return "OP_NOT";
  89. case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
  90. case OP_ADD : return "OP_ADD";
  91. case OP_SUB : return "OP_SUB";
  92. case OP_MUL : return "OP_MUL";
  93. case OP_DIV : return "OP_DIV";
  94. case OP_MOD : return "OP_MOD";
  95. case OP_LSHIFT : return "OP_LSHIFT";
  96. case OP_RSHIFT : return "OP_RSHIFT";
  97. case OP_BOOLAND : return "OP_BOOLAND";
  98. case OP_BOOLOR : return "OP_BOOLOR";
  99. case OP_NUMEQUAL : return "OP_NUMEQUAL";
  100. case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
  101. case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
  102. case OP_LESSTHAN : return "OP_LESSTHAN";
  103. case OP_GREATERTHAN : return "OP_GREATERTHAN";
  104. case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
  105. case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
  106. case OP_MIN : return "OP_MIN";
  107. case OP_MAX : return "OP_MAX";
  108. case OP_WITHIN : return "OP_WITHIN";
  109. // crypto
  110. case OP_RIPEMD160 : return "OP_RIPEMD160";
  111. case OP_SHA1 : return "OP_SHA1";
  112. case OP_SHA256 : return "OP_SHA256";
  113. case OP_HASH160 : return "OP_HASH160";
  114. case OP_HASH256 : return "OP_HASH256";
  115. case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
  116. case OP_CHECKSIG : return "OP_CHECKSIG";
  117. case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
  118. case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
  119. case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
  120. // expansion
  121. case OP_NOP1 : return "OP_NOP1";
  122. case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
  123. case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
  124. case OP_NOP4 : return "OP_NOP4";
  125. case OP_NOP5 : return "OP_NOP5";
  126. case OP_NOP6 : return "OP_NOP6";
  127. case OP_NOP7 : return "OP_NOP7";
  128. case OP_NOP8 : return "OP_NOP8";
  129. case OP_NOP9 : return "OP_NOP9";
  130. case OP_NOP10 : return "OP_NOP10";
  131. // Opcode added by BIP 342 (Tapscript)
  132. case OP_CHECKSIGADD : return "OP_CHECKSIGADD";
  133. case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
  134. default:
  135. return "OP_UNKNOWN";
  136. }
  137. }
  138. unsigned int CScript::GetSigOpCount(bool fAccurate) const
  139. {
  140. unsigned int n = 0;
  141. const_iterator pc = begin();
  142. opcodetype lastOpcode = OP_INVALIDOPCODE;
  143. while (pc < end())
  144. {
  145. opcodetype opcode;
  146. if (!GetOp(pc, opcode))
  147. break;
  148. if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
  149. n++;
  150. else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
  151. {
  152. if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
  153. n += DecodeOP_N(lastOpcode);
  154. else
  155. n += MAX_PUBKEYS_PER_MULTISIG;
  156. }
  157. lastOpcode = opcode;
  158. }
  159. return n;
  160. }
  161. unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
  162. {
  163. if (!IsPayToScriptHash())
  164. return GetSigOpCount(true);
  165. // This is a pay-to-script-hash scriptPubKey;
  166. // get the last item that the scriptSig
  167. // pushes onto the stack:
  168. const_iterator pc = scriptSig.begin();
  169. std::vector<unsigned char> vData;
  170. while (pc < scriptSig.end())
  171. {
  172. opcodetype opcode;
  173. if (!scriptSig.GetOp(pc, opcode, vData))
  174. return 0;
  175. if (opcode > OP_16)
  176. return 0;
  177. }
  178. /// ... and return its opcount:
  179. CScript subscript(vData.begin(), vData.end());
  180. return subscript.GetSigOpCount(true);
  181. }
  182. bool CScript::IsPayToScriptHash() const
  183. {
  184. // Extra-fast test for pay-to-script-hash CScripts:
  185. return (this->size() == 23 &&
  186. (*this)[0] == OP_HASH160 &&
  187. (*this)[1] == 0x14 &&
  188. (*this)[22] == OP_EQUAL);
  189. }
  190. bool CScript::IsPayToWitnessScriptHash() const
  191. {
  192. // Extra-fast test for pay-to-witness-script-hash CScripts:
  193. return (this->size() == 34 &&
  194. (*this)[0] == OP_0 &&
  195. (*this)[1] == 0x20);
  196. }
  197. // A witness program is any valid CScript that consists of a 1-byte push opcode
  198. // followed by a data push between 2 and 40 bytes.
  199. bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
  200. {
  201. if (this->size() < 4 || this->size() > 42) {
  202. return false;
  203. }
  204. if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
  205. return false;
  206. }
  207. if ((size_t)((*this)[1] + 2) == this->size()) {
  208. version = DecodeOP_N((opcodetype)(*this)[0]);
  209. program = std::vector<unsigned char>(this->begin() + 2, this->end());
  210. return true;
  211. }
  212. return false;
  213. }
  214. bool CScript::IsPushOnly(const_iterator pc) const
  215. {
  216. while (pc < end())
  217. {
  218. opcodetype opcode;
  219. if (!GetOp(pc, opcode))
  220. return false;
  221. // Note that IsPushOnly() *does* consider OP_RESERVED to be a
  222. // push-type opcode, however execution of OP_RESERVED fails, so
  223. // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
  224. // the P2SH special validation code being executed.
  225. if (opcode > OP_16)
  226. return false;
  227. }
  228. return true;
  229. }
  230. bool CScript::IsPushOnly() const
  231. {
  232. return this->IsPushOnly(begin());
  233. }
  234. std::string CScriptWitness::ToString() const
  235. {
  236. std::string ret = "CScriptWitness(";
  237. for (unsigned int i = 0; i < stack.size(); i++) {
  238. if (i) {
  239. ret += ", ";
  240. }
  241. ret += HexStr(stack[i]);
  242. }
  243. return ret + ")";
  244. }
  245. bool CScript::HasValidOps() const
  246. {
  247. CScript::const_iterator it = begin();
  248. while (it < end()) {
  249. opcodetype opcode;
  250. std::vector<unsigned char> item;
  251. if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
  252. return false;
  253. }
  254. }
  255. return true;
  256. }
  257. bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
  258. {
  259. opcodeRet = OP_INVALIDOPCODE;
  260. if (pvchRet)
  261. pvchRet->clear();
  262. if (pc >= end)
  263. return false;
  264. // Read instruction
  265. if (end - pc < 1)
  266. return false;
  267. unsigned int opcode = *pc++;
  268. // Immediate operand
  269. if (opcode <= OP_PUSHDATA4)
  270. {
  271. unsigned int nSize = 0;
  272. if (opcode < OP_PUSHDATA1)
  273. {
  274. nSize = opcode;
  275. }
  276. else if (opcode == OP_PUSHDATA1)
  277. {
  278. if (end - pc < 1)
  279. return false;
  280. nSize = *pc++;
  281. }
  282. else if (opcode == OP_PUSHDATA2)
  283. {
  284. if (end - pc < 2)
  285. return false;
  286. nSize = ReadLE16(&pc[0]);
  287. pc += 2;
  288. }
  289. else if (opcode == OP_PUSHDATA4)
  290. {
  291. if (end - pc < 4)
  292. return false;
  293. nSize = ReadLE32(&pc[0]);
  294. pc += 4;
  295. }
  296. if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
  297. return false;
  298. if (pvchRet)
  299. pvchRet->assign(pc, pc + nSize);
  300. pc += nSize;
  301. }
  302. opcodeRet = static_cast<opcodetype>(opcode);
  303. return true;
  304. }
  305. bool IsOpSuccess(const opcodetype& opcode)
  306. {
  307. return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
  308. (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
  309. (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
  310. (opcode >= 187 && opcode <= 254);
  311. }