You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

interpreter.h 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. #ifndef STARWELS_SCRIPT_INTERPRETER_H
  6. #define STARWELS_SCRIPT_INTERPRETER_H
  7. #include "script_error.h"
  8. #include "primitives/transaction.h"
  9. #include <vector>
  10. #include <stdint.h>
  11. #include <string>
  12. class CPubKey;
  13. class CScript;
  14. class CTransaction;
  15. class uint256;
  16. /** Signature hash types/flags */
  17. enum
  18. {
  19. SIGHASH_ALL = 1,
  20. SIGHASH_NONE = 2,
  21. SIGHASH_SINGLE = 3,
  22. SIGHASH_ANYONECANPAY = 0x80,
  23. };
  24. /** Script verification flags */
  25. enum
  26. {
  27. SCRIPT_VERIFY_NONE = 0,
  28. // Evaluate P2SH subscripts (softfork safe, BIP16).
  29. SCRIPT_VERIFY_P2SH = (1U << 0),
  30. // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
  31. // Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
  32. // (softfork safe, but not used or intended as a consensus rule).
  33. SCRIPT_VERIFY_STRICTENC = (1U << 1),
  34. // Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, BIP62 rule 1)
  35. SCRIPT_VERIFY_DERSIG = (1U << 2),
  36. // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
  37. // (softfork safe, BIP62 rule 5).
  38. SCRIPT_VERIFY_LOW_S = (1U << 3),
  39. // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7).
  40. SCRIPT_VERIFY_NULLDUMMY = (1U << 4),
  41. // Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2).
  42. SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),
  43. // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
  44. // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
  45. // any other push causes the script to fail (BIP62 rule 3).
  46. // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
  47. // (softfork safe)
  48. SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
  49. // Discourage use of NOPs reserved for upgrades (NOP1-10)
  50. //
  51. // Provided so that nodes can avoid accepting or mining transactions
  52. // containing executed NOP's whose meaning may change after a soft-fork,
  53. // thus rendering the script invalid; with this flag set executing
  54. // discouraged NOPs fails the script. This verification flag will never be
  55. // a mandatory flag applied to scripts in a block. NOPs that are not
  56. // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
  57. SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),
  58. // Require that only a single stack element remains after evaluation. This changes the success criterion from
  59. // "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
  60. // "Exactly one stack element must remain, and when interpreted as a boolean, it must be true".
  61. // (softfork safe, BIP62 rule 6)
  62. // Note: CLEANSTACK should never be used without P2SH or WITNESS.
  63. SCRIPT_VERIFY_CLEANSTACK = (1U << 8),
  64. // Verify CHECKLOCKTIMEVERIFY
  65. //
  66. // See BIP65 for details.
  67. SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),
  68. // support CHECKSEQUENCEVERIFY opcode
  69. //
  70. // See BIP112 for details
  71. SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10),
  72. // Support segregated witness
  73. //
  74. SCRIPT_VERIFY_WITNESS = (1U << 11),
  75. // Making v1-v16 witness program non-standard
  76. //
  77. SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM = (1U << 12),
  78. // Segwit script only: Require the argument of OP_IF/NOTIF to be exactly 0x01 or empty vector
  79. //
  80. SCRIPT_VERIFY_MINIMALIF = (1U << 13),
  81. // Signature(s) must be empty vector if an CHECK(MULTI)SIG operation failed
  82. //
  83. SCRIPT_VERIFY_NULLFAIL = (1U << 14),
  84. // Public keys in segregated witness scripts must be compressed
  85. //
  86. SCRIPT_VERIFY_WITNESS_PUBKEYTYPE = (1U << 15),
  87. };
  88. bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
  89. struct PrecomputedTransactionData
  90. {
  91. uint256 hashPrevouts, hashSequence, hashOutputs;
  92. PrecomputedTransactionData(const CTransaction& tx);
  93. };
  94. enum SigVersion
  95. {
  96. SIGVERSION_BASE = 0,
  97. SIGVERSION_WITNESS_V0 = 1,
  98. };
  99. uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache = nullptr);
  100. class BaseSignatureChecker
  101. {
  102. public:
  103. virtual bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
  104. {
  105. return false;
  106. }
  107. virtual bool CheckLockTime(const CScriptNum& nLockTime) const
  108. {
  109. return false;
  110. }
  111. virtual bool CheckSequence(const CScriptNum& nSequence) const
  112. {
  113. return false;
  114. }
  115. virtual ~BaseSignatureChecker() {}
  116. };
  117. class TransactionSignatureChecker : public BaseSignatureChecker
  118. {
  119. private:
  120. const CTransaction* txTo;
  121. unsigned int nIn;
  122. const CAmount amount;
  123. const PrecomputedTransactionData* txdata;
  124. protected:
  125. virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
  126. public:
  127. TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
  128. TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
  129. bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override;
  130. bool CheckLockTime(const CScriptNum& nLockTime) const override;
  131. bool CheckSequence(const CScriptNum& nSequence) const override;
  132. };
  133. class MutableTransactionSignatureChecker : public TransactionSignatureChecker
  134. {
  135. private:
  136. const CTransaction txTo;
  137. public:
  138. MutableTransactionSignatureChecker(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn) : TransactionSignatureChecker(&txTo, nInIn, amountIn), txTo(*txToIn) {}
  139. };
  140. bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* error = nullptr);
  141. bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror = nullptr);
  142. size_t CountWitnessSigOps(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags);
  143. #endif // STARWELS_SCRIPT_INTERPRETER_H