2014-10-14 19:22:55 -03:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-07-26 18:36:45 -04:00
|
|
|
// Copyright (c) 2009-2018 The Bitcoin Core developers
|
2014-10-14 19:22:55 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <script/bitcoinconsensus.h>
|
2014-10-14 19:22:55 -03:00
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <primitives/transaction.h>
|
|
|
|
#include <pubkey.h>
|
|
|
|
#include <script/interpreter.h>
|
|
|
|
#include <version.h>
|
2014-10-14 19:22:55 -03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
/** A class that deserializes a single CTransaction one time. */
|
|
|
|
class TxInputStream
|
|
|
|
{
|
|
|
|
public:
|
2020-09-22 10:38:54 -03:00
|
|
|
TxInputStream(int nVersionIn, const unsigned char *txTo, size_t txToLen) :
|
2014-10-14 19:22:55 -03:00
|
|
|
m_version(nVersionIn),
|
|
|
|
m_data(txTo),
|
|
|
|
m_remaining(txToLen)
|
|
|
|
{}
|
|
|
|
|
2016-10-29 01:55:12 -03:00
|
|
|
void read(char* pch, size_t nSize)
|
2014-10-14 19:22:55 -03:00
|
|
|
{
|
|
|
|
if (nSize > m_remaining)
|
|
|
|
throw std::ios_base::failure(std::string(__func__) + ": end of data");
|
|
|
|
|
2017-08-07 01:36:37 -04:00
|
|
|
if (pch == nullptr)
|
2014-10-14 19:22:55 -03:00
|
|
|
throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
|
|
|
|
|
2017-08-07 01:36:37 -04:00
|
|
|
if (m_data == nullptr)
|
2014-10-14 19:22:55 -03:00
|
|
|
throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
|
|
|
|
|
|
|
|
memcpy(pch, m_data, nSize);
|
|
|
|
m_remaining -= nSize;
|
|
|
|
m_data += nSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2017-07-07 22:09:55 -04:00
|
|
|
TxInputStream& operator>>(T&& obj)
|
2014-10-14 19:22:55 -03:00
|
|
|
{
|
2016-10-28 20:29:17 -03:00
|
|
|
::Unserialize(*this, obj);
|
2014-10-14 19:22:55 -03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-10-28 20:29:17 -03:00
|
|
|
int GetVersion() const { return m_version; }
|
2014-10-14 19:22:55 -03:00
|
|
|
private:
|
|
|
|
const int m_version;
|
|
|
|
const unsigned char* m_data;
|
|
|
|
size_t m_remaining;
|
|
|
|
};
|
|
|
|
|
|
|
|
inline int set_error(bitcoinconsensus_error* ret, bitcoinconsensus_error serror)
|
|
|
|
{
|
|
|
|
if (ret)
|
|
|
|
*ret = serror;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-07-28 15:11:20 -03:00
|
|
|
struct ECCryptoClosure
|
|
|
|
{
|
|
|
|
ECCVerifyHandle handle;
|
|
|
|
};
|
|
|
|
|
|
|
|
ECCryptoClosure instance_of_eccryptoclosure;
|
2017-05-31 16:21:25 -04:00
|
|
|
} // namespace
|
2014-10-14 19:22:55 -03:00
|
|
|
|
2016-10-20 03:30:03 -03:00
|
|
|
/** Check that all specified flags are part of the libconsensus interface. */
|
|
|
|
static bool verify_flags(unsigned int flags)
|
|
|
|
{
|
|
|
|
return (flags & ~(bitcoinconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
|
|
|
|
}
|
|
|
|
|
2016-01-24 13:29:39 -03:00
|
|
|
static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
|
2014-10-14 19:22:55 -03:00
|
|
|
const unsigned char *txTo , unsigned int txToLen,
|
|
|
|
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
|
|
|
|
{
|
2016-10-20 03:30:03 -03:00
|
|
|
if (!verify_flags(flags)) {
|
2018-07-13 07:47:07 -04:00
|
|
|
return set_error(err, bitcoinconsensus_ERR_INVALID_FLAGS);
|
2016-10-20 03:30:03 -03:00
|
|
|
}
|
2014-10-14 19:22:55 -03:00
|
|
|
try {
|
2020-09-22 10:38:54 -03:00
|
|
|
TxInputStream stream(PROTOCOL_VERSION, txTo, txToLen);
|
2016-11-11 21:23:17 -03:00
|
|
|
CTransaction tx(deserialize, stream);
|
2014-10-14 19:22:55 -03:00
|
|
|
if (nIn >= tx.vin.size())
|
|
|
|
return set_error(err, bitcoinconsensus_ERR_TX_INDEX);
|
2018-06-22 14:27:18 -04:00
|
|
|
if (GetSerializeSize(tx, PROTOCOL_VERSION) != txToLen)
|
2014-10-14 19:22:55 -03:00
|
|
|
return set_error(err, bitcoinconsensus_ERR_TX_SIZE_MISMATCH);
|
|
|
|
|
2016-01-24 13:29:39 -03:00
|
|
|
// Regardless of the verification result, the tx did not error.
|
|
|
|
set_error(err, bitcoinconsensus_ERR_OK);
|
2016-08-03 20:49:16 -04:00
|
|
|
|
2016-08-26 13:38:20 -03:00
|
|
|
PrecomputedTransactionData txdata(tx);
|
2021-03-01 23:07:14 -03:00
|
|
|
return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata, MissingDataBehavior::FAIL), nullptr);
|
2014-12-07 09:29:06 -03:00
|
|
|
} catch (const std::exception&) {
|
2014-10-14 19:22:55 -03:00
|
|
|
return set_error(err, bitcoinconsensus_ERR_TX_DESERIALIZE); // Error deserializing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 13:29:39 -03:00
|
|
|
int bitcoinconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
|
|
|
|
const unsigned char *txTo , unsigned int txToLen,
|
|
|
|
unsigned int nIn, unsigned int flags, bitcoinconsensus_error* err)
|
|
|
|
{
|
|
|
|
CAmount am(amount);
|
|
|
|
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
if (flags & bitcoinconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
|
|
|
|
return set_error(err, bitcoinconsensus_ERR_AMOUNT_REQUIRED);
|
|
|
|
}
|
|
|
|
|
|
|
|
CAmount am(0);
|
|
|
|
return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
|
|
|
|
}
|
|
|
|
|
2014-10-14 19:22:55 -03:00
|
|
|
unsigned int bitcoinconsensus_version()
|
|
|
|
{
|
|
|
|
// Just use the API version for now
|
|
|
|
return BITCOINCONSENSUS_API_VER;
|
|
|
|
}
|