Electroneum
cn_deserialize.cpp
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 #include <boost/filesystem.hpp>
31 #include <boost/algorithm/string/join.hpp>
32 #include <boost/range/adaptor/transformed.hpp>
36 #include "common/command_line.h"
37 #include "version.h"
38 
39 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
40 #define ELECTRONEUM_DEFAULT_LOG_CATEGORY "debugtools.deserialize"
41 
42 namespace po = boost::program_options;
43 using namespace epee;
44 
45 using namespace cryptonote;
46 
47 static std::string extra_nonce_to_string(const cryptonote::tx_extra_nonce &extra_nonce)
48 {
49  if (extra_nonce.nonce.size() == 9 && extra_nonce.nonce[0] == TX_EXTRA_NONCE_ENCRYPTED_PAYMENT_ID)
50  return "encrypted payment ID: " + epee::string_tools::buff_to_hex_nodelimer(extra_nonce.nonce.substr(1));
51  if (extra_nonce.nonce.size() == 33 && extra_nonce.nonce[0] == TX_EXTRA_NONCE_PAYMENT_ID)
52  return "plaintext payment ID: " + epee::string_tools::buff_to_hex_nodelimer(extra_nonce.nonce.substr(1));
54 }
55 
56 static void print_extra_fields(const std::vector<cryptonote::tx_extra_field> &fields)
57 {
58  std::cout << "tx_extra has " << fields.size() << " field(s)" << std::endl;
59  for (size_t n = 0; n < fields.size(); ++n)
60  {
61  std::cout << "field " << n << ": ";
62  if (typeid(cryptonote::tx_extra_padding) == fields[n].type()) std::cout << "extra padding: " << boost::get<cryptonote::tx_extra_padding>(fields[n]).size << " bytes";
63  else if (typeid(cryptonote::tx_extra_pub_key) == fields[n].type()) std::cout << "extra pub key: " << boost::get<cryptonote::tx_extra_pub_key>(fields[n]).pub_key;
64  else if (typeid(cryptonote::tx_extra_nonce) == fields[n].type()) std::cout << "extra nonce: " << extra_nonce_to_string(boost::get<cryptonote::tx_extra_nonce>(fields[n]));
65  else if (typeid(cryptonote::tx_extra_merge_mining_tag) == fields[n].type()) std::cout << "extra merge mining tag: depth " << boost::get<cryptonote::tx_extra_merge_mining_tag>(fields[n]).depth << ", merkle root " << boost::get<cryptonote::tx_extra_merge_mining_tag>(fields[n]).merkle_root;
66  else if (typeid(cryptonote::tx_extra_additional_pub_keys) == fields[n].type()) std::cout << "additional tx pubkeys: " << boost::join(boost::get<cryptonote::tx_extra_additional_pub_keys>(fields[n]).data | boost::adaptors::transformed([](const crypto::public_key &key){ return epee::string_tools::pod_to_hex(key); }), ", " );
67  else if (typeid(cryptonote::tx_extra_mysterious_minergate) == fields[n].type()) std::cout << "extra minergate custom: " << epee::string_tools::buff_to_hex_nodelimer(boost::get<cryptonote::tx_extra_mysterious_minergate>(fields[n]).data);
68  else std::cout << "unknown";
69  std::cout << std::endl;
70  }
71 }
72 
73 int main(int argc, char* argv[])
74 {
75  uint32_t log_level = 0;
76  std::string input;
77 
79 
80  boost::filesystem::path output_file_path;
81 
82  po::options_description desc_cmd_only("Command line options");
83  po::options_description desc_cmd_sett("Command line options and settings options");
84  const command_line::arg_descriptor<uint32_t> arg_log_level = {"log-level", "", log_level};
85  const command_line::arg_descriptor<std::string> arg_input = {"input", "Specify input has a hexadecimal string", ""};
86 
87  command_line::add_arg(desc_cmd_sett, arg_log_level);
88  command_line::add_arg(desc_cmd_sett, arg_input);
89 
91 
92  po::options_description desc_options("Allowed options");
93  desc_options.add(desc_cmd_only).add(desc_cmd_sett);
94 
95  po::variables_map vm;
96  bool r = command_line::handle_error_helper(desc_options, [&]()
97  {
98  po::store(po::parse_command_line(argc, argv, desc_options), vm);
99  po::notify(vm);
100  return true;
101  });
102  if (! r)
103  return 1;
104 
106  {
107  std::cout << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")" << ENDL << ENDL;
108  std::cout << desc_options << std::endl;
109  return 1;
110  }
111 
112  log_level = command_line::get_arg(vm, arg_log_level);
113  input = command_line::get_arg(vm, arg_input);
114  if (input.empty())
115  {
116  std::cerr << "--input is mandatory" << std::endl;
117  return 1;
118  }
119 
120  mlog_configure("", true);
121 
124  {
125  std::cerr << "Invalid hex input" << std::endl;
126  return 1;
127  }
128 
129  bool full;
132  std::vector<cryptonote::tx_extra_field> fields;
134  {
135  std::cout << "Parsed block:" << std::endl;
136  std::cout << cryptonote::obj_to_json_str(block) << std::endl;
137  }
139  {
140  if (tx.pruned)
141  std::cout << "Parsed pruned transaction:" << std::endl;
142  else
143  std::cout << "Parsed transaction:" << std::endl;
144  std::cout << cryptonote::obj_to_json_str(tx) << std::endl;
145 
146  bool parsed = cryptonote::parse_tx_extra(tx.extra, fields);
147  if (!parsed)
148  std::cout << "Failed to parse tx_extra" << std::endl;
149 
150  if (!fields.empty())
151  {
152  print_extra_fields(fields);
153  }
154  else
155  {
156  std::cout << "No fields were found in tx_extra" << std::endl;
157  }
158  }
159  else if (((full = cryptonote::parse_tx_extra(std::vector<uint8_t>(blob.begin(), blob.end()), fields)) || true) && !fields.empty())
160  {
161  std::cout << "Parsed" << (full ? "" : " partial") << " tx_extra:" << std::endl;
162  print_extra_fields(fields);
163  }
164  else
165  {
166  std::cerr << "Not a recognized CN type" << std::endl;
167  return 1;
168  }
169 
170 
171 
172  return 0;
173 }
std::vector< uint8_t > extra
int main(int argc, char *argv[])
const char * key
Definition: hmac_keccak.cpp:39
void mlog_configure(const std::string &filename_base, bool console, const std::size_t max_log_file_size=MAX_LOG_FILE_SIZE, const std::size_t max_log_files=MAX_LOG_FILES)
Definition: mlog.cpp:148
#define ENDL
Definition: misc_log_ex.h:149
boost::program_options::basic_parsed_options< charT > parse_command_line(int argc, const charT *const argv[], const boost::program_options::options_description &desc, bool allow_unregistered=false)
Definition: command_line.h:224
void add_arg(boost::program_options::options_description &description, const arg_descriptor< T, required, dependent, NUM_DEPS > &arg, bool unique=true)
Definition: command_line.h:188
const arg_descriptor< bool > arg_help
bool handle_error_helper(const boost::program_options::options_description &desc, F parser)
Definition: command_line.h:237
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
POD_CLASS public_key
Definition: crypto.h:76
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
std::string obj_to_json_str(T &obj)
bool parse_and_validate_block_from_blob(const blobdata &b_blob, block &b, crypto::hash *block_hash)
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 parse_and_validate_tx_from_blob(const blobdata &tx_blob, transaction &tx)
bool parse_and_validate_tx_base_from_blob(const blobdata &tx_blob, transaction &tx)
const command_line::arg_descriptor< std::string > arg_log_level
bool parse_hexstr_to_binbuff(const epee::span< const char > s, epee::span< char > &res)
Definition: string_tools.h:92
std::string pod_to_hex(const t_pod_type &s)
Definition: string_tools.h:317
std::string buff_to_hex_nodelimer(const std::string &src)
Definition: string_tools.h:87
::std::string string
Definition: gtest-port.h:1097
bool on_startup()
Definition: util.cpp:778
unsigned int uint32_t
Definition: stdint.h:126
#define TX_EXTRA_NONCE_PAYMENT_ID
Definition: tx_extra.h:45
#define TX_EXTRA_NONCE_ENCRYPTED_PAYMENT_ID
Definition: tx_extra.h:46
const char *const ELECTRONEUM_RELEASE_NAME
const char *const ELECTRONEUM_VERSION_FULL