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 if (typeid(cryptonote::tx_extra_bridge_source_address) == fields[n].type()) std::cout << "extra bridge source address: " << epee::string_tools::buff_to_hex_nodelimer(boost::get<cryptonote::tx_extra_bridge_source_address>(fields[n]).data);
69  else if (typeid(cryptonote::tx_extra_bridge_smartchain_address) == fields[n].type()) std::cout << "extra bridge smartchain address : " << epee::string_tools::buff_to_hex_nodelimer(boost::get<cryptonote::tx_extra_bridge_smartchain_address>(fields[n]).data);
70  else std::cout << "unknown";
71  std::cout << std::endl;
72  }
73 }
74 
75 int main(int argc, char* argv[])
76 {
77  uint32_t log_level = 0;
78  std::string input;
79 
81 
82  boost::filesystem::path output_file_path;
83 
84  po::options_description desc_cmd_only("Command line options");
85  po::options_description desc_cmd_sett("Command line options and settings options");
86  const command_line::arg_descriptor<uint32_t> arg_log_level = {"log-level", "", log_level};
87  const command_line::arg_descriptor<std::string> arg_input = {"input", "Specify input has a hexadecimal string", ""};
88 
89  command_line::add_arg(desc_cmd_sett, arg_log_level);
90  command_line::add_arg(desc_cmd_sett, arg_input);
91 
93 
94  po::options_description desc_options("Allowed options");
95  desc_options.add(desc_cmd_only).add(desc_cmd_sett);
96 
97  po::variables_map vm;
98  bool r = command_line::handle_error_helper(desc_options, [&]()
99  {
100  po::store(po::parse_command_line(argc, argv, desc_options), vm);
101  po::notify(vm);
102  return true;
103  });
104  if (! r)
105  return 1;
106 
108  {
109  std::cout << "Electroneum '" << ELECTRONEUM_RELEASE_NAME << "' (v" << ELECTRONEUM_VERSION_FULL << ")" << ENDL << ENDL;
110  std::cout << desc_options << std::endl;
111  return 1;
112  }
113 
114  log_level = command_line::get_arg(vm, arg_log_level);
115  input = command_line::get_arg(vm, arg_input);
116  if (input.empty())
117  {
118  std::cerr << "--input is mandatory" << std::endl;
119  return 1;
120  }
121 
122  mlog_configure("", true);
123 
126  {
127  std::cerr << "Invalid hex input" << std::endl;
128  return 1;
129  }
130 
131  bool full;
134  std::vector<cryptonote::tx_extra_field> fields;
136  {
137  std::cout << "Parsed block:" << std::endl;
138  std::cout << cryptonote::obj_to_json_str(block) << std::endl;
139  }
141  {
142  if (tx.pruned)
143  std::cout << "Parsed pruned transaction:" << std::endl;
144  else
145  std::cout << "Parsed transaction:" << std::endl;
146  std::cout << cryptonote::obj_to_json_str(tx) << std::endl;
147 
148  bool parsed = cryptonote::parse_tx_extra(tx.extra, fields);
149  if (!parsed)
150  std::cout << "Failed to parse tx_extra" << std::endl;
151 
152  if (!fields.empty())
153  {
154  print_extra_fields(fields);
155  }
156  else
157  {
158  std::cout << "No fields were found in tx_extra" << std::endl;
159  }
160  }
161  else if (((full = cryptonote::parse_tx_extra(std::vector<uint8_t>(blob.begin(), blob.end()), fields)) || true) && !fields.empty())
162  {
163  std::cout << "Parsed" << (full ? "" : " partial") << " tx_extra:" << std::endl;
164  print_extra_fields(fields);
165  }
166  else
167  {
168  std::cerr << "Not a recognized CN type" << std::endl;
169  return 1;
170  }
171 
172 
173 
174  return 0;
175 }
const char *const ELECTRONEUM_RELEASE_NAME
bool parse_tx_extra(const std::vector< uint8_t > &tx_extra, std::vector< tx_extra_field > &tx_extra_fields)
::std::string string
Definition: gtest-port.h:1097
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
const char * key
Definition: hmac_keccak.cpp:39
const arg_descriptor< bool > arg_help
std::vector< uint8_t > extra
int main(int argc, char *argv[])
bool on_startup()
Definition: util.cpp:778
Holds cryptonote related classes and helpers.
Definition: ban.cpp:40
const char *const ELECTRONEUM_VERSION_FULL
std::string pod_to_hex(const t_pod_type &s)
Definition: string_tools.h:317
bool handle_error_helper(const boost::program_options::options_description &desc, F parser)
Definition: command_line.h:237
unsigned int uint32_t
Definition: stdint.h:126
const command_line::arg_descriptor< std::string > arg_log_level
std::string obj_to_json_str(T &obj)
bool parse_and_validate_tx_base_from_blob(const blobdata &tx_blob, transaction &tx)
#define TX_EXTRA_NONCE_ENCRYPTED_PAYMENT_ID
Definition: tx_extra.h:48
bool parse_and_validate_tx_from_blob(const blobdata &tx_blob, transaction &tx)
POD_CLASS public_key
Definition: crypto.h:76
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
std::string blobdata
Definition: blobdatatype.h:39
#define ENDL
Definition: misc_log_ex.h:149
std::string buff_to_hex_nodelimer(const std::string &src)
Definition: string_tools.h:87
T get_arg(const boost::program_options::variables_map &vm, const arg_descriptor< T, false, true > &arg)
Definition: command_line.h:271
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
#define TX_EXTRA_NONCE_PAYMENT_ID
Definition: tx_extra.h:47
bool parse_hexstr_to_binbuff(const epee::span< const char > s, epee::span< char > &res)
Definition: string_tools.h:92
bool parse_and_validate_block_from_blob(const blobdata &b_blob, block &b, crypto::hash *block_hash)