Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "starwelsconsensus.h"
  6. #include "primitives/transaction.h"
  7. #include "pubkey.h"
  8. #include "script/interpreter.h"
  9. #include "version.h"
  10. namespace {
  11. /** A class that deserializes a single CTransaction one time. */
  12. class TxInputStream
  13. {
  14. public:
  15. TxInputStream(int nTypeIn, int nVersionIn, const unsigned char *txTo, size_t txToLen) :
  16. m_type(nTypeIn),
  17. m_version(nVersionIn),
  18. m_data(txTo),
  19. m_remaining(txToLen)
  20. {}
  21. void read(char* pch, size_t nSize)
  22. {
  23. if (nSize > m_remaining)
  24. throw std::ios_base::failure(std::string(__func__) + ": end of data");
  25. if (pch == nullptr)
  26. throw std::ios_base::failure(std::string(__func__) + ": bad destination buffer");
  27. if (m_data == nullptr)
  28. throw std::ios_base::failure(std::string(__func__) + ": bad source buffer");
  29. memcpy(pch, m_data, nSize);
  30. m_remaining -= nSize;
  31. m_data += nSize;
  32. }
  33. template<typename T>
  34. TxInputStream& operator>>(T& obj)
  35. {
  36. ::Unserialize(*this, obj);
  37. return *this;
  38. }
  39. int GetVersion() const { return m_version; }
  40. int GetType() const { return m_type; }
  41. private:
  42. const int m_type;
  43. const int m_version;
  44. const unsigned char* m_data;
  45. size_t m_remaining;
  46. };
  47. inline int set_error(starwelsconsensus_error* ret, starwelsconsensus_error serror)
  48. {
  49. if (ret)
  50. *ret = serror;
  51. return 0;
  52. }
  53. struct ECCryptoClosure
  54. {
  55. ECCVerifyHandle handle;
  56. };
  57. ECCryptoClosure instance_of_eccryptoclosure;
  58. } // namespace
  59. /** Check that all specified flags are part of the libconsensus interface. */
  60. static bool verify_flags(unsigned int flags)
  61. {
  62. return (flags & ~(starwelsconsensus_SCRIPT_FLAGS_VERIFY_ALL)) == 0;
  63. }
  64. static int verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, CAmount amount,
  65. const unsigned char *txTo , unsigned int txToLen,
  66. unsigned int nIn, unsigned int flags, starwelsconsensus_error* err)
  67. {
  68. if (!verify_flags(flags)) {
  69. return starwelsconsensus_ERR_INVALID_FLAGS;
  70. }
  71. try {
  72. TxInputStream stream(SER_NETWORK, PROTOCOL_VERSION, txTo, txToLen);
  73. CTransaction tx(deserialize, stream);
  74. if (nIn >= tx.vin.size())
  75. return set_error(err, starwelsconsensus_ERR_TX_INDEX);
  76. if (GetSerializeSize(tx, SER_NETWORK, PROTOCOL_VERSION) != txToLen)
  77. return set_error(err, starwelsconsensus_ERR_TX_SIZE_MISMATCH);
  78. // Regardless of the verification result, the tx did not error.
  79. set_error(err, starwelsconsensus_ERR_OK);
  80. PrecomputedTransactionData txdata(tx);
  81. return VerifyScript(tx.vin[nIn].scriptSig, CScript(scriptPubKey, scriptPubKey + scriptPubKeyLen), &tx.vin[nIn].scriptWitness, flags, TransactionSignatureChecker(&tx, nIn, amount, txdata), nullptr);
  82. } catch (const std::exception&) {
  83. return set_error(err, starwelsconsensus_ERR_TX_DESERIALIZE); // Error deserializing
  84. }
  85. }
  86. int starwelsconsensus_verify_script_with_amount(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen, int64_t amount,
  87. const unsigned char *txTo , unsigned int txToLen,
  88. unsigned int nIn, unsigned int flags, starwelsconsensus_error* err)
  89. {
  90. CAmount am(amount);
  91. return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
  92. }
  93. int starwelsconsensus_verify_script(const unsigned char *scriptPubKey, unsigned int scriptPubKeyLen,
  94. const unsigned char *txTo , unsigned int txToLen,
  95. unsigned int nIn, unsigned int flags, starwelsconsensus_error* err)
  96. {
  97. if (flags & starwelsconsensus_SCRIPT_FLAGS_VERIFY_WITNESS) {
  98. return set_error(err, starwelsconsensus_ERR_AMOUNT_REQUIRED);
  99. }
  100. CAmount am(0);
  101. return ::verify_script(scriptPubKey, scriptPubKeyLen, am, txTo, txToLen, nIn, flags, err);
  102. }
  103. unsigned int starwelsconsensus_version()
  104. {
  105. // Just use the API version for now
  106. return STARWELSCONSENSUS_API_VER;
  107. }