diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crypto/crypto.cpp | 134 | ||||
-rw-r--r-- | src/crypto/crypto.h | 14 | ||||
-rw-r--r-- | src/cryptonote_core/blockchain.h | 5 | ||||
-rw-r--r-- | src/cryptonote_protocol/cryptonote_protocol_handler.inl | 2 | ||||
-rw-r--r-- | src/mnemonics/electrum-words.cpp | 5 | ||||
-rw-r--r-- | src/mnemonics/electrum-words.h | 2 | ||||
-rw-r--r-- | src/p2p/net_node.inl | 24 | ||||
-rw-r--r-- | src/p2p/net_peerlist_boost_serialization.h | 12 | ||||
-rw-r--r-- | src/p2p/p2p_protocol_defs.h | 4 | ||||
-rw-r--r-- | src/rpc/core_rpc_server.cpp | 8 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.cpp | 264 | ||||
-rw-r--r-- | src/simplewallet/simplewallet.h | 3 |
12 files changed, 418 insertions, 59 deletions
diff --git a/src/crypto/crypto.cpp b/src/crypto/crypto.cpp index 98da466cc..1c7adff3b 100644 --- a/src/crypto/crypto.cpp +++ b/src/crypto/crypto.cpp @@ -81,12 +81,16 @@ namespace crypto { } /* generate a random 32-byte (256-bit) integer and copy it to res */ - static inline void random_scalar(ec_scalar &res) { + static inline void random_scalar_not_thread_safe(ec_scalar &res) { unsigned char tmp[64]; generate_random_bytes_not_thread_safe(64, tmp); sc_reduce(tmp); memcpy(&res, tmp, 32); } + static inline void random_scalar(ec_scalar &res) { + boost::lock_guard<boost::mutex> lock(random_lock); + random_scalar_not_thread_safe(res); + } static inline void hash_to_scalar(const void *data, size_t length, ec_scalar &res) { cn_fast_hash(data, length, reinterpret_cast<hash &>(res)); @@ -99,7 +103,6 @@ namespace crypto { * */ secret_key crypto_ops::generate_keys(public_key &pub, secret_key &sec, const secret_key& recovery_key, bool recover) { - boost::lock_guard<boost::mutex> lock(random_lock); ge_p3 point; secret_key rng; @@ -197,8 +200,14 @@ namespace crypto { ec_point comm; }; + struct s_comm_2 { + hash msg; + ec_point D; + ec_point X; + ec_point Y; + }; + void crypto_ops::generate_signature(const hash &prefix_hash, const public_key &pub, const secret_key &sec, signature &sig) { - boost::lock_guard<boost::mutex> lock(random_lock); ge_p3 tmp3; ec_scalar k; s_comm buf; @@ -242,6 +251,124 @@ namespace crypto { return sc_isnonzero(&c) == 0; } + void crypto_ops::generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const secret_key &r, signature &sig) { + // sanity check + ge_p3 R_p3; + ge_p3 A_p3; + ge_p3 D_p3; + if (ge_frombytes_vartime(&R_p3, &R) != 0) throw std::runtime_error("tx pubkey is invalid"); + if (ge_frombytes_vartime(&A_p3, &A) != 0) throw std::runtime_error("recipient view pubkey is invalid"); + if (ge_frombytes_vartime(&D_p3, &D) != 0) throw std::runtime_error("key derivation is invalid"); +#if !defined(NDEBUG) + { + assert(sc_check(&r) == 0); + // check R == r*G + ge_p3 dbg_R_p3; + ge_scalarmult_base(&dbg_R_p3, &r); + public_key dbg_R; + ge_p3_tobytes(&dbg_R, &dbg_R_p3); + assert(R == dbg_R); + // check D == r*A + ge_p2 dbg_D_p2; + ge_scalarmult(&dbg_D_p2, &r, &A_p3); + public_key dbg_D; + ge_tobytes(&dbg_D, &dbg_D_p2); + assert(D == dbg_D); + } +#endif + + // pick random k + ec_scalar k; + random_scalar(k); + + // compute X = k*G + ge_p3 X_p3; + ge_scalarmult_base(&X_p3, &k); + + // compute Y = k*A + ge_p2 Y_p2; + ge_scalarmult(&Y_p2, &k, &A_p3); + + // sig.c = Hs(Msg || D || X || Y) + s_comm_2 buf; + buf.msg = prefix_hash; + buf.D = D; + ge_p3_tobytes(&buf.X, &X_p3); + ge_tobytes(&buf.Y, &Y_p2); + hash_to_scalar(&buf, sizeof(s_comm_2), sig.c); + + // sig.r = k - sig.c*r + sc_mulsub(&sig.r, &sig.c, &r, &k); + } + + bool crypto_ops::check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const signature &sig) { + // sanity check + ge_p3 R_p3; + ge_p3 A_p3; + ge_p3 D_p3; + if (ge_frombytes_vartime(&R_p3, &R) != 0) return false; + if (ge_frombytes_vartime(&A_p3, &A) != 0) return false; + if (ge_frombytes_vartime(&D_p3, &D) != 0) return false; + if (sc_check(&sig.c) != 0 || sc_check(&sig.r) != 0) return false; + + // compute sig.c*R + ge_p2 cR_p2; + ge_scalarmult(&cR_p2, &sig.c, &R_p3); + + // compute sig.r*G + ge_p3 rG_p3; + ge_scalarmult_base(&rG_p3, &sig.r); + + // compute sig.c*D + ge_p2 cD_p2; + ge_scalarmult(&cD_p2, &sig.c, &D_p3); + + // compute sig.r*A + ge_p2 rA_p2; + ge_scalarmult(&rA_p2, &sig.r, &A_p3); + + // compute X = sig.c*R + sig.r*G + public_key cR; + ge_tobytes(&cR, &cR_p2); + ge_p3 cR_p3; + if (ge_frombytes_vartime(&cR_p3, &cR) != 0) return false; + ge_cached rG_cached; + ge_p3_to_cached(&rG_cached, &rG_p3); + ge_p1p1 X_p1p1; + ge_add(&X_p1p1, &cR_p3, &rG_cached); + ge_p2 X_p2; + ge_p1p1_to_p2(&X_p2, &X_p1p1); + + // compute Y = sig.c*D + sig.r*A + public_key cD; + public_key rA; + ge_tobytes(&cD, &cD_p2); + ge_tobytes(&rA, &rA_p2); + ge_p3 cD_p3; + ge_p3 rA_p3; + if (ge_frombytes_vartime(&cD_p3, &cD) != 0) return false; + if (ge_frombytes_vartime(&rA_p3, &rA) != 0) return false; + ge_cached rA_cached; + ge_p3_to_cached(&rA_cached, &rA_p3); + ge_p1p1 Y_p1p1; + ge_add(&Y_p1p1, &cD_p3, &rA_cached); + ge_p2 Y_p2; + ge_p1p1_to_p2(&Y_p2, &Y_p1p1); + + // compute c2 = Hs(Msg || D || X || Y) + s_comm_2 buf; + buf.msg = prefix_hash; + buf.D = D; + ge_tobytes(&buf.X, &X_p2); + ge_tobytes(&buf.Y, &Y_p2); + ec_scalar c2; + hash_to_scalar(&buf, sizeof(s_comm_2), c2); + + // test if c2 == sig.c + sc_sub(&c2, &c2, &sig.c); + return sc_isnonzero(&c2) == 0; + } + static void hash_to_ec(const public_key &key, ge_p3 &res) { hash h; ge_p2 point; @@ -280,7 +407,6 @@ POP_WARNINGS const public_key *const *pubs, size_t pubs_count, const secret_key &sec, size_t sec_index, signature *sig) { - boost::lock_guard<boost::mutex> lock(random_lock); size_t i; ge_p3 image_unp; ge_dsmp image_pre; diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 3b8c7996b..e99b6651f 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -123,6 +123,10 @@ namespace crypto { friend void generate_signature(const hash &, const public_key &, const secret_key &, signature &); static bool check_signature(const hash &, const public_key &, const signature &); friend bool check_signature(const hash &, const public_key &, const signature &); + static void generate_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const secret_key &, signature &); + friend void generate_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const secret_key &, signature &); + static bool check_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const signature &); + friend bool check_tx_proof(const hash &, const public_key &, const public_key &, const public_key &, const signature &); static void generate_key_image(const public_key &, const secret_key &, key_image &); friend void generate_key_image(const public_key &, const secret_key &, key_image &); static void generate_ring_signature(const hash &, const key_image &, @@ -200,6 +204,16 @@ namespace crypto { return crypto_ops::check_signature(prefix_hash, pub, sig); } + /* Generation and checking of a tx proof; given a tx pubkey R, the recipient's view pubkey A, and the key + * derivation D, the signature proves the knowledge of the tx secret key r such that R=r*G and D=r*A + */ + inline void generate_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const secret_key &r, signature &sig) { + crypto_ops::generate_tx_proof(prefix_hash, R, A, D, r, sig); + } + inline bool check_tx_proof(const hash &prefix_hash, const public_key &R, const public_key &A, const public_key &D, const signature &sig) { + return crypto_ops::check_tx_proof(prefix_hash, R, A, D, sig); + } + /* To send money to a key: * * The sender generates an ephemeral key and includes it in transaction output. * * To spend the money, the receiver generates a key image from it. diff --git a/src/cryptonote_core/blockchain.h b/src/cryptonote_core/blockchain.h index 56373adf9..4f2e4f0d3 100644 --- a/src/cryptonote_core/blockchain.h +++ b/src/cryptonote_core/blockchain.h @@ -689,16 +689,15 @@ namespace cryptonote // user options, must be called before calling init() - //FIXME: parameter names don't match function definition in .cpp file /** * @brief sets various performance options * - * @param block_threads max number of threads when preparing blocks for addition + * @param maxthreads max number of threads when preparing blocks for addition * @param blocks_per_sync number of blocks to cache before syncing to database * @param sync_mode the ::blockchain_db_sync_mode to use * @param fast_sync sync using built-in block hashes as trusted */ - void set_user_options(uint64_t block_threads, uint64_t blocks_per_sync, + void set_user_options(uint64_t maxthreads, uint64_t blocks_per_sync, blockchain_db_sync_mode sync_mode, bool fast_sync); /** diff --git a/src/cryptonote_protocol/cryptonote_protocol_handler.inl b/src/cryptonote_protocol/cryptonote_protocol_handler.inl index 0b99aa7bd..c5bc834ad 100644 --- a/src/cryptonote_protocol/cryptonote_protocol_handler.inl +++ b/src/cryptonote_protocol/cryptonote_protocol_handler.inl @@ -192,7 +192,7 @@ namespace cryptonote cnx.host = cntxt.m_remote_address.host_str(); cnx.ip = ""; cnx.port = ""; - if (cntxt.m_remote_address.type() == typeid(epee::net_utils::ipv4_network_address)) + if (cntxt.m_remote_address.get_type_id() == epee::net_utils::ipv4_network_address::ID) { cnx.ip = cnx.host; cnx.port = std::to_string(cntxt.m_remote_address.as<epee::net_utils::ipv4_network_address>().port()); diff --git a/src/mnemonics/electrum-words.cpp b/src/mnemonics/electrum-words.cpp index ef1100a10..3b1dc53d7 100644 --- a/src/mnemonics/electrum-words.cpp +++ b/src/mnemonics/electrum-words.cpp @@ -422,10 +422,11 @@ namespace crypto * \param seed The seed to check (a space delimited concatenated word list) * \return true if the seed passed is a old style seed false if not. */ - bool get_is_old_style_seed(const std::string &seed) + bool get_is_old_style_seed(std::string seed) { std::vector<std::string> word_list; - boost::split(word_list, seed, boost::is_any_of(" ")); + boost::algorithm::trim(seed); + boost::split(word_list, seed, boost::is_any_of(" "), boost::token_compress_on); return word_list.size() != (seed_length + 1); } diff --git a/src/mnemonics/electrum-words.h b/src/mnemonics/electrum-words.h index 3655dd201..94ce9c200 100644 --- a/src/mnemonics/electrum-words.h +++ b/src/mnemonics/electrum-words.h @@ -92,7 +92,7 @@ namespace crypto * \param seed The seed to check (a space delimited concatenated word list) * \return true if the seed passed is a old style seed false if not. */ - bool get_is_old_style_seed(const std::string &seed); + bool get_is_old_style_seed(std::string seed); } } diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index 47582cb96..b23090c7d 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -812,6 +812,7 @@ namespace nodetool bool r = epee::net_utils::async_invoke_remote_command2<typename COMMAND_TIMED_SYNC::response>(context_.m_connection_id, COMMAND_TIMED_SYNC::ID, arg, m_net_server.get_config_object(), [this](int code, const typename COMMAND_TIMED_SYNC::response& rsp, p2p_connection_context& context) { + context.m_in_timedsync = false; if(code < 0) { LOG_ERROR_CC(context, "COMMAND_TIMED_SYNC invoke failed. (" << code << ", " << epee::levin::get_err_descr(code) << ")"); @@ -938,8 +939,8 @@ namespace nodetool << (last_seen_stamp ? epee::misc_utils::get_time_interval_string(time(NULL) - last_seen_stamp):"never") << ")..."); - CHECK_AND_ASSERT_MES(na.type() == typeid(epee::net_utils::ipv4_network_address), false, - "Only IPv4 addresses are supported here, got " << na.type().name()); + CHECK_AND_ASSERT_MES(na.get_type_id() == epee::net_utils::ipv4_network_address::ID, false, + "Only IPv4 addresses are supported here"); const epee::net_utils::ipv4_network_address &ipv4 = na.as<const epee::net_utils::ipv4_network_address>(); typename net_server::t_connection_context con = AUTO_VAL_INIT(con); @@ -1003,8 +1004,8 @@ namespace nodetool << (last_seen_stamp ? epee::misc_utils::get_time_interval_string(time(NULL) - last_seen_stamp):"never") << ")..."); - CHECK_AND_ASSERT_MES(na.type() == typeid(epee::net_utils::ipv4_network_address), false, - "Only IPv4 addresses are supported here, got " << na.type().name()); + CHECK_AND_ASSERT_MES(na.get_type_id() == epee::net_utils::ipv4_network_address::ID, false, + "Only IPv4 addresses are supported here"); const epee::net_utils::ipv4_network_address &ipv4 = na.as<epee::net_utils::ipv4_network_address>(); typename net_server::t_connection_context con = AUTO_VAL_INIT(con); @@ -1295,10 +1296,13 @@ namespace nodetool MDEBUG("STARTED PEERLIST IDLE HANDSHAKE"); typedef std::list<std::pair<epee::net_utils::connection_context_base, peerid_type> > local_connects_type; local_connects_type cncts; - m_net_server.get_config_object().foreach_connection([&](const p2p_connection_context& cntxt) + m_net_server.get_config_object().foreach_connection([&](p2p_connection_context& cntxt) { - if(cntxt.peer_id) + if(cntxt.peer_id && !cntxt.m_in_timedsync) + { + cntxt.m_in_timedsync = true; cncts.push_back(local_connects_type::value_type(cntxt, cntxt.peer_id));//do idle sync only with handshaked connections + } return true; }); @@ -1506,8 +1510,8 @@ namespace nodetool if(!node_data.my_port) return false; - CHECK_AND_ASSERT_MES(context.m_remote_address.type() == typeid(epee::net_utils::ipv4_network_address), false, - "Only IPv4 addresses are supported here, got " << context.m_remote_address.type().name()); + CHECK_AND_ASSERT_MES(context.m_remote_address.get_type_id() == epee::net_utils::ipv4_network_address::ID, false, + "Only IPv4 addresses are supported here"); const epee::net_utils::network_address na = context.m_remote_address; uint32_t actual_ip = na.as<const epee::net_utils::ipv4_network_address>().ip(); @@ -1666,8 +1670,8 @@ namespace nodetool //try ping to be sure that we can add this peer to peer_list try_ping(arg.node_data, context, [peer_id_l, port_l, context, this]() { - CHECK_AND_ASSERT_MES(context.m_remote_address.type() == typeid(epee::net_utils::ipv4_network_address), void(), - "Only IPv4 addresses are supported here, got " << context.m_remote_address.type().name()); + CHECK_AND_ASSERT_MES(context.m_remote_address.get_type_id() == epee::net_utils::ipv4_network_address::ID, void(), + "Only IPv4 addresses are supported here"); //called only(!) if success pinged, update local peerlist peerlist_entry pe; const epee::net_utils::network_address na = context.m_remote_address; diff --git a/src/p2p/net_peerlist_boost_serialization.h b/src/p2p/net_peerlist_boost_serialization.h index 0a21895cf..43c5ea5f0 100644 --- a/src/p2p/net_peerlist_boost_serialization.h +++ b/src/p2p/net_peerlist_boost_serialization.h @@ -36,24 +36,16 @@ namespace boost { namespace serialization { - enum { sertype_ipv4_address }; - static inline uint8_t get_type(const epee::net_utils::network_address &na) - { - if (na.type() == typeid(epee::net_utils::ipv4_network_address)) - return sertype_ipv4_address; - throw std::runtime_error("Unsupported network address type"); - return 0; - } template <class Archive, class ver_type> inline void serialize(Archive &a, epee::net_utils::network_address& na, const ver_type ver) { uint8_t type; if (typename Archive::is_saving()) - type = get_type(na); + type = na.get_type_id(); a & type; switch (type) { - case sertype_ipv4_address: + case epee::net_utils::ipv4_network_address::ID: if (!typename Archive::is_saving()) na.reset(new epee::net_utils::ipv4_network_address(0, 0)); a & na.as<epee::net_utils::ipv4_network_address>(); diff --git a/src/p2p/p2p_protocol_defs.h b/src/p2p/p2p_protocol_defs.h index d60990f8a..a471211a6 100644 --- a/src/p2p/p2p_protocol_defs.h +++ b/src/p2p/p2p_protocol_defs.h @@ -188,7 +188,7 @@ namespace nodetool std::list<peerlist_entry_base<network_address_old>> local_peerlist; for (const auto &p: this_ref.local_peerlist_new) { - if (p.adr.type() == typeid(epee::net_utils::ipv4_network_address)) + if (p.adr.get_type_id() == epee::net_utils::ipv4_network_address::ID) { const epee::net_utils::network_address &na = p.adr; const epee::net_utils::ipv4_network_address &ipv4 = na.as<const epee::net_utils::ipv4_network_address>(); @@ -247,7 +247,7 @@ namespace nodetool std::list<peerlist_entry_base<network_address_old>> local_peerlist; for (const auto &p: this_ref.local_peerlist_new) { - if (p.adr.type() == typeid(epee::net_utils::ipv4_network_address)) + if (p.adr.get_type_id() == epee::net_utils::ipv4_network_address::ID) { const epee::net_utils::network_address &na = p.adr; const epee::net_utils::ipv4_network_address &ipv4 = na.as<const epee::net_utils::ipv4_network_address>(); diff --git a/src/rpc/core_rpc_server.cpp b/src/rpc/core_rpc_server.cpp index 7dc5a1fd9..97fe18696 100644 --- a/src/rpc/core_rpc_server.cpp +++ b/src/rpc/core_rpc_server.cpp @@ -736,7 +736,7 @@ namespace cryptonote for (auto & entry : white_list) { - if (entry.adr.type() == typeid(epee::net_utils::ipv4_network_address)) + if (entry.adr.get_type_id() == epee::net_utils::ipv4_network_address::ID) res.white_list.emplace_back(entry.id, entry.adr.as<epee::net_utils::ipv4_network_address>().ip(), entry.adr.as<epee::net_utils::ipv4_network_address>().port(), entry.last_seen); else @@ -745,7 +745,7 @@ namespace cryptonote for (auto & entry : gray_list) { - if (entry.adr.type() == typeid(epee::net_utils::ipv4_network_address)) + if (entry.adr.get_type_id() == epee::net_utils::ipv4_network_address::ID) res.gray_list.emplace_back(entry.id, entry.adr.as<epee::net_utils::ipv4_network_address>().ip(), entry.adr.as<epee::net_utils::ipv4_network_address>().port(), entry.last_seen); else @@ -1130,8 +1130,8 @@ namespace cryptonote return false; } res.headers.push_back(block_header_response()); - bool responce_filled = fill_block_header_response(blk, false, block_height, block_hash, res.headers.back()); - if (!responce_filled) + bool response_filled = fill_block_header_response(blk, false, block_height, block_hash, res.headers.back()); + if (!response_filled) { error_resp.code = CORE_RPC_ERROR_CODE_INTERNAL_ERROR; error_resp.message = "Internal error: can't produce valid response."; diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index ff607728d..0153c4ffc 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -47,6 +47,7 @@ #include "common/command_line.h" #include "common/util.h" #include "common/dns_utils.h" +#include "common/base58.h" #include "p2p/net_node.h" #include "cryptonote_protocol/cryptonote_protocol_handler.h" #include "simplewallet.h" @@ -714,6 +715,8 @@ simple_wallet::simple_wallet() 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 <txid>")); m_cmd_binder.set_handler("check_tx_key", boost::bind(&simple_wallet::check_tx_key, this, _1), tr("Check amount going to <address> in <txid>")); + m_cmd_binder.set_handler("get_tx_proof", boost::bind(&simple_wallet::get_tx_proof, this, _1), tr("Generate a signature to prove payment to <address> in <txid> using the transaction secret key (r) without revealing it")); + m_cmd_binder.set_handler("check_tx_proof", boost::bind(&simple_wallet::check_tx_proof, this, _1), tr("Check tx proof for payment going to <address> in <txid>")); m_cmd_binder.set_handler("show_transfers", boost::bind(&simple_wallet::show_transfers, this, _1), tr("show_transfers [in|out|pending|failed|pool] [<min_height> [<max_height>]] - Show incoming/outgoing transfers within an optional height range")); m_cmd_binder.set_handler("unspent_outputs", boost::bind(&simple_wallet::unspent_outputs, this, _1), tr("unspent_outputs [<min_amount> <max_amount>] - Show unspent outputs within an optional amount range")); m_cmd_binder.set_handler("rescan_bc", boost::bind(&simple_wallet::rescan_blockchain, this, _1), tr("Rescan blockchain from scratch")); @@ -1190,7 +1193,11 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm) bool r = open_wallet(vm); CHECK_AND_ASSERT_MES(r, false, tr("failed to open account")); } - assert(m_wallet); + if (!m_wallet) + { + fail_msg_writer() << tr("wallet is null"); + return false; + } // set --trusted-daemon if local try @@ -1570,7 +1577,11 @@ bool simple_wallet::start_mining(const std::vector<std::string>& args) if (!try_connect_to_daemon()) return true; - assert(m_wallet); + if (!m_wallet) + { + fail_msg_writer() << tr("wallet is null"); + return true; + } COMMAND_RPC_START_MINING::request req = AUTO_VAL_INIT(req); req.miner_address = m_wallet->get_account().get_public_address_str(m_wallet->testnet()); @@ -1613,7 +1624,11 @@ bool simple_wallet::stop_mining(const std::vector<std::string>& args) if (!try_connect_to_daemon()) return true; - assert(m_wallet); + if (!m_wallet) + { + fail_msg_writer() << tr("wallet is null"); + return true; + } COMMAND_RPC_STOP_MINING::request req; COMMAND_RPC_STOP_MINING::response res; bool r = net_utils::invoke_http_json("/stop_mining", req, res, m_http_client); @@ -1630,7 +1645,11 @@ bool simple_wallet::save_bc(const std::vector<std::string>& args) if (!try_connect_to_daemon()) return true; - assert(m_wallet); + if (!m_wallet) + { + fail_msg_writer() << tr("wallet is null"); + return true; + } COMMAND_RPC_SAVE_BC::request req; COMMAND_RPC_SAVE_BC::response res; bool r = net_utils::invoke_http_json("/save_bc", req, res, m_http_client); @@ -3232,6 +3251,85 @@ bool simple_wallet::get_tx_key(const std::vector<std::string> &args_) } } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::get_tx_proof(const std::vector<std::string> &args) +{ + if(args.size() != 2 && args.size() != 3) { + fail_msg_writer() << tr("usage: get_tx_proof <txid> <dest_address> [<tx_key>]"); + return true; + } + if (m_wallet->ask_password() && !get_and_verify_password()) { return true; } + + cryptonote::blobdata txid_data; + if(!epee::string_tools::parse_hexstr_to_binbuff(args[0], txid_data) || txid_data.size() != sizeof(crypto::hash)) + { + fail_msg_writer() << tr("failed to parse txid"); + return true; + } + crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); + + cryptonote::account_public_address address; + bool has_payment_id; + crypto::hash8 payment_id; + if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1])) + { + fail_msg_writer() << tr("failed to parse address"); + return true; + } + + LOCK_IDLE_SCOPE(); + + crypto::secret_key tx_key, tx_key2; + bool r = m_wallet->get_tx_key(txid, tx_key); + cryptonote::blobdata tx_key_data; + if (args.size() == 3) + { + if(!epee::string_tools::parse_hexstr_to_binbuff(args[2], tx_key_data) || tx_key_data.size() != sizeof(crypto::secret_key)) + { + fail_msg_writer() << tr("failed to parse tx_key"); + return true; + } + tx_key2 = *reinterpret_cast<const crypto::secret_key*>(tx_key_data.data()); + } + if (r) + { + if (args.size() == 3 && tx_key != rct::sk2rct(tx_key2)) + { + fail_msg_writer() << tr("Tx secret key was found for the given txid, but you've also provided another tx secret key which doesn't match the found one."); + return true; + } + } + else + { + if (tx_key_data.empty()) + { + fail_msg_writer() << tr("Tx secret key wasn't found in the wallet file. Provide it as the optional third parameter if you have it elsewhere."); + return true; + } + tx_key = tx_key2; + } + + crypto::public_key R; + crypto::secret_key_to_public_key(tx_key, R); + crypto::public_key rA = rct::rct2pk(rct::scalarmultKey(rct::pk2rct(address.m_view_public_key), rct::sk2rct(tx_key))); + crypto::signature sig; + try + { + crypto::generate_tx_proof(txid, R, address.m_view_public_key, rA, tx_key, sig); + } + catch (const std::runtime_error &e) + { + fail_msg_writer() << e.what(); + return true; + } + + std::string sig_str = std::string("ProofV1") + + tools::base58::encode(std::string((const char *)&rA, sizeof(crypto::public_key))) + + tools::base58::encode(std::string((const char *)&sig, sizeof(crypto::signature))); + + success_msg_writer() << tr("Signature: ") << sig_str; + return true; +} +//---------------------------------------------------------------------------------------------------- bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) { std::vector<std::string> local_args = args_; @@ -3244,7 +3342,11 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) if (!try_connect_to_daemon()) return true; - assert(m_wallet); + if (!m_wallet) + { + fail_msg_writer() << tr("wallet is null"); + return true; + } cryptonote::blobdata txid_data; if(!epee::string_tools::parse_hexstr_to_binbuff(local_args[0], txid_data) || txid_data.size() != sizeof(crypto::hash)) { @@ -3253,8 +3355,6 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) } crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); - LOCK_IDLE_SCOPE(); - if (local_args[1].size() < 64 || local_args[1].size() % 64) { fail_msg_writer() << tr("failed to parse tx key"); @@ -3278,6 +3378,18 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) 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; + } + + return check_tx_key_helper(txid, address, derivation); +} +//---------------------------------------------------------------------------------------------------- +bool simple_wallet::check_tx_key_helper(const crypto::hash &txid, const cryptonote::account_public_address &address, const crypto::key_derivation &derivation) +{ COMMAND_RPC_GET_TRANSACTIONS::request req; COMMAND_RPC_GET_TRANSACTIONS::response res; req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid)); @@ -3311,13 +3423,6 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) 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) @@ -3340,14 +3445,6 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) { rct::key Ctmp; //rct::key amount_key = rct::hash_to_scalar(rct::scalarmultKey(rct::pk2rct(address.m_view_public_key), rct::sk2rct(tx_key))); - crypto::key_derivation derivation; - bool r = crypto::generate_key_derivation(address.m_view_public_key, tx_key, derivation); - if (!r) - { - LOG_ERROR("Failed to generate key derivation to decode rct output " << n); - amount = 0; - } - else { crypto::secret_key scalar1; crypto::derivation_to_scalar(derivation, n, scalar1); @@ -3404,6 +3501,129 @@ bool simple_wallet::check_tx_key(const std::vector<std::string> &args_) return true; } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::check_tx_proof(const std::vector<std::string> &args) +{ + if(args.size() != 3) { + fail_msg_writer() << tr("usage: check_tx_proof <txid> <address> <signature>"); + return true; + } + + if (!try_connect_to_daemon()) + return true; + + // parse txid + cryptonote::blobdata txid_data; + if(!epee::string_tools::parse_hexstr_to_binbuff(args[0], txid_data) || txid_data.size() != sizeof(crypto::hash)) + { + fail_msg_writer() << tr("failed to parse txid"); + return true; + } + crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data()); + + // parse address + cryptonote::account_public_address address; + bool has_payment_id; + crypto::hash8 payment_id; + if(!cryptonote::get_account_address_from_str_or_url(address, has_payment_id, payment_id, m_wallet->testnet(), args[1])) + { + fail_msg_writer() << tr("failed to parse address"); + return true; + } + + // parse pubkey r*A & signature + std::string sig_str = args[2]; + const size_t header_len = strlen("ProofV1"); + if (sig_str.size() < header_len || sig_str.substr(0, header_len) != "ProofV1") + { + fail_msg_writer() << tr("Signature header check error"); + return true; + } + crypto::public_key rA; + crypto::signature sig; + const size_t rA_len = tools::base58::encode(std::string((const char *)&rA, sizeof(crypto::public_key))).size(); + const size_t sig_len = tools::base58::encode(std::string((const char *)&sig, sizeof(crypto::signature))).size(); + std::string rA_decoded; + std::string sig_decoded; + if (!tools::base58::decode(sig_str.substr(header_len, rA_len), rA_decoded)) + { + fail_msg_writer() << tr("Signature decoding error"); + return true; + } + if (!tools::base58::decode(sig_str.substr(header_len + rA_len, sig_len), sig_decoded)) + { + fail_msg_writer() << tr("Signature decoding error"); + return true; + } + if (sizeof(crypto::public_key) != rA_decoded.size() || sizeof(crypto::signature) != sig_decoded.size()) + { + fail_msg_writer() << tr("Signature decoding error"); + return true; + } + memcpy(&rA, rA_decoded.data(), sizeof(crypto::public_key)); + memcpy(&sig, sig_decoded.data(), sizeof(crypto::signature)); + + // fetch tx pubkey from the daemon + 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("/gettransactions", req, res, m_http_client) || + (res.txs.size() != 1 && res.txs_as_hex.size() != 1)) + { + fail_msg_writer() << tr("failed to get transaction from daemon"); + return true; + } + cryptonote::blobdata tx_data; + bool ok; + if (res.txs.size() == 1) + ok = string_tools::parse_hexstr_to_binbuff(res.txs.front().as_hex, tx_data); + else + ok = string_tools::parse_hexstr_to_binbuff(res.txs_as_hex.front(), tx_data); + if (!ok) + { + 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::public_key R = get_tx_pub_key_from_extra(tx); + if (R == null_pkey) + { + fail_msg_writer() << tr("Tx pubkey was not found"); + return true; + } + + // check signature + if (crypto::check_tx_proof(txid, R, address.m_view_public_key, rA, sig)) + { + success_msg_writer() << tr("Good signature"); + } + else + { + fail_msg_writer() << tr("Bad signature"); + return true; + } + + // obtain key derivation by multiplying scalar 1 to the pubkey r*A included in the signature + crypto::key_derivation derivation; + if (!crypto::generate_key_derivation(rA, rct::rct2sk(rct::I), derivation)) + { + fail_msg_writer() << tr("failed to generate key derivation"); + return true; + } + + return check_tx_key_helper(txid, address, derivation); +} +//---------------------------------------------------------------------------------------------------- static std::string get_human_readable_timestamp(uint64_t ts) { char buffer[64]; diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index edef47e01..e04352295 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -154,6 +154,9 @@ namespace cryptonote 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); + bool check_tx_key_helper(const crypto::hash &txid, const cryptonote::account_public_address &address, const crypto::key_derivation &derivation); + bool get_tx_proof(const std::vector<std::string> &args); + bool check_tx_proof(const std::vector<std::string> &args); bool show_transfers(const std::vector<std::string> &args); bool unspent_outputs(const std::vector<std::string> &args); bool rescan_blockchain(const std::vector<std::string> &args); |