From 6c995710d8cdc8dbbb3da2d11dc8dfe335074ec9 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Wed, 19 Aug 2015 20:59:44 +0100 Subject: [PATCH] make tx keys available to the user They are also stored in the cache file, to be retrieved using a new get_tx_key command. --- .../cryptonote_format_utils.cpp | 3 +- src/cryptonote_core/cryptonote_format_utils.h | 2 +- src/simplewallet/simplewallet.cpp | 32 +++++++++++++++++++ src/simplewallet/simplewallet.h | 1 + src/wallet/wallet2.cpp | 26 ++++++++++++--- src/wallet/wallet2.h | 14 ++++++-- src/wallet/wallet_rpc_server.cpp | 3 ++ src/wallet/wallet_rpc_server_commands_defs.h | 6 ++++ 8 files changed, 77 insertions(+), 10 deletions(-) diff --git a/src/cryptonote_core/cryptonote_format_utils.cpp b/src/cryptonote_core/cryptonote_format_utils.cpp index af8e703bf..cac51c042 100644 --- a/src/cryptonote_core/cryptonote_format_utils.cpp +++ b/src/cryptonote_core/cryptonote_format_utils.cpp @@ -408,7 +408,7 @@ namespace cryptonote return encrypt_payment_id(payment_id, public_key, secret_key); } //--------------------------------------------------------------- - bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, std::vector extra, transaction& tx, uint64_t unlock_time) + bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, std::vector extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &tx_key) { tx.vin.clear(); tx.vout.clear(); @@ -420,6 +420,7 @@ namespace cryptonote tx.extra = extra; keypair txkey = keypair::generate(); add_tx_pub_key_to_extra(tx, txkey.pub); + tx_key = txkey.sec; // if we have a stealth payment id, find it and encrypt it with the tx key now std::vector tx_extra_fields; diff --git a/src/cryptonote_core/cryptonote_format_utils.h b/src/cryptonote_core/cryptonote_format_utils.h index 319205368..ddc0eadaa 100644 --- a/src/cryptonote_core/cryptonote_format_utils.h +++ b/src/cryptonote_core/cryptonote_format_utils.h @@ -69,7 +69,7 @@ namespace cryptonote }; //--------------------------------------------------------------- - bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, std::vector extra, transaction& tx, uint64_t unlock_time); + bool construct_tx(const account_keys& sender_account_keys, const std::vector& sources, const std::vector& destinations, std::vector extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &txkey); template bool find_tx_extra_field_by_type(const std::vector& tx_extra_fields, T& field) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 0c91d9787..ff60ea38e 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -365,6 +365,7 @@ simple_wallet::simple_wallet() m_cmd_binder.set_handler("seed", boost::bind(&simple_wallet::seed, this, _1), tr("Get deterministic seed")); 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")); 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("help", boost::bind(&simple_wallet::help, this, _1), tr("Show this help")); } //---------------------------------------------------------------------------------------------------- @@ -1724,6 +1725,37 @@ bool simple_wallet::sweep_dust(const std::vector &args_) return true; } //---------------------------------------------------------------------------------------------------- +bool simple_wallet::get_tx_key(const std::vector &args_) +{ + std::vector local_args = args_; + + if(local_args.size() != 1) { + fail_msg_writer() << tr("Usage: get_tx_key "); + return true; + } + + cryptonote::blobdata txid_data; + if(!epee::string_tools::parse_hexstr_to_binbuff(local_args.front(), txid_data)) + { + fail_msg_writer() << tr("Failed to parse txid"); + return false; + } + crypto::hash txid = *reinterpret_cast(txid_data.data()); + + crypto::secret_key tx_key; + bool r = m_wallet->get_tx_key(txid, tx_key); + if (r) + { + success_msg_writer() << tr("tx key: ") << tx_key; + return true; + } + else + { + fail_msg_writer() << tr("No tx key found for this 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 8aca93210..7db56cdec 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -122,6 +122,7 @@ namespace cryptonote bool set_variable(const std::vector &args); bool rescan_spent(const std::vector &args); bool set_log(const std::vector &args); + bool get_tx_key(const std::vector &args); uint64_t get_daemon_blockchain_height(std::string& err); bool try_connect_to_daemon(); diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 11ea4678a..e3ddb7c53 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -1251,6 +1251,8 @@ std::string wallet2::address_from_txt_record(const std::string& s) void wallet2::commit_tx(pending_tx& ptx) { using namespace cryptonote; + crypto::hash txid; + COMMAND_RPC_SEND_RAW_TX::request req; req.tx_as_hex = epee::string_tools::buff_to_hex_nodelimer(tx_to_blob(ptx.tx)); COMMAND_RPC_SEND_RAW_TX::response daemon_send_resp; @@ -1259,14 +1261,16 @@ void wallet2::commit_tx(pending_tx& ptx) THROW_WALLET_EXCEPTION_IF(daemon_send_resp.status == CORE_RPC_STATUS_BUSY, error::daemon_busy, "sendrawtransaction"); THROW_WALLET_EXCEPTION_IF(daemon_send_resp.status != CORE_RPC_STATUS_OK, error::tx_rejected, ptx.tx, daemon_send_resp.status); + txid = get_transaction_hash(ptx.tx); add_unconfirmed_tx(ptx.tx, ptx.change_dts.amount); + m_tx_keys.insert(std::make_pair(txid, ptx.tx_key)); - LOG_PRINT_L2("transaction " << get_transaction_hash(ptx.tx) << " generated ok and sent to daemon, key_images: [" << ptx.key_images << "]"); + LOG_PRINT_L2("transaction " << txid << " generated ok and sent to daemon, key_images: [" << ptx.key_images << "]"); BOOST_FOREACH(transfer_container::iterator it, ptx.selected_transfers) it->m_spent = true; - LOG_PRINT_L0("Transaction successfully sent. <" << get_transaction_hash(ptx.tx) << ">" << ENDL + LOG_PRINT_L0("Transaction successfully sent. <" << txid << ">" << ENDL << "Commission: " << print_money(ptx.fee+ptx.dust) << " (dust: " << print_money(ptx.dust) << ")" << ENDL << "Balance: " << print_money(balance()) << ENDL << "Unlocked: " << print_money(unlocked_balance()) << ENDL @@ -1511,7 +1515,8 @@ void wallet2::transfer_selected(const std::vector