PRCYCoin  2.0.0.7rc1
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 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "bitcoinconsensus.h"
7 
9 #include "script/interpreter.h"
10 #include "version.h"
11 
12 namespace {
13 
15 class TxInputStream
16 {
17 public:
18  TxInputStream(int nTypeIn, int nVersionIn, const unsigned char *txTo, size_t txToLen) :
19  m_type(nTypeIn),
20  m_version(nVersionIn),
21  m_data(txTo),
22  m_remaining(txToLen)
23  {}
24 
25  TxInputStream& read(char* pch, size_t nSize)
26  {
27  if (nSize > m_remaining)
28  throw std::ios_base::failure(std::string(__func__) + ": end of data");
29 
30  if (pch == NULL)
31  throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
32 
33  if (m_data == NULL)
34  throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
35 
36  memcpy(pch, m_data, nSize);
37  m_remaining -= nSize;
38  m_data += nSize;
39  return *this;
40  }
41 
42  template<typename T>
43  TxInputStream& operator>>(T& obj)
44  {
45  ::Unserialize(*this, obj, m_type, m_version);
46  return *this;
47  }
48 
49 private:
50  const int m_type;
51  const int m_version;
52  const unsigned char* m_data;
53  size_t m_remaining;
54 };
55 
56 inline int set_error(bitcoinconsensus_error* ret, bitcoinconsensus_error serror)
57 {
58  if (ret)
59  *ret = serror;
60  return 0;
61 }
62 
63 } // anon namespace
64 
65 int bitcoinconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
66  const unsigned char *txTo , unsigned int txToLen,
67  unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
68 {
69  try {
70  TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
71  CTransaction tx;
72  stream >> tx;
73  if (nIn >= tx.vin.size())
74  return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
75  if (tx.GetSerializeSize(SER_NETWORK, PROTOCOL_VERSION) != txToLen)
76  return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
77 
78  // Regardless of the verification result, the tx did not error.
79  set_error(err, bitcoinconsensus_ERR_OK);
80 
81  return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), flags, TransactionSignatureChecker(&tx, nIn), NULL);
82  } catch (const std::exception&) {
83  return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
84  }
85 }
86 
88 {
89  // Just use the API version for now
91 }
transaction.h
bitcoinconsensus_ERR_TX_INDEX
@ bitcoinconsensus_ERR_TX_INDEX
Definition: bitcoinconsensus.h:39
BITCOINCONSENSUS_API_VER
#define BITCOINCONSENSUS_API_VER
Definition: bitcoinconsensus.h:34
flags
int flags
Definition: prcycoin-tx.cpp:297
operator>>
const CBigNum operator>>(const CBigNum &a, unsigned int shift)
Definition: bignum.h:786
interpreter.h
bitcoinconsensus_ERR_OK
@ bitcoinconsensus_ERR_OK
Definition: bitcoinconsensus.h:38
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:15
Unserialize
void Unserialize(Stream &s, char &a, int, int=0)
Definition: serialize.h:218
SER_NETWORK
@ SER_NETWORK
Definition: serialize.h:159
version.h
CTransaction
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:269
bitcoinconsensus.h
TransactionSignatureChecker
Definition: interpreter.h:104
bitcoinconsensus_version
unsigned int bitcoinconsensus_version()
Definition: bitcoinconsensus.cpp:87
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:65
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:363
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
VerifyScript
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror)
Definition: interpreter.cpp:1163
bitcoinconsensus_error
enum bitcoinconsensus_error_t bitcoinconsensus_error