Monero C++ Library
monero_utils.h
1 
53 #ifndef monero_utils_h
54 #define monero_utils_h
55 
56 #include "wallet/monero_wallet_model.h"
57 #include "cryptonote_basic/cryptonote_basic.h"
58 #include "serialization/keyvalue_serialization.h" // TODO: consolidate with other binary deps?
59 #include "storages/portable_storage.h"
60 
64 namespace monero_utils
65 {
66  using namespace std;
67  using namespace cryptonote;
68 
69  // ------------------------ CONSTANTS ---------------------------
70 
71  static const int RING_SIZE = 12; // network-enforced ring size
72 
73  // ------------------------ BINARY SERIALIZATION ----------------------------
74 
75  void json_to_binary(const std::string &json, std::string &bin);
76  void binary_to_json(const std::string &bin, std::string &json);
77  void binary_blocks_to_json(const std::string &bin, std::string &json);
78 
79  // ------------------------------ RAPIDJSON ---------------------------------
80 
81  string serialize(const rapidjson::Document& doc);
82 
83  void addJsonMember(string key, uint8_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
84  void addJsonMember(string key, uint32_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
85  void addJsonMember(string key, uint64_t val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
86  void addJsonMember(string key, string val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
87  void addJsonMember(string key, bool val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root);
88 
89  // 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
90  template <class T> rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const vector<shared_ptr<T>>& vals) {
91  rapidjson::Value value_arr(rapidjson::kArrayType);
92  for (const auto& val : vals) {
93  value_arr.PushBack(val->to_rapidjson_val(allocator), allocator);
94  }
95  return value_arr;
96  }
97 
98  // 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
99  template <class T> rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const vector<T>& vals) {
100  rapidjson::Value value_arr(rapidjson::kArrayType);
101  for (const auto& val : vals) {
102  value_arr.PushBack(val.to_rapidjson_val(allocator), allocator);
103  }
104  return value_arr;
105  }
106 
107  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const vector<string>& strs);
108  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const vector<uint8_t>& nums);
109  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const vector<uint32_t>& nums);
110  rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const vector<uint64_t>& nums);
111 
112  // ------------------------ PROPERTY TREES ---------------------------
113 
114  // TODO: fully switch from property trees to rapidjson
115 
116  string serialize(const boost::property_tree::ptree& node);
117  void deserialize(const string& json, boost::property_tree::ptree& root);
118 
119  // --------------------------------------------------------------------------
120 
121  void deserialize(const string& json, boost::property_tree::ptree& root);
122 
129  shared_ptr<monero_block> cn_block_to_block(const cryptonote::block& cn_block);
130 
138  shared_ptr<monero_tx> cn_tx_to_tx(const cryptonote::transaction& cn_tx, bool init_as_tx_wallet = false);
139 
145  static std::string get_pruned_tx_json(cryptonote::transaction &tx)
146  {
147  std::stringstream ss;
148  json_archive<true> ar(ss);
149  bool r = tx.serialize_base(ar);
150  CHECK_AND_ASSERT_MES(r, std::string(), "Failed to serialize rct signatures base");
151  return ss.str();
152  }
153 }
154 #endif /* monero_utils_h */
shared_ptr< monero_block > cn_block_to_block(const cryptonote::block &cn_block)
Definition: monero_utils.cpp:236
shared_ptr< monero_tx > cn_tx_to_tx(const cryptonote::transaction &cn_tx, bool init_as_tx_wallet=false)
Definition: monero_utils.cpp:252
Definition: monero_utils.h:64