Bitcoin ABC  0.22.12
P2P Digital Currency
interpreter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2020 The Bitcoin 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 BITCOIN_SCRIPT_INTERPRETER_H
8 #define BITCOIN_SCRIPT_INTERPRETER_H
9 
10 #include <primitives/transaction.h>
11 #include <script/script_error.h>
12 #include <script/script_flags.h>
13 #include <script/script_metrics.h>
14 #include <script/sighashtype.h>
15 
16 #include <cstdint>
17 #include <vector>
18 
19 class CPubKey;
20 class CScript;
21 class CTransaction;
22 class uint256;
23 
24 template <class T>
25 uint256 SignatureHash(const CScript &scriptCode, const T &txTo,
26  unsigned int nIn, SigHashType sigHashType,
27  const Amount amount,
28  const PrecomputedTransactionData *cache = nullptr,
30 
32 public:
33  virtual bool VerifySignature(const std::vector<uint8_t> &vchSig,
34  const CPubKey &vchPubKey,
35  const uint256 &sighash) const;
36 
37  virtual bool CheckSig(const std::vector<uint8_t> &vchSigIn,
38  const std::vector<uint8_t> &vchPubKey,
39  const CScript &scriptCode, uint32_t flags) const {
40  return false;
41  }
42 
43  virtual bool CheckLockTime(const CScriptNum &nLockTime) const {
44  return false;
45  }
46 
47  virtual bool CheckSequence(const CScriptNum &nSequence) const {
48  return false;
49  }
50 
51  virtual ~BaseSignatureChecker() {}
52 };
53 
54 template <class T>
56 private:
57  const T *txTo;
58  unsigned int nIn;
59  const Amount amount;
61 
62 public:
63  GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn,
64  const Amount &amountIn)
65  : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
67  const T *txToIn, unsigned int nInIn, const Amount &amountIn,
68  const PrecomputedTransactionData &txdataIn)
69  : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
70 
71  // The overridden functions are now final.
72  bool CheckSig(const std::vector<uint8_t> &vchSigIn,
73  const std::vector<uint8_t> &vchPubKey,
74  const CScript &scriptCode,
75  uint32_t flags) const final override;
76  bool CheckLockTime(const CScriptNum &nLockTime) const final override;
77  bool CheckSequence(const CScriptNum &nSequence) const final override;
78 };
79 
84 
85 bool EvalScript(std::vector<std::vector<uint8_t>> &stack, const CScript &script,
86  uint32_t flags, const BaseSignatureChecker &checker,
87  ScriptExecutionMetrics &metrics, ScriptError *error = nullptr);
88 static inline bool EvalScript(std::vector<std::vector<uint8_t>> &stack,
89  const CScript &script, uint32_t flags,
90  const BaseSignatureChecker &checker,
91  ScriptError *error = nullptr) {
92  ScriptExecutionMetrics dummymetrics;
93  return EvalScript(stack, script, flags, checker, dummymetrics, error);
94 }
95 
102 bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey,
103  uint32_t flags, const BaseSignatureChecker &checker,
104  ScriptExecutionMetrics &metricsOut,
105  ScriptError *serror = nullptr);
106 static inline bool VerifyScript(const CScript &scriptSig,
107  const CScript &scriptPubKey, uint32_t flags,
108  const BaseSignatureChecker &checker,
109  ScriptError *serror = nullptr) {
110  ScriptExecutionMetrics dummymetrics;
111  return VerifyScript(scriptSig, scriptPubKey, flags, checker, dummymetrics,
112  serror);
113 }
114 
115 int FindAndDelete(CScript &script, const CScript &b);
116 
117 #endif // BITCOIN_SCRIPT_INTERPRETER_H
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
Definition: interpreter.h:43
virtual ~BaseSignatureChecker()
Definition: interpreter.h:51
bool EvalScript(std::vector< std::vector< uint8_t >> &stack, const CScript &script, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metrics, ScriptError *error=nullptr)
virtual bool VerifySignature(const std::vector< uint8_t > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:348
virtual bool CheckSig(const std::vector< uint8_t > &vchSigIn, const std::vector< uint8_t > &vchPubKey, const CScript &scriptCode, uint32_t flags) const
Definition: interpreter.h:37
Definition: amount.h:17
ScriptError
Definition: script_error.h:9
Struct for holding cumulative results from executing a script or a sequence of scripts.
const PrecomputedTransactionData * txdata
Definition: interpreter.h:60
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror=nullptr)
Execute an unlocking and locking script together.
An encapsulated public key.
Definition: pubkey.h:31
int flags
Definition: bitcoin-tx.cpp:529
256-bit opaque blob.
Definition: uint256.h:120
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const Amount &amountIn, const PrecomputedTransactionData &txdataIn)
Definition: interpreter.h:66
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const Amount &amountIn)
Definition: interpreter.h:63
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:429
virtual bool CheckSequence(const CScriptNum &nSequence) const
Definition: interpreter.h:47
int FindAndDelete(CScript &script, const CScript &b)
Definition: interpreter.cpp:45
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
bool error(const char *fmt, const Args &... args)
Definition: system.h:47
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, SigHashType sigHashType, const Amount amount, const PrecomputedTransactionData *cache=nullptr, uint32_t flags=SCRIPT_ENABLE_SIGHASH_FORKID)
Signature hash type wrapper class.
Definition: sighashtype.h:37