From f4e894a526e9865772acedade803c4ebfe3db21d Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 1 Sep 2016 11:04:53 +0100 Subject: [PATCH 1/2] simplewallet: make the key image export format binary Saves on space, and on some pointless hex conversions --- src/simplewallet/simplewallet.cpp | 23 +++++------------------ 1 file changed, 5 insertions(+), 18 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 44b5b0c70..db9411313 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -3586,8 +3586,8 @@ bool simple_wallet::export_key_images(const std::vector &args) std::string data; for (const auto &i: ski) { - data += epee::string_tools::pod_to_hex(i.first); - data += epee::string_tools::pod_to_hex(i.second); + data += std::string((const char *)&i.first, sizeof(crypto::key_image)); + data += std::string((const char *)&i.second, sizeof(crypto::signature)); } bool r = epee::file_io_utils::save_string_to_file(filename, data); if (!r) @@ -3624,7 +3624,7 @@ bool simple_wallet::import_key_images(const std::vector &args) return true; } - const size_t record_size = sizeof(crypto::key_image)*2 + sizeof(crypto::signature)*2; + const size_t record_size = sizeof(crypto::key_image) + sizeof(crypto::signature); if (data.size() % record_size) { fail_msg_writer() << "Bad data size from file " << filename; @@ -3636,21 +3636,8 @@ bool simple_wallet::import_key_images(const std::vector &args) ski.reserve(nki); for (size_t n = 0; n < nki; ++n) { - cryptonote::blobdata bd; - - if(!epee::string_tools::parse_hexstr_to_binbuff(std::string(&data[n * record_size], sizeof(crypto::key_image)*2), bd)) - { - fail_msg_writer() << tr("failed to parse key image"); - return false; - } - crypto::key_image key_image = *reinterpret_cast(bd.data()); - - if(!epee::string_tools::parse_hexstr_to_binbuff(std::string(&data[n * record_size + sizeof(crypto::key_image)*2], sizeof(crypto::signature)*2), bd)) - { - fail_msg_writer() << tr("failed to parse signature"); - return false; - } - crypto::signature signature = *reinterpret_cast(bd.data()); + crypto::key_image key_image = *reinterpret_cast(&data[n * record_size]); + crypto::signature signature = *reinterpret_cast(&data[n * record_size + sizeof(crypto::key_image)]); ski.push_back(std::make_pair(key_image, signature)); } From f1d87c8064cb6cc3e314c6e6cc85a9efba4ce3b5 Mon Sep 17 00:00:00 2001 From: moneromooo-monero Date: Thu, 1 Sep 2016 11:32:44 +0100 Subject: [PATCH 2/2] simplewallet: add magic and public keys to key image export file This ensures it's hard to mix files up --- src/simplewallet/simplewallet.cpp | 39 +++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index db9411313..c1b0a2a95 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -77,6 +77,8 @@ typedef cryptonote::simple_wallet sw; #define DEFAULT_MIX 4 +#define KEY_IMAGE_EXPORT_FILE_MAGIC "Monero key image export\001" + // workaround for a suspected bug in pthread/kernel on MacOS X #ifdef __APPLE__ #define DEFAULT_MAX_CONCURRENCY 1 @@ -3583,7 +3585,10 @@ bool simple_wallet::export_key_images(const std::vector &args) try { std::vector> ski = m_wallet->export_key_images(); - std::string data; + std::string data(KEY_IMAGE_EXPORT_FILE_MAGIC, strlen(KEY_IMAGE_EXPORT_FILE_MAGIC)); + const cryptonote::account_public_address &keys = m_wallet->get_account().get_keys().m_account_address; + data += std::string((const char *)&keys.m_spend_public_key, sizeof(crypto::public_key)); + data += std::string((const char *)&keys.m_view_public_key, sizeof(crypto::public_key)); for (const auto &i: ski) { data += std::string((const char *)&i.first, sizeof(crypto::key_image)); @@ -3623,21 +3628,41 @@ bool simple_wallet::import_key_images(const std::vector &args) fail_msg_writer() << tr("failed to read file ") << filename; return true; } - - const size_t record_size = sizeof(crypto::key_image) + sizeof(crypto::signature); - if (data.size() % record_size) + const size_t magiclen = strlen(KEY_IMAGE_EXPORT_FILE_MAGIC); + if (data.size() < magiclen || memcmp(data.data(), KEY_IMAGE_EXPORT_FILE_MAGIC, magiclen)) + { + fail_msg_writer() << "Bad key image export file magic in " << filename; + return true; + } + const size_t headerlen = magiclen + 2 * sizeof(crypto::public_key); + if (data.size() < headerlen) { fail_msg_writer() << "Bad data size from file " << filename; return true; } - size_t nki = data.size() / record_size; + const crypto::public_key &public_spend_key = *(const crypto::public_key*)&data[magiclen]; + const crypto::public_key &public_view_key = *(const crypto::public_key*)&data[magiclen + sizeof(crypto::public_key)]; + const cryptonote::account_public_address &keys = m_wallet->get_account().get_keys().m_account_address; + if (public_spend_key != keys.m_spend_public_key || public_view_key != keys.m_view_public_key) + { + fail_msg_writer() << "Key images from " << filename << " are for a different account"; + return true; + } + + const size_t record_size = sizeof(crypto::key_image) + sizeof(crypto::signature); + if ((data.size() - headerlen) % record_size) + { + fail_msg_writer() << "Bad data size from file " << filename; + return true; + } + size_t nki = (data.size() - headerlen) / record_size; std::vector> ski; ski.reserve(nki); for (size_t n = 0; n < nki; ++n) { - crypto::key_image key_image = *reinterpret_cast(&data[n * record_size]); - crypto::signature signature = *reinterpret_cast(&data[n * record_size + sizeof(crypto::key_image)]); + crypto::key_image key_image = *reinterpret_cast(&data[headerlen + n * record_size]); + crypto::signature signature = *reinterpret_cast(&data[headerlen + n * record_size + sizeof(crypto::key_image)]); ski.push_back(std::make_pair(key_image, signature)); }