Bitcoin Core  22.99.0
P2P Digital Currency
bitcoinconsensus.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
7 
9 #include <pubkey.h>
10 #include <script/interpreter.h>
11 #include <version.h>
12 
13 namespace {
14 
16 class TxInputStream
17 {
18 public:
19  TxInputStream(int nVersionIn, const unsigned char *txTo, size_t txToLen) :
20  m_version(nVersionIn),
21  m_data(txTo),
22  m_remaining(txToLen)
23  {}
24 
25  void read(Span<std::byte> dst)
26  {
27  if (dst.size() > m_remaining) {
28  throw std::ios_base::failure(std::string(__func__) + ": end of data");
29  }
30 
31  if (dst.data() == nullptr) {
32  throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
33  }
34 
35  if (m_data == nullptr) {
36  throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
37  }
38 
39  memcpy(dst.data(), m_data, dst.size());
40  m_remaining -= dst.size();
41  m_data += dst.size();
42  }
43 
44  template<typename T>
45  TxInputStream& operator>>(T&& obj)
46  {
47  ::Unserialize(*this, obj);
48  return *this;
49  }
50 
51  int GetVersion() const { return m_version; }
52 private:
53  const int m_version;
54  const unsigned char* m_data;
55  size_t m_remaining;
56 };
57 
58 inline int set_error(bitcoinconsensus_error* ret, bitcoinconsensus_error serror)
59 {
60  if (ret)
61  *ret = serror;
62  return 0;
63 }
64 
65 struct ECCryptoClosure
66 {
67  ECCVerifyHandle handle;
68 };
69 
70 ECCryptoClosure instance_of_eccryptoclosure;
71 } // namespace
72 
74 static bool verify_flags(unsigned int flags)
75 {
77 }
78 
79 static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
80  const unsigned char *txTo , unsigned int txToLen,
81  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
82 {
83  if (!verify_flags(flags)) {
84  return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
85  }
86  try {
87  TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen);
88  CTransaction tx(deserialize, stream);
89  if (nIn >= tx.vin.size())
90  return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
91  if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
92  return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
93 
94  // Regardless of the verification result, the tx did not error.
95  set_error(err, bitcoinconsensus_ERR_OK);
96 
97  PrecomputedTransactionData txdata(tx);
98  return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata, MissingDataBehavior::FAIL), nullptr);
99  } catch (const std::exception&) {
100  return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
101  }
102 }
103 
104 int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
105  const unsigned char *txTo , unsigned int txToLen,
106  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
107 {
108  CAmount am(amount);
109  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
110 }
111 
112 
113 int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
114  const unsigned char *txTo , unsigned int txToLen,
115  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
116 {
118  return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
119  }
120 
121  CAmount am(0);
122  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
123 }
124 
126 {
127  // Just use the API version for now
129 }
GetSerializeSize
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1086
CTransaction::vin
const std::vector< CTxIn > vin
Definition: transaction.h:270
flags
int flags
Definition: bitcoin-tx.cpp:529
bitcoinconsensus_ERR_TX_INDEX
@ bitcoinconsensus_ERR_TX_INDEX
Definition: bitcoinconsensus.h:39
transaction.h
verify_flags
static bool verify_flags(unsigned int flags)
Check that all specified flags are part of the libconsensus interface.
Definition: bitcoinconsensus.cpp:74
BITCOINCONSENSUS_API_VER
#define BITCOINCONSENSUS_API_VER
Definition: bitcoinconsensus.h:34
GenericTransactionSignatureChecker
Definition: interpreter.h:282
interpreter.h
bitcoinconsensus_ERR_OK
@ bitcoinconsensus_ERR_OK
Definition: bitcoinconsensus.h:38
bitcoinconsensus_ERR_INVALID_FLAGS
@ bitcoinconsensus_ERR_INVALID_FLAGS
Definition: bitcoinconsensus.h:43
pubkey.h
verify_script
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
Definition: bitcoinconsensus.cpp:79
MissingDataBehavior::FAIL
@ FAIL
Just act as if the signature was invalid.
version.h
Unserialize
void Unserialize(Stream &, char)=delete
VerifyScript
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
Definition: interpreter.cpp:1972
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:259
bitcoinconsensus.h
Span::size
constexpr std::size_t size() const noexcept
Definition: span.h:186
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:96
bitcoinconsensus_ERR_AMOUNT_REQUIRED
@ bitcoinconsensus_ERR_AMOUNT_REQUIRED
Definition: bitcoinconsensus.h:42
bitcoinconsensus_version
unsigned int bitcoinconsensus_version()
Definition: bitcoinconsensus.cpp:125
CAmount
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bitcoinconsensus_verify_script
int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
Returns 1 if the input nIn of the serialized transaction pointed to by txTo correctly spends the scri...
Definition: bitcoinconsensus.cpp:113
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS
@ bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS
Definition: bitcoinconsensus.h:55
PrecomputedTransactionData
Definition: interpreter.h:151
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
bitcoinconsensus_ERR_TX_DESERIALIZE
@ bitcoinconsensus_ERR_TX_DESERIALIZE
Definition: bitcoinconsensus.h:41
bitcoinconsensus_ERR_TX_SIZE_MISMATCH
@ bitcoinconsensus_ERR_TX_SIZE_MISMATCH
Definition: bitcoinconsensus.h:40
Span::data
constexpr C * data() const noexcept
Definition: span.h:173
ECCVerifyHandle
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:332
ByteUnit::T
@ T
bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL
@ bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL
Definition: bitcoinconsensus.h:56
bitcoinconsensus_error
enum bitcoinconsensus_error_t bitcoinconsensus_error
bitcoinconsensus_verify_script_with_amount
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const unsigned char *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
Definition: bitcoinconsensus.cpp:104
deserialize
constexpr deserialize_type deserialize
Definition: serialize.h:48
PROTOCOL_VERSION
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12