Electroneum
tools.h
Go to the documentation of this file.
1 #ifndef ETN01_TOOLS_H
2 #define ETN01_TOOLS_H
3 
4 #define PATH_SEPARARTOR '/'
5 
6 #define REMOVE_HASH_BRAKETS(a_hash) \
7  a_hash.substr(1, a_hash.size()-2)
8 
9 
10 
11 #include "electroneum_headers.h"
12 
13 #include "ext/fmt/ostream.h"
14 #include "ext/fmt/format.h"
15 #include "ext/json.hpp"
16 
17 #include <boost/lexical_cast.hpp>
18 #include <boost/filesystem.hpp>
19 #include <boost/optional.hpp>
20 
21 
22 #include <string>
23 #include <vector>
24 #include <iterator>
25 #include <algorithm>
26 #include <type_traits>
27 
28 
34 namespace etneg
35 {
36 
37  using namespace cryptonote;
38  using namespace crypto;
39  using namespace std;
40 
41  namespace bf = boost::filesystem;
42 
44 
46  {
47  std::vector<crypto::public_key >& m_output_keys;
48 
49  const Blockchain& m_bch;
50 
51  outputs_visitor(std::vector<crypto::public_key>& output_keys, const Blockchain& bch) :
52  m_output_keys(output_keys), m_bch(bch)
53  {
54  }
55 
56  bool handle_output(uint64_t unlock_time, const crypto::public_key &pubkey)
57  {
58  m_output_keys.push_back(pubkey);
59  return true;
60  }
61  };
62 
63 
64  template <typename T>
65  bool
66  parse_str_secret_key(const string& key_str, T& secret_key);
67 
68 
69  bool
71  const string& hash_str,
72  transaction& tx);
73 
74  bool
75  parse_str_address(const string& address_str,
76  account_public_address& address,
77  bool testnet = false);
78 
79  inline bool
80  is_separator(char c);
81 
82  string
84  bool testnet = false);
85 
86  string
87  print_sig (const signature& sig);
88 
89  string
90  remove_trailing_path_separator(const string& in_path);
91 
92  bf::path
93  remove_trailing_path_separator(const bf::path& in_path);
94 
95  string
96  timestamp_to_str_gm(time_t timestamp, const char* format = "%F %T");
97 
98  ostream&
99  operator<< (ostream& os, const account_public_address& addr);
100 
101 
102  string
103  get_default_lmdb_folder(bool testnet = false);
104 
105  bool
106  generate_key_image(const crypto::key_derivation& derivation,
107  const std::size_t output_index,
108  const crypto::secret_key& sec_key,
109  const crypto::public_key& pub_key,
110  crypto::key_image& key_img);
111 
112  bool
113  get_blockchain_path(const boost::optional<string>& bc_path,
114  bf::path& blockchain_path,
115  bool testnet = false);
116 
117  uint64_t
119 
120  pair<uint64_t, uint64_t>
121  sum_money_in_outputs(const string& json_str);
122 
123  pair<uint64_t, uint64_t>
124  sum_money_in_outputs(const json& _json);
125 
126 
127  array<uint64_t, 4>
129  const transaction& tx,
130  vector<pair<txout_to_key, uint64_t>>& output_pub_keys,
131  vector<txin_to_key>& input_key_imgs);
132 
133  // this version for mempool txs from json
134  array<uint64_t, 6>
135  summary_of_in_out_rct(const json& _json);
136 
137  uint64_t
138  sum_money_in_inputs(const transaction& tx);
139 
140  pair<uint64_t, uint64_t>
141  sum_money_in_inputs(const string& json_str);
142 
143  pair<uint64_t, uint64_t>
144  sum_money_in_inputs(const json& _json);
145 
146  uint64_t
147  count_nonrct_inputs(const transaction& tx);
148 
149  uint64_t
150  count_nonrct_inputs(const string& json_str);
151 
152  uint64_t
153  count_nonrct_inputs(const json& _json);
154 
155  array<uint64_t, 2>
156  sum_money_in_tx(const transaction& tx);
157 
158  array<uint64_t, 2>
159  sum_money_in_txs(const vector<transaction>& txs);
160 
161  uint64_t
162  sum_fees_in_txs(const vector<transaction>& txs);
163 
164  uint64_t
165  get_mixin_no(const transaction& tx);
166 
167  vector<uint64_t>
168  get_mixin_no(const string& json_str);
169 
170  vector<uint64_t>
171  get_mixin_no(const json& _json);
172 
173  vector<uint64_t>
174  get_mixin_no_in_txs(const vector<transaction>& txs);
175 
176  vector<pair<txout_to_key, uint64_t>>
177  get_ouputs(const transaction& tx);
178 
179  vector<tuple<txout_to_key, uint64_t, uint64_t>>
180  get_ouputs_tuple(const transaction& tx);
181 
182  vector<txin_to_key>
183  get_key_images(const transaction& tx);
184 
185 
186  bool
187  get_payment_id(const vector<uint8_t>& extra,
188  crypto::hash& payment_id,
189  crypto::hash8& payment_id8);
190 
191  bool
192  get_payment_id(const transaction& tx,
193  crypto::hash& payment_id,
194  crypto::hash8& payment_id8);
195 
196 
197  inline double
198  get_xmr(uint64_t core_amount)
199  {
200  return static_cast<double>(core_amount) / 1e2;
201  }
202 
203  array<size_t, 5>
204  timestamp_difference(uint64_t t1, uint64_t t2);
205 
206  string
207  read(string filename);
208 
209 
210  pair<string, double>
211  timestamps_time_scale(const vector<uint64_t>& timestamps,
212  uint64_t timeN, uint64_t resolution = 80,
213  uint64_t time0 = 1397818193 /* timestamp of the second block */);
214 
215  bool
216  decode_ringct(const rct::rctSig & rv,
217  const crypto::public_key pub,
218  const crypto::secret_key &sec,
219  unsigned int i,
220  rct::key & mask,
221  uint64_t & amount);
222 
223  bool
224  url_decode(const std::string& in, std::string& out);
225 
226  map<std::string, std::string>
227  parse_crow_post_data(const string& req_body);
228 
229 // from wallet2::decrypt
230  string
231  decrypt(const std::string &ciphertext,
232  const crypto::secret_key &skey,
233  bool authenticated = true);
234 
235 // based on
236 // crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const
237  public_key
239 
240  bool
241  is_output_ours(const size_t& output_index,
242  const transaction& tx,
243  const public_key& pub_tx_key,
244  const secret_key& private_view_key,
245  const public_key& public_spend_key);
246 
247  bool
249  const transaction& tx,
250  const secret_key& private_view_key,
251  const public_key& public_spend_key,
252  uint64_t output_idx,
253  public_key output_pub_key);
254 
255 // based on http://stackoverflow.com/a/9943098/248823
256  template<typename Iterator, typename Func>
257  void chunks(Iterator begin,
258  Iterator end,
259  iterator_traits<string::iterator>::difference_type k,
260  Func f)
261  {
262  Iterator chunk_begin;
263  Iterator chunk_end;
264  chunk_end = chunk_begin = begin;
265 
266  do
267  {
268  if(std::distance(chunk_end, end) < k)
269  chunk_end = end;
270  else
271  std::advance(chunk_end, k);
272  f(chunk_begin,chunk_end);
273  chunk_begin = chunk_end;
274  }
275  while(std::distance(chunk_begin,end) > 0);
276  }
277 
278  bool
279  make_tx_from_json(const string& json_str, transaction& tx);
280 
281  string
282  make_printable(const string& in_s);
283 
284  string
286 
287 // Get the median of an unordered set of numbers of arbitrary
288 // type without modifying the underlying dataset.
289 // taken from http://stackoverflow.com/a/19695285
290  template <typename It>
291  typename std::iterator_traits<It>::value_type
292  calc_median(It it_begin, It it_end)
293  {
294  using T = typename std::iterator_traits<It>::value_type;
295  std::vector<T> data(it_begin, it_end);
296  std::nth_element(data.begin(), data.begin() + data.size() / 2, data.end());
297  return data[data.size() / 2];
298  }
299 
300 
301  void
302  pause_execution(uint64_t no_seconds, const string& text = "now");
303 
304 }
305 
306 #endif
array< size_t, 5 > timestamp_difference(uint64_t t1, uint64_t t2)
Definition: tools.cpp:794
const uint32_t T[512]
Definition: groestl_tables.h:34
double get_xmr(uint64_t core_amount)
Definition: tools.h:198
bool get_tx_pub_key_from_str_hash(Blockchain &core_storage, const string &hash_str, transaction &tx)
Definition: tools.cpp:52
Definition: tools.h:45
nlohmann::json json
Definition: tools.h:43
uint64_t sum_fees_in_txs(const vector< transaction > &txs)
Definition: tools.cpp:583
POD_CLASS key_derivation
Definition: crypto.h:89
a class to store JSON values
Definition: json.hpp:1298
std::string format(CStringRef format_str, ArgList args)
Definition: format.h:3300
string make_printable(const string &in_s)
Definition: tools.cpp:1425
bool handle_output(uint64_t unlock_time, const crypto::public_key &pubkey)
Definition: tools.h:56
crypto namespace.
Definition: crypto.cpp:47
Definition: block_queue.cpp:41
uint64_t sum_money_in_outputs(const transaction &tx)
Definition: tools.cpp:266
string decrypt(const std::string &ciphertext, const crypto::secret_key &skey, bool authenticated)
Definition: tools.cpp:998
uint64_t count_nonrct_inputs(const transaction &tx)
Definition: tools.cpp:488
vector< pair< txout_to_key, uint64_t > > get_ouputs(const transaction &tx)
Definition: tools.cpp:598
vector< tuple< txout_to_key, uint64_t, uint64_t > > get_ouputs_tuple(const transaction &tx)
Definition: tools.cpp:623
const Blockchain & m_bch
Definition: tools.h:49
bool decode_ringct(const rct::rctSig &rv, const crypto::public_key pub, const crypto::secret_key &sec, unsigned int i, rct::key &mask, uint64_t &amount)
Definition: tools.cpp:877
outputs_visitor(std::vector< crypto::public_key > &output_keys, const Blockchain &bch)
Definition: tools.h:51
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
bool get_real_output_for_key_image(const key_image &ki, const transaction &tx, const secret_key &private_view_key, const public_key &public_spend_key, uint64_t output_idx, public_key output_pub_key)
Definition: tools.cpp:1143
void pause_execution(uint64_t no_seconds, const string &text)
Definition: tools.cpp:1482
string print_address(const account_public_address &address, bool testnet)
Definition: tools.cpp:91
void chunks(Iterator begin, Iterator end, iterator_traits< string::iterator >::difference_type k, Func f)
Definition: tools.h:257
map< std::string, std::string > parse_crow_post_data(const string &req_body)
Definition: tools.cpp:969
vector< uint64_t > get_mixin_no_in_txs(const vector< transaction > &txs)
Definition: tools.cpp:711
bool generate_key_image(const crypto::key_derivation &derivation, const std::size_t i, const crypto::secret_key &sec_key, const crypto::public_key &pub_key, crypto::key_image &key_img)
Definition: tools.cpp:169
array< uint64_t, 2 > sum_money_in_txs(const vector< transaction > &txs)
Definition: tools.cpp:568
uint64_t sum_money_in_inputs(const transaction &tx)
Definition: tools.cpp:423
Definition: rctTypes.h:82
#define ts
Definition: skein.c:522
Definition: rctTypes.h:323
ostream & operator<<(ostream &os, const account_public_address &addr)
Definition: tools.cpp:159
bool get_blockchain_path(const boost::optional< string > &bc_path, bf::path &blockchain_path, bool testnet)
Definition: tools.cpp:239
string read(string filename)
Definition: tools.cpp:831
string get_default_lmdb_folder(bool testnet)
Definition: tools.cpp:217
std::iterator_traits< It >::value_type calc_median(It it_begin, It it_end)
Definition: tools.h:292
POD_CLASS public_key
Definition: crypto.h:65
std::vector< crypto::public_key > & m_output_keys
Definition: tools.h:47
bool make_tx_from_json(const string &json_str, transaction &tx)
Definition: tools.cpp:1158
array< uint64_t, 2 > sum_money_in_tx(const transaction &tx)
Definition: tools.cpp:556
t2
Definition: pow22523.h:103
POD_CLASS signature
Definition: crypto.h:99
Definition: cryptonote_basic.h:421
POD_CLASS hash8
Definition: hash.h:49
bool is_separator(char c)
Definition: tools.cpp:109
string remove_trailing_path_separator(const string &in_path)
Definition: tools.cpp:122
POD_CLASS key_image
Definition: crypto.h:93
POD_CLASS secret_key
Definition: crypto.h:69
t1
Definition: pow22523.h:58
bool parse_str_secret_key(const string &key_str, T &secret_key)
Definition: tools.cpp:18
bool parse_str_address(const string &address_str, account_public_address &address, bool testnet)
Definition: tools.cpp:75
POD_CLASS hash
Definition: hash.h:46
pair< string, double > timestamps_time_scale(const vector< uint64_t > &timestamps, uint64_t timeN, uint64_t resolution, uint64_t time0)
Definition: tools.cpp:845
string get_human_readable_timestamp(uint64_t ts)
Definition: tools.cpp:1464
basic_json<> json
default JSON class
Definition: json.hpp:14369
array< uint64_t, 4 > summary_of_in_out_rct(const transaction &tx, vector< pair< txout_to_key, uint64_t >> &output_pub_keys, vector< txin_to_key > &input_key_imgs)
Definition: tools.cpp:321
public_key get_tx_pub_key_from_received_outs(const transaction &tx)
Definition: tools.cpp:1049
uint64_t get_mixin_no(const transaction &tx)
Definition: tools.cpp:646
string timestamp_to_str_gm(time_t timestamp, const char *format)
Definition: tools.cpp:141
bool get_payment_id(const vector< uint8_t > &extra, crypto::hash &payment_id, crypto::hash8 &payment_id8)
Definition: tools.cpp:751
vector< txin_to_key > get_key_images(const transaction &tx)
Definition: tools.cpp:725
bool is_output_ours(const size_t &output_index, const transaction &tx, const public_key &pub_tx_key, const secret_key &private_view_key, const public_key &public_spend_key)
Definition: tools.cpp:1096
bool url_decode(const std::string &in, std::string &out)
Definition: tools.cpp:929
Definition: cryptonote_basic.h:198
Definition: MicroCore.cpp:13
string print_sig(const signature &sig)
Definition: tools.cpp:96
Definition: blockchain.h:80