Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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
13namespace {
14
16class TxInputStream {
17public:
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(Span<std::byte> dst) {
24 if (dst.size() > m_remaining) {
25 throw std::ios_base::failure(std::string(__func__) +
26 ": end of data");
27 }
28
29 if (dst.data() == 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(dst.data(), m_data, dst.size());
40 m_remaining -= dst.size();
41 m_data += dst.size();
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
52private:
53 const int m_type;
54 const int m_version;
55 const uint8_t *m_data;
56 size_t m_remaining;
57};
58
61 if (ret) {
62 *ret = serror;
63 }
64
65 return 0;
66}
67
68struct ECCryptoClosure {
69 ECCVerifyHandle handle;
70};
71
72ECCryptoClosure instance_of_eccryptoclosure;
73} // namespace
74
76static bool verify_flags(unsigned int flags) {
78}
79
80static 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)) {
87 }
88 try {
89 TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
90 CTransaction tx(deserialize, stream);
91 if (nIn >= tx.vin.size()) {
93 }
94
97 }
98
99 // Regardless of the verification result, the tx did not error.
101
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
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
123 unsigned int scriptPubKeyLen,
124 const uint8_t *txTo, unsigned int txToLen,
125 unsigned int nIn, unsigned int flags,
130 }
131
132 return ::verify_script(scriptPubKey, scriptPubKeyLen, Amount::zero(), txTo,
133 txToLen, nIn, flags, err);
134}
135
137 // Just use the API version for now
139}
static constexpr Amount SATOSHI
Definition amount.h:143
int flags
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:431
The basic transaction that is broadcasted on the network and contained in blocks.
const std::vector< CTxIn > vin
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:93
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:84
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
constexpr deserialize_type deserialize
Definition serialize.h:50
@ SER_NETWORK
Definition serialize.h:152
void Unserialize(Stream &, char)=delete
size_t GetSerializeSize(const T &t, int nVersion=0)
Definition serialize.h:1258
static constexpr Amount zero() noexcept
Definition amount.h:32
Precompute sighash midstate to avoid quadratic hashing.
static const int PROTOCOL_VERSION
network protocol versioning
Definition version.h:11