| 1 | // Copyright (c) 2014-2022, 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 | #include <boost/filesystem.hpp> |
| 30 | #include <boost/algorithm/string/join.hpp> |
| 31 | #include <boost/range/adaptor/transformed.hpp> |
| 32 | #include "cryptonote_basic/cryptonote_basic.h" |
| 33 | #include "cryptonote_basic/tx_extra.h" |
| 34 | #include "cryptonote_core/blockchain.h" |
| 35 | #include "common/command_line.h" |
| 36 | #include "version.h" |
| 37 | |
| 38 | #undef MONERO_DEFAULT_LOG_CATEGORY |
| 39 | #define MONERO_DEFAULT_LOG_CATEGORY "debugtools.deserialize" |
| 40 | |
| 41 | namespace po = boost::program_options; |
| 42 | using namespace epee; |
| 43 | |
| 44 | using namespace cryptonote; |
| 45 | |
| 46 | static std::string extra_nonce_to_string(const cryptonote::tx_extra_nonce &extra_nonce) |
| 47 | { |
| 48 | if (extra_nonce.nonce.size() == 9 && extra_nonce.nonce[0] == TX_EXTRA_NONCE_ENCRYPTED_PAYMENT_ID) |
| 49 | return "encrypted payment ID: " + epee::string_tools::buff_to_hex_nodelimer(extra_nonce.nonce.substr(1)); |
| 50 | if (extra_nonce.nonce.size() == 33 && extra_nonce.nonce[0] == TX_EXTRA_NONCE_PAYMENT_ID) |
| 51 | return "plaintext payment ID: " + epee::string_tools::buff_to_hex_nodelimer(extra_nonce.nonce.substr(1)); |
| 52 | return epee::string_tools::buff_to_hex_nodelimer(extra_nonce.nonce); |
| 53 | } |
| 54 | |
| 55 | static void print_extra_fields(const std::vector<cryptonote::tx_extra_field> &fields) |
| 56 | { |
| 57 | std::cout << "tx_extra has " << fields.size() << " field(s)" << std::endl; |
| 58 | for (size_t n = 0; n < fields.size(); ++n) |
| 59 | { |
| 60 | std::cout << "field " << n << ": "; |
| 61 | if (typeid(cryptonote::tx_extra_padding) == fields[n].type()) std::cout << "extra padding: " << boost::get<cryptonote::tx_extra_padding>(fields[n]).size << " bytes"; |
| 62 | 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; |
| 63 | 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])); |
| 64 | 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; |
| 65 | 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); }), ", " ); |
| 66 | 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); |
| 67 | else std::cout << "unknown"; |
| 68 | std::cout << std::endl; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | int main(int argc, char* argv[]) |
| 73 | { |
| 74 | uint32_t log_level = 0; |
| 75 | std::string input; |
| 76 | |
| 77 | tools::on_startup(); |
| 78 | |
| 79 | boost::filesystem::path output_file_path; |
| 80 | |
| 81 | po::options_description desc_cmd_only("Command line options"); |
| 82 | po::options_description desc_cmd_sett("Command line options and settings options"); |
| 83 | const command_line::arg_descriptor<uint32_t> arg_log_level = {"log-level", "", log_level}; |
| 84 | const command_line::arg_descriptor<std::string> arg_input = {"input", "Specify input has a hexadecimal string", ""}; |
| 85 | |
| 86 | command_line::add_arg(desc_cmd_sett, arg_log_level); |
| 87 | command_line::add_arg(desc_cmd_sett, arg_input); |
| 88 | |
| 89 | command_line::add_arg(desc_cmd_only, command_line::arg_help); |
| 90 | |
| 91 | po::options_description desc_options("Allowed options"); |
| 92 | desc_options.add(desc_cmd_only).add(desc_cmd_sett); |
| 93 | |
| 94 | po::variables_map vm; |
| 95 | bool r = command_line::handle_error_helper(desc_options, [&]() |
| 96 | { |
| 97 | po::store(po::parse_command_line(argc, argv, desc_options), vm); |
| 98 | po::notify(vm); |
| 99 | return true; |
| 100 | }); |
| 101 | if (! r) |
| 102 | return 1; |
| 103 | |
| 104 | if (command_line::get_arg(vm, command_line::arg_help)) |
| 105 | { |
| 106 | std::cout << "Monero '" << MONERO_RELEASE_NAME << "' (v" << MONERO_VERSION_FULL << ")" << ENDL << ENDL; |
| 107 | std::cout << desc_options << std::endl; |
| 108 | return 1; |
| 109 | } |
| 110 | |
| 111 | log_level = command_line::get_arg(vm, arg_log_level); |
| 112 | input = command_line::get_arg(vm, arg_input); |
| 113 | if (input.empty()) |
| 114 | { |
| 115 | std::cerr << "--input is mandatory" << std::endl; |
| 116 | return 1; |
| 117 | } |
| 118 | |
| 119 | mlog_configure("", true); |
| 120 | |
| 121 | cryptonote::blobdata blob; |
| 122 | if (!epee::string_tools::parse_hexstr_to_binbuff(input, blob)) |
| 123 | { |
| 124 | std::cerr << "Invalid hex input" << std::endl; |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | bool full; |
| 129 | cryptonote::block block; |
| 130 | cryptonote::transaction tx; |
| 131 | std::vector<cryptonote::tx_extra_field> fields; |
| 132 | if (cryptonote::parse_and_validate_block_from_blob(blob, block)) |
| 133 | { |
| 134 | std::cout << "Parsed block:" << std::endl; |
| 135 | std::cout << cryptonote::obj_to_json_str(block) << std::endl; |
| 136 | bool parsed = cryptonote::parse_tx_extra(block.miner_tx.extra, fields); |
| 137 | if (!parsed) |
| 138 | std::cout << "Failed to parse tx_extra" << std::endl; |
| 139 | |
| 140 | if (!fields.empty()) |
| 141 | { |
| 142 | print_extra_fields(fields); |
| 143 | } |
| 144 | else |
| 145 | { |
| 146 | std::cout << "No fields were found in tx_extra" << std::endl; |
| 147 | } |
| 148 | } |
| 149 | else if (cryptonote::parse_and_validate_tx_from_blob(blob, tx) || cryptonote::parse_and_validate_tx_base_from_blob(blob, tx)) |
| 150 | { |
| 151 | if (tx.pruned) |
| 152 | std::cout << "Parsed pruned transaction:" << std::endl; |
| 153 | else |
| 154 | std::cout << "Parsed transaction:" << std::endl; |
| 155 | std::cout << cryptonote::obj_to_json_str(tx) << std::endl; |
| 156 | |
| 157 | bool parsed = cryptonote::parse_tx_extra(tx.extra, fields); |
| 158 | if (!parsed) |
| 159 | std::cout << "Failed to parse tx_extra" << std::endl; |
| 160 | |
| 161 | if (!fields.empty()) |
| 162 | { |
| 163 | print_extra_fields(fields); |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | std::cout << "No fields were found in tx_extra" << std::endl; |
| 168 | } |
| 169 | } |
| 170 | else if (((full = cryptonote::parse_tx_extra(std::vector<uint8_t>(blob.begin(), blob.end()), fields)) || true) && !fields.empty()) |
| 171 | { |
| 172 | std::cout << "Parsed" << (full ? "" : " partial") << " tx_extra:" << std::endl; |
| 173 | print_extra_fields(fields); |
| 174 | } |
| 175 | else |
| 176 | { |
| 177 | std::cerr << "Not a recognized CN type" << std::endl; |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | |
| 183 | return 0; |
| 184 | } |