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/simplewallet/simplewallet.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 4cb9cff21..791309f3b 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -509,8 +509,8 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string // convert rng value to electrum-style word list std::string electrum_words; - bool was_deprecated_wallet = (old_language == crypto::ElectrumWords::old_language_name) || - crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed); + bool was_deprecated_wallet = m_restore_deterministic_wallet && ((old_language == crypto::ElectrumWords::old_language_name) || + crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed)); std::string mnemonic_language = old_language; // Ask for seed language if it is not a wallet restore or if it was a deprecated wallet -- 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/simplewallet/simplewallet.cpp | 45 ++++++++++++++++++++++++--------------- src/wallet/wallet2.cpp | 13 +++++++---- src/wallet/wallet2.h | 2 +- 3 files changed, 38 insertions(+), 22 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 791309f3b..bb9049015 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -485,10 +485,28 @@ std::string simple_wallet::get_mnemonic_language() bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key, bool recover, bool two_random, bool testnet, const std::string &old_language) { + bool was_deprecated_wallet = m_restore_deterministic_wallet && ((old_language == crypto::ElectrumWords::old_language_name) || + crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed)); + + std::string mnemonic_language = old_language; + // Ask for seed language if it is not a wallet restore or if it was a deprecated wallet + // that was earlier used before this restore. + if (!m_restore_deterministic_wallet || was_deprecated_wallet) + { + if (was_deprecated_wallet) + { + // The user had used an older version of the wallet with old style mnemonics. + message_writer(epee::log_space::console_color_green, false) << "\nYou had been using " << + "a deprecated version of the wallet. Please use the new seed that we provide.\n"; + } + mnemonic_language = get_mnemonic_language(); + } + m_wallet_file = wallet_file; m_wallet.reset(new tools::wallet2(testnet)); m_wallet->callback(this); + m_wallet->set_seed_language(mnemonic_language); crypto::secret_key recovery_val; try @@ -509,24 +527,7 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string // convert rng value to electrum-style word list std::string electrum_words; - bool was_deprecated_wallet = m_restore_deterministic_wallet && ((old_language == crypto::ElectrumWords::old_language_name) || - crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed)); - - std::string mnemonic_language = old_language; - // Ask for seed language if it is not a wallet restore or if it was a deprecated wallet - // that was earlier used before this restore. - if (!m_restore_deterministic_wallet || was_deprecated_wallet) - { - if (was_deprecated_wallet) - { - // The user had used an older version of the wallet with old style mnemonics. - message_writer(epee::log_space::console_color_green, false) << "\nYou had been using " << - "a deprecated version of the wallet. Please use the new seed that we provide.\n"; - } - mnemonic_language = get_mnemonic_language(); - } crypto::ElectrumWords::bytes_to_words(recovery_val, electrum_words, mnemonic_language); - m_wallet->set_seed_language(mnemonic_language); std::string print_electrum = ""; @@ -564,6 +565,16 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa m_wallet->load(m_wallet_file, password); message_writer(epee::log_space::console_color_white, true) << "Opened wallet: " << m_wallet->get_account().get_public_address_str(m_wallet->testnet()); + // If the wallet file is deprecated, we should ask for mnemonic language again and store + // everything in the new format. + if (!m_non_deterministic && m_wallet->is_deprecated()) + { + message_writer(epee::log_space::console_color_green, false) << "\nYou had been using " << + "a deprecated version of the wallet. Please proceed to upgrade your wallet.\n"; + std::string mnemonic_language = get_mnemonic_language(); + m_wallet->set_seed_language(mnemonic_language); + m_wallet->generate(m_wallet_file, password, m_recovery_key, false, true); + } } catch (const std::exception& e) { 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 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/simplewallet/simplewallet.cpp | 2 +- src/wallet/wallet2.cpp | 14 +++++++++++++- src/wallet/wallet2.h | 5 ++++- 3 files changed, 18 insertions(+), 3 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index bb9049015..d3b61c631 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -573,7 +573,7 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa "a deprecated version of the wallet. Please proceed to upgrade your wallet.\n"; std::string mnemonic_language = get_mnemonic_language(); m_wallet->set_seed_language(mnemonic_language); - m_wallet->generate(m_wallet_file, password, m_recovery_key, false, true); + m_wallet->rewrite(m_wallet_file, password); } } catch (const std::exception& e) 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 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/simplewallet/simplewallet.cpp | 25 +++++++++++++++++++++---- src/simplewallet/simplewallet.h | 5 +++++ src/wallet/wallet2.cpp | 2 -- 3 files changed, 26 insertions(+), 6 deletions(-) (limited to 'src/simplewallet/simplewallet.cpp') diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index d3b61c631..68f5cba06 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -317,6 +317,21 @@ bool simple_wallet::ask_wallet_create_if_needed() return r; } + +/*! + * \brief Prints the seed with a nice message + * \param seed seed to print + */ +void simple_wallet::print_seed(std::string seed) +{ + success_msg_writer(true) << "\nPLEASE NOTE: the following 25 words can be used to recover access to your wallet. " << + "Please write them down and store them somewhere safe and secure. Please do not store them in " << + "your email or on file storage services outside of your immediate control.\n"; + boost::replace_nth(seed, " ", 15, "\n"); + boost::replace_nth(seed, " ", 7, "\n"); + std::cout << seed << std::endl; +} + //---------------------------------------------------------------------------------------------------- bool simple_wallet::init(const boost::program_options::variables_map& vm) { @@ -544,10 +559,7 @@ bool simple_wallet::new_wallet(const std::string &wallet_file, const std::string if (!two_random) { - success_msg_writer(true) << "\nPLEASE NOTE: the following 25 words can be used to recover access to your wallet. Please write them down and store them somewhere safe and secure. Please do not store them in your email or on file storage services outside of your immediate control.\n"; - boost::replace_nth(electrum_words, " ", 15, "\n"); - boost::replace_nth(electrum_words, " ", 7, "\n"); - std::cout << electrum_words << std::endl; + print_seed(electrum_words); } success_msg_writer() << "**********************************************************************"; @@ -574,6 +586,11 @@ bool simple_wallet::open_wallet(const string &wallet_file, const std::string& pa std::string mnemonic_language = get_mnemonic_language(); m_wallet->set_seed_language(mnemonic_language); m_wallet->rewrite(m_wallet_file, password); + + // Display the seed + std::string seed; + m_wallet->get_seed(seed); + print_seed(seed); } } catch (const std::exception& e) diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index af6d4172a..ebc85ed17 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -101,6 +101,11 @@ namespace cryptonote uint64_t get_daemon_blockchain_height(std::string& err); bool try_connect_to_daemon(); bool ask_wallet_create_if_needed(); + /*! + * \brief Prints the seed with a nice message + * \param seed seed to print + */ + void print_seed(std::string seed); /*! * \brief Gets the word seed language from the user. 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