Electroneum
cryptonote_format_utils.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
32 #pragma once
34 #include "cryptonote_basic_impl.h"
35 #include "account.h"
36 #include "include_base_utils.h"
37 #include "crypto/crypto.h"
38 #include "crypto/hash.h"
39 
40 namespace cryptonote
41 {
42  //---------------------------------------------------------------
43  void get_transaction_prefix_hash(const transaction_prefix& tx, crypto::hash& h);
44  crypto::hash get_transaction_prefix_hash(const transaction_prefix& tx);
45  bool parse_and_validate_tx_from_blob(const blobdata& tx_blob, transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash);
46  bool parse_and_validate_tx_from_blob(const blobdata& tx_blob, transaction& tx);
47  bool parse_and_validate_tx_base_from_blob(const blobdata& tx_blob, transaction& tx);
50 
51  template<typename T>
52  bool find_tx_extra_field_by_type(const std::vector<tx_extra_field>& tx_extra_fields, T& field, size_t index = 0)
53  {
54  auto it = std::find_if(tx_extra_fields.begin(), tx_extra_fields.end(), [&index](const tx_extra_field& f) { return typeid(T) == f.type() && !index--; });
55  if(tx_extra_fields.end() == it)
56  return false;
57 
58  field = boost::get<T>(*it);
59  return true;
60  }
61 
62  bool parse_tx_extra(const std::vector<uint8_t>& tx_extra, std::vector<tx_extra_field>& tx_extra_fields);
63  crypto::public_key get_tx_pub_key_from_extra(const std::vector<uint8_t>& tx_extra, size_t pk_index = 0);
64  crypto::public_key get_tx_pub_key_from_extra(const transaction_prefix& tx, size_t pk_index = 0);
65  crypto::public_key get_tx_pub_key_from_extra(const transaction& tx, size_t pk_index = 0);
66  bool add_tx_pub_key_to_extra(transaction& tx, const crypto::public_key& tx_pub_key);
67  bool add_extra_nonce_to_tx_extra(std::vector<uint8_t>& tx_extra, const blobdata& extra_nonce);
68  bool remove_field_from_tx_extra(std::vector<uint8_t>& tx_extra, const std::type_info &type);
69  void set_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash& payment_id);
70  void set_encrypted_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash8& payment_id);
71  bool get_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash& payment_id);
72  bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash8& payment_id);
73  bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::public_key& tx_pub_key, size_t output_index);
74  bool is_out_to_acc_precomp(const crypto::public_key& spend_public_key, const txout_to_key& out_key, const crypto::key_derivation& derivation, size_t output_index);
75  bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, std::vector<size_t>& outs, uint64_t& money_transfered);
76  bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector<size_t>& outs, uint64_t& money_transfered);
77  bool get_tx_fee(const transaction& tx, uint64_t & fee);
78  uint64_t get_tx_fee(const transaction& tx);
79  bool generate_key_image_helper(const account_keys& ack, const crypto::public_key& tx_public_key, size_t real_output_index, keypair& in_ephemeral, crypto::key_image& ki);
80  void get_blob_hash(const blobdata& blob, crypto::hash& res);
81  crypto::hash get_blob_hash(const blobdata& blob);
82  std::string short_hash_str(const crypto::hash& h);
83 
84  crypto::hash get_transaction_hash(const transaction& t);
85  bool get_transaction_hash(const transaction& t, crypto::hash& res);
86  bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t& blob_size);
87  bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t* blob_size);
88  bool calculate_transaction_hash(const transaction& t, crypto::hash& res, size_t* blob_size);
89  blobdata get_block_hashing_blob(const block& b);
90  bool calculate_block_hash(const block& b, crypto::hash& res);
91  bool get_block_hash(const block& b, crypto::hash& res);
92  crypto::hash get_block_hash(const block& b);
93  bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height);
94  crypto::hash get_block_longhash(const block& b, uint64_t height);
95  bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b);
96  bool get_inputs_money_amount(const transaction& tx, uint64_t& money);
97  uint64_t get_outs_money_amount(const transaction& tx);
98  bool check_inputs_types_supported(const transaction& tx);
99  bool check_outs_valid(const transaction& tx);
100  bool parse_amount(uint64_t& amount, const std::string& str_amount);
101 
102  bool check_money_overflow(const transaction& tx);
103  bool check_outs_overflow(const transaction& tx);
104  bool check_inputs_overflow(const transaction& tx);
105  uint64_t get_block_height(const block& b);
106  std::vector<uint64_t> relative_output_offsets_to_absolute(const std::vector<uint64_t>& off);
107  std::vector<uint64_t> absolute_output_offsets_to_relative(const std::vector<uint64_t>& off);
108  void set_default_decimal_point(unsigned int decimal_point = CRYPTONOTE_DISPLAY_DECIMAL_POINT);
109  unsigned int get_default_decimal_point();
110  std::string get_unit(unsigned int decimal_point = -1);
111  std::string print_money(uint64_t amount, unsigned int decimal_point = -1);
112  //---------------------------------------------------------------
113  template<class t_object>
114  bool t_serializable_object_to_blob(const t_object& to, blobdata& b_blob)
115  {
116  std::stringstream ss;
117  binary_archive<true> ba(ss);
118  bool r = ::serialization::serialize(ba, const_cast<t_object&>(to));
119  b_blob = ss.str();
120  return r;
121  }
122  //---------------------------------------------------------------
123  template<class t_object>
125  {
126  blobdata b;
128  return b;
129  }
130  //---------------------------------------------------------------
131  template<class t_object>
132  bool get_object_hash(const t_object& o, crypto::hash& res)
133  {
135  return true;
136  }
137  //---------------------------------------------------------------
138  template<class t_object>
139  size_t get_object_blobsize(const t_object& o)
140  {
142  return b.size();
143  }
144  //---------------------------------------------------------------
145  template<class t_object>
146  bool get_object_hash(const t_object& o, crypto::hash& res, size_t& blob_size)
147  {
149  blob_size = bl.size();
150  get_blob_hash(bl, res);
151  return true;
152  }
153  //---------------------------------------------------------------
154  template <typename T>
155  std::string obj_to_json_str(T& obj)
156  {
157  std::stringstream ss;
158  json_archive<true> ar(ss, true);
159  bool r = ::serialization::serialize(ar, obj);
160  CHECK_AND_ASSERT_MES(r, "", "obj_to_json_str failed: serialization::serialize returned false");
161  return ss.str();
162  }
163  //---------------------------------------------------------------
164  // 62387455827 -> 455827 + 7000000 + 80000000 + 300000000 + 2000000000 + 60000000000, where 455827 <= dust_threshold
165  template<typename chunk_handler_t, typename dust_handler_t>
166  void decompose_amount_into_digits(uint64_t amount, uint64_t dust_threshold, const chunk_handler_t& chunk_handler, const dust_handler_t& dust_handler)
167  {
168  if (0 == amount)
169  {
170  return;
171  }
172 
173  bool is_dust_handled = false;
174  uint64_t dust = 0;
175  uint64_t order = 1;
176  while (0 != amount)
177  {
178  uint64_t chunk = (amount % 10) * order;
179  amount /= 10;
180  order *= 10;
181 
182  if (dust + chunk <= dust_threshold)
183  {
184  dust += chunk;
185  }
186  else
187  {
188  if (!is_dust_handled && 0 != dust)
189  {
190  dust_handler(dust);
191  is_dust_handled = true;
192  }
193  if (0 != chunk)
194  {
195  chunk_handler(chunk);
196  }
197  }
198  }
199 
200  if (!is_dust_handled && 0 != dust)
201  {
202  dust_handler(dust);
203  }
204  }
205  //---------------------------------------------------------------
206  blobdata block_to_blob(const block& b);
207  bool block_to_blob(const block& b, blobdata& b_blob);
208  blobdata tx_to_blob(const transaction& b);
209  bool tx_to_blob(const transaction& b, blobdata& b_blob);
210  void get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes, crypto::hash& h);
211  crypto::hash get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes);
212  crypto::hash get_tx_tree_hash(const block& b);
213  bool is_valid_decomposed_amount(uint64_t amount);
214  void get_hash_stats(uint64_t &tx_hashes_calculated, uint64_t &tx_hashes_cached, uint64_t &block_hashes_calculated, uint64_t & block_hashes_cached);
215 
216 #define CHECKED_GET_SPECIFIC_VARIANT(variant_var, specific_type, variable_name, fail_return_val) \
217  CHECK_AND_ASSERT_MES(variant_var.type() == typeid(specific_type), fail_return_val, "wrong variant type: " << variant_var.type().name() << ", expected " << typeid(specific_type).name()); \
218  specific_type& variable_name = boost::get<specific_type>(variant_var);
219 
220 }
bool parse_tx_extra(const std::vector< uint8_t > &tx_extra, std::vector< tx_extra_field > &tx_extra_fields)
Definition: cryptonote_format_utils.cpp:218
bool check_outs_overflow(const transaction &tx)
Definition: cryptonote_format_utils.cpp:447
uint64_t get_outs_money_amount(const transaction &tx)
Definition: cryptonote_format_utils.cpp:459
const uint32_t T[512]
Definition: groestl_tables.h:34
bool get_tx_fee(const transaction &tx, uint64_t &fee)
Definition: cryptonote_format_utils.cpp:193
bool check_outs_valid(const transaction &tx)
Definition: cryptonote_format_utils.cpp:413
#define CRYPTONOTE_DISPLAY_DECIMAL_POINT
Definition: cryptonote_config.h:67
POD_CLASS key_derivation
Definition: crypto.h:89
void serialize(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition: unordered_containers_boost_serialization.h:127
size_t get_object_blobsize(const t_object &o)
Definition: cryptonote_format_utils.h:139
void decompose_amount_into_digits(uint64_t amount, uint64_t dust_threshold, const chunk_handler_t &chunk_handler, const dust_handler_t &dust_handler)
Definition: cryptonote_format_utils.h:166
uint64_t height
Definition: blockchain.cpp:87
bool calculate_block_hash(const block &b, crypto::hash &res)
Definition: cryptonote_format_utils.cpp:691
Definition: binary_archive.h:182
bool find_tx_extra_field_by_type(const std::vector< tx_extra_field > &tx_extra_fields, T &field, size_t index=0)
Definition: cryptonote_format_utils.h:52
std::vector< uint64_t > relative_output_offsets_to_absolute(const std::vector< uint64_t > &off)
Definition: cryptonote_format_utils.cpp:731
bool encrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key)
Definition: cryptonote_format_utils.cpp:359
std::string get_unit(unsigned int decimal_point)
Definition: cryptonote_format_utils.cpp:539
bool get_object_hash(const t_object &o, crypto::hash &res)
Definition: cryptonote_format_utils.h:132
bool calculate_transaction_hash(const transaction &t, crypto::hash &res, size_t *blob_size)
Definition: cryptonote_format_utils.cpp:588
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:225
bool parse_amount(uint64_t &amount, const std::string &str_amount_)
Definition: cryptonote_format_utils.cpp:158
bool get_block_longhash(const block &b, crypto::hash &res, uint64_t height)
Definition: cryptonote_format_utils.cpp:723
blobdata tx_to_blob(const transaction &tx)
Definition: cryptonote_format_utils.cpp:780
bool is_out_to_acc_precomp(const crypto::public_key &spend_public_key, const txout_to_key &out_key, const crypto::key_derivation &derivation, size_t output_index)
Definition: cryptonote_format_utils.cpp:485
bool is_valid_decomposed_amount(uint64_t amount)
Definition: cryptonote_format_utils.cpp:814
bool add_tx_pub_key_to_extra(transaction &tx, const crypto::public_key &tx_pub_key)
Definition: cryptonote_format_utils.cpp:268
unsigned int get_default_decimal_point()
Definition: cryptonote_format_utils.cpp:534
Definition: json_archive.h:115
bool get_inputs_money_amount(const transaction &tx, uint64_t &money)
Definition: cryptonote_format_utils.cpp:383
bool check_inputs_types_supported(const transaction &tx)
Definition: cryptonote_format_utils.cpp:401
bool get_block_hash(const block &b, crypto::hash &res)
Definition: cryptonote_format_utils.cpp:696
bool check_money_overflow(const transaction &tx)
Definition: cryptonote_format_utils.cpp:429
std::vector< uint64_t > absolute_output_offsets_to_relative(const std::vector< uint64_t > &off)
Definition: cryptonote_format_utils.cpp:739
blobdata get_block_hashing_blob(const block &b)
Definition: cryptonote_format_utils.cpp:682
std::string obj_to_json_str(T &obj)
Definition: cryptonote_format_utils.h:155
int b
Definition: base.py:1
bool t_serializable_object_to_blob(const t_object &to, blobdata &b_blob)
Definition: cryptonote_format_utils.h:114
bool get_payment_id_from_tx_extra_nonce(const blobdata &extra_nonce, crypto::hash &payment_id)
Definition: cryptonote_format_utils.cpp:339
crypto::public_key get_tx_pub_key_from_extra(const std::vector< uint8_t > &tx_extra, size_t pk_index)
Definition: cryptonote_format_utils.cpp:246
bool is_out_to_acc(const account_keys &acc, const txout_to_key &out_key, const crypto::public_key &tx_pub_key, size_t output_index)
Definition: cryptonote_format_utils.cpp:476
type
Definition: json.h:74
boost::variant< tx_extra_padding, tx_extra_pub_key, tx_extra_nonce, tx_extra_merge_mining_tag, tx_extra_mysterious_minergate > tx_extra_field
Definition: tx_extra.h:176
bool parse_and_validate_tx_base_from_blob(const blobdata &tx_blob, transaction &tx)
Definition: cryptonote_format_utils.cpp:108
bool parse_and_validate_tx_from_blob(const blobdata &tx_blob, transaction &tx)
Definition: cryptonote_format_utils.cpp:97
POD_CLASS public_key
Definition: crypto.h:65
void set_default_decimal_point(unsigned int decimal_point)
Definition: cryptonote_format_utils.cpp:522
bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata &extra_nonce, crypto::hash8 &payment_id)
Definition: cryptonote_format_utils.cpp:349
bool check_inputs_overflow(const transaction &tx)
Definition: cryptonote_format_utils.cpp:434
std::string blobdata
Definition: blobdatatype.h:36
void get_transaction_prefix_hash(const transaction_prefix &tx, crypto::hash &h)
Definition: cryptonote_format_utils.cpp:82
POD_CLASS hash8
Definition: hash.h:49
POD_CLASS key_image
Definition: crypto.h:93
bool decrypt_payment_id(crypto::hash8 &payment_id, const crypto::public_key &public_key, const crypto::secret_key &secret_key)
Definition: cryptonote_format_utils.cpp:377
POD_CLASS secret_key
Definition: crypto.h:69
crypto::hash get_transaction_hash(const transaction &t)
Definition: cryptonote_format_utils.cpp:575
bool remove_field_from_tx_extra(std::vector< uint8_t > &tx_extra, const std::type_info &type)
Definition: cryptonote_format_utils.cpp:292
bool add_extra_nonce_to_tx_extra(std::vector< uint8_t > &tx_extra, const blobdata &extra_nonce)
Definition: cryptonote_format_utils.cpp:276
POD_CLASS hash
Definition: hash.h:46
void get_tx_tree_hash(const std::vector< crypto::hash > &tx_hashes, crypto::hash &h)
Definition: cryptonote_format_utils.cpp:790
void set_encrypted_payment_id_to_tx_extra_nonce(blobdata &extra_nonce, const crypto::hash8 &payment_id)
Definition: cryptonote_format_utils.cpp:331
void get_hash_stats(uint64_t &tx_hashes_calculated, uint64_t &tx_hashes_cached, uint64_t &block_hashes_calculated, uint64_t &block_hashes_cached)
Definition: cryptonote_format_utils.cpp:821
void set_payment_id_to_tx_extra_nonce(blobdata &extra_nonce, const crypto::hash &payment_id)
Definition: cryptonote_format_utils.cpp:323
std::string print_money(uint64_t amount, unsigned int decimal_point)
Definition: cryptonote_format_utils.cpp:554
bool lookup_acc_outs(const account_keys &acc, const transaction &tx, std::vector< size_t > &outs, uint64_t &money_transfered)
Definition: cryptonote_format_utils.cpp:492
blobdata block_to_blob(const block &b)
Definition: cryptonote_format_utils.cpp:770
bool parse_and_validate_block_from_blob(const blobdata &b_blob, block &b)
Definition: cryptonote_format_utils.cpp:758
uint64_t get_block_height(const block &b)
Definition: cryptonote_format_utils.cpp:394
bool generate_key_image_helper(const account_keys &ack, const crypto::public_key &tx_public_key, size_t real_output_index, keypair &in_ephemeral, crypto::key_image &ki)
Definition: cryptonote_format_utils.cpp:133
std::string short_hash_str(const crypto::hash &h)
Definition: cryptonote_format_utils.cpp:467
void get_blob_hash(const blobdata &blob, crypto::hash &res)
Definition: cryptonote_format_utils.cpp:517