PageRenderTime 68ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/src/script.h

https://github.com/dooglus/bitcoin
C Header | 571 lines | 464 code | 79 blank | 28 comment | 58 complexity | a469337ffe0ac1f5d9662fb3594bf097 MD5 | raw file
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file license.txt or http://www.opensource.org/licenses/mit-license.php.
  5. #ifndef H_BITCOIN_SCRIPT
  6. #define H_BITCOIN_SCRIPT
  7. #include "base58.h"
  8. #include <string>
  9. #include <vector>
  10. #include <boost/foreach.hpp>
  11. class CTransaction;
  12. class CKeyStore;
  13. enum
  14. {
  15. SIGHASH_ALL = 1,
  16. SIGHASH_NONE = 2,
  17. SIGHASH_SINGLE = 3,
  18. SIGHASH_ANYONECANPAY = 0x80,
  19. };
  20. enum txnouttype
  21. {
  22. TX_NONSTANDARD,
  23. // 'standard' transaction types:
  24. TX_PUBKEY,
  25. TX_PUBKEYHASH,
  26. TX_SCRIPTHASH,
  27. TX_MULTISIG,
  28. };
  29. const char* GetTxnOutputType(txnouttype t);
  30. enum opcodetype
  31. {
  32. // push value
  33. OP_0=0,
  34. OP_FALSE=OP_0,
  35. OP_PUSHDATA1=76,
  36. OP_PUSHDATA2,
  37. OP_PUSHDATA4,
  38. OP_1NEGATE,
  39. OP_RESERVED,
  40. OP_1,
  41. OP_TRUE=OP_1,
  42. OP_2,
  43. OP_3,
  44. OP_4,
  45. OP_5,
  46. OP_6,
  47. OP_7,
  48. OP_8,
  49. OP_9,
  50. OP_10,
  51. OP_11,
  52. OP_12,
  53. OP_13,
  54. OP_14,
  55. OP_15,
  56. OP_16,
  57. // control
  58. OP_NOP,
  59. OP_VER,
  60. OP_IF,
  61. OP_NOTIF,
  62. OP_VERIF,
  63. OP_VERNOTIF,
  64. OP_ELSE,
  65. OP_ENDIF,
  66. OP_VERIFY,
  67. OP_RETURN,
  68. // stack ops
  69. OP_TOALTSTACK,
  70. OP_FROMALTSTACK,
  71. OP_2DROP,
  72. OP_2DUP,
  73. OP_3DUP,
  74. OP_2OVER,
  75. OP_2ROT,
  76. OP_2SWAP,
  77. OP_IFDUP,
  78. OP_DEPTH,
  79. OP_DROP,
  80. OP_DUP,
  81. OP_NIP,
  82. OP_OVER,
  83. OP_PICK,
  84. OP_ROLL,
  85. OP_ROT,
  86. OP_SWAP,
  87. OP_TUCK,
  88. // splice ops
  89. OP_CAT,
  90. OP_SUBSTR,
  91. OP_LEFT,
  92. OP_RIGHT,
  93. OP_SIZE,
  94. // bit logic
  95. OP_INVERT,
  96. OP_AND,
  97. OP_OR,
  98. OP_XOR,
  99. OP_EQUAL,
  100. OP_EQUALVERIFY,
  101. OP_RESERVED1,
  102. OP_RESERVED2,
  103. // numeric
  104. OP_1ADD,
  105. OP_1SUB,
  106. OP_2MUL,
  107. OP_2DIV,
  108. OP_NEGATE,
  109. OP_ABS,
  110. OP_NOT,
  111. OP_0NOTEQUAL,
  112. OP_ADD,
  113. OP_SUB,
  114. OP_MUL,
  115. OP_DIV,
  116. OP_MOD,
  117. OP_LSHIFT,
  118. OP_RSHIFT,
  119. OP_BOOLAND,
  120. OP_BOOLOR,
  121. OP_NUMEQUAL,
  122. OP_NUMEQUALVERIFY,
  123. OP_NUMNOTEQUAL,
  124. OP_LESSTHAN,
  125. OP_GREATERTHAN,
  126. OP_LESSTHANOREQUAL,
  127. OP_GREATERTHANOREQUAL,
  128. OP_MIN,
  129. OP_MAX,
  130. OP_WITHIN,
  131. // crypto
  132. OP_RIPEMD160,
  133. OP_SHA1,
  134. OP_SHA256,
  135. OP_HASH160,
  136. OP_HASH256,
  137. OP_CODESEPARATOR,
  138. OP_CHECKSIG,
  139. OP_CHECKSIGVERIFY,
  140. OP_CHECKMULTISIG,
  141. OP_CHECKMULTISIGVERIFY,
  142. // expansion
  143. OP_NOP1,
  144. OP_NOP2,
  145. OP_NOP3,
  146. OP_NOP4,
  147. OP_NOP5,
  148. OP_NOP6,
  149. OP_NOP7,
  150. OP_NOP8,
  151. OP_NOP9,
  152. OP_NOP10,
  153. // template matching params
  154. OP_SMALLINTEGER = 0xfa,
  155. OP_PUBKEYS = 0xfb,
  156. OP_PUBKEYHASH = 0xfd,
  157. OP_PUBKEY = 0xfe,
  158. OP_INVALIDOPCODE = 0xff,
  159. };
  160. const char* GetOpName(opcodetype opcode);
  161. inline std::string ValueString(const std::vector<unsigned char>& vch)
  162. {
  163. if (vch.size() <= 4)
  164. return strprintf("%d", CBigNum(vch).getint());
  165. else
  166. return HexStr(vch);
  167. }
  168. inline std::string StackString(const std::vector<std::vector<unsigned char> >& vStack)
  169. {
  170. std::string str;
  171. BOOST_FOREACH(const std::vector<unsigned char>& vch, vStack)
  172. {
  173. if (!str.empty())
  174. str += " ";
  175. str += ValueString(vch);
  176. }
  177. return str;
  178. }
  179. class CScript : public std::vector<unsigned char>
  180. {
  181. protected:
  182. CScript& push_int64(int64 n)
  183. {
  184. if (n == -1 || (n >= 1 && n <= 16))
  185. {
  186. push_back(n + (OP_1 - 1));
  187. }
  188. else
  189. {
  190. CBigNum bn(n);
  191. *this << bn.getvch();
  192. }
  193. return *this;
  194. }
  195. CScript& push_uint64(uint64 n)
  196. {
  197. if (n >= 1 && n <= 16)
  198. {
  199. push_back(n + (OP_1 - 1));
  200. }
  201. else
  202. {
  203. CBigNum bn(n);
  204. *this << bn.getvch();
  205. }
  206. return *this;
  207. }
  208. public:
  209. CScript() { }
  210. CScript(const CScript& b) : std::vector<unsigned char>(b.begin(), b.end()) { }
  211. CScript(const_iterator pbegin, const_iterator pend) : std::vector<unsigned char>(pbegin, pend) { }
  212. #ifndef _MSC_VER
  213. CScript(const unsigned char* pbegin, const unsigned char* pend) : std::vector<unsigned char>(pbegin, pend) { }
  214. #endif
  215. CScript& operator+=(const CScript& b)
  216. {
  217. insert(end(), b.begin(), b.end());
  218. return *this;
  219. }
  220. friend CScript operator+(const CScript& a, const CScript& b)
  221. {
  222. CScript ret = a;
  223. ret += b;
  224. return ret;
  225. }
  226. explicit CScript(char b) { operator<<(b); }
  227. explicit CScript(short b) { operator<<(b); }
  228. explicit CScript(int b) { operator<<(b); }
  229. explicit CScript(long b) { operator<<(b); }
  230. explicit CScript(int64 b) { operator<<(b); }
  231. explicit CScript(unsigned char b) { operator<<(b); }
  232. explicit CScript(unsigned int b) { operator<<(b); }
  233. explicit CScript(unsigned short b) { operator<<(b); }
  234. explicit CScript(unsigned long b) { operator<<(b); }
  235. explicit CScript(uint64 b) { operator<<(b); }
  236. explicit CScript(opcodetype b) { operator<<(b); }
  237. explicit CScript(const uint256& b) { operator<<(b); }
  238. explicit CScript(const CBigNum& b) { operator<<(b); }
  239. explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
  240. CScript& operator<<(char b) { return push_int64(b); }
  241. CScript& operator<<(short b) { return push_int64(b); }
  242. CScript& operator<<(int b) { return push_int64(b); }
  243. CScript& operator<<(long b) { return push_int64(b); }
  244. CScript& operator<<(int64 b) { return push_int64(b); }
  245. CScript& operator<<(unsigned char b) { return push_uint64(b); }
  246. CScript& operator<<(unsigned int b) { return push_uint64(b); }
  247. CScript& operator<<(unsigned short b) { return push_uint64(b); }
  248. CScript& operator<<(unsigned long b) { return push_uint64(b); }
  249. CScript& operator<<(uint64 b) { return push_uint64(b); }
  250. CScript& operator<<(opcodetype opcode)
  251. {
  252. if (opcode < 0 || opcode > 0xff)
  253. throw std::runtime_error("CScript::operator<<() : invalid opcode");
  254. insert(end(), (unsigned char)opcode);
  255. return *this;
  256. }
  257. CScript& operator<<(const uint160& b)
  258. {
  259. insert(end(), sizeof(b));
  260. insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
  261. return *this;
  262. }
  263. CScript& operator<<(const uint256& b)
  264. {
  265. insert(end(), sizeof(b));
  266. insert(end(), (unsigned char*)&b, (unsigned char*)&b + sizeof(b));
  267. return *this;
  268. }
  269. CScript& operator<<(const CBigNum& b)
  270. {
  271. *this << b.getvch();
  272. return *this;
  273. }
  274. CScript& operator<<(const std::vector<unsigned char>& b)
  275. {
  276. if (b.size() < OP_PUSHDATA1)
  277. {
  278. insert(end(), (unsigned char)b.size());
  279. }
  280. else if (b.size() <= 0xff)
  281. {
  282. insert(end(), OP_PUSHDATA1);
  283. insert(end(), (unsigned char)b.size());
  284. }
  285. else if (b.size() <= 0xffff)
  286. {
  287. insert(end(), OP_PUSHDATA2);
  288. unsigned short nSize = b.size();
  289. insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
  290. }
  291. else
  292. {
  293. insert(end(), OP_PUSHDATA4);
  294. unsigned int nSize = b.size();
  295. insert(end(), (unsigned char*)&nSize, (unsigned char*)&nSize + sizeof(nSize));
  296. }
  297. insert(end(), b.begin(), b.end());
  298. return *this;
  299. }
  300. CScript& operator<<(const CScript& b)
  301. {
  302. // I'm not sure if this should push the script or concatenate scripts.
  303. // If there's ever a use for pushing a script onto a script, delete this member fn
  304. assert(!"warning: pushing a CScript onto a CScript with << is probably not intended, use + to concatenate");
  305. return *this;
  306. }
  307. bool GetOp(iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet)
  308. {
  309. // Wrapper so it can be called with either iterator or const_iterator
  310. const_iterator pc2 = pc;
  311. bool fRet = GetOp2(pc2, opcodeRet, &vchRet);
  312. pc = begin() + (pc2 - begin());
  313. return fRet;
  314. }
  315. bool GetOp(iterator& pc, opcodetype& opcodeRet)
  316. {
  317. const_iterator pc2 = pc;
  318. bool fRet = GetOp2(pc2, opcodeRet, NULL);
  319. pc = begin() + (pc2 - begin());
  320. return fRet;
  321. }
  322. bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
  323. {
  324. return GetOp2(pc, opcodeRet, &vchRet);
  325. }
  326. bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
  327. {
  328. return GetOp2(pc, opcodeRet, NULL);
  329. }
  330. bool GetOp2(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet) const
  331. {
  332. opcodeRet = OP_INVALIDOPCODE;
  333. if (pvchRet)
  334. pvchRet->clear();
  335. if (pc >= end())
  336. return false;
  337. // Read instruction
  338. if (end() - pc < 1)
  339. return false;
  340. unsigned int opcode = *pc++;
  341. // Immediate operand
  342. if (opcode <= OP_PUSHDATA4)
  343. {
  344. unsigned int nSize;
  345. if (opcode < OP_PUSHDATA1)
  346. {
  347. nSize = opcode;
  348. }
  349. else if (opcode == OP_PUSHDATA1)
  350. {
  351. if (end() - pc < 1)
  352. return false;
  353. nSize = *pc++;
  354. }
  355. else if (opcode == OP_PUSHDATA2)
  356. {
  357. if (end() - pc < 2)
  358. return false;
  359. nSize = 0;
  360. memcpy(&nSize, &pc[0], 2);
  361. pc += 2;
  362. }
  363. else if (opcode == OP_PUSHDATA4)
  364. {
  365. if (end() - pc < 4)
  366. return false;
  367. memcpy(&nSize, &pc[0], 4);
  368. pc += 4;
  369. }
  370. if (end() - pc < nSize)
  371. return false;
  372. if (pvchRet)
  373. pvchRet->assign(pc, pc + nSize);
  374. pc += nSize;
  375. }
  376. opcodeRet = (opcodetype)opcode;
  377. return true;
  378. }
  379. // Encode/decode small integers:
  380. static int DecodeOP_N(opcodetype opcode)
  381. {
  382. if (opcode == OP_0)
  383. return 0;
  384. assert(opcode >= OP_1 && opcode <= OP_16);
  385. return (int)opcode - (int)(OP_1 - 1);
  386. }
  387. static opcodetype EncodeOP_N(int n)
  388. {
  389. assert(n >= 0 && n <= 16);
  390. if (n == 0)
  391. return OP_0;
  392. return (opcodetype)(OP_1+n-1);
  393. }
  394. int FindAndDelete(const CScript& b)
  395. {
  396. int nFound = 0;
  397. if (b.empty())
  398. return nFound;
  399. iterator pc = begin();
  400. opcodetype opcode;
  401. do
  402. {
  403. while (end() - pc >= b.size() && memcmp(&pc[0], &b[0], b.size()) == 0)
  404. {
  405. erase(pc, pc + b.size());
  406. ++nFound;
  407. }
  408. }
  409. while (GetOp(pc, opcode));
  410. return nFound;
  411. }
  412. int Find(opcodetype op) const
  413. {
  414. int nFound = 0;
  415. opcodetype opcode;
  416. for (const_iterator pc = begin(); pc != end() && GetOp(pc, opcode);)
  417. if (opcode == op)
  418. ++nFound;
  419. return nFound;
  420. }
  421. // Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
  422. // as 20 sigops. With pay-to-script-hash, that changed:
  423. // CHECKMULTISIGs serialized in scriptSigs are
  424. // counted more accurately, assuming they are of the form
  425. // ... OP_N CHECKMULTISIG ...
  426. int GetSigOpCount(bool fAccurate) const;
  427. // Accurately count sigOps, including sigOps in
  428. // pay-to-script-hash transactions:
  429. int GetSigOpCount(const CScript& scriptSig) const;
  430. bool IsPayToScriptHash() const;
  431. // Called by CTransaction::IsStandard
  432. bool IsPushOnly() const
  433. {
  434. const_iterator pc = begin();
  435. while (pc < end())
  436. {
  437. opcodetype opcode;
  438. if (!GetOp(pc, opcode))
  439. return false;
  440. if (opcode > OP_16)
  441. return false;
  442. }
  443. return true;
  444. }
  445. void SetBitcoinAddress(const CBitcoinAddress& address);
  446. void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
  447. {
  448. SetBitcoinAddress(CBitcoinAddress(vchPubKey));
  449. }
  450. void SetMultisig(int nRequired, const std::vector<CKey>& keys);
  451. void SetPayToScriptHash(const CScript& subscript);
  452. void PrintHex() const
  453. {
  454. printf("CScript(%s)\n", HexStr(begin(), end(), true).c_str());
  455. }
  456. std::string ToString() const
  457. {
  458. std::string str;
  459. opcodetype opcode;
  460. std::vector<unsigned char> vch;
  461. const_iterator pc = begin();
  462. while (pc < end())
  463. {
  464. if (!str.empty())
  465. str += " ";
  466. if (!GetOp(pc, opcode, vch))
  467. {
  468. str += "[error]";
  469. return str;
  470. }
  471. if (0 <= opcode && opcode <= OP_PUSHDATA4)
  472. str += ValueString(vch);
  473. else
  474. str += GetOpName(opcode);
  475. }
  476. return str;
  477. }
  478. void print() const
  479. {
  480. printf("%s\n", ToString().c_str());
  481. }
  482. };
  483. bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, const CTransaction& txTo, unsigned int nIn, int nHashType);
  484. bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<std::vector<unsigned char> >& vSolutionsRet);
  485. int ScriptSigArgsExpected(txnouttype t, const std::vector<std::vector<unsigned char> >& vSolutions);
  486. bool IsStandard(const CScript& scriptPubKey);
  487. bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
  488. bool ExtractAddress(const CScript& scriptPubKey, CBitcoinAddress& addressRet);
  489. bool ExtractAddresses(const CScript& scriptPubKey, txnouttype& typeRet, std::vector<CBitcoinAddress>& addressRet, int& nRequiredRet);
  490. bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL);
  491. bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, bool fValidatePayToScriptHash, int nHashType);
  492. #endif