Skip to content

Commit

Permalink
make tx keys available to the user
Browse files Browse the repository at this point in the history
They are also stored in the cache file, to be retrieved using
a new get_tx_key command.
  • Loading branch information
moneromooo-monero committed Aug 19, 2015
1 parent 776b4fc commit bc97938
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/cryptonote_core/cryptonote_format_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time)
bool construct_tx(const account_keys& sender_account_keys, const std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &tx_key)
{
tx.vin.clear();
tx.vout.clear();
Expand All @@ -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_field> tx_extra_fields;
Expand Down
2 changes: 1 addition & 1 deletion src/cryptonote_core/cryptonote_format_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace cryptonote
};

//---------------------------------------------------------------
bool construct_tx(const account_keys& sender_account_keys, const std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time);
bool construct_tx(const account_keys& sender_account_keys, const std::vector<tx_source_entry>& sources, const std::vector<tx_destination_entry>& destinations, std::vector<uint8_t> extra, transaction& tx, uint64_t unlock_time, crypto::secret_key &txkey);

template<typename T>
bool find_tx_extra_field_by_type(const std::vector<tx_extra_field>& tx_extra_fields, T& field)
Expand Down
36 changes: 34 additions & 2 deletions src/simplewallet/simplewallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
//----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1493,7 +1494,7 @@ bool simple_wallet::transfer_main(bool new_algorithm, const std::vector<std::str
{
auto & ptx = ptx_vector.back();
m_wallet->commit_tx(ptx);
success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx);
success_msg_writer(true) << tr("Money successfully sent, transaction ") << get_transaction_hash(ptx.tx) << ", r " << ptx.tx_key;

// if no exception, remove element from vector
ptx_vector.pop_back();
Expand Down Expand Up @@ -1640,7 +1641,7 @@ bool simple_wallet::sweep_dust(const std::vector<std::string> &args_)
{
auto & ptx = ptx_vector.back();
m_wallet->commit_tx(ptx);
success_msg_writer(true) << tr("Money successfully sent, transaction: ") << get_transaction_hash(ptx.tx);
success_msg_writer(true) << tr("Money successfully sent, transaction: ") << get_transaction_hash(ptx.tx) << ", r " << ptx.tx_key;

// if no exception, remove element from vector
ptx_vector.pop_back();
Expand Down Expand Up @@ -1724,6 +1725,37 @@ bool simple_wallet::sweep_dust(const std::vector<std::string> &args_)
return true;
}
//----------------------------------------------------------------------------------------------------
bool simple_wallet::get_tx_key(const std::vector<std::string> &args_)
{
std::vector<std::string> local_args = args_;

if(local_args.size() != 1) {
fail_msg_writer() << tr("Usage: get_tx_key <txid>");
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<const crypto::hash*>(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);
Expand Down
1 change: 1 addition & 0 deletions src/simplewallet/simplewallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ namespace cryptonote
bool set_variable(const std::vector<std::string> &args);
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);

uint64_t get_daemon_blockchain_height(std::string& err);
bool try_connect_to_daemon();
Expand Down
26 changes: 21 additions & 5 deletions src/wallet/wallet2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -1511,7 +1515,8 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent
splitted_dsts.push_back(cryptonote::tx_destination_entry(dust, dust_policy.addr_for_dust));
}

bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time);
crypto::secret_key tx_key;
bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key);
THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet);
THROW_WALLET_EXCEPTION_IF(m_upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, m_upper_transaction_size_limit);

Expand All @@ -1530,7 +1535,7 @@ void wallet2::transfer_selected(const std::vector<cryptonote::tx_destination_ent
ptx.tx = tx;
ptx.change_dts = change_dts;
ptx.selected_transfers = selected_transfers;

ptx.tx_key = tx_key;
}

// Another implementation of transaction creation that is hopefully better
Expand Down Expand Up @@ -1857,7 +1862,8 @@ void wallet2::transfer_dust(size_t num_outputs, uint64_t unlock_time, uint64_t n
THROW_WALLET_EXCEPTION_IF(dust_policy.dust_threshold < dust, error::wallet_internal_error, "invalid dust value: dust = " +
std::to_string(dust) + ", dust_threshold = " + std::to_string(dust_policy.dust_threshold));

bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time);
crypto::secret_key tx_key;
bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key);
THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet);
THROW_WALLET_EXCEPTION_IF(m_upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, m_upper_transaction_size_limit);

Expand All @@ -1876,6 +1882,7 @@ void wallet2::transfer_dust(size_t num_outputs, uint64_t unlock_time, uint64_t n
ptx.tx = tx;
ptx.change_dts = change_dts;
ptx.selected_transfers = selected_transfers;
ptx.tx_key = tx_key;
}

//----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1989,6 +1996,15 @@ std::vector<wallet2::pending_tx> wallet2::create_dust_sweep_transactions()
}
}

