Bitcoin ABC  0.22.12
P2P Digital Currency
bitcoinconsensus.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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 public:
18  TxInputStream(int nTypeIn, int nVersionIn, const uint8_t *txTo,
19  size_t txToLen)
20  : m_type(nTypeIn), m_version(nVersionIn), m_data(txTo),
21  m_remaining(txToLen) {}
22 
23  void read(char *pch, size_t nSize) {
24  if (nSize > m_remaining) {
25  throw std::ios_base::failure(std::string(__func__) +
26  ": end of data");
27  }
28 
29  if (pch == nullptr) {
30  throw std::ios_base::failure(std::string(__func__) +
31  ": bad destination buffer");
32  }
33 
34  if (m_data == nullptr) {
35  throw std::ios_base::failure(std::string(__func__) +
36  ": bad source buffer");
37  }
38 
39  memcpy(pch, m_data, nSize);
40  m_remaining -= nSize;
41  m_data += nSize;
42  }
43 
44  template <typename T> TxInputStream &operator>>(T &&obj) {
45  ::Unserialize(*this, obj);
46  return *this;
47  }
48 
49  int GetVersion() const { return m_version; }
50  int GetType() const { return m_type; }
51 
52 private:
53  const int m_type;
54  const int m_version;
55  const uint8_t *m_data;
56  size_t m_remaining;
57 };
58 
59 inline int set_error(bitcoinconsensus_error *ret,
60  bitcoinconsensus_error serror) {
61  if (ret) {
62  *ret = serror;
63  }
64 
65  return 0;
66 }
67 
68 struct ECCryptoClosure {
69  ECCVerifyHandle handle;
70 };
71 
72 ECCryptoClosure instance_of_eccryptoclosure;
73 } // namespace
74 
76 static bool verify_flags(unsigned int flags) {
77  return (flags & ~(bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
78 }
79 
80 static int verify_script(const uint8_t *scriptPubKey,
81  unsigned int scriptPubKeyLen, Amount amount,
82  const uint8_t *txTo, unsigned int txToLen,
83  unsigned int nIn, unsigned int flags,
85  if (!verify_flags(flags)) {
86  return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
87  }
88  try {
89  TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
90  CTransaction tx(deserialize, stream);
91  if (nIn >= tx.vin.size()) {
92  return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
93  }
94 
95  if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen) {
96  return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
97  }
98 
99  // Regardless of the verification result, the tx did not error.
100  set_error(err, bitcoinconsensus_ERR_OK);
101 
102  PrecomputedTransactionData txdata(tx);
103  return VerifyScript(
104  tx.vin[nIn].scriptSig,
105  CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), flags,
106  TransactionSignatureChecker(&tx, nIn, amount, txdata), nullptr);
107  } catch (const std::exception &) {
108  // Error deserializing
109  return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE);
110  }
111 }
112 
114  const uint8_t *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
115  const uint8_t *txTo, unsigned int txToLen, unsigned int nIn,
116  unsigned int flags, bitcoinconsensus_error *err) {
117  Amount am(amount * SATOSHI);
118  return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen,
119  nIn, flags, err);
120 }
121 
122 int bitcoinconsensus_verify_script(const uint8_t *scriptPubKey,
123  unsigned int scriptPubKeyLen,
124  const uint8_t *txTo, unsigned int txToLen,
125  unsigned int nIn, unsigned int flags,
126  bitcoinconsensus_error *err) {
129  return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
130  }
131 
132  return ::verify_script(scriptPubKey, scriptPubKeyLen, Amount::zero(), txTo,
133  txToLen, nIn, flags, err);
134 }
135 
136 unsigned int bitcoinconsensus_version() {
137  // Just use the API version for now
139 }
static constexpr Amount zero()
Definition: amount.h:35
unsigned int bitcoinconsensus_version()
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:348
constexpr deserialize_type deserialize
Definition: serialize.h:45
Definition: amount.h:17
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, uint32_t flags, const BaseSignatureChecker &checker, ScriptExecutionMetrics &metricsOut, ScriptError *serror)
Execute an unlocking and locking script together.
static constexpr Amount SATOSHI
Definition: amount.h:151
int bitcoinconsensus_verify_script(const uint8_t *scriptPubKey, unsigned int scriptPubKeyLen, const uint8_t *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...
int bitcoinconsensus_verify_script_with_amount(const uint8_t *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount, const uint8_t *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
const std::vector< CTxIn > vin
Definition: transaction.h:227
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition: serialize.h:1193
#define BITCOINCONSENSUS_API_VER
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
enum bitcoinconsensus_error_t bitcoinconsensus_error
int flags
Definition: bitcoin-tx.cpp:529
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:429
static const int PROTOCOL_VERSION
network protocol versioning
Definition: version.h:11
GenericTransactionSignatureChecker< CTransaction > TransactionSignatureChecker
Definition: interpreter.h:81
void Unserialize(Stream &s, char &a)
Definition: serialize.h:313
static int verify_script(const uint8_t *scriptPubKey, unsigned int scriptPubKeyLen, Amount amount, const uint8_t *txTo, unsigned int txToLen, unsigned int nIn, unsigned int flags, bitcoinconsensus_error *err)
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:211
static bool verify_flags(unsigned int flags)
Check that all specified flags are part of the libconsensus interface.