Bitcoin ABC 0.32.6
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
12namespace {
13
15class TxInputStream {
16public:
17 TxInputStream(const uint8_t *txTo, size_t txToLen)
18 : m_data(txTo), m_remaining(txToLen) {}
19
20 void read(Span<std::byte> dst) {
21 if (dst.size() > m_remaining) {
22 throw std::ios_base::failure(std::string(__func__) +
23 ": end of data");
24 }
25
26 if (dst.data() == nullptr) {
27 throw std::ios_base::failure(std::string(__func__) +
28 ": bad destination buffer");
29 }
30
31 if (m_data == nullptr) {
32 throw std::ios_base::failure(std::string(__func__) +
33 ": bad source buffer");
34 }
35
36 memcpy(dst.data(), m_data, dst.size());
37 m_remaining -= dst.size();
38 m_data += dst.size();
39 }
40
41 template <typename T> TxInputStream &operator>>(T &&obj) {
42 ::Unserialize(*this, obj);
43 return *this;
44 }
45
46private:
47 const uint8_t *m_data;
48 size_t m_remaining;
49};
50
51inline int set_error(bitcoinconsensus_error *ret,
53 if (ret) {
54 *ret = serror;
55 }
56
57 return 0;
58}
59
60struct ECCryptoClosure {
61 ECCVerifyHandle handle;
62};
63
64ECCryptoClosure instance_of_eccryptoclosure;
65} // namespace
66
68static bool verify_flags(unsigned int flags) {
70}
71
72static int verify_script(const uint8_t *scriptPubKey,
73 unsigned int scriptPubKeyLen, Amount amount,
74 const uint8_t *txTo, unsigned int txToLen,
75 unsigned int nIn, unsigned int flags,
77 if (!verify_flags(flags)) {
78 return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
79 }
80 try {
81 TxInputStream stream{txTo, txToLen};
82 CTransaction tx(deserialize, stream);
83 if (nIn >= tx.vin.size()) {
84 return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
85 }
86
87 if (GetSerializeSize(tx) != txToLen) {
88 return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
89 }
90
91 // Regardless of the verification result, the tx did not error.
92 set_error(err, bitcoinconsensus_ERR_OK);
93
95 return VerifyScript(
96 tx.vin[nIn].scriptSig,
97 CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), flags,
98 TransactionSignatureChecker(&tx, nIn, amount, txdata), nullptr);
99 } catch (const std::exception &) {
100 // Error deserializing
101 return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE);
102 }
103}
104
106 const uint8_t *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
107 const uint8_t *txTo, unsigned int txToLen, unsigned int nIn,
108 unsigned int flags, bitcoinconsensus_error *err) {
109 Amount am(amount * SATOSHI);
110 return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen,
111 nIn, flags, err);
112}
113
114int bitcoinconsensus_verify_script(const uint8_t *scriptPubKey,
115 unsigned int scriptPubKeyLen,
116 const uint8_t *txTo, unsigned int txToLen,
117 unsigned int nIn, unsigned int flags,
121 return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
122 }
123
124 return ::verify_script(scriptPubKey, scriptPubKeyLen, Amount::zero(), txTo,
125 txToLen, nIn, flags, err);
126}
127
129 // Just use the API version for now
131}
static constexpr Amount SATOSHI
Definition: amount.h:148
int flags
Definition: bitcoin-tx.cpp:542
static bool verify_flags(unsigned int flags)
Check that all specified flags are part of the libconsensus interface.
unsigned int bitcoinconsensus_version()
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)
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)
enum bitcoinconsensus_error_t bitcoinconsensus_error
@ bitcoinconsensus_SCRIPT_ENABLE_SIGHASH_FORKID
@ bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL
@ bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS_DEPRECATED
@ bitcoinconsensus_ERR_OK
@ bitcoinconsensus_ERR_TX_DESERIALIZE
@ bitcoinconsensus_ERR_AMOUNT_REQUIRED
@ bitcoinconsensus_ERR_TX_INDEX
@ bitcoinconsensus_ERR_INVALID_FLAGS
@ bitcoinconsensus_ERR_TX_SIZE_MISMATCH
#define BITCOINCONSENSUS_API_VER
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:432
The basic transaction that is broadcasted on the network and contained in blocks.
Definition: transaction.h:192
const std::vector< CTxIn > vin
Definition: transaction.h:206
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:223
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:94
constexpr std::size_t size() const noexcept
Definition: span.h:210
constexpr C * data() const noexcept
Definition: span.h:199
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.
GenericTransactionSignatureChecker< CTransaction > TransactionSignatureChecker
Definition: interpreter.h:125
size_t GetSerializeSize(const T &t)
Definition: serialize.h:1262
constexpr deserialize_type deserialize
Definition: serialize.h:53
void Unserialize(Stream &, V)=delete
Definition: amount.h:21
static constexpr Amount zero() noexcept
Definition: amount.h:34
Precompute sighash midstate to avoid quadratic hashing.
Definition: transaction.h:325