PIVX Core  5.6.99
P2P Digital Currency
interpreter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 The Bitcoin developers
3 // Copyright (c) 2018-2021 The PIVX Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #ifndef PIVX_SCRIPT_INTERPRETER_H
8 #define PIVX_SCRIPT_INTERPRETER_H
9 
10 #include "primitives/transaction.h"
11 #include "script_error.h"
12 #include "uint256.h"
13 
14 #include <vector>
15 #include <stdint.h>
16 #include <string>
17 
19 const unsigned int NOT_AN_INPUT = UINT_MAX;
20 
22 enum
23 {
28 };
29 
31 enum
32 {
34 
35  // Evaluate P2SH subscripts (softfork safe, BIP16).
36  SCRIPT_VERIFY_P2SH = (1U << 0),
37 
38  // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
39  // Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
40  // (softfork safe, but not used or intended as a consensus rule).
42 
43  // Passing a non-strict-DER signature to a checksig operation causes script failure (softfork safe, BIP62 rule 1)
44  SCRIPT_VERIFY_DERSIG = (1U << 2),
45 
46  // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
47  // (softfork safe, BIP62 rule 5).
48  SCRIPT_VERIFY_LOW_S = (1U << 3),
49 
50  // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (softfork safe, BIP62 rule 7).
52 
53  // Using a non-push operator in the scriptSig causes script failure (softfork safe, BIP62 rule 2).
55 
56  // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
57  // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
58  // any other push causes the script to fail (BIP62 rule 3).
59  // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
60  // (softfork safe)
62 
63  // Discourage use of NOPs reserved for upgrades (NOP1-10)
64  //
65  // Provided so that nodes can avoid accepting or mining transactions
66  // containing executed NOP's whose meaning may change after a soft-fork,
67  // thus rendering the script invalid; with this flag set executing
68  // discouraged NOPs fails the script. This verification flag will never be
69  // a mandatory flag applied to scripts in a block. NOPs that are not
70  // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
72 
73  // Require that only a single stack element remains after evaluation. This changes the success criterion from
74  // "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
75  // "Exactly one stack element must remain, and when interpreted as a boolean, it must be true".
76  // (softfork safe, BIP62 rule 6)
77  // Note: CLEANSTACK should never be used without P2SH.
79 
80  // Verify CHECKLOCKTIMEVERIFY
81  //
82  // See BIP65 for details.
84 
85  // Verify OP_EXCHANGEADDR.
86  // Treat as UNKNOWN before 5.6 activation
88 };
89 
90 bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
91 
93 {
95 
96  explicit PrecomputedTransactionData(const CTransaction& tx);
97 };
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 
102 {
103 public:
104  virtual bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
105  {
106  return false;
107  }
108 
109  virtual bool CheckLockTime(const CScriptNum& nLockTime) const
110  {
111  return false;
112  }
113 
114  virtual bool CheckColdStake(bool fAllowLastOutputFree, const CScript& prevoutScript, std::vector<valtype>& stack, unsigned int flags, ScriptError* error) const
115  {
116  return false;
117  }
118 
120 };
121 
123 {
124 private:
126  unsigned int nIn;
129 
130 protected:
131  virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
132 
133 public:
134  TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), precomTxData(nullptr) {}
135  TransactionSignatureChecker(const CTransaction* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& cachedHashesIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), precomTxData(&cachedHashesIn) {}
136 
137  bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override ;
138  bool CheckLockTime(const CScriptNum& nLockTime) const override;
139  bool CheckColdStake(bool fAllowLastOutputFree, const CScript& prevoutScript, std::vector<valtype>& stack, unsigned int flags, ScriptError* serror) const override;
140 };
141 
143 {
144 private:
146 
147 public:
148  MutableTransactionSignatureChecker(const CMutableTransaction* txToIn, unsigned int nInIn, const CAmount& amount) : TransactionSignatureChecker(&txTo, nInIn, amount), txTo(*txToIn) {}
149 };
150 
151 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* error = nullptr);
152 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror = nullptr);
153 
154 #endif // PIVX_SCRIPT_INTERPRETER_H
int64_t CAmount
Amount in PIV (Can be negative)
Definition: amount.h:13
virtual bool CheckColdStake(bool fAllowLastOutputFree, const CScript &prevoutScript, std::vector< valtype > &stack, unsigned int flags, ScriptError *error) const
Definition: interpreter.h:114
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
Definition: interpreter.h:109
virtual bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Definition: interpreter.h:104
virtual ~BaseSignatureChecker()
Definition: interpreter.h:119
An encapsulated public key.
Definition: pubkey.h:44
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:381
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:244
MutableTransactionSignatureChecker(const CMutableTransaction *txToIn, unsigned int nInIn, const CAmount &amount)
Definition: interpreter.h:148
const PrecomputedTransactionData * precomTxData
Definition: interpreter.h:128
TransactionSignatureChecker(const CTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn)
Definition: interpreter.h:134
bool CheckColdStake(bool fAllowLastOutputFree, const CScript &prevoutScript, std::vector< valtype > &stack, unsigned int flags, ScriptError *serror) const override
bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
virtual bool VerifySignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
bool CheckLockTime(const CScriptNum &nLockTime) const override
const CTransaction * txTo
Definition: interpreter.h:125
TransactionSignatureChecker(const CTransaction *txToIn, unsigned int nInIn, const CAmount &amountIn, const PrecomputedTransactionData &cachedHashesIn)
Definition: interpreter.h:135
256-bit opaque blob.
Definition: uint256.h:138
@ SCRIPT_VERIFY_NULLDUMMY
Definition: interpreter.h:51
@ SCRIPT_VERIFY_P2SH
Definition: interpreter.h:36
@ SCRIPT_VERIFY_SIGPUSHONLY
Definition: interpreter.h:54
@ SCRIPT_VERIFY_LOW_S
Definition: interpreter.h:48
@ SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY
Definition: interpreter.h:83
@ SCRIPT_VERIFY_STRICTENC
Definition: interpreter.h:41
@ SCRIPT_VERIFY_DERSIG
Definition: interpreter.h:44
@ SCRIPT_VERIFY_CLEANSTACK
Definition: interpreter.h:78
@ SCRIPT_VERIFY_NONE
Definition: interpreter.h:33
@ SCRIPT_VERIFY_MINIMALDATA
Definition: interpreter.h:61
@ SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS
Definition: interpreter.h:71
@ SCRIPT_VERIFY_EXCHANGEADDR
Definition: interpreter.h:87
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
const unsigned int NOT_AN_INPUT
Special case nIn for signing Sapling txs.
Definition: interpreter.h:19
uint256 SignatureHash(const CScript &scriptCode, const CTransaction &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache=nullptr)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror=nullptr)
@ SIGHASH_ANYONECANPAY
Definition: interpreter.h:27
@ SIGHASH_ALL
Definition: interpreter.h:24
@ SIGHASH_NONE
Definition: interpreter.h:25
@ SIGHASH_SINGLE
Definition: interpreter.h:26
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *error=nullptr)
int flags
Definition: pivx-tx.cpp:400
enum ScriptError_t ScriptError
A mutable version of CTransaction.
Definition: transaction.h:409
PrecomputedTransactionData(const CTransaction &tx)
bool error(const char *fmt, const Args &... args)
Definition: system.h:77
SigVersion
Definition: transaction.h:26