Tx proof (revised):

- refactoring: proof generation/checking code was moved from simplewallet.cpp to wallet2.cpp
- allow an arbitrary message to be signed together with txid
- introduce two types (outbound & inbound) of tx proofs; with the same syntax, inbound is selected when <address> belongs to this wallet, outbound otherwise. see GitHub thread for more discussion
- wallet RPC: added get_tx_key, check_tx_key, get_tx_proof, check_tx_proof
- wallet API: moved WalletManagerImpl::checkPayment to Wallet::checkTxKey, added Wallet::getTxProof/checkTxProof
- get_tx_key/check_tx_key: handle additional tx keys by concatenating them into a single string
This commit is contained in:
stoffu 2017-09-12 10:05:41 +09:00
parent dc6a8014bd
commit 998777ecd7
No known key found for this signature in database
GPG key ID: 41DAB8343A9EC012
13 changed files with 876 additions and 532 deletions

View file

@ -1385,24 +1385,148 @@ std::string WalletImpl::getUserNote(const std::string &txid) const
return m_wallet->get_tx_note(htxid);
}
std::string WalletImpl::getTxKey(const std::string &txid) const
std::string WalletImpl::getTxKey(const std::string &txid_str) const
{
cryptonote::blobdata txid_data;
if(!epee::string_tools::parse_hexstr_to_binbuff(txid, txid_data) || txid_data.size() != sizeof(crypto::hash))
crypto::hash txid;
if(!epee::string_tools::hex_to_pod(txid_str, txid))
{
return "";
m_status = Status_Error;
m_errorString = tr("Failed to parse txid");
return "";
}
const crypto::hash htxid = *reinterpret_cast<const crypto::hash*>(txid_data.data());
crypto::secret_key tx_key;
std::vector<crypto::secret_key> additional_tx_keys;
if (m_wallet->get_tx_key(htxid, tx_key, additional_tx_keys))
if (m_wallet->get_tx_key(txid, tx_key, additional_tx_keys))
{
return epee::string_tools::pod_to_hex(tx_key);
m_status = Status_Ok;
std::ostringstream oss;
oss << epee::string_tools::pod_to_hex(tx_key);
for (size_t i = 0; i < additional_tx_keys.size(); ++i)
oss << epee::string_tools::pod_to_hex(additional_tx_keys[i]);
return oss.str();
}
else
{
return "";
m_status = Status_Error;
m_errorString = tr("no tx keys found for this txid");
return "";
}
}
bool WalletImpl::checkTxKey(const std::string &txid_str, std::string tx_key_str, const std::string &address_str, uint64_t &received, bool &in_pool, uint64_t &confirmations)
{
crypto::hash txid;
if (!epee::string_tools::hex_to_pod(txid_str, txid))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse txid");
return false;
}
crypto::secret_key tx_key;
std::vector<crypto::secret_key> additional_tx_keys;
if (!epee::string_tools::hex_to_pod(tx_key_str.substr(0, 64), tx_key))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse tx key");
return false;
}
tx_key_str = tx_key_str.substr(64);
while (!tx_key_str.empty())
{
additional_tx_keys.resize(additional_tx_keys.size() + 1);
if (!epee::string_tools::hex_to_pod(tx_key_str.substr(0, 64), additional_tx_keys.back()))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse tx key");
return false;
}
tx_key_str = tx_key_str.substr(64);
}
cryptonote::address_parse_info info;
if (!cryptonote::get_account_address_from_str(info, m_wallet->testnet(), address_str))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse address");
return false;
}
try
{
m_wallet->check_tx_key(txid, tx_key, additional_tx_keys, info.address, received, in_pool, confirmations);
m_status = Status_Ok;
return true;
}
catch (const std::exception &e)
{
m_status = Status_Error;
m_errorString = e.what();
return false;
}
}
std::string WalletImpl::getTxProof(const std::string &txid_str, const std::string &address_str, const std::string &message, std::string &error_str) const
{
crypto::hash txid;
if (!epee::string_tools::hex_to_pod(txid_str, txid))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse txid");
return "";
}
cryptonote::address_parse_info info;
if (!cryptonote::get_account_address_from_str(info, m_wallet->testnet(), address_str))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse address");
return "";
}
try
{
m_status = Status_Ok;
return m_wallet->get_tx_proof(txid, info.address, info.is_subaddress, message, error_str);
}
catch (const std::exception &e)
{
m_status = Status_Error;
m_errorString = e.what();
return "";
}
}
bool WalletImpl::checkTxProof(const std::string &txid_str, const std::string &address_str, const std::string &message, const std::string &signature, bool &good, uint64_t &received, bool &in_pool, uint64_t &confirmations)
{
crypto::hash txid;
if (!epee::string_tools::hex_to_pod(txid_str, txid))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse txid");
return false;
}
cryptonote::address_parse_info info;
if (!cryptonote::get_account_address_from_str(info, m_wallet->testnet(), address_str))
{
m_status = Status_Error;
m_errorString = tr("Failed to parse address");
return false;
}
try
{
good = m_wallet->check_tx_proof(txid, info.address, info.is_subaddress, message, signature, received, in_pool, confirmations);
m_status = Status_Ok;
return true;
}
catch (const std::exception &e)
{
m_status = Status_Error;
m_errorString = e.what();
return false;
}
}

View file

