diff options
author | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2016-04-23 21:46:48 +0100 |
---|---|---|
committer | moneromooo-monero <moneromooo-monero@users.noreply.github.com> | 2016-07-19 20:39:03 +0100 |
commit | 89d9f382a03235338bae65661e66d2d5ffa736d5 (patch) | |
tree | a375ca47ca2cf9f03cfb5dd064f3d6090efc6b13 /src/wallet/wallet2.cpp | |
parent | Merge pull request #889 (diff) | |
download | monero-89d9f382a03235338bae65661e66d2d5ffa736d5.tar.xz |
wallet: add command and RPC to sign/verify data
Signing is done using the spend key, since the view key may
be shared. This could be extended later, to let the user choose
which key (even a per tx key).
simplewallet's sign/verify API uses a file. The RPC uses a
string (simplewallet can't easily do strings since commands
receive a tokenized set of arguments).
Diffstat (limited to 'src/wallet/wallet2.cpp')
-rw-r--r-- | src/wallet/wallet2.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index a153967ce..6d676c6bd 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -53,6 +53,7 @@ using namespace epee; #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" #include "common/json_util.h" +#include "common/base58.h" extern "C" { @@ -3106,6 +3107,40 @@ std::string wallet2::get_tx_note(const crypto::hash &txid) const return std::string(); return i->second; } + +std::string wallet2::sign(const std::string &data) const +{ + crypto::hash hash; + crypto::cn_fast_hash(data.data(), data.size(), hash); + const cryptonote::account_keys &keys = m_account.get_keys(); + crypto::signature signature; + crypto::generate_signature(hash, keys.m_account_address.m_spend_public_key, keys.m_spend_secret_key, signature); + return std::string("SigV1") + tools::base58::encode(std::string((const char *)&signature, sizeof(signature))); +} + +bool wallet2::verify(const std::string &data, const cryptonote::account_public_address &address, const std::string &signature) const +{ + const size_t header_len = strlen("SigV1"); + if (signature.size() < header_len || signature.substr(0, header_len) != "SigV1") { + LOG_PRINT_L0("Signature header check error"); + return false; + } + crypto::hash hash; + crypto::cn_fast_hash(data.data(), data.size(), hash); + std::string decoded; + if (!tools::base58::decode(signature.substr(header_len), decoded)) { + LOG_PRINT_L0("Signature decoding error"); + return false; + } + crypto::signature s; + if (sizeof(s) != decoded.size()) { + LOG_PRINT_L0("Signature decoding error"); + return false; + } + memcpy(&s, decoded.data(), sizeof(s)); + return crypto::check_signature(hash, address.m_spend_public_key, s); +} + //---------------------------------------------------------------------------------------------------- void wallet2::generate_genesis(cryptonote::block& b) { if (m_testnet) |