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 .
6 #ifndef BITCOIN_SCRIPT_INTERPRETER_H
7 #define BITCOIN_SCRIPT_INTERPRETER_H
9 #include "script_error.h"
10 #include "primitives/transaction.h"
11 #include "script/cc.h"
23 /** Special case nIn for signing JoinSplits. */
24 const unsigned int NOT_AN_INPUT = UINT_MAX;
26 /** Signature hash types/flags */
32 SIGHASH_ANYONECANPAY = 0x80,
35 /** Script verification flags */
38 SCRIPT_VERIFY_NONE = 0,
40 // Evaluate P2SH subscripts (softfork safe, BIP16).
41 SCRIPT_VERIFY_P2SH = (1U << 0),
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),
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),
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),
56 // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7).
57 SCRIPT_VERIFY_NULLDUMMY = (1U << 4),
59 // Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2).
60 SCRIPT_VERIFY_SIGPUSHONLY = (1U << 5),
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).
67 SCRIPT_VERIFY_MINIMALDATA = (1U << 6),
69 // Discourage use of NOPs reserved for upgrades (NOP1-10)
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),
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),
86 // Verify CHECKLOCKTIMEVERIFY
88 // See BIP65 for details.
89 SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY = (1U << 9),
92 bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
94 struct PrecomputedTransactionData
96 uint256 hashPrevouts, hashSequence, hashOutputs, hashJoinSplits, hashShieldedSpends, hashShieldedOutputs;
98 PrecomputedTransactionData(const CTransaction& tx);
103 SIGVERSION_SPROUT = 0,
104 SIGVERSION_OVERWINTER = 1,
105 SIGVERSION_SAPLING = 2,
108 uint256 SignatureHash(
109 const CScript &scriptCode,
110 const CTransaction& txTo,
113 const CAmount& amount,
114 uint32_t consensusBranchId,
115 const PrecomputedTransactionData* cache = NULL);
117 class BaseSignatureChecker
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
129 virtual bool CheckLockTime(const CScriptNum& nLockTime) const
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
143 virtual ~BaseSignatureChecker() {}
146 class TransactionSignatureChecker : public BaseSignatureChecker
149 const CTransaction* txTo;
151 const CAmount amount;
152 const PrecomputedTransactionData* txdata;
154 virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
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;
169 class MutableTransactionSignatureChecker : public TransactionSignatureChecker
172 const CTransaction txTo;
175 MutableTransactionSignatureChecker(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amount) : TransactionSignatureChecker(&txTo, nInIn, amount), txTo(*txToIn) {}
179 std::vector<std::vector<unsigned char> >& stack,
180 const CScript& script,
182 const BaseSignatureChecker& checker,
183 uint32_t consensusBranchId,
184 ScriptError* error = NULL);
186 const CScript& scriptSig,
187 const CScript& scriptPubKey,
189 const BaseSignatureChecker& checker,
190 uint32_t consensusBranchId,
191 ScriptError* serror = NULL);
192 #endif // BITCOIN_SCRIPT_INTERPRETER_H