Monero C++ Library
monero_utils.h
1 
53 #pragma once
54 
55 #ifndef monero_utils_h
56 #define monero_utils_h
57 
58 #include "wallet/monero_wallet_model.h"
59 #include "cryptonote_basic/cryptonote_basic.h"
60 #include "serialization/keyvalue_serialization.h" // TODO: consolidate with other binary deps?
61 #include "storages/portable_storage.h"
62 
66 namespace monero_utils
67 {
68  using namespace cryptonote;
69 
70  // ------------------------ CONSTANTS ---------------------------
71 
72  static const int RING_SIZE = 12; // network-enforced ring size
73 
74  // ------------------------ BINARY SERIALIZATION ----------------------------
75 
76  void json_to_binary(const std::string &json, std::string &bin);
77  void binary_to_json(const std::string &bin, std::string &json);
78  void binary_blocks_to_json(const std::string &bin, std::string &json);
79 
80  // ------------------------------ RAPIDJSON ---------------------------------
81 
82  std::string serialize(const rapidjson::Document& doc);
83 
84  void addJsonMember(std::string key, uint8_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
85  void addJsonMember(std::string key, uint32_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
86  void addJsonMember(std::string key, uint64_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
87  void addJsonMember(std::string key, std::string val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
88  void addJsonMember(std::string key, bool val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root);
89 
90  // TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
91  template <class T> rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<std::shared_ptr<T>>& vals) {
92  rapidjson::Value value_arr(rapidjson::kArrayType);
93  for (const auto& val : vals) {
94  value_arr.PushBack(val->to_rapidjson_val(allocator), allocator);
95  }
96  return value_arr;
97  }
98 
99  // TODO: template implementation here, could move to monero_utils.hpp per https://stackoverflow.com/questions/3040480/c-template-function-compiles-in-header-but-not-implementation
100  template <class T> rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<T>& vals) {
101  rapidjson::Value value_arr(rapidjson::kArrayType);
102  for (const auto& val : vals) {
103  value_arr.PushBack(val.to_rapidjson_val(allocator), allocator);
104  }
105  return value_arr;
106  }
107 
108  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<std::string>& strs);
109  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<uint8_t>& nums);
110  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<uint32_t>& nums);
111  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<uint64_t>& nums);
112 
113  // ------------------------ PROPERTY TREES ---------------------------
114 
115  // TODO: fully switch from property trees to rapidjson
116 
117  std::string serialize(const boost::property_tree::ptree& node);
118  void deserialize(const std::string& json, boost::property_tree::ptree& root);
119 
120  // --------------------------------------------------------------------------
121 
128  bool is_valid_language(const std::string& language);
129 
136  std::shared_ptr<monero_block> cn_block_to_block(const cryptonote::block& cn_block);
137 
145  std::shared_ptr<monero_tx> cn_tx_to_tx(const cryptonote::transaction& cn_tx, bool init_as_tx_wallet = false);
146 
152  static std::string get_pruned_tx_json(cryptonote::transaction &tx)
153  {
154  std::stringstream ss;
155  json_archive<true> ar(ss);
156  bool r = tx.serialize_base(ar);
157  CHECK_AND_ASSERT_MES(r, std::string(), "Failed to serialize rct signatures base");
158  return ss.str();
159  }
160 
166  static void free(std::shared_ptr<monero_block> block) {
167  for (std::shared_ptr<monero_tx>& tx : block->m_txs) {
168  tx->m_block.reset();
169  monero_tx_wallet* tx_wallet = dynamic_cast<monero_tx_wallet*>(tx.get());
170  if (tx_wallet != nullptr) {
171  if (tx_wallet->m_outgoing_transfer != boost::none) tx_wallet->m_outgoing_transfer.get()->m_tx.reset();
172  for (std::shared_ptr<monero_transfer> transfer : tx_wallet->m_incoming_transfers) transfer->m_tx.reset();
173  for (std::shared_ptr<monero_output> output : tx_wallet->m_outputs) output->m_tx.reset();
174  for (std::shared_ptr<monero_output> input : tx_wallet->m_inputs) {
175  input->m_key_image.reset();
176  input->m_tx.reset();
177  }
178  }
179  }
180  block.reset();
181  }
182 
188  static void free(std::vector<std::shared_ptr<monero_block>> blocks) {
189  for (std::shared_ptr<monero_block>& block : blocks) monero_utils::free(block);
190  }
191 }
192 #endif /* monero_utils_h */
monero_utils::is_valid_language
bool is_valid_language(const std::string &language)
Definition: monero_utils.cpp:235
monero_utils::cn_tx_to_tx
std::shared_ptr< monero_tx > cn_tx_to_tx(const cryptonote::transaction &cn_tx, bool init_as_tx_wallet=false)
Definition: monero_utils.cpp:264
monero::monero_tx_wallet
Definition: monero_wallet_model.h:244
monero_utils
Definition: monero_utils.h:67
monero_utils::cn_block_to_block
std::shared_ptr< monero_block > cn_block_to_block(const cryptonote::block &cn_block)
Definition: monero_utils.cpp:248