Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2014 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 :
6 : #include "bitcoinconsensus.h"
7 :
8 : #include "primitives/transaction.h"
9 : #include "script/interpreter.h"
10 : #include "version.h"
11 :
12 : namespace {
13 :
14 : /** A class that deserializes a single CTransaction one time. */
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 1126 : m_remaining(txToLen)
23 : {}
24 :
25 12320 : TxInputStream& read(char* pch, size_t nSize)
26 : {
27 12320 : if (nSize > m_remaining)
28 0 : throw std::ios_base::failure(std::string(__func__) + ": end of data");
29 :
30 12320 : if (pch == NULL)
31 0 : throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
32 :
33 12320 : if (m_data == NULL)
34 0 : throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
35 :
36 12320 : memcpy(pch, m_data, nSize);
37 12320 : m_remaining -= nSize;
38 12320 : m_data += nSize;
39 12320 : return *this;
40 : }
41 :
42 : template<typename T>
43 : TxInputStream& operator>>(T& obj)
44 : {
45 1126 : ::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 1126 : if (ret)
59 0 : *ret = serror;
60 : return 0;
61 : }
62 :
63 : } // anon namespace
64 :
65 1126 : 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 1126 : TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
71 1126 : CTransaction tx;
72 : stream >> tx;
73 2252 : if (nIn >= tx.vin.size())
74 : return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
75 1126 : 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 5630 : return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), flags, TransactionSignatureChecker(&tx, nIn), NULL);
82 0 : } catch (const std::exception&) {
83 : return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
84 : }
85 : }
86 :
87 0 : unsigned int bitcoinconsensus_version()
88 : {
89 : // Just use the API version for now
90 0 : return BITCOINCONSENSUS_API_VER;
91 : }
|