Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

script.cpp 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 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.h"
  6. #include "tinyformat.h"
  7. #include "utilstrencodings.h"
  8. namespace {
  9. inline std::string ValueString(const std::vector<unsigned char>& vch)
  10. {
  11. if (vch.size() <= 4)
  12. return strprintf("%d", CScriptNum(vch, false).getint());
  13. else
  14. return HexStr(vch);
  15. }
  16. } // anon namespace
  17. using namespace std;
  18. const char* GetOpName(opcodetype opcode)
  19. {
  20. switch (opcode)
  21. {
  22. // push value
  23. case OP_0 : return "0";
  24. case OP_PUSHDATA1 : return "OP_PUSHDATA1";
  25. case OP_PUSHDATA2 : return "OP_PUSHDATA2";
  26. case OP_PUSHDATA4 : return "OP_PUSHDATA4";
  27. case OP_1NEGATE : return "-1";
  28. case OP_RESERVED : return "OP_RESERVED";
  29. case OP_1 : return "1";
  30. case OP_2 : return "2";
  31. case OP_3 : return "3";
  32. case OP_4 : return "4";
  33. case OP_5 : return "5";
  34. case OP_6 : return "6";
  35. case OP_7 : return "7";
  36. case OP_8 : return "8";
  37. case OP_9 : return "9";
  38. case OP_10 : return "10";
  39. case OP_11 : return "11";
  40. case OP_12 : return "12";
  41. case OP_13 : return "13";
  42. case OP_14 : return "14";
  43. case OP_15 : return "15";
  44. case OP_16 : return "16";
  45. // control
  46. case OP_NOP : return "OP_NOP";
  47. case OP_VER : return "OP_VER";
  48. case OP_IF : return "OP_IF";
  49. case OP_NOTIF : return "OP_NOTIF";
  50. case OP_VERIF : return "OP_VERIF";
  51. case OP_VERNOTIF : return "OP_VERNOTIF";
  52. case OP_ELSE : return "OP_ELSE";
  53. case OP_ENDIF : return "OP_ENDIF";
  54. case OP_VERIFY : return "OP_VERIFY";
  55. case OP_RETURN : return "OP_RETURN";
  56. // stack ops
  57. case OP_TOALTSTACK : return "OP_TOALTSTACK";
  58. case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
  59. case OP_2DROP : return "OP_2DROP";
  60. case OP_2DUP : return "OP_2DUP";
  61. case OP_3DUP : return "OP_3DUP";
  62. case OP_2OVER : return "OP_2OVER";
  63. case OP_2ROT : return "OP_2ROT";
  64. case OP_2SWAP : return "OP_2SWAP";
  65. case OP_IFDUP : return "OP_IFDUP";
  66. case OP_DEPTH : return "OP_DEPTH";
  67. case OP_DROP : return "OP_DROP";
  68. case OP_DUP : return "OP_DUP";
  69. case OP_NIP : return "OP_NIP";
  70. case OP_OVER : return "OP_OVER";
  71. case OP_PICK : return "OP_PICK";
  72. case OP_ROLL : return "OP_ROLL";
  73. case OP_ROT : return "OP_ROT";
  74. case OP_SWAP : return "OP_SWAP";
  75. case OP_TUCK : return "OP_TUCK";
  76. // splice ops
  77. case OP_CAT : return "OP_CAT";
  78. case OP_SUBSTR : return "OP_SUBSTR";
  79. case OP_LEFT : return "OP_LEFT";
  80. case OP_RIGHT : return "OP_RIGHT";
  81. case OP_SIZE : return "OP_SIZE";
  82. // bit logic
  83. case OP_INVERT : return "OP_INVERT";
  84. case OP_AND : return "OP_AND";
  85. case OP_OR : return "OP_OR";
  86. case OP_XOR : return "OP_XOR";
  87. case OP_EQUAL : return "OP_EQUAL";
  88. case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
  89. case OP_RESERVED1 : return "OP_RESERVED1";
  90. case OP_RESERVED2 : return "OP_RESERVED2";
  91. // numeric
  92. case OP_1ADD : return "OP_1ADD";
  93. case OP_1SUB : return "OP_1SUB";
  94. case OP_2MUL : return "OP_2MUL";
  95. case OP_2DIV : return "OP_2DIV";
  96. case OP_NEGATE : return "OP_NEGATE";
  97. case OP_ABS : return "OP_ABS";
  98. case OP_NOT : return "OP_NOT";
  99. case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
  100. case OP_ADD : return "OP_ADD";
  101. case OP_SUB : return "OP_SUB";
  102. case OP_MUL : return "OP_MUL";
  103. case OP_DIV : return "OP_DIV";
  104. case OP_MOD : return "OP_MOD";
  105. case OP_LSHIFT : return "OP_LSHIFT";
  106. case OP_RSHIFT : return "OP_RSHIFT";
  107. case OP_BOOLAND : return "OP_BOOLAND";
  108. case OP_BOOLOR : return "OP_BOOLOR";
  109. case OP_NUMEQUAL : return "OP_NUMEQUAL";
  110. case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
  111. case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
  112. case OP_LESSTHAN : return "OP_LESSTHAN";
  113. case OP_GREATERTHAN : return "OP_GREATERTHAN";
  114. case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
  115. case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
  116. case OP_MIN : return "OP_MIN";
  117. case OP_MAX : return "OP_MAX";
  118. case OP_WITHIN : return "OP_WITHIN";
  119. // crypto
  120. case OP_RIPEMD160 : return "OP_RIPEMD160";
  121. case OP_SHA1 : return "OP_SHA1";
  122. case OP_SHA256 : return "OP_SHA256";
  123. case OP_HASH160 : return "OP_HASH160";
  124. case OP_HASH256 : return "OP_HASH256";
  125. case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
  126. case OP_CHECKSIG : return "OP_CHECKSIG";
  127. case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
  128. case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
  129. case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
  130. // expanson
  131. case OP_NOP1 : return "OP_NOP1";
  132. case OP_NOP2 : return "OP_NOP2";
  133. case OP_NOP3 : return "OP_NOP3";
  134. case OP_NOP4 : return "OP_NOP4";
  135. case OP_NOP5 : return "OP_NOP5";
  136. case OP_NOP6 : return "OP_NOP6";
  137. case OP_NOP7 : return "OP_NOP7";
  138. case OP_NOP8 : return "OP_NOP8";
  139. case OP_NOP9 : return "OP_NOP9";
  140. case OP_NOP10 : return "OP_NOP10";
  141. case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
  142. // Note:
  143. // The template matching params OP_SMALLDATA/etc are defined in opcodetype enum
  144. // as kind of implementation hack, they are *NOT* real opcodes. If found in real
  145. // Script, just let the default: case deal with them.
  146. default:
  147. return "OP_UNKNOWN";
  148. }
  149. }
  150. unsigned int CScript::GetSigOpCount(bool fAccurate) const
  151. {
  152. unsigned int n = 0;
  153. const_iterator pc = begin();
  154. opcodetype lastOpcode = OP_INVALIDOPCODE;
  155. while (pc < end())
  156. {
  157. opcodetype opcode;
  158. if (!GetOp(pc, opcode))
  159. break;
  160. if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
  161. n++;
  162. else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
  163. {
  164. if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
  165. n += DecodeOP_N(lastOpcode);
  166. else
  167. n += 20;
  168. }
  169. lastOpcode = opcode;
  170. }
  171. return n;
  172. }
  173. unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
  174. {
  175. if (!IsPayToScriptHash())
  176. return GetSigOpCount(true);
  177. // This is a pay-to-script-hash scriptPubKey;
  178. // get the last item that the scriptSig
  179. // pushes onto the stack:
  180. const_iterator pc = scriptSig.begin();
  181. vector<unsigned char> data;
  182. while (pc < scriptSig.end())
  183. {
  184. opcodetype opcode;
  185. if (!scriptSig.GetOp(pc, opcode, data))
  186. return 0;
  187. if (opcode > OP_16)
  188. return 0;
  189. }
  190. /// ... and return its opcount:
  191. CScript subscript(data.begin(), data.end());
  192. return subscript.GetSigOpCount(true);
  193. }
  194. bool CScript::IsPayToScriptHash() const
  195. {
  196. // Extra-fast test for pay-to-script-hash CScripts:
  197. return (this->size() == 23 &&
  198. this->at(0) == OP_HASH160 &&
  199. this->at(1) == 0x14 &&
  200. this->at(22) == OP_EQUAL);
  201. }
  202. bool CScript::IsPushOnly() const
  203. {
  204. const_iterator pc = begin();
  205. while (pc < end())
  206. {
  207. opcodetype opcode;
  208. if (!GetOp(pc, opcode))
  209. return false;
  210. // Note that IsPushOnly() *does* consider OP_RESERVED to be a
  211. // push-type opcode, however execution of OP_RESERVED fails, so
  212. // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
  213. // the P2SH special validation code being executed.
  214. if (opcode > OP_16)
  215. return false;
  216. }
  217. return true;
  218. }
  219. std::string CScript::ToString() const
  220. {
  221. std::string str;
  222. opcodetype opcode;
  223. std::vector<unsigned char> vch;
  224. const_iterator pc = begin();
  225. while (pc < end())
  226. {
  227. if (!str.empty())
  228. str += " ";
  229. if (!GetOp(pc, opcode, vch))
  230. {
  231. str += "[error]";
  232. return str;
  233. }
  234. if (0 <= opcode && opcode <= OP_PUSHDATA4)
  235. str += ValueString(vch);
  236. else
  237. str += GetOpName(opcode);
  238. }
  239. return str;
  240. }
  241. size_t CScript::DynamicMemoryUsage() const
  242. {
  243. return memusage::DynamicUsage(*(static_cast<const std::vector<unsigned char>*>(this)));
  244. }