bool wallet2::get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const
{
const std::unordered_map<crypto::hash, crypto::secret_key>::const_iterator i = m_tx_keys.find(txid);
if (i == m_tx_keys.end())
return false;
tx_key = i->second;
return true;
}

//----------------------------------------------------------------------------------------------------
void wallet2::generate_genesis(cryptonote::block& b) {
if (m_testnet)
Expand Down
14 changes: 11 additions & 3 deletions src/wallet/wallet2.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ namespace tools
cryptonote::tx_destination_entry change_dts;
std::list<transfer_container::iterator> selected_transfers;
std::string key_images;
crypto::secret_key tx_key;
};

struct keys_file_data
Expand Down Expand Up @@ -251,6 +252,9 @@ namespace tools
if(ver < 7)
return;
a & m_payments;
if(ver < 8)
return;
a & m_tx_keys;
}

/*!
Expand Down Expand Up @@ -278,6 +282,8 @@ namespace tools
bool always_confirm_transfers() const { return m_always_confirm_transfers; }
void always_confirm_transfers(bool always) { m_always_confirm_transfers = always; }

bool get_tx_key(const crypto::hash &txid, crypto::secret_key &tx_key) const;

private:
/*!
* \brief Stores wallet information to wallet file.
Expand Down Expand Up @@ -316,6 +322,7 @@ namespace tools
std::vector<crypto::hash> m_blockchain;
std::atomic<uint64_t> m_local_bc_height; //temporary workaround
std::unordered_map<crypto::hash, unconfirmed_transfer_details> m_unconfirmed_txs;
std::unordered_map<crypto::hash, crypto::secret_key> m_tx_keys;

transfer_container m_transfers;
payment_container m_payments;
Expand All @@ -334,7 +341,7 @@ namespace tools
bool m_always_confirm_transfers;
};
}
BOOST_CLASS_VERSION(tools::wallet2, 7)
BOOST_CLASS_VERSION(tools::wallet2, 8)

namespace boost
{
Expand Down Expand Up @@ -565,7 +572,8 @@ namespace tools
splitted_dsts.push_back(cryptonote::tx_destination_entry(dust, dust_policy.addr_for_dust));
}

bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time);
crypto::secret_key tx_key;
bool r = cryptonote::construct_tx(m_account.get_keys(), sources, splitted_dsts, extra, tx, unlock_time, tx_key);
THROW_WALLET_EXCEPTION_IF(!r, error::tx_not_constructed, sources, splitted_dsts, unlock_time, m_testnet);
THROW_WALLET_EXCEPTION_IF(m_upper_transaction_size_limit <= get_object_blobsize(tx), error::tx_too_big, tx, m_upper_transaction_size_limit);

Expand All @@ -584,7 +592,7 @@ namespace tools
ptx.tx = tx;
ptx.change_dts = change_dts;
ptx.selected_transfers = selected_transfers;

ptx.tx_key = tx_key;
}


Expand Down
3 changes: 3 additions & 0 deletions src/wallet/wallet_rpc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ namespace tools

// populate response with tx hash
res.tx_hash = boost::lexical_cast<std::string>(cryptonote::get_transaction_hash(ptx_vector.back().tx));
res.tx_key = boost::lexical_cast<std::string>(ptx_vector.back().tx_key);
return true;
}
catch (const tools::error::daemon_busy& e)
Expand Down Expand Up @@ -274,6 +275,7 @@ namespace tools
for (auto & ptx : ptx_vector)
{
res.tx_hash_list.push_back(boost::lexical_cast<std::string>(cryptonote::get_transaction_hash(ptx.tx)));
res.tx_key_list.push_back(boost::lexical_cast<std::string>(ptx.tx_key));
}

return true;
Expand Down Expand Up @@ -318,6 +320,7 @@ namespace tools
for (auto & ptx : ptx_vector)
{
res.tx_hash_list.push_back(boost::lexical_cast<std::string>(cryptonote::get_transaction_hash(ptx.tx)));
res.tx_key_list.push_back(boost::lexical_cast<std::string>(ptx.tx_key));
}

return true;
Expand Down
6 changes: 6 additions & 0 deletions src/wallet/wallet_rpc_server_commands_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ namespace wallet_rpc
struct response
{
std::string tx_hash;
std::string tx_key;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tx_hash)
KV_SERIALIZE(tx_key)
END_KV_SERIALIZE_MAP()
};
};
Expand Down Expand Up @@ -141,9 +143,11 @@ namespace wallet_rpc
struct response
{
std::list<std::string> tx_hash_list;
std::list<std::string> tx_key_list;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tx_hash_list)
KV_SERIALIZE(tx_key_list)
END_KV_SERIALIZE_MAP()
};
};
Expand All @@ -159,9 +163,11 @@ namespace wallet_rpc
struct response
{
std::list<std::string> tx_hash_list;
std::list<std::string> tx_key_list;

BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(tx_hash_list)
KV_SERIALIZE(tx_key_list)
END_KV_SERIALIZE_MAP()
};
};
Expand Down

0 comments on commit bc97938

Please sign in to comment.