diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/daemon/rpc_command_executor.cpp | 18 | ||||
-rw-r--r-- | src/rpc/core_rpc_server.cpp | 62 | ||||
-rw-r--r-- | src/rpc/core_rpc_server.h | 2 | ||||
-rw-r--r-- | src/rpc/core_rpc_server_commands_defs.h | 36 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 111 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.h | 1 |
6 files changed, 222 insertions, 8 deletions
diff --git a/src/daemon/rpc_command_executor.cpp b/src/daemon/rpc_command_executor.cpp index c4fe642e8..b0fe5945b 100644 --- a/src/daemon/rpc_command_executor.cpp +++ b/src/daemon/rpc_command_executor.cpp @@ -408,8 +408,8 @@ bool t_rpc_command_executor::print_height() { } bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) { - cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request req; - cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response res; + cryptonote::COMMAND_RPC_GET_BLOCK::request req; + cryptonote::COMMAND_RPC_GET_BLOCK::response res; epee::json_rpc::error error_resp; req.hash = epee::string_tools::pod_to_hex(block_hash); @@ -418,14 +418,14 @@ bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) { if (m_is_rpc) { - if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyhash", fail_message.c_str())) + if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str())) { return true; } } else { - if (!m_rpc_server->on_get_block_header_by_hash(req, res, error_resp)) + if (!m_rpc_server->on_get_block(req, res, error_resp)) { tools::fail_msg_writer() << fail_message.c_str(); return true; @@ -433,13 +433,14 @@ bool t_rpc_command_executor::print_block_by_hash(crypto::hash block_hash) { } print_block_header(res.block_header); + tools::success_msg_writer() << res.json << ENDL; return true; } bool t_rpc_command_executor::print_block_by_height(uint64_t height) { - cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request req; - cryptonote::COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response res; + cryptonote::COMMAND_RPC_GET_BLOCK::request req; + cryptonote::COMMAND_RPC_GET_BLOCK::response res; epee::json_rpc::error error_resp; req.height = height; @@ -448,14 +449,14 @@ bool t_rpc_command_executor::print_block_by_height(uint64_t height) { if (m_is_rpc) { - if (!m_rpc_client->json_rpc_request(req, res, "getblockheaderbyheight", fail_message.c_str())) + if (!m_rpc_client->json_rpc_request(req, res, "getblock", fail_message.c_str())) { return true; } } else { - if (!m_rpc_server->on_get_block_header_by_height(req, res, error_resp)) + if (!m_rpc_server->on_get_block(req, res, error_resp)) { tools::fail_msg_writer() << fail_message.c_str(); return true; @@ -463,6 +464,7 @@ bool t_rpc_command_executor::print_block_by_height(uint64_t height) { } print_block_header(res.block_header); + tools::success_msg_writer() << res.json << ENDL; return true; } diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index f9ff632ac..08005ec72 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -253,6 +253,8 @@ namespace cryptonote { blobdata blob = t_serializable_object_to_blob(tx); res.txs_as_hex.push_back(string_tools::buff_to_hex_nodelimer(blob)); + if (req.decode_as_json) + res.txs_as_json.push_back(obj_to_json_str(tx)); } BOOST_FOREACH(const auto& miss_tx, missed_txs) @@ -772,6 +774,66 @@ namespace cryptonote return true; } //------------------------------------------------------------------------------------------------------------------------------ + bool core_rpc_server::on_get_block(const COMMAND_RPC_GET_BLOCK::request& req, COMMAND_RPC_GET_BLOCK::response& res, epee::json_rpc::error& error_resp){ + if(!check_core_busy()) + { + error_resp.code = CORE_RPC_ERROR_CODE_CORE_BUSY; + error_resp.message = "Core is busy."; + return false; + } + crypto::hash block_hash; + if (!req.hash.empty()) + { + bool hash_parsed = parse_hash256(req.hash, block_hash); + if(!hash_parsed) + { + error_resp.code = CORE_RPC_ERROR_CODE_WRONG_PARAM; + error_resp.message = "Failed to parse hex representation of block hash. Hex = " + req.hash + '.'; + return false; + } + } + else + { + if(m_core.get_current_blockchain_height() <= req.height) + { + error_resp.code = CORE_RPC_ERROR_CODE_TOO_BIG_HEIGHT; + error_resp.message = std::string("To big height: ") + std::to_string(req.height) + ", current blockchain height = " + std::to_string(m_core.get_current_blockchain_height()); + return false; + } + block_hash = m_core.get_block_id_by_height(req.height); + } + block blk; + bool have_block = m_core.get_block_by_hash(block_hash, blk); + if (!have_block) + { + error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; + error_resp.message = "Internal error: can't get block by hash. Hash = " + req.hash + '.'; + return false; + } + if (blk.miner_tx.vin.front().type() != typeid(txin_gen)) + { + error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; + error_resp.message = "Internal error: coinbase transaction in the block has the wrong type"; + return false; + } + uint64_t block_height = boost::get<txin_gen>(blk.miner_tx.vin.front()).height; + bool responce_filled = fill_block_header_responce(blk, false, block_height, block_hash, res.block_header); + if (!responce_filled) + { + error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; + error_resp.message = "Internal error: can't produce valid response."; + return false; + } + for (size_t n = 0; n < blk.tx_hashes.size(); ++n) + { + res.tx_hashes.push_back(epee::string_tools::pod_to_hex(blk.tx_hashes[n])); + } + res.blob = t_serializable_object_to_blob(blk); + res.json = obj_to_json_str(blk); + res.status = CORE_RPC_STATUS_OK; + return true; + } + //------------------------------------------------------------------------------------------------------------------------------ bool core_rpc_server::on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp) { if(!check_core_busy()) diff --git a/src/rpc/core_rpc_server.h b/src/rpc/core_rpc_server.h index 60e7d00d5..4f62f92ac 100644 --- a/src/rpc/core_rpc_server.h +++ b/src/rpc/core_rpc_server.h @@ -101,6 +101,7 @@ namespace cryptonote MAP_JON_RPC_WE("getlastblockheader", on_get_last_block_header, COMMAND_RPC_GET_LAST_BLOCK_HEADER) MAP_JON_RPC_WE("getblockheaderbyhash", on_get_block_header_by_hash, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH) MAP_JON_RPC_WE("getblockheaderbyheight", on_get_block_header_by_height, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT) + MAP_JON_RPC_WE("getblock", on_get_block, COMMAND_RPC_GET_BLOCK) MAP_JON_RPC_WE("get_connections", on_get_connections, COMMAND_RPC_GET_CONNECTIONS) MAP_JON_RPC_WE("get_info", on_get_info_json, COMMAND_RPC_GET_INFO) END_JSON_RPC_MAP() @@ -136,6 +137,7 @@ namespace cryptonote bool on_get_last_block_header(const COMMAND_RPC_GET_LAST_BLOCK_HEADER::request& req, COMMAND_RPC_GET_LAST_BLOCK_HEADER::response& res, epee::json_rpc::error& error_resp); bool on_get_block_header_by_hash(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HASH::response& res, epee::json_rpc::error& error_resp); bool on_get_block_header_by_height(const COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::request& req, COMMAND_RPC_GET_BLOCK_HEADER_BY_HEIGHT::response& res, epee::json_rpc::error& error_resp); + bool on_get_block(const COMMAND_RPC_GET_BLOCK::request& req, COMMAND_RPC_GET_BLOCK::response& res, epee::json_rpc::error& error_resp); bool on_get_connections(const COMMAND_RPC_GET_CONNECTIONS::request& req, COMMAND_RPC_GET_CONNECTIONS::response& res, epee::json_rpc::error& error_resp); bool on_get_info_json(const COMMAND_RPC_GET_INFO::request& req, COMMAND_RPC_GET_INFO::response& res, epee::json_rpc::error& error_resp); bool on_hard_fork_info(const COMMAND_RPC_HARD_FORK_INFO::request& req, COMMAND_RPC_HARD_FORK_INFO::response& res, epee::json_rpc::error& error_resp); diff --git a/src/rpc/core_rpc_server_commands_defs.h b/src/rpc/core_rpc_server_commands_defs.h index a806cbae9..cb8845e8c 100644 --- a/src/rpc/core_rpc_server_commands_defs.h +++ b/src/rpc/core_rpc_server_commands_defs.h @@ -95,9 +95,11 @@ namespace cryptonote struct request { std::list<std::string> txs_hashes; + bool decode_as_json; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(txs_hashes) + KV_SERIALIZE(decode_as_json) END_KV_SERIALIZE_MAP() }; @@ -106,11 +108,13 @@ namespace cryptonote { std::list<std::string> txs_as_hex; //transactions blobs as hex std::list<std::string> missed_tx; //not found transactions + std::list<std::string> txs_as_json; //transactions decoded as json std::string status; BEGIN_KV_SERIALIZE_MAP() KV_SERIALIZE(txs_as_hex) KV_SERIALIZE(missed_tx) + KV_SERIALIZE(txs_as_json) KV_SERIALIZE(status) END_KV_SERIALIZE_MAP() }; @@ -536,6 +540,38 @@ namespace cryptonote }; + struct COMMAND_RPC_GET_BLOCK + { + struct request + { + std::string hash; + uint64_t height; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(hash) + KV_SERIALIZE(height) + END_KV_SERIALIZE_MAP() + }; + + struct response + { + std::string status; + block_header_responce block_header; + std::vector<std::string> tx_hashes; + std::string blob; + std::string json; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(block_header) + KV_SERIALIZE(tx_hashes) + KV_SERIALIZE(status) + KV_SERIALIZE(blob) + KV_SERIALIZE(json) + END_KV_SERIALIZE_MAP() + }; + + }; + struct peer { uint64_t id; uint32_t ip; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 445304dc7..2661dd597 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -396,6 +396,7 @@ simple_wallet::simple_wallet() m_cmd_binder.set_handler("set", boost::bind(&simple_wallet::set_variable, this, _1), tr("available options: seed language - Set wallet seed langage; always-confirm-transfers <1|0> - whether to confirm unsplit txes; store-tx-keys <1|0> - whether to store per-tx private keys for future reference")); m_cmd_binder.set_handler("rescan_spent", boost::bind(&simple_wallet::rescan_spent, this, _1), tr("Rescan blockchain for spent outputs")); m_cmd_binder.set_handler("get_tx_key", boost::bind(&simple_wallet::get_tx_key, this, _1), tr("Get transaction key (r) for a given tx")); + m_cmd_binder.set_handler("check_tx_key", boost::bind(&simple_wallet::check_tx_key, this, _1), tr("Check amount going to a given address in a partcular tx")); m_cmd_binder.set_handler("help", boost::bind(&simple_wallet::help, this, _1), tr("Show this help")); } //---------------------------------------------------------------------------------------------------- @@ -985,6 +986,12 @@ bool simple_wallet::save_watch_only(const std::vector<std::string> &args/* = std //---------------------------------------------------------------------------------------------------- bool simple_wallet::start_mining(const std::vector<std::string>& args) { + if (!m_trusted_daemon) + { + fail_msg_writer() << tr("This command assume a trusted daemon. Enable with --trusted-daemon"); + return true; + } + if (!try_connect_to_daemon()) return true; @@ -1808,6 +1815,110 @@ bool simple_wallet::get_tx_key(const std::vector<std::string> &args_) } } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) +{ + std::vector<std::string> local_args = args_; + + if(local_args.size() != 3) { + fail_msg_writer() << tr("Usage: check_tx_key <txid> <txkey> <address>"); + return true; + } + + if (!try_connect_to_daemon()) + return true; + + cryptonote::blobdata txid_data; + if(!epee::string_tools::parse_hexstr_to_binbuff(local_args[0], txid_data)) + { + fail_msg_writer() << tr("Failed to parse txid"); + return true; + } + crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); + + cryptonote::blobdata tx_key_data; + if(!epee::string_tools::parse_hexstr_to_binbuff(local_args[1], tx_key_data)) + { + fail_msg_writer() << tr("Failed to parse tx key"); + return true; + } + crypto::secret_key tx_key = *reinterpret_cast<const crypto::secret_key*>(tx_key_data.data()); + + cryptonote::account_public_address address; + bool has_payment_id; + crypto::hash8 payment_id; + if(!get_account_integrated_address_from_str(address, has_payment_id, payment_id, m_wallet->testnet(), local_args[2])) + { + fail_msg_writer() << tr("Failed to parse address"); + return true; + } + + COMMAND_RPC_GET_TRANSACTIONS::request req; + COMMAND_RPC_GET_TRANSACTIONS::response res; + req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid)); + if (!net_utils::invoke_http_json_remote_command2(m_daemon_address + "/gettransactions", req, res, m_http_client) || + res.txs_as_hex.empty()) + { + fail_msg_writer() << tr("Failed to get transaction from daemon"); + return true; + } + cryptonote::blobdata tx_data; + if (!string_tools::parse_hexstr_to_binbuff(res.txs_as_hex.front(), tx_data)) + { + fail_msg_writer() << tr("Failed to parse transaction from daemon"); + return true; + } + crypto::hash tx_hash, tx_prefix_hash; + cryptonote::transaction tx; + if (!cryptonote::parse_and_validate_tx_from_blob(tx_data, tx, tx_hash, tx_prefix_hash)) + { + fail_msg_writer() << tr("Failed to validate transaction from daemon"); + return true; + } + if (tx_hash != txid) + { + fail_msg_writer() << tr("Failed to get the right transaction from daemon"); + return true; + } + + crypto::key_derivation derivation; + if (!crypto::generate_key_derivation(address.m_view_public_key, tx_key, derivation)) + { + fail_msg_writer() << tr("Failed to generate key derivation from supplied parameters"); + return true; + } + + uint64_t received = 0; + try { + for (size_t n = 0; n < tx.vout.size(); ++n) + { + if (typeid(txout_to_key) != tx.vout[n].target.type()) + continue; + const txout_to_key tx_out_to_key = boost::get<txout_to_key>(tx.vout[n].target); + crypto::public_key pubkey; + derive_public_key(derivation, n, address.m_spend_public_key, pubkey); + if (pubkey == tx_out_to_key.key) + received += tx.vout[n].amount; + } + } + catch(...) + { + LOG_ERROR("Unknown error"); + fail_msg_writer() << tr("unknown error"); + return true; + } + + if (received > 0) + { + success_msg_writer() << get_account_address_as_str(m_wallet->testnet(), address) << " received " << print_money(received) << " in txid " << txid; + } + else + { + fail_msg_writer() << get_account_address_as_str(m_wallet->testnet(), address) << " received nothing in txid " << txid; + } + + return true; +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::run() { std::string addr_start = m_wallet->get_account().get_public_address_str(m_wallet->testnet()).substr(0, 6); diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 5a5401928..78f061130 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -124,6 +124,7 @@ namespace cryptonote bool rescan_spent(const std::vector<std::string> &args); bool set_log(const std::vector<std::string> &args); bool get_tx_key(const std::vector<std::string> &args); + bool check_tx_key(const std::vector<std::string> &args); uint64_t get_daemon_blockchain_height(std::string& err); bool try_connect_to_daemon(); |