bitcoin/bitcoin/blob/master/src/script/script.cpp


SUBMITTED BY: mnurul199

DATE: Jan. 14, 2019, 8:57 p.m.

FORMAT: Text only

SIZE: 12.1 kB

HITS: 166

  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2018 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 <tinyformat.h>
  7. #include <util/strencodings.h>
  8. const char* 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. case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
  132. default:
  133. return "OP_UNKNOWN";
  134. }
  135. }
  136. unsigned int CScript::GetSigOpCount(bool fAccurate) const
  137. {
  138. unsigned int n = 0;
  139. const_iterator pc = begin();
  140. opcodetype lastOpcode = OP_INVALIDOPCODE;
  141. while (pc < end())
  142. {
  143. opcodetype opcode;
  144. if (!GetOp(pc, opcode))
  145. break;
  146. if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
  147. n++;
  148. else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
  149. {
  150. if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
  151. n += DecodeOP_N(lastOpcode);
  152. else
  153. n += MAX_PUBKEYS_PER_MULTISIG;
  154. }
  155. lastOpcode = opcode;
  156. }
  157. return n;
  158. }
  159. unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
  160. {
  161. if (!IsPayToScriptHash())
  162. return GetSigOpCount(true);
  163. // This is a pay-to-script-hash scriptPubKey;
  164. // get the last item that the scriptSig
  165. // pushes onto the stack:
  166. const_iterator pc = scriptSig.begin();
  167. std::vector<unsigned char> vData;
  168. while (pc < scriptSig.end())
  169. {
  170. opcodetype opcode;
  171. if (!scriptSig.GetOp(pc, opcode, vData))
  172. return 0;
  173. if (opcode > OP_16)
  174. return 0;
  175. }
  176. /// ... and return its opcount:
  177. CScript subscript(vData.begin(), vData.end());
  178. return subscript.GetSigOpCount(true);
  179. }
  180. bool CScript::IsPayToScriptHash() const
  181. {
  182. // Extra-fast test for pay-to-script-hash CScripts:
  183. return (this->size() == 23 &&
  184. (*this)[0] == OP_HASH160 &&
  185. (*this)[1] == 0x14 &&
  186. (*this)[22] == OP_EQUAL);
  187. }
  188. bool CScript::IsPayToWitnessScriptHash() const
  189. {
  190. // Extra-fast test for pay-to-witness-script-hash CScripts:
  191. return (this->size() == 34 &&
  192. (*this)[0] == OP_0 &&
  193. (*this)[1] == 0x20);
  194. }
  195. // A witness program is any valid CScript that consists of a 1-byte push opcode
  196. // followed by a data push between 2 and 40 bytes.
  197. bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
  198. {
  199. if (this->size() < 4 || this->size() > 42) {
  200. return false;
  201. }
  202. if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
  203. return false;
  204. }
  205. if ((size_t)((*this)[1] + 2) == this->size()) {
  206. version = DecodeOP_N((opcodetype)(*this)[0]);
  207. program = std::vector<unsigned char>(this->begin() + 2, this->end());
  208. return true;
  209. }
  210. return false;
  211. }
  212. bool CScript::IsPushOnly(const_iterator pc) const
  213. {
  214. while (pc < end())
  215. {
  216. opcodetype opcode;
  217. if (!GetOp(pc, opcode))
  218. return false;
  219. // Note that IsPushOnly() *does* consider OP_RESERVED to be a
  220. // push-type opcode, however execution of OP_RESERVED fails, so
  221. // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
  222. // the P2SH special validation code being executed.
  223. if (opcode > OP_16)
  224. return false;
  225. }
  226. return true;
  227. }
  228. bool CScript::IsPushOnly() const
  229. {
  230. return this->IsPushOnly(begin());
  231. }
  232. std::string CScriptWitness::ToString() const
  233. {
  234. std::string ret = "CScriptWitness(";
  235. for (unsigned int i = 0; i < stack.size(); i++) {
  236. if (i) {
  237. ret += ", ";
  238. }
  239. ret += HexStr(stack[i]);
  240. }
  241. return ret + ")";
  242. }
  243. bool CScript::HasValidOps() const
  244. {
  245. CScript::const_iterator it = begin();
  246. while (it < end()) {
  247. opcodetype opcode;
  248. std::vector<unsigned char> item;
  249. if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
  250. return false;
  251. }
  252. }
  253. return true;
  254. }
  255. bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
  256. {
  257. opcodeRet = OP_INVALIDOPCODE;
  258. if (pvchRet)
  259. pvchRet->clear();
  260. if (pc >= end)
  261. return false;
  262. // Read instruction
  263. if (end - pc < 1)
  264. return false;
  265. unsigned int opcode = *pc++;
  266. // Immediate operand
  267. if (opcode <= OP_PUSHDATA4)
  268. {
  269. unsigned int nSize = 0;
  270. if (opcode < OP_PUSHDATA1)
  271. {
  272. nSize = opcode;
  273. }
  274. else if (opcode == OP_PUSHDATA1)
  275. {
  276. if (end - pc < 1)
  277. return false;
  278. nSize = *pc++;
  279. }
  280. else if (opcode == OP_PUSHDATA2)
  281. {
  282. if (end - pc < 2)
  283. return false;
  284. nSize = ReadLE16(&pc[0]);
  285. pc += 2;
  286. }
  287. else if (opcode == OP_PUSHDATA4)
  288. {
  289. if (end - pc < 4)
  290. return false;
  291. nSize = ReadLE32(&pc[0]);
  292. pc += 4;
  293. }
  294. if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
  295. return false;
  296. if (pvchRet)
  297. pvchRet->assign(pc, pc + nSize);
  298. pc += nSize;
  299. }
  300. opcodeRet = static_cast<opcodetype>(opcode);
  301. return true;
  302. }

comments powered by Disqus