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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels 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. 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. // Note:
  133. // The template matching params OP_SMALLINTEGER/etc are defined in opcodetype enum
  134. // as kind of implementation hack, they are *NOT* real opcodes. If found in real
  135. // Script, just let the default: case deal with them.
  136. default:
  137. return "OP_UNKNOWN";
  138. }
  139. }
  140. unsigned int CScript::GetSigOpCount(bool fAccurate) const
  141. {
  142. unsigned int n = 0;
  143. const_iterator pc = begin();
  144. opcodetype lastOpcode = OP_INVALIDOPCODE;
  145. while (pc < end())
  146. {
  147. opcodetype opcode;
  148. if (!GetOp(pc, opcode))
  149. break;
  150. if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
  151. n++;
  152. else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
  153. {
  154. if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
  155. n += DecodeOP_N(lastOpcode);
  156. else
  157. n += MAX_PUBKEYS_PER_MULTISIG;
  158. }
  159. lastOpcode = opcode;
  160. }
  161. return n;
  162. }
  163. unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
  164. {
  165. if (!IsPayToScriptHash())
  166. return GetSigOpCount(true);
  167. // This is a pay-to-script-hash scriptPubKey;
  168. // get the last item that the scriptSig
  169. // pushes onto the stack:
  170. const_iterator pc = scriptSig.begin();
  171. std::vector<unsigned char> vData;
  172. while (pc < scriptSig.end())
  173. {
  174. opcodetype opcode;
  175. if (!scriptSig.GetOp(pc, opcode, vData))
  176. return 0;
  177. if (opcode > OP_16)
  178. return 0;
  179. }
  180. /// ... and return its opcount:
  181. CScript subscript(vData.begin(), vData.end());
  182. return subscript.GetSigOpCount(true);
  183. }
  184. bool CScript::IsPayToScriptHash() const
  185. {
  186. // Extra-fast test for pay-to-script-hash CScripts:
  187. return (this->size() == 23 &&
  188. (*this)[0] == OP_HASH160 &&
  189. (*this)[1] == 0x14 &&
  190. (*this)[22] == OP_EQUAL);
  191. }
  192. bool CScript::IsPayToWitnessScriptHash() const
  193. {
  194. // Extra-fast test for pay-to-witness-script-hash CScripts:
  195. return (this->size() == 34 &&
  196. (*this)[0] == OP_0 &&
  197. (*this)[1] == 0x20);
  198. }
  199. // A witness program is any valid CScript that consists of a 1-byte push opcode
  200. // followed by a data push between 2 and 40 bytes.
  201. bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
  202. {
  203. if (this->size() < 4 || this->size() > 42) {
  204. return false;
  205. }
  206. if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
  207. return false;
  208. }
  209. if ((size_t)((*this)[1] + 2) == this->size()) {
  210. version = DecodeOP_N((opcodetype)(*this)[0]);
  211. program = std::vector<unsigned char>(this->begin() + 2, this->end());
  212. return true;
  213. }
  214. return false;
  215. }
  216. bool CScript::IsPushOnly(const_iterator pc) const
  217. {
  218. while (pc < end())
  219. {
  220. opcodetype opcode;
  221. if (!GetOp(pc, opcode))
  222. return false;
  223. // Note that IsPushOnly() *does* consider OP_RESERVED to be a
  224. // push-type opcode, however execution of OP_RESERVED fails, so
  225. // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
  226. // the P2SH special validation code being executed.
  227. if (opcode > OP_16)
  228. return false;
  229. }
  230. return true;
  231. }
  232. bool CScript::IsPushOnly() const
  233. {
  234. return this->IsPushOnly(begin());
  235. }
  236. std::string CScriptWitness::ToString() const
  237. {
  238. std::string ret = "CScriptWitness(";
  239. for (unsigned int i = 0; i < stack.size(); i++) {
  240. if (i) {
  241. ret += ", ";
  242. }
  243. ret += HexStr(stack[i]);
  244. }
  245. return ret + ")";
  246. }
  247. bool CScript::HasValidOps() const
  248. {
  249. CScript::const_iterator it = begin();
  250. while (it < end()) {
  251. opcodetype opcode;
  252. std::vector<unsigned char> item;
  253. if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
  254. return false;
  255. }
  256. }
  257. return true;
  258. }