Line data Source code
1 : // Copyright (c) 2018-2024 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_EVO_SPECIALTX_H 6 : #define BITCOIN_EVO_SPECIALTX_H 7 : 8 : #include <primitives/transaction.h> 9 : #include <serialize.h> 10 : #include <streams.h> 11 : #include <uint256.h> 12 : #include <version.h> 13 : 14 : #include <optional> 15 : #include <vector> 16 : 17 : template <typename T> 18 233196 : std::optional<T> GetTxPayload(const std::vector<unsigned char>& payload) 19 : { 20 233196 : CDataStream ds(payload, SER_NETWORK, PROTOCOL_VERSION); 21 : try { 22 233196 : T obj; 23 233196 : ds >> obj; 24 233196 : return ds.empty() ? std::make_optional(std::move(obj)) : std::nullopt; 25 174618 : } catch (const std::exception& e) { 26 0 : return std::nullopt; 27 0 : } 28 233196 : } 29 : template <typename T, typename TxType> 30 225761 : std::optional<T> GetTxPayload(const TxType& tx, bool assert_type = true) 31 : { 32 225761 : if (assert_type) { ASSERT_IF_DEBUG(tx.nType == T::SPECIALTX_TYPE); } 33 225761 : if (tx.nType != T::SPECIALTX_TYPE) return std::nullopt; 34 225761 : return GetTxPayload<T>(tx.vExtraPayload); 35 225761 : } 36 : 37 : template <typename T> 38 32908 : void SetTxPayload(CMutableTransaction& tx, const T& payload) 39 : { 40 32908 : CDataStream ds(SER_NETWORK, PROTOCOL_VERSION); 41 32908 : ds << payload; 42 32908 : tx.vExtraPayload.assign(UCharCast(ds.data()), UCharCast(ds.data() + ds.size())); 43 32908 : } 44 : 45 : uint256 CalcTxInputsHash(const CTransaction& tx); 46 : 47 : #endif // BITCOIN_EVO_SPECIALTX_H