Monero C++ Library
Loading...
Searching...
No Matches
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
66namespace monero_utils
67{
68 using namespace cryptonote;
69
70 // ------------------------------ CONSTANTS ---------------------------------
71
72 static const int RING_SIZE = 12; // network-enforced ring size
73
74 // -------------------------------- UTILS -----------------------------------
75
76 void set_log_level(int level);
77 void configure_logging(const std::string& path, bool console);
78 monero_integrated_address get_integrated_address(monero_network_type network_type, const std::string& standard_address, const std::string& payment_id);
79 bool is_valid_address(const std::string& address, monero_network_type network_type);
80 bool is_valid_private_view_key(const std::string& private_view_key);
81 bool is_valid_private_spend_key(const std::string& private_spend_key);
82 void validate_address(const std::string& address, monero_network_type network_type);
83 void validate_private_view_key(const std::string& private_view_key);
84 void validate_private_spend_key(const std::string& private_spend_key);
85 void json_to_binary(const std::string &json, std::string &bin);
86 void binary_to_json(const std::string &bin, std::string &json);
87 void binary_blocks_to_json(const std::string &bin, std::string &json);
88
89 // ------------------------------ RAPIDJSON ---------------------------------
90
91 std::string serialize(const rapidjson::Document& doc);
92
98 template <class T>
99 void add_json_member(std::string key, T val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field) {
100 rapidjson::Value field_key(key.c_str(), key.size(), allocator);
101 field.SetInt64((uint64_t) val);
102 root.AddMember(field_key, field, allocator);
103 }
104 void add_json_member(std::string key, std::string val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root, rapidjson::Value& field);
105 void add_json_member(std::string key, bool val, rapidjson::Document::AllocatorType& allocator, rapidjson::Value& root);
106
107 // 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
108 template <class T> rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<std::shared_ptr<T>>& vals) {
109 rapidjson::Value value_arr(rapidjson::kArrayType);
110 for (const auto& val : vals) {
111 value_arr.PushBack(val->to_rapidjson_val(allocator), allocator);
112 }
113 return value_arr;
114 }
115
116 // 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
117 template <class T> rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<T>& vals) {
118 rapidjson::Value value_arr(rapidjson::kArrayType);
119 for (const auto& val : vals) {
120 value_arr.PushBack(val.to_rapidjson_val(allocator), allocator);
121 }
122 return value_arr;
123 }
124
125 rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<std::string>& strs);
126 rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<uint8_t>& nums);
127 rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<uint32_t>& nums);
128 rapidjson::Value to_rapidjson_val(rapidjson::Document::AllocatorType& allocator, const std::vector<uint64_t>& nums);
129
130 // ------------------------ PROPERTY TREES ---------------------------
131
132 // TODO: fully switch from property trees to rapidjson
133
134 std::string serialize(const boost::property_tree::ptree& node);
135 void deserialize(const std::string& json, boost::property_tree::ptree& root);
136
137 // --------------------------------------------------------------------------
138
145 bool is_valid_language(const std::string& language);
146
153 std::shared_ptr<monero_block> cn_block_to_block(const cryptonote::block& cn_block);
154
162 std::shared_ptr<monero_tx> cn_tx_to_tx(const cryptonote::transaction& cn_tx, bool init_as_tx_wallet = false);
163
169 static std::string get_pruned_tx_json(cryptonote::transaction &tx)
170 {
171 std::stringstream ss;
172 json_archive<true> ar(ss);
173 bool r = tx.serialize_base(ar);
174 CHECK_AND_ASSERT_MES(r, std::string(), "Failed to serialize rct signatures base");
175 return ss.str();
176 }
177
178 // ----------------------------- GATHER BLOCKS ------------------------------
179
180 static std::vector<std::shared_ptr<monero_block>> get_blocks_from_txs(std::vector<std::shared_ptr<monero_tx_wallet>> txs) {
181 std::shared_ptr<monero_block> unconfirmed_block = nullptr; // placeholder for unconfirmed txs
182 std::vector<std::shared_ptr<monero_block>> blocks;
183 std::unordered_set<std::shared_ptr<monero_block>> seen_block_ptrs;
184 for (const std::shared_ptr<monero_tx_wallet>& tx : txs) {
185 if (tx->m_block == boost::none) {
186 if (unconfirmed_block == nullptr) unconfirmed_block = std::make_shared<monero_block>();
187 tx->m_block = unconfirmed_block;
188 unconfirmed_block->m_txs.push_back(tx);
189 }
190 std::unordered_set<std::shared_ptr<monero_block>>::const_iterator got = seen_block_ptrs.find(tx->m_block.get());
191 if (got == seen_block_ptrs.end()) {
192 seen_block_ptrs.insert(tx->m_block.get());
193 blocks.push_back(tx->m_block.get());
194 }
195 }
196 return blocks;
197 }
198
199 static std::vector<std::shared_ptr<monero_block>> get_blocks_from_transfers(std::vector<std::shared_ptr<monero_transfer>> transfers) {
200 std::shared_ptr<monero_block> unconfirmed_block = nullptr; // placeholder for unconfirmed txs in return json
201 std::vector<std::shared_ptr<monero_block>> blocks;
202 std::unordered_set<std::shared_ptr<monero_block>> seen_block_ptrs;
203 for (auto const& transfer : transfers) {
204 std::shared_ptr<monero_tx_wallet> tx = transfer->m_tx;
205 if (tx->m_block == boost::none) {
206 if (unconfirmed_block == nullptr) unconfirmed_block = std::make_shared<monero_block>();
207 tx->m_block = unconfirmed_block;
208 unconfirmed_block->m_txs.push_back(tx);
209 }
210 std::unordered_set<std::shared_ptr<monero_block>>::const_iterator got = seen_block_ptrs.find(tx->m_block.get());
211 if (got == seen_block_ptrs.end()) {
212 seen_block_ptrs.insert(tx->m_block.get());
213 blocks.push_back(tx->m_block.get());
214 }
215 }
216 return blocks;
217 }
218
219 static std::vector<std::shared_ptr<monero_block>> get_blocks_from_outputs(std::vector<std::shared_ptr<monero_output_wallet>> outputs) {
220 std::vector<std::shared_ptr<monero_block>> blocks;
221 std::unordered_set<std::shared_ptr<monero_block>> seen_block_ptrs;
222 for (auto const& output : outputs) {
223 std::shared_ptr<monero_tx_wallet> tx = std::static_pointer_cast<monero_tx_wallet>(output->m_tx);
224 if (tx->m_block == boost::none) throw std::runtime_error("Need to handle unconfirmed output");
225 std::unordered_set<std::shared_ptr<monero_block>>::const_iterator got = seen_block_ptrs.find(*tx->m_block);
226 if (got == seen_block_ptrs.end()) {
227 seen_block_ptrs.insert(*tx->m_block);
228 blocks.push_back(*tx->m_block);
229 }
230 }
231 return blocks;
232 }
233
234 // ------------------------------ FREE MEMORY -------------------------------
235
236 static void free(std::shared_ptr<monero_block> block) {
237 for (std::shared_ptr<monero_tx>& tx : block->m_txs) {
238 tx->m_block->reset();
239 monero_tx_wallet* tx_wallet = dynamic_cast<monero_tx_wallet*>(tx.get());
240 if (tx_wallet != nullptr) {
241 if (tx_wallet->m_tx_set != boost::none) tx_wallet->m_tx_set->reset();
242 if (tx_wallet->m_outgoing_transfer != boost::none) tx_wallet->m_outgoing_transfer.get()->m_tx.reset();
243 for (std::shared_ptr<monero_transfer> transfer : tx_wallet->m_incoming_transfers) transfer->m_tx.reset();
244 for (std::shared_ptr<monero_output> output : tx_wallet->m_outputs) output->m_tx.reset();
245 for (std::shared_ptr<monero_output> input : tx_wallet->m_inputs) {
246 input->m_key_image.reset();
247 input->m_tx.reset();
248 }
249 }
250 monero_tx_query* tx_query = dynamic_cast<monero_tx_query*>(tx.get());
251 if (tx_query != nullptr) {
252 if (tx_query->m_transfer_query != boost::none) {
253 tx_query->m_transfer_query.get()->m_tx_query->reset();
254 tx_query->m_transfer_query.get().reset();
255 }
256 if (tx_query->m_output_query != boost::none) {
257 tx_query->m_output_query.get()->m_tx_query->reset();
258 tx_query->m_output_query.get().reset();
259 }
260 }
261 }
262 block.reset();
263 }
264
265 static void free(std::vector<std::shared_ptr<monero_block>> blocks) {
266 for (std::shared_ptr<monero_block>& block : blocks) monero_utils::free(block);
267 }
268
269 static void free(std::shared_ptr<monero_tx> tx) {
270 if (tx->m_block == boost::none) {
271 std::shared_ptr<monero_block> block = std::make_shared<monero_block>();
272 tx->m_block = block;
273 block->m_txs.push_back(tx);
274 }
275 monero_utils::free(tx->m_block.get());
276 }
277
278 static void free(std::vector<std::shared_ptr<monero_tx_wallet>> txs) {
279 return monero_utils::free(monero_utils::get_blocks_from_txs(txs));
280 }
281
282 static void free(std::vector<std::shared_ptr<monero_transfer>> transfers) {
283 return monero_utils::free(monero_utils::get_blocks_from_transfers(transfers));
284 }
285
286 static void free(std::vector<std::shared_ptr<monero_output_wallet>> outputs) {
287 return monero_utils::free(monero_utils::get_blocks_from_outputs(outputs));
288 }
289}
290#endif /* monero_utils_h */
Definition monero_utils.h:67
bool is_valid_language(const std::string &language)
Definition monero_utils.cpp:303
std::shared_ptr< monero_block > cn_block_to_block(const cryptonote::block &cn_block)
Definition monero_utils.cpp:316
void add_json_member(std::string key, T val, rapidjson::Document::AllocatorType &allocator, rapidjson::Value &root, rapidjson::Value &field)
Definition monero_utils.h:99
std::shared_ptr< monero_tx > cn_tx_to_tx(const cryptonote::transaction &cn_tx, bool init_as_tx_wallet=false)
Definition monero_utils.cpp:332
monero_network_type
Definition monero_daemon_model.h:90
Definition monero_wallet_model.h:350
Definition monero_wallet_model.h:305
Definition monero_wallet_model.h:271