53 #ifndef monero_utils_h 54 #define monero_utils_h 56 #include "wallet/monero_wallet.h" 57 #include "cryptonote_basic/cryptonote_basic.h" 58 #include "serialization/keyvalue_serialization.h" 59 #include "storages/portable_storage.h" 67 using namespace cryptonote;
69 void json_to_binary(
const std::string &json, std::string &bin);
71 void binary_to_json(
const std::string &bin, std::string &json);
73 void binary_blocks_to_json(
const std::string &bin, std::string &json);
75 shared_ptr<monero_tx_query> deserialize_tx_query(
const string& txQueryStr);
77 shared_ptr<monero_transfer_query> deserialize_transfer_query(
const string& transfer_query_str);
79 shared_ptr<monero_output_query> deserialize_output_query(
const string& output_query_str);
81 shared_ptr<monero_send_request> deserialize_send_request(
const string& send_request_str);
83 vector<shared_ptr<monero_key_image>> deserialize_key_images(
const string& key_images_json);
85 string serialize(
const boost::property_tree::ptree& node);
103 shared_ptr<monero_tx>
cn_tx_to_tx(
const cryptonote::transaction& cn_tx,
bool init_as_tx_wallet =
false);
110 static std::string get_pruned_tx_json(cryptonote::transaction &tx)
112 std::stringstream ss;
113 json_archive<true> ar(ss);
114 bool r = tx.serialize_base(ar);
115 CHECK_AND_ASSERT_MES(r, std::string(),
"Failed to serialize rct signatures base");
122 template <
class T> boost::property_tree::ptree to_property_tree(
const vector<shared_ptr<T>>& types) {
123 boost::property_tree::ptree typeNodes;
124 for (
const auto& type : types) {
125 typeNodes.push_back(std::make_pair(
"", type->to_property_tree()));
131 template <
class T> boost::property_tree::ptree to_property_tree(
const vector<T>& types) {
132 boost::property_tree::ptree typeNodes;
133 for (
const auto& type : types) {
134 typeNodes.push_back(std::make_pair(
"", type.to_property_tree()));
139 boost::property_tree::ptree to_property_tree(
const vector<string>& strs);
140 boost::property_tree::ptree to_property_tree(
const vector<uint8_t>& nums);
141 boost::property_tree::ptree to_property_tree(
const vector<uint32_t>& nums);
142 boost::property_tree::ptree to_property_tree(
const vector<uint64_t>& nums);
147 template <class T, typename std::enable_if<std::is_same<T, string>::value, T>::type* =
nullptr>
148 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max,
const string& err_msg =
"") {
151 if (val1 == val2)
return val1;
154 if (val1 == boost::none || val2 == boost::none) {
155 if (resolve_defined != boost::none && *resolve_defined ==
false)
return boost::none;
156 else return val1 == boost::none ? val2 : val1;
159 throw runtime_error(
string(
"Cannot reconcile strings: ") + to_string(val1) +
string(
" vs ") + to_string(val2) + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
161 template <class T, typename std::enable_if<std::is_same<T, string>::value, T>::type* =
nullptr>
162 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2,
const string& err_msg =
"") {
163 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
166 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* =
nullptr>
167 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max,
const string& err_msg =
"") {
170 if (val1 == val2)
return val1;
173 if (val1 == boost::none || val2 == boost::none) {
174 if (resolve_defined != boost::none && *resolve_defined ==
false)
return boost::none;
175 else return val1 == boost::none ? val2 : val1;
179 if (resolve_max != boost::none) {
180 return *resolve_max ? max(*val1, *val2) : min(*val1, *val2);
184 throw runtime_error(
string(
"Cannot reconcile integrals: ") + to_string(val1) +
string(
" vs ") + to_string(val2) + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
186 template <class T, typename std::enable_if<std::is_integral<T>::value, T>::type* =
nullptr>
187 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2,
const string& err_msg =
"") {
188 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
191 template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type>
192 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2, boost::optional<bool> resolve_defined, boost::optional<bool> resolve_true, boost::optional<bool> resolve_max,
const string& err_msg =
"") {
195 if (val1 == val2)
return val1;
198 if (val1 == boost::none || val2 == boost::none) {
199 if (resolve_defined != boost::none && *resolve_defined ==
false)
return boost::none;
200 else return val1 == boost::none ? val2 : val1;
204 if (resolve_true != boost::none) {
205 return val1 == resolve_true ? val1 : val2;
207 throw runtime_error(
string(
"Cannot reconcile booleans: ") + to_string(val1) +
string(
" vs ") + to_string(val2) + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
210 template <class T, typename std::enable_if<std::is_same<T, bool>::value, T>::type>
211 boost::optional<T> reconcile(
const boost::optional<T>& val1,
const boost::optional<T>& val2,
const string& err_msg =
"") {
212 return reconcile(val1, val2, boost::none, boost::none, boost::none, err_msg);
216 vector<T> reconcile(
const vector<T>& v1,
const vector<T>& v2,
const string& err_msg =
"") {
219 if (v1 == v2)
return v1;
222 if (v1.empty())
return v2;
223 if (v2.empty())
return v1;
226 throw runtime_error(
"Cannot reconcile vectors" + (!err_msg.empty() ? string(
". ") + err_msg : string(
"")));
shared_ptr< monero_block > cn_block_to_block(const cryptonote::block &cn_block)
Definition: monero_utils.cpp:494
shared_ptr< monero_tx > cn_tx_to_tx(const cryptonote::transaction &cn_tx, bool init_as_tx_wallet=false)
Definition: monero_utils.cpp:510
Definition: monero_utils.h:64