PIVX Core  5.6.99
P2P Digital Currency
bitcoinconsensus.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 #include "bitcoinconsensus.h"
8 
10 #include "script/interpreter.h"
11 #include "version.h"
12 
13 namespace {
14 
16 class TxInputStream
17 {
18 public:
19  TxInputStream(int nTypeIn, int nVersionIn, const unsigned char *txTo, size_t txToLen) :
20  m_type(nTypeIn),
21  m_version(nVersionIn),
22  m_data(txTo),
23  m_remaining(txToLen)
24  {}
25 
26  void read(char* pch, size_t nSize)
27  {
28  if (nSize > m_remaining)
29  throw std::ios_base::failure(std::string(__func__) + ": end of data");
30 
31  if (pch == nullptr)
32  throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
33 
34  if (m_data == nullptr)
35  throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
36 
37  memcpy(pch, m_data, nSize);
38  m_remaining -= nSize;
39  m_data += nSize;
40  }
41 
42  template<typename T>
43  TxInputStream& operator>>(T&& obj)
44  {
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 unsigned char* m_data;
56  size_t m_remaining;
57 };
58 
59 inline int set_error(bitcoinconsensus_error* ret, bitcoinconsensus_error serror)
60 {
61  if (ret)
62  *ret = serror;
63  return 0;
64 }
65 
66 struct ECCryptoClosure
67 {
68  ECCVerifyHandle handle;
69 };
70 
71 ECCryptoClosure instance_of_eccryptoclosure;
72 
73 } // anon namespace
74 
75 int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
76  const unsigned char *txTo , unsigned int txToLen,
77  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
78 {
79  try {
80  TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
81  CTransaction tx(deserialize, stream);
82  if (nIn >= tx.vin.size())
83  return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
84  if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
85  return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
86 
87  // Regardless of the verification result, the tx did not error.
88  set_error(err, bitcoinconsensus_ERR_OK);
89 
90  return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen),
91  flags, TransactionSignatureChecker(&tx, nIn), tx.GetRequiredSigVersion(), nullptr);
92  } catch (const std::exception&) {
93  return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
94  }
95 }
96 
98 {
99  // Just use the API version for now
101 }
const CBigNum operator>>(const CBigNum &a, unsigned int shift)
Definition: bignum.h:215
unsigned int bitcoinconsensus_version()
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...
enum bitcoinconsensus_error_t bitcoinconsensus_error
@ bitcoinconsensus_ERR_OK
@ bitcoinconsensus_ERR_TX_DESERIALIZE
@ bitcoinconsensus_ERR_TX_INDEX
@ bitcoinconsensus_ERR_TX_SIZE_MISMATCH
#define BITCOINCONSENSUS_API_VER
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
std::vector< CTxIn > vin
Definition: transaction.h:270
SigVersion GetRequiredSigVersion() const
Definition: transaction.h:347
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:273
void * memcpy(void *a, const void *b, size_t c)
#define T(expected, seed, data)
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
int flags
Definition: pivx-tx.cpp:400
@ SER_NETWORK
Definition: serialize.h:174
unsigned int GetSerializeSize(const std::array< T, N > &item)
array
Definition: serialize.h:847
constexpr deserialize_type deserialize
Definition: serialize.h:57
void Unserialize(Stream &s, char &a)
Definition: serialize.h:253