Bitcoin Core  26.1.0
P2P Digital Currency
bitcoinconsensus.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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 } // namespace
66 
68 static bool verify_flags(unsigned int flags)
69 {
71 }
72 
73 static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
74  const unsigned char *txTo , unsigned int txToLen,
75  const UTXO *spentOutputs, unsigned int spentOutputsLen,
76  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
77 {
78  if (!verify_flags(flags)) {
79  return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
80  }
81 
82  if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT && spentOutputs == nullptr) {
83  return set_error(err, bitcoinconsensus_ERR_SPENT_OUTPUTS_REQUIRED);
84  }
85 
86  try {
87  TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen);
88  CTransaction tx(deserialize, stream);
89 
90  std::vector<CTxOut> spent_outputs;
91  if (spentOutputs != nullptr) {
92  if (spentOutputsLen != tx.vin.size()) {
93  return set_error(err, bitcoinconsensus_ERR_SPENT_OUTPUTS_MISMATCH);
94  }
95  for (size_t i = 0; i < spentOutputsLen; i++) {
96  CScript spk = CScript(spentOutputs[i].scriptPubKey, spentOutputs[i].scriptPubKey + spentOutputs[i].scriptPubKeySize);
97  const CAmount& value = spentOutputs[i].value;
98  CTxOut tx_out = CTxOut(value, spk);
99  spent_outputs.push_back(tx_out);
100  }
101  }
102 
103  if (nIn >= tx.vin.size())
104  return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
105  if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
106  return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
107 
108  // Regardless of the verification result, the tx did not error.
109  set_error(err, bitcoinconsensus_ERR_OK);
110 
111  PrecomputedTransactionData txdata(tx);
112 
113  if (spentOutputs != nullptr && flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_TAPROOT) {
114  txdata.Init(tx, std::move(spent_outputs));
115  }
116 
117  return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata, MissingDataBehavior::FAIL), nullptr);
118  } catch (const std::exception&) {
119  return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
120  }
121 }
122 
123 int bitcoinconsensus_verify_script_with_spent_outputs(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
124  const unsigned char *txTo , unsigned int txToLen,
125  const UTXO *spentOutputs, unsigned int spentOutputsLen,
126  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
127 {
128  CAmount am(amount);
129  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, spentOutputs, spentOutputsLen, nIn, flags, err);
130 }
131 
132 int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
133  const unsigned char *txTo , unsigned int txToLen,
134  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
135 {
136  CAmount am(amount);
137  UTXO *spentOutputs = nullptr;
138  unsigned int spentOutputsLen = 0;
139  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, spentOutputs, spentOutputsLen, nIn, flags, err);
140 }
141 
142 
143 int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
144  const unsigned char *txTo , unsigned int txToLen,
145  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
146 {
148  return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
149  }
150 
151  CAmount am(0);
152  UTXO *spentOutputs = nullptr;
153  unsigned int spentOutputsLen = 0;
154  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, spentOutputs, spentOutputsLen, nIn, flags, err);
155 }
156 
158 {
159  // Just use the API version for now
161 }
int ret
unsigned int bitcoinconsensus_version()
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
void Unserialize(Stream &, char)=delete
constexpr deserialize_type deserialize
Definition: serialize.h:49
constexpr std::size_t size() const noexcept
Definition: span.h:186
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...
QDataStream & operator>>(QDataStream &in, BitcoinUnit &unit)
const std::vector< CTxIn > vin
Definition: transaction.h:305
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1127
int64_t value
#define BITCOINCONSENSUS_API_VER
int bitcoinconsensus_verify_script_with_spent_outputs(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const unsigned char *txTo, unsigned int txToLen, const UTXO *spentOutputs, unsigned int spentOutputsLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
enum bitcoinconsensus_error_t bitcoinconsensus_error
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
Initialize this PrecomputedTransactionData with transaction data.
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount, const unsigned char *txTo, unsigned int txToLen, const UTXO *spentOutputs, unsigned int spentOutputsLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
Just act as if the signature was invalid.
An output of a transaction.
Definition: transaction.h:157
int flags
Definition: bitcoin-tx.cpp:528
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)
constexpr C * data() const noexcept
Definition: span.h:173
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:412
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:12
A Span is an object that can refer to a contiguous sequence of objects.
Definition: solver.h:20
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:294
static bool verify_flags(unsigned int flags)
Check that all specified flags are part of the libconsensus interface.