]>
Commit | Line | Data |
---|---|---|
da03e6ed | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2014 The Bitcoin Core developers |
2d79bba3 | 3 | // Distributed under the MIT software license, see the accompanying |
da03e6ed | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | ||
84738627 PJ |
6 | #ifndef BITCOIN_SCRIPT_INTERPRETER_H |
7 | #define BITCOIN_SCRIPT_INTERPRETER_H | |
da03e6ed | 8 | |
ab9edbd6 | 9 | #include "script_error.h" |
858809a3 | 10 | #include "primitives/transaction.h" |
691b8708 | 11 | #include "komodo_cryptoconditions.h" |
ab9edbd6 | 12 | |
da03e6ed | 13 | #include <vector> |
14 | #include <stdint.h> | |
15 | #include <string> | |
6aae9d1a | 16 | #include <climits> |
da03e6ed | 17 | |
5c1e798a | 18 | class CPubKey; |
da03e6ed | 19 | class CScript; |
20 | class CTransaction; | |
5eed8c21 | 21 | class uint256; |
da03e6ed | 22 | |
6aae9d1a TH |
23 | /** Special case nIn for signing JoinSplits. */ |
24 | const unsigned int NOT_AN_INPUT = UINT_MAX; | |
25 | ||
da03e6ed | 26 | /** Signature hash types/flags */ |
27 | enum | |
28 | { | |
29 | SIGHASH_ALL = 1, | |
30 | SIGHASH_NONE = 2, | |
31 | SIGHASH_SINGLE = 3, | |
32 | SIGHASH_ANYONECANPAY = 0x80, | |
33 | }; | |
34 | ||
35 | /** Script verification flags */ | |
36 | enum | |
37 | { | |
38 | SCRIPT_VERIFY_NONE = 0, | |
da03e6ed | 39 | |
9df9cf5a PW |
40 | // Evaluate P2SH subscripts (softfork safe, BIP16). |
41 | SCRIPT_VERIFY_P2SH = (1U << 0), | |
42 | ||
43 | // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure. | |
98b135f9 PW |
44 | // Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure. |
45 | // (softfork safe, but not used or intended as a consensus rule). | |
9df9cf5a PW |
46 | SCRIPT_VERIFY_STRICTENC = (1U << 1), |
47 | ||
48 | // Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, BIP62 rule 1) | |
de609b8c DH |
49 | // In Zcash this is required, and validation of non-strict-DER signatures is not implemented. |
50 | //SCRIPT_VERIFY_DERSIG = (1U << 2), | |
9df9cf5a PW |
51 | |
52 | // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure | |
53 | // (softfork safe, BIP62 rule 5). | |
54 | SCRIPT_VERIFY_LOW_S = (1U << 3), | |
55 | ||
56 | // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7). | |
57 | SCRIPT_VERIFY_NULLDUMMY = (1U << 4), | |
d752ba86 PW |
58 | |
59 | // Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2). | |
60 | SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5), | |
698c6abb PW |
61 | |
62 | // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct | |
63 | // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating | |
64 | // any other push causes the script to fail (BIP62 rule 3). | |
65 | // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4). | |
66 | // (softfork safe) | |
03914234 PT |
67 | SCRIPT_VERIFY_MINIMALDATA = (1U << 6), |
68 | ||
69 | // Discourage use of NOPs reserved for upgrades (NOP1-10) | |
70 | // | |
71 | // Provided so that nodes can avoid accepting or mining transactions | |
72 | // containing executed NOP's whose meaning may change after a soft-fork, | |
73 | // thus rendering the script invalid; with this flag set executing | |
74 | // discouraged NOPs fails the script. This verification flag will never be | |
75 | // a mandatory flag applied to scripts in a block. NOPs that are not | |
76 | // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected. | |
b6e03cc5 PW |
77 | SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS = (1U << 7), |
78 | ||
79 | // Require that only a single stack element remains after evaluation. This changes the success criterion from | |
80 | // "At least one stack element must remain, and when interpreted as a boolean, it must be true" to | |
81 | // "Exactly one stack element must remain, and when interpreted as a boolean, it must be true". | |
82 | // (softfork safe, BIP62 rule 6) | |
83 | // Note: CLEANSTACK should never be used without P2SH. | |
84 | SCRIPT_VERIFY_CLEANSTACK = (1U << 8), | |
4fa7a048 PT |
85 | |
86 | // Verify CHECKLOCKTIMEVERIFY | |
87 | // | |
88 | // See BIP65 for details. | |
89 | SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9), | |
9df9cf5a | 90 | }; |
da03e6ed | 91 | |
6514771a | 92 | struct PrecomputedTransactionData |
f762d449 | 93 | { |
7245f328 | 94 | uint256 hashPrevouts, hashSequence, hashOutputs, hashJoinSplits; |
f762d449 | 95 | |
6514771a | 96 | PrecomputedTransactionData(const CTransaction& tx); |
f762d449 PW |
97 | }; |
98 | ||
c86a1cb8 PW |
99 | enum SigVersion |
100 | { | |
be126699 JG |
101 | SIGVERSION_SPROUT = 0, |
102 | SIGVERSION_OVERWINTER = 1, | |
c86a1cb8 PW |
103 | }; |
104 | ||
d55e5e77 JG |
105 | uint256 SignatureHash( |
106 | const CScript &scriptCode, | |
107 | const CTransaction& txTo, | |
108 | unsigned int nIn, | |
109 | int nHashType, | |
110 | const CAmount& amount, | |
111 | uint32_t consensusBranchId, | |
112 | const PrecomputedTransactionData* cache = NULL); | |
c7829ea7 | 113 | |
5c1e798a PW |
114 | class BaseSignatureChecker |
115 | { | |
116 | public: | |
d55e5e77 JG |
117 | virtual bool CheckSig( |
118 | const std::vector<unsigned char>& scriptSig, | |
119 | const std::vector<unsigned char>& vchPubKey, | |
120 | const CScript& scriptCode, | |
121 | uint32_t consensusBranchId) const | |
5c1e798a PW |
122 | { |
123 | return false; | |
124 | } | |
125 | ||
4fa7a048 PT |
126 | virtual bool CheckLockTime(const CScriptNum& nLockTime) const |
127 | { | |
128 | return false; | |
129 | } | |
130 | ||
991c422a | 131 | virtual bool CheckCryptoCondition(const CC *cond, const std::vector<unsigned char>& condBin, const CScript& scriptCode, uint32_t consensusBranchId) const |
07f83521 | 132 | { |
691b8708 | 133 | return false; |
07f83521 SS |
134 | } |
135 | ||
5c1e798a PW |
136 | virtual ~BaseSignatureChecker() {} |
137 | }; | |
138 | ||
858809a3 | 139 | class TransactionSignatureChecker : public BaseSignatureChecker |
c7829ea7 PW |
140 | { |
141 | private: | |
9fddceda | 142 | const CTransaction* txTo; |
c7829ea7 | 143 | unsigned int nIn; |
c86a1cb8 | 144 | const CAmount amount; |
6514771a | 145 | const PrecomputedTransactionData* txdata; |
c7829ea7 | 146 | |
5c1e798a | 147 | protected: |
e790c370 | 148 | virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const; |
5c1e798a | 149 | |
c7829ea7 | 150 | public: |
6514771a PW |
151 | TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(NULL) {} |
152 | TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {} | |
be126699 | 153 | bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, uint32_t consensusBranchId) const; |
4fa7a048 | 154 | bool CheckLockTime(const CScriptNum& nLockTime) const; |
991c422a | 155 | bool CheckCryptoCondition(const CC *cond, const std::vector<unsigned char>& condBin, const CScript& scriptCode, uint32_t consensusBranchId) const; |
2b2c75de | 156 | bool CheckEvalCondition(const CC *cond) const; |
c7829ea7 PW |
157 | }; |
158 | ||
858809a3 PW |
159 | class MutableTransactionSignatureChecker : public TransactionSignatureChecker |
160 | { | |
161 | private: | |
162 | const CTransaction txTo; | |
163 | ||
164 | public: | |
2d42e1a9 | 165 | MutableTransactionSignatureChecker(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amount) : TransactionSignatureChecker(&txTo, nInIn, amount), txTo(*txToIn) {} |
858809a3 PW |
166 | }; |
167 | ||
d55e5e77 JG |
168 | bool EvalScript( |
169 | std::vector<std::vector<unsigned char> >& stack, | |
170 | const CScript& script, | |
171 | unsigned int flags, | |
172 | const BaseSignatureChecker& checker, | |
173 | uint32_t consensusBranchId, | |
174 | ScriptError* error = NULL); | |
175 | bool VerifyScript( | |
176 | const CScript& scriptSig, | |
177 | const CScript& scriptPubKey, | |
178 | unsigned int flags, | |
179 | const BaseSignatureChecker& checker, | |
180 | uint32_t consensusBranchId, | |
181 | ScriptError* serror = NULL); | |
da03e6ed | 182 | |
84738627 | 183 | #endif // BITCOIN_SCRIPT_INTERPRETER_H |