]> Git Repo - VerusCoin.git/blob - src/script/interpreter.h
Incorporate all Zcash updates through 2.0.7-3 in addition to PBaaS, reserves, etc.
[VerusCoin.git] / src / script / interpreter.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or https://www.opensource.org/licenses/mit-license.php .
5
6 #ifndef BITCOIN_SCRIPT_INTERPRETER_H
7 #define BITCOIN_SCRIPT_INTERPRETER_H
8
9 #include "script_error.h"
10 #include "primitives/transaction.h"
11 #include "script/cc.h"
12
13 #include <vector>
14 #include <stdint.h>
15 #include <string>
16 #include <climits>
17
18 class CPubKey;
19 class CScript;
20 class CTransaction;
21 class uint256;
22
23 /** Special case nIn for signing JoinSplits. */
24 const unsigned int NOT_AN_INPUT = UINT_MAX;
25
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,
39
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.
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).
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)
49     // In Zcash this is required, and validation of non-strict-DER signatures is not implemented.
50     //SCRIPT_VERIFY_DERSIG    = (1U << 2),
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),
58
59     // Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2).
60     SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),
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)
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.
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),
85
86     // Verify CHECKLOCKTIMEVERIFY
87     //
88     // See BIP65 for details.
89     SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),
90 };
91
92 bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
93
94 struct PrecomputedTransactionData
95 {
96     uint256 hashPrevouts, hashSequence, hashOutputs, hashJoinSplits, hashShieldedSpends, hashShieldedOutputs;
97
98     PrecomputedTransactionData(const CTransaction& tx);
99 };
100
101 enum SigVersion
102 {
103     SIGVERSION_SPROUT = 0,
104     SIGVERSION_OVERWINTER = 1,
105     SIGVERSION_SAPLING = 2,
106 };
107
108 uint256 SignatureHash(
109     const CScript &scriptCode,
110     const CTransaction& txTo,
111     unsigned int nIn,
112     int nHashType,
113     const CAmount& amount,
114     uint32_t consensusBranchId,
115     const PrecomputedTransactionData* cache = NULL);
116
117 class BaseSignatureChecker
118 {
119 public:
120     virtual bool CheckSig(
121         const std::vector<unsigned char>& scriptSig,
122         const std::vector<unsigned char>& vchPubKey,
123         const CScript& scriptCode,
124         uint32_t consensusBranchId) const
125     {
126         return false;
127     }
128
129     virtual bool CheckLockTime(const CScriptNum& nLockTime) const
130     {
131          return false;
132     }
133
134     virtual int CheckCryptoCondition(
135             const std::vector<unsigned char>& condBin,
136             const std::vector<unsigned char>& ffillBin,
137             const CScript& scriptCode,
138             uint32_t consensusBranchId) const
139     {
140         return false;
141     }
142
143     virtual ~BaseSignatureChecker() {}
144 };
145
146 class TransactionSignatureChecker : public BaseSignatureChecker
147 {
148 protected:
149     const CTransaction* txTo;
150     unsigned int nIn;
151     const CAmount amount;
152     const PrecomputedTransactionData* txdata;
153
154     virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
155
156 public:
157     TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(NULL) {}
158     TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
159     bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, uint32_t consensusBranchId) const;
160     bool CheckLockTime(const CScriptNum& nLockTime) const;
161     int CheckCryptoCondition(
162         const std::vector<unsigned char>& condBin,
163         const std::vector<unsigned char>& ffillBin,
164         const CScript& scriptCode,
165         uint32_t consensusBranchId) const;
166     virtual int CheckEvalCondition(const CC *cond) const;
167 };
168
169 class MutableTransactionSignatureChecker : public TransactionSignatureChecker
170 {
171 private:
172     const CTransaction txTo;
173
174 public:
175     MutableTransactionSignatureChecker(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amount) : TransactionSignatureChecker(&txTo, nInIn, amount), txTo(*txToIn) {}
176 };
177
178 bool EvalScript(
179     std::vector<std::vector<unsigned char> >& stack,
180     const CScript& script,
181     unsigned int flags,
182     const BaseSignatureChecker& checker,
183     uint32_t consensusBranchId,
184     ScriptError* error = NULL);
185 bool VerifyScript(
186     const CScript& scriptSig,
187     const CScript& scriptPubKey,
188     unsigned int flags,
189     const BaseSignatureChecker& checker,
190     uint32_t consensusBranchId,
191     ScriptError* serror = NULL);
192 #endif // BITCOIN_SCRIPT_INTERPRETER_H
This page took 0.0348 seconds and 4 git commands to generate.