@ -137,6 +137,9 @@ public:
virtual bool setUserNote(const std::string &txid, const std::string &note);
virtual std::string getUserNote(const std::string &txid) const;
virtual std::string getTxKey(const std::string &txid) const;
virtual bool checkTxKey(const std::string &txid, std::string tx_key, const std::string &address, uint64_t &received, bool &in_pool, uint64_t &confirmations);
virtual std::string getTxProof(const std::string &txid, const std::string &address, const std::string &message, std::string &error_str) const;
virtual bool checkTxProof(const std::string &txid, const std::string &address, const std::string &message, const std::string &signature, bool &good, uint64_t &received, bool &in_pool, uint64_t &confirmations);
virtual std::string signMessage(const std::string &message);
virtual bool verifySignedMessage(const std::string &message, const std::string &address, const std::string &signature) const;
virtual void startRefresh();

View file

@ -189,155 +189,6 @@ bool WalletManagerImpl::connected(uint32_t *version) const
return true;
}
bool WalletManagerImpl::checkPayment(const std::string &address_text, const std::string &txid_text, const std::string &txkey_text, const std::string &daemon_address, uint64_t &received, uint64_t &height, std::string &error) const
{
error = "";
cryptonote::blobdata txid_data;
if(!epee::string_tools::parse_hexstr_to_binbuff(txid_text, txid_data) || txid_data.size() != sizeof(crypto::hash))
{
error = tr("failed to parse txid");
return false;
}
crypto::hash txid = *reinterpret_cast<const crypto::hash*>(txid_data.data());
if (txkey_text.size() < 64 || txkey_text.size() % 64)
{
error = tr("failed to parse tx key");
return false;
}
crypto::secret_key tx_key;
cryptonote::blobdata tx_key_data;
if(!epee::string_tools::parse_hexstr_to_binbuff(txkey_text, tx_key_data) || tx_key_data.size() != sizeof(crypto::hash))
{
error = tr("failed to parse tx key");
return false;
}
tx_key = *reinterpret_cast<const crypto::secret_key*>(tx_key_data.data());
bool testnet = address_text[0] != '4';
cryptonote::address_parse_info info;
if(!cryptonote::get_account_address_from_str(info, testnet, address_text))
{
error = tr("failed to parse address");
return false;
}
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res;
req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid));
if (!connect_and_invoke(m_daemonAddress, "/gettransactions", req, res) ||
(res.txs.size() != 1 && res.txs_as_hex.size() != 1))
{
error = tr("failed to get transaction from daemon");
return false;
}
cryptonote::blobdata tx_data;
bool ok;
if (res.txs.size() == 1)
ok = epee::string_tools::parse_hexstr_to_binbuff(res.txs.front().as_hex, tx_data);
else
ok = epee::string_tools::parse_hexstr_to_binbuff(res.txs_as_hex.front(), tx_data);
if (!ok)
{
error = tr("failed to parse transaction from daemon");
return false;
}
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))
{
error = tr("failed to validate transaction from daemon");
return false;
}
if (tx_hash != txid)
{
error = tr("failed to get the right transaction from daemon");
return false;
}
crypto::key_derivation derivation;
if (!crypto::generate_key_derivation(info.address.m_view_public_key, tx_key, derivation))
{
error = tr("failed to generate key derivation from supplied parameters");
return false;
}
received = 0;
try {
for (size_t n = 0; n < tx.vout.size(); ++n)
{
if (typeid(cryptonote::txout_to_key) != tx.vout[n].target.type())
continue;
const cryptonote::txout_to_key tx_out_to_key = boost::get<cryptonote::txout_to_key>(tx.vout[n].target);
crypto::public_key pubkey;
derive_public_key(derivation, n, info.address.m_spend_public_key, pubkey);
if (pubkey == tx_out_to_key.key)
{
uint64_t amount;
if (tx.version == 1)
{
amount = tx.vout[n].amount;
}
else
{
try
{
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(info.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);
rct::ecdhTuple ecdh_info = tx.rct_signatures.ecdhInfo[n];
rct::ecdhDecode(ecdh_info, rct::sk2rct(scalar1));
rct::key C = tx.rct_signatures.outPk[n].mask;
rct::addKeys2(Ctmp, ecdh_info.mask, ecdh_info.amount, rct::H);
if (rct::equalKeys(C, Ctmp))
amount = rct::h2d(ecdh_info.amount);
else
amount = 0;
}
}
catch (...) { amount = 0; }
}
received += amount;
}
}
}
catch(const std::exception &e)
{
LOG_ERROR("error: " << e.what());
error = std::string(tr("error: ")) + e.what();
return false;
}
if (received > 0)
{
LOG_PRINT_L1(get_account_address_as_str(testnet, info.is_subaddress, info.address) << " " << tr("received") << " " << cryptonote::print_money(received) << " " << tr("in txid") << " " << txid);
}
else
{
LOG_PRINT_L1(get_account_address_as_str(testnet, info.is_subaddress, info.address) << " " << tr("received nothing in txid") << " " << txid);
}
if (res.txs.front().in_pool)
{
height = 0;
}
else
{
height = res.txs.front().block_height;
}
return true;
}
uint64_t WalletManagerImpl::blockchainHeight() const
{
cryptonote::COMMAND_RPC_GET_INFO::request ireq;

View file

@ -55,7 +55,6 @@ public:
std::string errorString() const;
void setDaemonAddress(const std::string &address);
bool connected(uint32_t *version = NULL) const;
bool checkPayment(const std::string &address, const std::string &txid, const std::string &txkey, const std::string &daemon_address, uint64_t &received, uint64_t &height, std::string &error) const;
uint64_t blockchainHeight() const;
uint64_t blockchainTargetHeight() const;
uint64_t networkDifficulty() const;