Merge #10699: Make all script validation flags backward compatible
[bitcoinplatinum.git] / src / script / interpreter.h
blob83a96739b1704df0c1894153d42ea2ca7c695b08
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
6 #ifndef BITCOIN_SCRIPT_INTERPRETER_H
7 #define BITCOIN_SCRIPT_INTERPRETER_H
9 #include <script/script_error.h>
10 #include <primitives/transaction.h>
12 #include <vector>
13 #include <stdint.h>
14 #include <string>
16 class CPubKey;
17 class CScript;
18 class CTransaction;
19 class uint256;
21 /** Signature hash types/flags */
22 enum
24 SIGHASH_ALL = 1,
25 SIGHASH_NONE = 2,
26 SIGHASH_SINGLE = 3,
27 SIGHASH_ANYONECANPAY = 0x80,
30 /** Script verification flags.
32 * All flags are intended to be soft forks: the set of acceptable scripts under
33 * flags (A | B) is a subset of the acceptable scripts under flag (A).
35 enum
37 SCRIPT_VERIFY_NONE = 0,
39 // Evaluate P2SH subscripts (BIP16).
40 SCRIPT_VERIFY_P2SH = (1U << 0),
42 // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
43 // Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
44 // (not used or intended as a consensus rule).
45 SCRIPT_VERIFY_STRICTENC = (1U << 1),
47 // Passing a non-strict-DER signature to a checksig operation causes script failure (BIP62 rule 1)
48 SCRIPT_VERIFY_DERSIG = (1U << 2),
50 // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
51 // (BIP62 rule 5).
52 SCRIPT_VERIFY_LOW_S = (1U << 3),
54 // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (BIP62 rule 7).
55 SCRIPT_VERIFY_NULLDUMMY = (1U << 4),
57 // Using a non-push operator in the scriptSig causes script failure (BIP62 rule 2).
58 SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),
60 // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
61 // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
62 // any other push causes the script to fail (BIP62 rule 3).
63 // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
64 SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
66 // Discourage use of NOPs reserved for upgrades (NOP1-10)
68 // Provided so that nodes can avoid accepting or mining transactions
69 // containing executed NOP's whose meaning may change after a soft-fork,
70 // thus rendering the script invalid; with this flag set executing
71 // discouraged NOPs fails the script. This verification flag will never be
72 // a mandatory flag applied to scripts in a block. NOPs that are not
73 // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
74 // NOPs that have associated forks to give them new meaning (CLTV, CSV)
75 // are not subject to this rule.
76 SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7),
78 // Require that only a single stack element remains after evaluation. This changes the success criterion from
79 // "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
80 // "Exactly one stack element must remain, and when interpreted as a boolean, it must be true".
81 // (BIP62 rule 6)
82 // Note: CLEANSTACK should never be used without P2SH or WITNESS.
83 SCRIPT_VERIFY_CLEANSTACK = (1U << 8),
85 // Verify CHECKLOCKTIMEVERIFY
87 // See BIP65 for details.
88 SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),
90 // support CHECKSEQUENCEVERIFY opcode
92 // See BIP112 for details
93 SCRIPT_VERIFY_CHECKSEQUENCEVERIFY = (1U << 10),
95 // Support segregated witness
97 SCRIPT_VERIFY_WITNESS = (1U << 11),
99 // Making v1-v16 witness program non-standard
101 SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM = (1U << 12),
103 // Segwit script only: Require the argument of OP_IF/NOTIF to be exactly 0x01 or empty vector
105 SCRIPT_VERIFY_MINIMALIF = (1U << 13),
107 // Signature(s) must be empty vector if an CHECK(MULTI)SIG operation failed
109 SCRIPT_VERIFY_NULLFAIL = (1U << 14),
111 // Public keys in segregated witness scripts must be compressed
113 SCRIPT_VERIFY_WITNESS_PUBKEYTYPE = (1U << 15),
116 bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
118 struct PrecomputedTransactionData
120 uint256 hashPrevouts, hashSequence, hashOutputs;
121 bool ready = false;
123 explicit PrecomputedTransactionData(const CTransaction& tx);
126 enum SigVersion
128 SIGVERSION_BASE = 0,
129 SIGVERSION_WITNESS_V0 = 1,
132 uint256 SignatureHash(const CScript &scriptCode, const CTransaction& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache = nullptr);
134 class BaseSignatureChecker
136 public:
137 virtual bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
139 return false;
142 virtual bool CheckLockTime(const CScriptNum& nLockTime) const
144 return false;
147 virtual bool CheckSequence(const CScriptNum& nSequence) const
149 return false;
152 virtual ~BaseSignatureChecker() {}
155 class TransactionSignatureChecker : public BaseSignatureChecker
157 private:
158 const CTransaction* txTo;
159 unsigned int nIn;
160 const CAmount amount;
161 const PrecomputedTransactionData* txdata;
163 protected:
164 virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
166 public:
167 TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
168 TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
169 bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override;
170 bool CheckLockTime(const CScriptNum& nLockTime) const override;
171 bool CheckSequence(const CScriptNum& nSequence) const override;
174 class MutableTransactionSignatureChecker : public TransactionSignatureChecker
176 private:
177 const CTransaction txTo;
179 public:
180 MutableTransactionSignatureChecker(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn) : TransactionSignatureChecker(&txTo, nInIn, amountIn), txTo(*txToIn) {}
183 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* error = nullptr);
184 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror = nullptr);
186 size_t CountWitnessSigOps(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags);
188 #endif // BITCOIN_SCRIPT_INTERPRETER_H