Monero
cryptonote_format_utils.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30 
31 #pragma once
32 #include "blobdatatype.h"
33 #include "cryptonote_basic_impl.h"
34 #include "account.h"
35 #include "subaddress_index.h"
36 #include "include_base_utils.h"
37 #include "crypto/crypto.h"
38 #include "crypto/hash.h"
39 #include <unordered_map>
40 
41 namespace epee
42 {
43  class wipeable_string;
44 }
45 
46 namespace cryptonote
47 {
48  //---------------------------------------------------------------
49  void get_transaction_prefix_hash(const transaction_prefix& tx, crypto::hash& h);
50  crypto::hash get_transaction_prefix_hash(const transaction_prefix& tx);
51  bool parse_and_validate_tx_from_blob(const blobdata& tx_blob, transaction& tx, crypto::hash& tx_hash, crypto::hash& tx_prefix_hash);
52  bool parse_and_validate_tx_from_blob(const blobdata& tx_blob, transaction& tx);
53  bool parse_and_validate_tx_base_from_blob(const blobdata& tx_blob, transaction& tx);
54 
55  template<typename T>
56  bool find_tx_extra_field_by_type(const std::vector<tx_extra_field>& tx_extra_fields, T& field, size_t index = 0)
57  {
58  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--; });
59  if(tx_extra_fields.end() == it)
60  return false;
61 
62  field = boost::get<T>(*it);
63  return true;
64  }
65 
66  bool parse_tx_extra(const std::vector<uint8_t>& tx_extra, std::vector<tx_extra_field>& tx_extra_fields);
67  crypto::public_key get_tx_pub_key_from_extra(const std::vector<uint8_t>& tx_extra, size_t pk_index = 0);
68  crypto::public_key get_tx_pub_key_from_extra(const transaction_prefix& tx, size_t pk_index = 0);
69  crypto::public_key get_tx_pub_key_from_extra(const transaction& tx, size_t pk_index = 0);
70  bool add_tx_pub_key_to_extra(transaction& tx, const crypto::public_key& tx_pub_key);
71  bool add_tx_pub_key_to_extra(transaction_prefix& tx, const crypto::public_key& tx_pub_key);
72  bool add_tx_pub_key_to_extra(std::vector<uint8_t>& tx_extra, const crypto::public_key& tx_pub_key);
73  std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const std::vector<uint8_t>& tx_extra);
74  std::vector<crypto::public_key> get_additional_tx_pub_keys_from_extra(const transaction_prefix& tx);
75  bool add_additional_tx_pub_keys_to_extra(std::vector<uint8_t>& tx_extra, const std::vector<crypto::public_key>& additional_pub_keys);
76  bool add_extra_nonce_to_tx_extra(std::vector<uint8_t>& tx_extra, const blobdata& extra_nonce);
77  bool remove_field_from_tx_extra(std::vector<uint8_t>& tx_extra, const std::type_info &type);
78  void set_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash& payment_id);
79  void set_encrypted_payment_id_to_tx_extra_nonce(blobdata& extra_nonce, const crypto::hash8& payment_id);
80  bool get_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash& payment_id);
81  bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata& extra_nonce, crypto::hash8& payment_id);
82  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);
84  {
87  };
88  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);
89  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& money_transfered);
90  bool lookup_acc_outs(const account_keys& acc, const transaction& tx, std::vector<size_t>& outs, uint64_t& money_transfered);
91  bool get_tx_fee(const transaction& tx, uint64_t & fee);
92  uint64_t get_tx_fee(const transaction& tx);
93  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);
94  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);
95  void get_blob_hash(const blobdata& blob, crypto::hash& res);
96  crypto::hash get_blob_hash(const blobdata& blob);
97  std::string short_hash_str(const crypto::hash& h);
98 
100  bool get_transaction_hash(const transaction& t, crypto::hash& res);
101  bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t& blob_size);
102  bool get_transaction_hash(const transaction& t, crypto::hash& res, size_t* blob_size);
105  bool calculate_transaction_hash(const transaction& t, crypto::hash& res, size_t* blob_size);
106  crypto::hash get_pruned_transaction_hash(const transaction& t, const crypto::hash &pruned_data_hash);
107 
109  bool calculate_block_hash(const block& b, crypto::hash& res);
110  bool get_block_hash(const block& b, crypto::hash& res);
112  bool get_block_longhash(const block& b, crypto::hash& res, uint64_t height);
113  crypto::hash get_block_longhash(const block& b, uint64_t height);
114  bool parse_and_validate_block_from_blob(const blobdata& b_blob, block& b);
115  bool get_inputs_money_amount(const transaction& tx, uint64_t& money);
116  uint64_t get_outs_money_amount(const transaction& tx);
118  bool check_outs_valid(const transaction& tx);
119  bool parse_amount(uint64_t& amount, const std::string& str_amount);
120  uint64_t get_transaction_weight(const transaction &tx);
121  uint64_t get_transaction_weight(const transaction &tx, size_t blob_size);
122 
123  bool check_money_overflow(const transaction& tx);
124  bool check_outs_overflow(const transaction& tx);
125  bool check_inputs_overflow(const transaction& tx);
126  uint64_t get_block_height(const block& b);
127  std::vector<uint64_t> relative_output_offsets_to_absolute(const std::vector<uint64_t>& off);
128  std::vector<uint64_t> absolute_output_offsets_to_relative(const std::vector<uint64_t>& off);
129  void set_default_decimal_point(unsigned int decimal_point = CRYPTONOTE_DISPLAY_DECIMAL_POINT);
130  unsigned int get_default_decimal_point();
131  std::string get_unit(unsigned int decimal_point = -1);
132  std::string print_money(uint64_t amount, unsigned int decimal_point = -1);
133  //---------------------------------------------------------------
134  template<class t_object>
135  bool t_serializable_object_to_blob(const t_object& to, blobdata& b_blob)
136  {
137  std::stringstream ss;
138  binary_archive<true> ba(ss);
139  bool r = ::serialization::serialize(ba, const_cast<t_object&>(to));
140  b_blob = ss.str();
141  return r;
142  }
143  //---------------------------------------------------------------
144  template<class t_object>
146  {
147  blobdata b;
149  return b;
150  }
151  //---------------------------------------------------------------
152  template<class t_object>
153  bool get_object_hash(const t_object& o, crypto::hash& res)
154  {
156  return true;
157  }
158  //---------------------------------------------------------------
159  template<class t_object>
160  size_t get_object_blobsize(const t_object& o)
161  {
163  return b.size();
164  }
165  //---------------------------------------------------------------
166  template<class t_object>
167  bool get_object_hash(const t_object& o, crypto::hash& res, size_t& blob_size)
168  {
170  blob_size = bl.size();
171  get_blob_hash(bl, res);
172  return true;
173  }
174  //---------------------------------------------------------------
175  template <typename T>
176  std::string obj_to_json_str(T& obj)
177  {
178  std::stringstream ss;
179  json_archive<true> ar(ss, true);
180  bool r = ::serialization::serialize(ar, obj);
181  CHECK_AND_ASSERT_MES(r, "", "obj_to_json_str failed: serialization::serialize returned false");
182  return ss.str();
183  }
184  //---------------------------------------------------------------
185  // 62387455827 -> 455827 + 7000000 + 80000000 + 300000000 + 2000000000 + 60000000000, where 455827 <= dust_threshold
186  template<typename chunk_handler_t, typename dust_handler_t>
187  void decompose_amount_into_digits(uint64_t amount, uint64_t dust_threshold, const chunk_handler_t& chunk_handler, const dust_handler_t& dust_handler)
188  {
189  if (0 == amount)
190  {
191  return;
192  }
193 
194  bool is_dust_handled = false;
195  uint64_t dust = 0;
196  uint64_t order = 1;
197  while (0 != amount)
198  {
199  uint64_t chunk = (amount % 10) * order;
200  amount /= 10;
201  order *= 10;
202 
203  if (dust + chunk <= dust_threshold)
204  {
205  dust += chunk;
206  }
207  else
208  {
209  if (!is_dust_handled && 0 != dust)
210  {
211  dust_handler(dust);
212  is_dust_handled = true;
213  }
214  if (0 != chunk)
215  {
216  chunk_handler(chunk);
217  }
218  }
219  }
220 
221  if (!is_dust_handled && 0 != dust)
222  {
223  dust_handler(dust);
224  }
225  }
226  //---------------------------------------------------------------
227  blobdata block_to_blob(const block& b);
228  bool block_to_blob(const block& b, blobdata& b_blob);
229  blobdata tx_to_blob(const transaction& b);
230  bool tx_to_blob(const transaction& b, blobdata& b_blob);
231  void get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes, crypto::hash& h);
232  crypto::hash get_tx_tree_hash(const std::vector<crypto::hash>& tx_hashes);
233  crypto::hash get_tx_tree_hash(const block& b);
234  bool is_valid_decomposed_amount(uint64_t amount);
235  void get_hash_stats(uint64_t &tx_hashes_calculated, uint64_t &tx_hashes_cached, uint64_t &block_hashes_calculated, uint64_t & block_hashes_cached);
236 
237  crypto::secret_key encrypt_key(crypto::secret_key key, const epee::wipeable_string &passphrase);
238  crypto::secret_key decrypt_key(crypto::secret_key key, const epee::wipeable_string &passphrase);
239 #define CHECKED_GET_SPECIFIC_VARIANT(variant_var, specific_type, variable_name, fail_return_val) \
240  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()); \
241  specific_type& variable_name = boost::get<specific_type>(variant_var);
242 }
bool parse_tx_extra(const std::vector< uint8_t > &tx_extra, std::vector< tx_extra_field > &tx_extra_fields)
Definition: cryptonote_format_utils.cpp:410
bool check_outs_overflow(const transaction &tx)
Definition: cryptonote_format_utils.cpp:662
uint64_t get_outs_money_amount(const transaction &tx)
Definition: cryptonote_format_utils.cpp:674
const uint32_t T[512]
Definition: groestl_tables.h:33
bool get_tx_fee(const transaction &tx, uint64_t &fee)
Definition: cryptonote_format_utils.cpp:380
subaddress_index index
Definition: cryptonote_format_utils.h:85
bool check_outs_valid(const transaction &tx)
Definition: cryptonote_format_utils.cpp:625
Definition: cryptonote_basic.h:427
#define CRYPTONOTE_DISPLAY_DECIMAL_POINT
Definition: cryptonote_config.h:62
POD_CLASS key_derivation
Definition: crypto.h:85
Definition: cryptonote_basic.h:366
void serialize(Archive &a, std::unordered_map< h_key, hval > &x, const boost::serialization::version_type ver)
Definition: unordered_containers_boost_serialization.h:126
crypto::key_derivation derivation
Definition: cryptonote_format_utils.h:86
size_t get_object_blobsize(const t_object &o)
Definition: cryptonote_format_utils.h:160
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:187
uint64_t height
Definition: blockchain.cpp:88
epee::mlocked< tools::scrubbed< ec_scalar > > secret_key
Definition: crypto.h:69
crypto::hash get_transaction_prunable_hash(const transaction &t)
Definition: cryptonote_format_utils.cpp:857
bool calculate_block_hash(const block &b, crypto::hash &res)
Definition: cryptonote_format_utils.cpp:992
Definition: binary_archive.h:180
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:56
std::vector< uint64_t > relative_output_offsets_to_absolute(const std::vector< uint64_t > &off)
Definition: cryptonote_format_utils.cpp:1061
bool add_additional_tx_pub_keys_to_extra(std::vector< uint8_t > &tx_extra, const std::vector< crypto::public_key > &additional_pub_keys)
Definition: cryptonote_format_utils.cpp:495
std::string get_unit(unsigned int decimal_point)
Definition: cryptonote_format_utils.cpp:787
uint64_t get_transaction_weight(const transaction &tx, size_t blob_size)
Definition: cryptonote_format_utils.cpp:349
bool get_object_hash(const t_object &o, crypto::hash &res)
Definition: cryptonote_format_utils.h:153
crypto::secret_key encrypt_key(crypto::secret_key key, const epee::wipeable_string &passphrase)
Definition: cryptonote_format_utils.cpp:1159
bool calculate_transaction_hash(const transaction &t, crypto::hash &res, size_t *blob_size)
Definition: cryptonote_format_utils.cpp:896
Holds cryptonote related classes and helpers.
Definition: db_bdb.cpp:224
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)
Definition: cryptonote_format_utils.cpp:714
bool parse_amount(uint64_t &amount, const std::string &str_amount_)
Definition: cryptonote_format_utils.cpp:314
bool get_block_longhash(const block &b, crypto::hash &res, uint64_t height)
Definition: cryptonote_format_utils.cpp:1046
blobdata tx_to_blob(const transaction &tx)
Definition: cryptonote_format_utils.cpp:1110
bool is_valid_decomposed_amount(uint64_t amount)
Definition: cryptonote_format_utils.cpp:1144
bool add_tx_pub_key_to_extra(transaction &tx, const crypto::public_key &tx_pub_key)
Definition: cryptonote_format_utils.cpp:460
unsigned int get_default_decimal_point()
Definition: cryptonote_format_utils.cpp:782
Definition: json_archive.h:114
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)
Definition: cryptonote_format_utils.cpp:249
bool get_inputs_money_amount(const transaction &tx, uint64_t &money)
Definition: cryptonote_format_utils.cpp:595
bool check_inputs_types_supported(const transaction &tx)
Definition: cryptonote_format_utils.cpp:613
bool get_block_hash(const block &b, crypto::hash &res)
Definition: cryptonote_format_utils.cpp:1019
bool check_money_overflow(const transaction &tx)
Definition: cryptonote_format_utils.cpp:644
std::vector< uint64_t > absolute_output_offsets_to_relative(const std::vector< uint64_t > &off)
Definition: cryptonote_format_utils.cpp:1069
blobdata get_block_hashing_blob(const block &b)
Definition: cryptonote_format_utils.cpp:983
std::string obj_to_json_str(T &obj)
Definition: cryptonote_format_utils.h:176
int b
Definition: base.py:1
Definition: subaddress_index.h:38
bool t_serializable_object_to_blob(const t_object &to, blobdata &b_blob)
Definition: cryptonote_format_utils.h:135
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)
Definition: cryptonote_format_utils.cpp:218
bool get_payment_id_from_tx_extra_nonce(const blobdata &extra_nonce, crypto::hash &payment_id)
Definition: cryptonote_format_utils.cpp:575
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:438
Definition: device.hpp:82
Definition: account.h:40
bool parse_and_validate_tx_base_from_blob(const blobdata &tx_blob, transaction &tx)
Definition: cryptonote_format_utils.cpp:191
bool parse_and_validate_tx_from_blob(const blobdata &tx_blob, transaction &tx)
Definition: cryptonote_format_utils.cpp:179
crypto::hash get_pruned_transaction_hash(const transaction &t, const crypto::hash &pruned_data_hash)
Definition: cryptonote_format_utils.cpp:864
POD_CLASS public_key
Definition: crypto.h:63
void set_default_decimal_point(unsigned int decimal_point)
Definition: cryptonote_format_utils.cpp:766
bool get_encrypted_payment_id_from_tx_extra_nonce(const blobdata &extra_nonce, crypto::hash8 &payment_id)
Definition: cryptonote_format_utils.cpp:585
bool check_inputs_overflow(const transaction &tx)
Definition: cryptonote_format_utils.cpp:649
std::string blobdata
Definition: blobdatatype.h:35
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)
Definition: cryptonote_format_utils.cpp:691
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:186
Definition: cryptonote_format_utils.h:41
void get_transaction_prefix_hash(const transaction_prefix &tx, crypto::hash &h)
Definition: cryptonote_format_utils.cpp:112
POD_CLASS hash8
Definition: hash.h:52
bool calculate_transaction_prunable_hash(const transaction &t, crypto::hash &res)
Definition: cryptonote_format_utils.cpp:841
POD_CLASS key_image
Definition: crypto.h:89
crypto::hash get_transaction_hash(const transaction &t)
Definition: cryptonote_format_utils.cpp:829
bool remove_field_from_tx_extra(std::vector< uint8_t > &tx_extra, const std::type_info &type)
Definition: cryptonote_format_utils.cpp:528
bool add_extra_nonce_to_tx_extra(std::vector< uint8_t > &tx_extra, const blobdata &extra_nonce)
Definition: cryptonote_format_utils.cpp:512
crypto::secret_key decrypt_key(crypto::secret_key key, const epee::wipeable_string &passphrase)
Definition: cryptonote_format_utils.cpp:1167
POD_CLASS hash
Definition: hash.h:49
void get_tx_tree_hash(const std::vector< crypto::hash > &tx_hashes, crypto::hash &h)
Definition: cryptonote_format_utils.cpp:1120
void set_encrypted_payment_id_to_tx_extra_nonce(blobdata &extra_nonce, const crypto::hash8 &payment_id)
Definition: cryptonote_format_utils.cpp:567
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:1151
void set_payment_id_to_tx_extra_nonce(blobdata &extra_nonce, const crypto::hash &payment_id)
Definition: cryptonote_format_utils.cpp:559
std::string print_money(uint64_t amount, unsigned int decimal_point)
Definition: cryptonote_format_utils.cpp:808
std::vector< crypto::public_key > get_additional_tx_pub_keys_from_extra(const std::vector< uint8_t > &tx_extra)
Definition: cryptonote_format_utils.cpp:478
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:734
blobdata block_to_blob(const block &b)
Definition: cryptonote_format_utils.cpp:1100
bool parse_and_validate_block_from_blob(const blobdata &b_blob, block &b)
Definition: cryptonote_format_utils.cpp:1088
Definition: cryptonote_format_utils.h:83
Definition: cryptonote_basic.h:182
uint64_t get_block_height(const block &b)
Definition: cryptonote_format_utils.cpp:606
std::string short_hash_str(const crypto::hash &h)
Definition: cryptonote_format_utils.cpp:682
void get_blob_hash(const blobdata &blob, crypto::hash &res)
Definition: cryptonote_format_utils.cpp:761