From 09a659edb5f77ce1879338545f388076881b4e06 Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sat, 18 Oct 2014 02:21:37 +0530 Subject: Stores seed language in wallet file. added rapidjson. Yet to test backward compatibility --- src/wallet/wallet2.cpp | 40 +++++++++++++++++++++++++++++++++++++++- src/wallet/wallet2.h | 7 ++++++- 2 files changed, 45 insertions(+), 2 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 5af9a71bd..5f5197891 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -48,6 +48,11 @@ using namespace epee; #include "cryptonote_protocol/blobdatatype.h" #include "mnemonics/electrum-words.h" #include "common/dns_utils.h" +#include "rapidjson/document.h" +#include "rapidjson/writer.h" +#include "rapidjson/stringbuffer.h" + +#include extern "C" { @@ -104,6 +109,13 @@ void wallet2::set_seed_language(const std::string &language) { seed_language = language; } +/*! + * \brief Tells if the wallet file is deprecated. + */ +bool wallet2::is_deprecated() const +{ + return is_old_file_format; +} //---------------------------------------------------------------------------------------------------- void wallet2::process_new_transaction(const cryptonote::transaction& tx, uint64_t height) { @@ -440,6 +452,18 @@ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& p CHECK_AND_ASSERT_MES(r, false, "failed to serialize wallet keys"); wallet2::keys_file_data keys_file_data = boost::value_initialized(); + rapidjson::Document json; + json.SetObject(); + rapidjson::Value value(rapidjson::kStringType); + value.SetString(account_data.c_str(), account_data.length()); + json.AddMember("key_data", value, json.GetAllocator()); + value.SetString(seed_language.c_str(), seed_language.length()); + json.AddMember("seed_language", value, json.GetAllocator()); + rapidjson::StringBuffer buffer; + rapidjson::Writer writer(buffer); + json.Accept(writer); + + account_data = buffer.GetString(); crypto::chacha8_key key; crypto::generate_chacha8_key(password, key); std::string cipher; @@ -472,15 +496,29 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa std::string buf; bool r = epee::file_io_utils::load_file_to_string(keys_file_name, buf); THROW_WALLET_EXCEPTION_IF(!r, error::file_read_error, keys_file_name); + r = ::serialization::parse_binary(buf, keys_file_data); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "internal error: failed to deserialize \"" + keys_file_name + '\"'); - crypto::chacha8_key key; crypto::generate_chacha8_key(password, key); std::string account_data; account_data.resize(keys_file_data.account_data.size()); crypto::chacha8(keys_file_data.account_data.data(), keys_file_data.account_data.size(), key, keys_file_data.iv, &account_data[0]); + rapidjson::Document json; + if (json.Parse(account_data.c_str(), keys_file_data.account_data.size()).HasParseError()) + { + is_old_file_format = true; + } + else + { + account_data = std::string(json["key_data"].GetString(), json["key_data"].GetString() + + json["key_data"].GetStringLength()); + std::cout << "A/C " << json["key_data"].GetStringLength() << std::endl; + seed_language = std::string(json["seed_language"].GetString(), json["seed_language"].GetString() + + json["seed_language"].GetStringLength()); + } + const cryptonote::account_keys& keys = m_account.get_keys(); r = epee::serialization::load_t_from_binary(m_account, account_data); r = r && verify_keys(keys.m_view_secret_key, keys.m_account_address.m_view_public_key); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index afa42c2d3..35e02ce28 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -155,7 +155,11 @@ namespace tools /*! * \brief Sets the seed language */ - void set_seed_language(const std::string &language); + void set_seed_language(const std::string &language); + /*! + * \brief Tells if the wallet file is deprecated. + */ + bool is_deprecated() const; void refresh(); void refresh(uint64_t start_height, size_t & blocks_fetched); void refresh(uint64_t start_height, size_t & blocks_fetched, bool& received_money); @@ -240,6 +244,7 @@ namespace tools i_wallet2_callback* m_callback; bool m_testnet; std::string seed_language; + bool is_old_file_format; }; } BOOST_CLASS_VERSION(tools::wallet2, 7) -- cgit v1.2.3 From 0bd88ff256e18e13235eaafff388dad99845e4b1 Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sat, 18 Oct 2014 23:01:43 +0530 Subject: Writes seed language while generating wallet. Wallet open fix. --- src/wallet/wallet2.cpp | 13 +++++++++---- src/wallet/wallet2.h | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 5f5197891..1ac3805a7 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -452,6 +452,7 @@ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& p CHECK_AND_ASSERT_MES(r, false, "failed to serialize wallet keys"); wallet2::keys_file_data keys_file_data = boost::value_initialized(); + // Create a JSON object with "key_data" and "seed_language" as keys. rapidjson::Document json; json.SetObject(); rapidjson::Value value(rapidjson::kStringType); @@ -459,11 +460,14 @@ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& p json.AddMember("key_data", value, json.GetAllocator()); value.SetString(seed_language.c_str(), seed_language.length()); json.AddMember("seed_language", value, json.GetAllocator()); + + // Serialize the JSON object rapidjson::StringBuffer buffer; rapidjson::Writer writer(buffer); json.Accept(writer); - account_data = buffer.GetString(); + + // Encrypt the entire JSON object. crypto::chacha8_key key; crypto::generate_chacha8_key(password, key); std::string cipher; @@ -497,6 +501,7 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa bool r = epee::file_io_utils::load_file_to_string(keys_file_name, buf); THROW_WALLET_EXCEPTION_IF(!r, error::file_read_error, keys_file_name); + // Decrypt the contents r = ::serialization::parse_binary(buf, keys_file_data); THROW_WALLET_EXCEPTION_IF(!r, error::wallet_internal_error, "internal error: failed to deserialize \"" + keys_file_name + '\"'); crypto::chacha8_key key; @@ -505,6 +510,7 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa account_data.resize(keys_file_data.account_data.size()); crypto::chacha8(keys_file_data.account_data.data(), keys_file_data.account_data.size(), key, keys_file_data.iv, &account_data[0]); + // The contents should be JSON if the wallet follows the new format. rapidjson::Document json; if (json.Parse(account_data.c_str(), keys_file_data.account_data.size()).HasParseError()) { @@ -514,9 +520,8 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa { account_data = std::string(json["key_data"].GetString(), json["key_data"].GetString() + json["key_data"].GetStringLength()); - std::cout << "A/C " << json["key_data"].GetStringLength() << std::endl; - seed_language = std::string(json["seed_language"].GetString(), json["seed_language"].GetString() + - json["seed_language"].GetStringLength()); + set_seed_language(std::string(json["seed_language"].GetString(), json["seed_language"].GetString() + + json["seed_language"].GetStringLength())); } const cryptonote::account_keys& keys = m_account.get_keys(); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 35e02ce28..10b4d6f3f 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -82,7 +82,7 @@ namespace tools { wallet2(const wallet2&) : m_run(true), m_callback(0), m_testnet(false) {}; public: - wallet2(bool testnet = false) : m_run(true), m_callback(0), m_testnet(testnet) {}; + wallet2(bool testnet = false) : m_run(true), m_callback(0), m_testnet(testnet), is_old_file_format(false) {}; struct transfer_details { uint64_t m_block_height; -- cgit v1.2.3 From 1f833dcf7760094358c3037b4af6a263c3ef236d Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sat, 18 Oct 2014 23:11:05 +0530 Subject: Doxygen comments in --- src/wallet/wallet2.cpp | 27 ++++++++++++++++++++++++--- src/wallet/wallet2.h | 15 +++++++++++++-- 2 files changed, 37 insertions(+), 5 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 1ac3805a7..013dcbcb1 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -104,6 +104,7 @@ bool wallet2::get_seed(std::string& electrum_words) } /*! * \brief Sets the seed language + * \param language Seed language to set to */ void wallet2::set_seed_language(const std::string &language) { @@ -444,7 +445,13 @@ bool wallet2::clear() m_local_bc_height = 1; return true; } -//---------------------------------------------------------------------------------------------------- + +/*! + * Stores wallet information to wallet file. + * @param keys_file_name Name of wallet file + * @param password Password of wallet file + * @return Whether it was successful. + */ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& password) { std::string account_data; @@ -493,7 +500,12 @@ namespace return r && expected_pub == pub; } } -//---------------------------------------------------------------------------------------------------- + +/*! + * Load wallet information from wallet file. + * @param keys_file_name Name of wallet file + * @param password Password of wallet file + */ void wallet2::load_keys(const std::string& keys_file_name, const std::string& password) { wallet2::keys_file_data keys_file_data; @@ -530,7 +542,16 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa r = r && verify_keys(keys.m_spend_secret_key, keys.m_account_address.m_spend_public_key); THROW_WALLET_EXCEPTION_IF(!r, error::invalid_password); } -//---------------------------------------------------------------------------------------------------- + +/*! + * Generates a wallet or restores one. + * @param wallet_ Name of wallet file + * @param password Password of wallet file + * @param recovery_param If it is a restore, the recovery key + * @param recover Whether it is a restore + * @param two_random Whether it is a non-deterministic wallet + * @return The secret key of the generated wallet + */ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::string& password, const crypto::secret_key& recovery_param, bool recover, bool two_random) { clear(); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 10b4d6f3f..c3dfa4df6 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -207,7 +207,18 @@ namespace tools static std::string address_from_txt_record(const std::string& s); private: + /*! + * Stores wallet information to wallet file. + * @param keys_file_name Name of wallet file + * @param password Password of wallet file + * @return Whether it was successful. + */ bool store_keys(const std::string& keys_file_name, const std::string& password); + /*! + * Load wallet information from wallet file. + * @param keys_file_name Name of wallet file + * @param password Password of wallet file + */ void load_keys(const std::string& keys_file_name, const std::string& password); void process_new_transaction(const cryptonote::transaction& tx, uint64_t height); void process_new_blockchain_entry(const cryptonote::block& b, cryptonote::block_complete_entry& bche, crypto::hash& bl_id, uint64_t height); @@ -243,8 +254,8 @@ namespace tools i_wallet2_callback* m_callback; bool m_testnet; - std::string seed_language; - bool is_old_file_format; + std::string seed_language; /*!< Language of the mnemonics (seed). */ + bool is_old_file_format; /*!< Whether the wallet file is of an old file format */ }; } BOOST_CLASS_VERSION(tools::wallet2, 7) -- cgit v1.2.3 From 031ca23ce9adcdb802097f93017e4acab87d4b37 Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sun, 19 Oct 2014 01:00:18 +0530 Subject: Rewrites to old wallet file correctly --- src/wallet/wallet2.cpp | 14 +++++++++++++- src/wallet/wallet2.h | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index 013dcbcb1..b4d8068fa 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -552,7 +552,8 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa * @param two_random Whether it is a non-deterministic wallet * @return The secret key of the generated wallet */ -crypto::secret_key wallet2::generate(const std::string& wallet_, const std::string& password, const crypto::secret_key& recovery_param, bool recover, bool two_random) +crypto::secret_key wallet2::generate(const std::string& wallet_, const std::string& password, + const crypto::secret_key& recovery_param, bool recover, bool two_random) { clear(); prepare_file_names(wallet_); @@ -578,6 +579,17 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri store(); return retval; } + +void wallet2::rewrite(const std::string& wallet_name, const std::string& password) +{ + prepare_file_names(wallet_name); + boost::system::error_code ignored_ec; + THROW_WALLET_EXCEPTION_IF(!boost::filesystem::exists(m_wallet_file, ignored_ec), error::file_not_found, m_wallet_file); + THROW_WALLET_EXCEPTION_IF(!boost::filesystem::exists(m_keys_file, ignored_ec), error::file_not_found, m_keys_file); + bool r = store_keys(m_keys_file, password); + THROW_WALLET_EXCEPTION_IF(!r, error::file_save_error, m_keys_file); + store(); +} //---------------------------------------------------------------------------------------------------- void wallet2::wallet_exists(const std::string& file_path, bool& keys_file_exists, bool& wallet_file_exists) { diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index c3dfa4df6..90b0ec331 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -133,7 +133,10 @@ namespace tools END_SERIALIZE() }; - crypto::secret_key generate(const std::string& wallet, const std::string& password, const crypto::secret_key& recovery_param = crypto::secret_key(), bool recover = false, bool two_random = false); + crypto::secret_key generate(const std::string& wallet, const std::string& password, + const crypto::secret_key& recovery_param = crypto::secret_key(), bool recover = false, + bool two_random = false); + void rewrite(const std::string& wallet_name, const std::string& password); void load(const std::string& wallet, const std::string& password); void store(); cryptonote::account_base& get_account(){return m_account;} -- cgit v1.2.3 From 70971be96bce4ac7efad7160ac904161a45f5b9b Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sun, 19 Oct 2014 01:08:21 +0530 Subject: Doxygen comments --- src/wallet/wallet2.cpp | 33 +++++++++++++++++++-------------- src/wallet/wallet2.h | 28 +++++++++++++++++++++------- 2 files changed, 40 insertions(+), 21 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index b4d8068fa..aece1e9d6 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -447,10 +447,10 @@ bool wallet2::clear() } /*! - * Stores wallet information to wallet file. - * @param keys_file_name Name of wallet file - * @param password Password of wallet file - * @return Whether it was successful. + * \brief Stores wallet information to wallet file. + * \param keys_file_name Name of wallet file + * \param password Password of wallet file + * \return Whether it was successful. */ bool wallet2::store_keys(const std::string& keys_file_name, const std::string& password) { @@ -502,9 +502,9 @@ namespace } /*! - * Load wallet information from wallet file. - * @param keys_file_name Name of wallet file - * @param password Password of wallet file + * \brief Load wallet information from wallet file. + * \param keys_file_name Name of wallet file + * \param password Password of wallet file */ void wallet2::load_keys(const std::string& keys_file_name, const std::string& password) { @@ -544,13 +544,13 @@ void wallet2::load_keys(const std::string& keys_file_name, const std::string& pa } /*! - * Generates a wallet or restores one. - * @param wallet_ Name of wallet file - * @param password Password of wallet file - * @param recovery_param If it is a restore, the recovery key - * @param recover Whether it is a restore - * @param two_random Whether it is a non-deterministic wallet - * @return The secret key of the generated wallet + * \brief Generates a wallet or restores one. + * \param wallet_ Name of wallet file + * \param password Password of wallet file + * \param recovery_param If it is a restore, the recovery key + * \param recover Whether it is a restore + * \param two_random Whether it is a non-deterministic wallet + * \return The secret key of the generated wallet */ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::string& password, const crypto::secret_key& recovery_param, bool recover, bool two_random) @@ -580,6 +580,11 @@ crypto::secret_key wallet2::generate(const std::string& wallet_, const std::stri return retval; } +/*! + * \brief Rewrites to the wallet file for wallet upgrade (doesn't generate key, assumes it's already there) + * \param wallet_name Name of wallet file (should exist) + * \param password Password for wallet file + */ void wallet2::rewrite(const std::string& wallet_name, const std::string& password) { prepare_file_names(wallet_name); diff --git a/src/wallet/wallet2.h b/src/wallet/wallet2.h index 90b0ec331..d7693e10d 100644 --- a/src/wallet/wallet2.h +++ b/src/wallet/wallet2.h @@ -133,9 +133,23 @@ namespace tools END_SERIALIZE() }; + /*! + * \brief Generates a wallet or restores one. + * \param wallet_ Name of wallet file + * \param password Password of wallet file + * \param recovery_param If it is a restore, the recovery key + * \param recover Whether it is a restore + * \param two_random Whether it is a non-deterministic wallet + * \return The secret key of the generated wallet + */ crypto::secret_key generate(const std::string& wallet, const std::string& password, const crypto::secret_key& recovery_param = crypto::secret_key(), bool recover = false, bool two_random = false); + /*! + * \brief Rewrites to the wallet file for wallet upgrade (doesn't generate key, assumes it's already there) + * \param wallet_name Name of wallet file (should exist) + * \param password Password for wallet file + */ void rewrite(const std::string& wallet_name, const std::string& password); void load(const std::string& wallet, const std::string& password); void store(); @@ -211,16 +225,16 @@ namespace tools static std::string address_from_txt_record(const std::string& s); private: /*! - * Stores wallet information to wallet file. - * @param keys_file_name Name of wallet file - * @param password Password of wallet file - * @return Whether it was successful. + * \brief Stores wallet information to wallet file. + * \param keys_file_name Name of wallet file + * \param password Password of wallet file + * \return Whether it was successful. */ bool store_keys(const std::string& keys_file_name, const std::string& password); /*! - * Load wallet information from wallet file. - * @param keys_file_name Name of wallet file - * @param password Password of wallet file + * \brief Load wallet information from wallet file. + * \param keys_file_name Name of wallet file + * \param password Password of wallet file */ void load_keys(const std::string& keys_file_name, const std::string& password); void process_new_transaction(const cryptonote::transaction& tx, uint64_t height); -- cgit v1.2.3 From f1eaf88ba691fc37c07b664af9ebb6fb342b4c23 Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Sun, 19 Oct 2014 14:39:45 +0530 Subject: Prints seed after wallet upgrade. Removed iostream include. --- src/wallet/wallet2.cpp | 2 -- 1 file changed, 2 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/wallet2.cpp b/src/wallet/wallet2.cpp index aece1e9d6..a28eaaa7b 100644 --- a/src/wallet/wallet2.cpp +++ b/src/wallet/wallet2.cpp @@ -52,8 +52,6 @@ using namespace epee; #include "rapidjson/writer.h" #include "rapidjson/stringbuffer.h" -#include - extern "C" { #include "crypto/keccak.h" -- cgit v1.2.3