Electroneum
cryptonote_format_utils.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2021, The Electroneum Project
2 // Copyrights(c) 2014-2019, 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
33 #include "blobdatatype.h"
34 #include "cryptonote_basic_impl.h"
35 #include "tx_extra.h"
36 #include "account.h"
37 #include "subaddress_index.h"
38 #include "include_base_utils.h"
39 #include "crypto/crypto.h"
40 #include "crypto/hash.h"
41 #include <unordered_map>
42 
43 namespace epee
44 {
45  class wipeable_string;
46 }
47 
48 namespace cryptonote
49 {
50  //---------------------------------------------------------------
51  void get_transaction_prefix_hash(const transaction_prefix& tx, crypto::hash& h);
52  crypto::hash get_transaction_prefix_hash(const transaction_prefix& tx);
53  bool parse_and_validate_tx_prefix_from_blob(const blobdata& tx_blob, transaction_prefix& tx);
54  bool parse_and_validate_tx_from_blob(const blobdata& tx_blob, transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash);
55  bool parse_and_validate_tx_from_blob(const blobdata& tx_blob, transaction& tx, crypto::hash& tx_hash);
56  bool parse_and_validate_tx_from_blob(const blobdata& tx_blob, transaction& tx);
57  bool parse_and_validate_tx_base_from_blob(const blobdata& tx_blob, transaction& tx);
58  bool is_v1_tx(const blobdata_ref& tx_blob);
59  bool is_v1_tx(const blobdata& tx_blob);
60 
61  template<typename T>
62  bool find_tx_extra_field_by_type(const std::vector<tx_extra_field>& tx_extra_fields, T& field, size_t index = 0)
63  {
64  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--; }); // ie index only decremented IF we fiind the right type
65  if(tx_extra_fields.end() == it)
66  return false;
67 
68  field = boost::get<T>(*it);
69  return true;
70  }
71 
72  bool parse_tx_extra(const std::vector<uint8_t>& tx_extra, std::vector<tx_extra_field>& tx_extra_fields);
73  bool sort_tx_extra(const std::vector<uint8_t>& tx_extra, std::vector<uint8_t> &sorted_tx_extra, bool allow_partial = false);
74  crypto::public_key get_tx_pub_key_from_extra(const std::vector<uint8_t>& tx_extra, size_t pk_index = 0);
75  crypto::public_key get_tx_pub_key_from_extra(const transaction_prefix& tx, size_t pk_index = 0);
76  crypto::public_key get_tx_pub_key_from_extra(const transaction& tx, size_t pk_index = 0);
77  bool add_tx_pub_key_to_extra(transaction& tx, const crypto::public_key& tx_pub_key);
78  bool add_tx_pub_key_to_extra(transaction_prefix& tx, const crypto::public_key& tx_pub_key);
79  bool add_tx_pub_key_to_extra(std::vector<uint8_t>& tx_extra, const crypto::public_key& tx_pub_key);
80  std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const std::vector<uint8_t>& tx_extra);
81  std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const transaction_prefix& tx);
82  bool add_additional_tx_pub_keys_to_extra(std::vector<uint8_t>& tx_extra, const std::vector<crypto::public_key>& additional_pub_keys);
83  bool add_extra_nonce_to_tx_extra(std::vector<uint8_t>& tx_extra, const blobdata& extra_nonce);
84  bool remove_field_from_tx_extra(std::vector<uint8_t>& tx_extra, const std::type_info &type);
85  void set_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash& payment_id);
86  void set_encrypted_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash8& payment_id);
87  bool get_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash& payment_id);
88  bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash8& payment_id);
89  bool is_out_to_acc(const account_keys& acc, const txout_to_key& out_key, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_public_keys, size_t output_index);
91  {
94  };
95  boost::optional<subaddress_receive_info> is_out_to_acc_precomp_public(const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, const cryptonote::account_public_address output_address);
96  boost::optional<subaddress_receive_info> is_out_to_acc_precomp(const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, const crypto::public_key& out_key, const crypto::key_derivation& derivation, const std::vector<crypto::key_derivation>& additional_derivations, size_t output_index, hw::device &hwdev);
97  bool lookup_acc_outs(const account_keys& acc, const transaction& tx, const crypto::public_key& tx_pub_key, const std::vector<crypto::public_key>& additional_tx_public_keys, std::vector<size_t>& outs, uint64_t& etn_transfered);
98  bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector<size_t>& outs, uint64_t& etn_transfered);
99  bool get_tx_fee(const transaction& tx, uint64_t & fee);
100  uint64_t get_tx_fee(const transaction& tx);
101  bool generate_key_image_helper(const account_keys& ack, const std::unordered_map<crypto::public_key, subaddress_index>& subaddresses, const crypto::public_key& out_key, const crypto::public_key& tx_public_key, const std::vector<crypto::public_key>& additional_tx_public_keys, size_t real_output_index, keypair& in_ephemeral, crypto::key_image& ki, hw::device &hwdev, const uint32_t account_major_offset = 0);
102  bool generate_key_image_helper_precomp(const account_keys& ack, const crypto::public_key& out_key, const crypto::key_derivation& recv_derivation, size_t real_output_index, const subaddress_index& received_index, keypair& in_ephemeral, crypto::key_image& ki, hw::device &hwdev, const uint32_t account_major_offset = 0);
103  void get_blob_hash(const blobdata& blob, crypto::hash& res);
105  crypto::hash get_blob_hash(const blobdata& blob);
108 
111  bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t& blob_size);
112  bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t* blob_size);
115  bool calculate_transaction_hash(const transaction& t, crypto::hash& res, size_t* blob_size);
116  crypto::hash get_pruned_transaction_hash(const transaction& t, const crypto::hash &pruned_data_hash);
117 
119  bool calculate_block_hash(const block& b, crypto::hash& res, const blobdata *blob = NULL);
120  bool get_block_hash(const block& b, crypto::hash& res);
124  bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash *block_hash);
125  bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b);
126  bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b, crypto::hash &block_hash);
127  bool get_inputs_etn_amount(const transaction& tx, uint64_t& etn);
130  bool check_outs_valid(const transaction& tx);
131  bool parse_amount(uint64_t& amount, const std::string& str_amount);
133  uint64_t get_transaction_weight(const transaction &tx, size_t blob_size);
134 
135  bool check_etn_overflow(const transaction& tx);
136  bool check_outs_overflow(const transaction& tx);
137  bool check_inputs_overflow(const transaction& tx);
138  uint64_t get_block_height(const block& b);
139  std::vector<uint64_t> relative_output_offsets_to_absolute(const std::vector<uint64_t>& off);
140  std::vector<uint64_t> absolute_output_offsets_to_relative(const std::vector<uint64_t>& off);
141  void set_default_decimal_point(unsigned int decimal_point = CRYPTONOTE_DISPLAY_DECIMAL_POINT);
142  unsigned int get_default_decimal_point();
143  std::string get_unit(unsigned int decimal_point = -1);
144  std::string print_etn(uint64_t amount, unsigned int decimal_point = -1);
145  //---------------------------------------------------------------
146  template<class t_object>
147  bool t_serializable_object_from_blob(t_object& to, const blobdata& b_blob)
148  {
149  std::stringstream ss;
150  ss << b_blob;
151  binary_archive<false> ba(ss);
152  bool r = ::serialization::serialize(ba, to);
153  return r;
154  }
155  //---------------------------------------------------------------
156  template<class t_object>
157  bool t_serializable_object_to_blob(const t_object& to, blobdata& b_blob)
158  {
159  std::stringstream ss;
160  binary_archive<true> ba(ss);
161  bool r = ::serialization::serialize(ba, const_cast<t_object&>(to));
162  b_blob = ss.str();
163  return r;
164  }
165  //---------------------------------------------------------------
166  template<class t_object>
168  {
169  blobdata b;
171  return b;
172  }
173  //---------------------------------------------------------------
174  template<class t_object>
175  bool get_object_hash(const t_object& o, crypto::hash& res)
176  {
178  return true;
179  }
180  //---------------------------------------------------------------
181  template<class t_object>
182  size_t get_object_blobsize(const t_object& o)
183  {
185  return b.size();
186  }
187  //---------------------------------------------------------------
188  template<class t_object>
189  bool get_object_hash(const t_object& o, crypto::hash& res, size_t& blob_size)
190  {
192  blob_size = bl.size();
193  get_blob_hash(bl, res);
194  return true;
195  }
196  //---------------------------------------------------------------
197  template <typename T>
199  {
200  std::stringstream ss;
201  json_archive<true> ar(ss, true);
202  bool r = ::serialization::serialize(ar, obj);
203  CHECK_AND_ASSERT_MES(r, "", "obj_to_json_str failed: serialization::serialize returned false");
204  return ss.str();
205  }
206  //---------------------------------------------------------------
207  // 62387455827 -> 455827 + 7000000 + 80000000 + 300000000 + 2000000000 + 60000000000, where 455827 <= dust_threshold
208  template<typename chunk_handler_t, typename dust_handler_t>
209  void decompose_amount_into_digits(uint64_t amount, uint64_t dust_threshold, const chunk_handler_t& chunk_handler, const dust_handler_t& dust_handler)
210  {
211  if (0 == amount)
212  {
213  return;
214  }
215 
216  bool is_dust_handled = false;
217  uint64_t dust = 0;
218  uint64_t order = 1;
219  while (0 != amount)
220  {
221  uint64_t chunk = (amount % 10) * order;
222  amount /= 10;
223  order *= 10;
224 
225  if (dust + chunk <= dust_threshold)
226  {
227  dust += chunk;
228  }
229  else
230  {
231  if (!is_dust_handled && 0 != dust)
232  {
233  dust_handler(dust);
234  is_dust_handled = true;
235  }
236  if (0 != chunk)
237  {
238  chunk_handler(chunk);
239  }
240  }
241  }
242 
243  if (!is_dust_handled && 0 != dust)
244  {
245  dust_handler(dust);
246  }
247  }
248  //---------------------------------------------------------------
249  blobdata block_to_blob(const block& b);
250  bool block_to_blob(const block& b, blobdata& b_blob);
251  blobdata tx_to_blob(const transaction& b);
252  bool tx_to_blob(const transaction& b, blobdata& b_blob);
253  void get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes, crypto::hash& h);
254  crypto::hash get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes);
255  crypto::hash get_tx_tree_hash(const block& b);
257  void get_hash_stats(uint64_t &tx_hashes_calculated, uint64_t &tx_hashes_cached, uint64_t &block_hashes_calculated, uint64_t & block_hashes_cached);
258 
261 #define CHECKED_GET_SPECIFIC_VARIANT(variant_var, specific_type, variable_name, fail_return_val) \
262  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()); \
263  specific_type& variable_name = boost::get<specific_type>(variant_var);
264 }
uint64_t height
Definition: blockchain.cpp:91
Non-owning sequence of data. Does not deep copy.
Definition: span.h:57
#define CRYPTONOTE_DISPLAY_DECIMAL_POINT
const uint32_t T[512]
const char * res
Definition: hmac_keccak.cpp:41
const char * key
Definition: hmac_keccak.cpp:39
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
Definition: misc_log_ex.h:181
POD_CLASS hash8
Definition: hash.h:53
POD_CLASS key_derivation
Definition: crypto.h:98
POD_CLASS public_key
Definition: crypto.h:76
POD_CLASS key_image
Definition: crypto.h:102
POD_CLASS hash
Definition: hash.h:50
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
bool t_serializable_object_from_blob(t_object &to, const blobdata &b_blob)
std::string obj_to_json_str(T &obj)
void get_hash_stats(uint64_t &tx_hashes_calculated, uint64_t &tx_hashes_cached, uint64_t &block_hashes_calculated, uint64_t &block_hashes_cached)
void get_blob_hash(const epee::span< const char > &blob, crypto::hash &res)
bool is_valid_decomposed_amount(uint64_t amount)
void set_encrypted_payment_id_to_tx_extra_nonce(blobdata &extra_nonce, const crypto::hash8 &payment_id)
bool generate_key_image_helper(const account_keys &ack, const std::unordered_map< crypto::public_key, subaddress_index > &subaddresses, const crypto::public_key &out_key, const crypto::public_key &tx_public_key, const std::vector< crypto::public_key > &additional_tx_public_keys, size_t real_output_index, keypair &in_ephemeral, crypto::key_image &ki, hw::device &hwdev, const uint32_t account_major_offset)
std::string short_hash_str(const crypto::hash &h)
bool get_object_hash(const t_object &o, crypto::hash &res)
uint64_t get_outs_etn_amount(const transaction &tx)
bool calculate_transaction_hash(const transaction &t, crypto::hash &res, size_t *blob_size)
bool sort_tx_extra(const std::vector< uint8_t > &tx_extra, std::vector< uint8_t > &sorted_tx_extra, bool allow_partial)
bool calculate_transaction_prunable_hash(const transaction &t, const cryptonote::blobdata *blob, crypto::hash &res)
bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata &extra_nonce, crypto::hash8 &payment_id)
bool check_inputs_overflow(const transaction &tx)
void decompose_amount_into_digits(uint64_t amount, uint64_t dust_threshold, const chunk_handler_t &chunk_handler, const dust_handler_t &dust_handler)
blobdata get_block_hashing_blob(const block &b)
bool is_out_to_acc(const account_keys &acc, const txout_to_key &out_key, const crypto::public_key &tx_pub_key, const std::vector< crypto::public_key > &additional_tx_pub_keys, size_t output_index)
std::vector< crypto::public_key > get_additional_tx_pub_keys_from_extra(const std::vector< uint8_t > &tx_extra)
bool get_tx_fee(const transaction &tx, uint64_t &fee)
bool get_payment_id_from_tx_extra_nonce(const blobdata &extra_nonce, crypto::hash &payment_id)
void set_payment_id_to_tx_extra_nonce(blobdata &extra_nonce, const crypto::hash &payment_id)
void get_transaction_prefix_hash(const transaction_prefix &tx, crypto::hash &h)
unsigned int get_default_decimal_point()
bool is_v1_tx(const blobdata_ref &tx_blob)
boost::variant< tx_extra_padding, tx_extra_pub_key, tx_extra_nonce, tx_extra_merge_mining_tag, tx_extra_additional_pub_keys, tx_extra_mysterious_minergate > tx_extra_field
Definition: tx_extra.h:187
crypto::hash get_transaction_prunable_hash(const transaction &t, const cryptonote::blobdata *blobdata)
void get_tx_tree_hash(const std::vector< crypto::hash > &tx_hashes, crypto::hash &h)
std::vector< uint64_t > absolute_output_offsets_to_relative(const std::vector< uint64_t > &off)
epee::span< const char > blobdata_ref
Definition: blobdatatype.h:40
std::vector< uint64_t > relative_output_offsets_to_absolute(const std::vector< uint64_t > &off)
bool get_block_hash(const block &b, crypto::hash &res)
bool parse_and_validate_block_from_blob(const blobdata &b_blob, block &b, crypto::hash *block_hash)
crypto::secret_key encrypt_key(crypto::secret_key key, const epee::wipeable_string &passphrase)
uint64_t get_block_height(const block &b)
bool calculate_block_hash(const block &b, crypto::hash &res, const blobdata *blob)
crypto::public_key get_tx_pub_key_from_extra(const std::vector< uint8_t > &tx_extra, size_t pk_index)
boost::optional< subaddress_receive_info > is_out_to_acc_precomp(const std::unordered_map< crypto::public_key, subaddress_index > &subaddresses, const crypto::public_key &out_key, const crypto::key_derivation &derivation, const std::vector< crypto::key_derivation > &additional_derivations, size_t output_index, hw::device &hwdev)
bool add_tx_pub_key_to_extra(transaction &tx, const crypto::public_key &tx_pub_key)
crypto::hash get_transaction_hash(const transaction &t)
bool check_outs_valid(const transaction &tx)
bool find_tx_extra_field_by_type(const std::vector< tx_extra_field > &tx_extra_fields, T &field, size_t index=0)
blobdata block_to_blob(const block &b)
crypto::secret_key decrypt_key(crypto::secret_key key, const epee::wipeable_string &passphrase)
bool lookup_acc_outs(const account_keys &acc, const transaction &tx, std::vector< size_t > &outs, uint64_t &etn_transfered)
bool check_outs_overflow(const transaction &tx)
bool get_block_longhash(const block &b, crypto::hash &res, uint64_t height)
void set_default_decimal_point(unsigned int decimal_point)
bool check_etn_overflow(const transaction &tx)
blobdata tx_to_blob(const transaction &tx)
bool parse_tx_extra(const std::vector< uint8_t > &tx_extra, std::vector< tx_extra_field > &tx_extra_fields)
std::string blobdata
Definition: blobdatatype.h:39
bool add_additional_tx_pub_keys_to_extra(std::vector< uint8_t > &tx_extra, const std::vector< crypto::public_key > &additional_pub_keys)
size_t get_object_blobsize(const t_object &o)
bool parse_amount(uint64_t &amount, const std::string &str_amount_)
bool parse_and_validate_tx_from_blob(const blobdata &tx_blob, transaction &tx)
bool get_inputs_etn_amount(const transaction &tx, uint64_t &etn)
bool parse_and_validate_tx_prefix_from_blob(const blobdata &tx_blob, transaction_prefix &tx)
bool remove_field_from_tx_extra(std::vector< uint8_t > &tx_extra, const std::type_info &type)
bool check_inputs_types_supported(const transaction &tx)
std::string print_etn(uint64_t amount, unsigned int decimal_point)
crypto::hash get_pruned_transaction_hash(const transaction &t, const crypto::hash &pruned_data_hash)
boost::optional< subaddress_receive_info > is_out_to_acc_precomp_public(const std::unordered_map< crypto::public_key, subaddress_index > &subaddresses, const cryptonote::account_public_address output_address)
std::string get_unit(unsigned int decimal_point)
bool generate_key_image_helper_precomp(const account_keys &ack, const crypto::public_key &out_key, const crypto::key_derivation &recv_derivation, size_t real_output_index, const subaddress_index &received_index, keypair &in_ephemeral, crypto::key_image &ki, hw::device &hwdev, const uint32_t account_major_offset)
bool parse_and_validate_tx_base_from_blob(const blobdata &tx_blob, transaction &tx)
bool t_serializable_object_to_blob(const t_object &to, blobdata &b_blob)
uint64_t get_transaction_weight(const transaction &tx, size_t blob_size)
bool add_extra_nonce_to_tx_extra(std::vector< uint8_t > &tx_extra, const blobdata &extra_nonce)
::std::string string
Definition: gtest-port.h:1097
void serialize(Archive &a, unsigned_tx_set &x, const boost::serialization::version_type ver)
unsigned int uint32_t
Definition: stdint.h:126
unsigned __int64 uint64_t
Definition: stdint.h:136