From a1ac92e185c49c5db0956bf4506f910fac5024e7 Mon Sep 17 00:00:00 2001 From: Oran Juice Date: Thu, 25 Sep 2014 18:04:30 +0530 Subject: [PATCH] Accepts seed language choice from user. --- src/mnemonics/electrum-words.cpp | 19 +++++++++++++++- src/mnemonics/electrum-words.h | 3 ++- src/simplewallet/simplewallet.cpp | 38 +++++++++++++++++++++++++++++++ src/simplewallet/simplewallet.h | 1 + 4 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/mnemonics/electrum-words.cpp b/src/mnemonics/electrum-words.cpp index 3d79ecf6e..756306014 100644 --- a/src/mnemonics/electrum-words.cpp +++ b/src/mnemonics/electrum-words.cpp @@ -42,6 +42,7 @@ #include #include "mnemonics/electrum-words.h" #include +#include namespace { @@ -82,7 +83,7 @@ namespace crypto namespace ElectrumWords { - void init(const std::string &language, bool old_word_list = false) + void init(const std::string &language, bool old_word_list) { if (old_word_list) { @@ -188,6 +189,22 @@ namespace crypto return false; } + void get_language_list(std::vector &languages) + { + languages.clear(); + boost::filesystem::path languages_directory("wordlists/languages"); + if (!boost::filesystem::exists(languages_directory) || + !boost::filesystem::is_directory(languages_directory)) + { + throw std::runtime_error("Word list languages directory is missing."); + } + boost::filesystem::directory_iterator end; + for (boost::filesystem::directory_iterator it(languages_directory); it != end; it++) + { + languages.push_back(it->path().filename().string()); + } + } + } // namespace ElectrumWords } // namespace crypto diff --git a/src/mnemonics/electrum-words.h b/src/mnemonics/electrum-words.h index 0ae01b6a6..4a3cdeb04 100644 --- a/src/mnemonics/electrum-words.h +++ b/src/mnemonics/electrum-words.h @@ -41,8 +41,9 @@ namespace crypto { namespace ElectrumWords { - void init(const std::string &language, bool old_word_list); + void init(const std::string &language, bool old_word_list=false); bool words_to_bytes(const std::string& words, crypto::secret_key& dst); bool bytes_to_words(const crypto::secret_key& src, std::string& words); + void get_language_list(std::vector &languages); } } diff --git a/src/simplewallet/simplewallet.cpp b/src/simplewallet/simplewallet.cpp index 7c1246f32..c9f4d6ec8 100644 --- a/src/simplewallet/simplewallet.cpp +++ b/src/simplewallet/simplewallet.cpp @@ -430,6 +430,39 @@ bool simple_wallet::try_connect_to_daemon() return true; } +std::string simple_wallet::get_mnemonic_language() +{ + std::vector language_list; + std::string language_choice; + int language_number = -1; + crypto::ElectrumWords::get_language_list(language_list); + std::cout << "List of available languages for your wallet's seed:" << std::endl; + int ii; + std::vector::iterator it; + for (it = language_list.begin(), ii = 0; it != language_list.end(); it++, ii++) + { + std::cout << ii << " : " << *it << std::endl; + } + while (language_number < 0) + { + language_choice = command_line::input_line("Enter the number corresponding to the language of your choice: "); + try + { + language_number = std::stoi(language_choice); + if (!((language_number >= 0) && (static_cast(language_number) < language_list.size()))) + { + language_number = -1; + fail_msg_writer() << "Invalid language choice passed. Please try again.\n"; + } + } + catch (std::exception &e) + { + fail_msg_writer() << "Invalid language choice passed. Please try again.\n"; + } + } + return language_list[language_number]; +} + //---------------------------------------------------------------------------------------------------- bool simple_wallet::new_wallet(const string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key, bool recover, bool two_random, bool testnet) { @@ -456,6 +489,11 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas // convert rng value to electrum-style word list std::string electrum_words; + std::string mnemonic_language = get_mnemonic_language(); + + std::cout << "(" << (mnemonic_language) << ")" << std::endl; + + crypto::ElectrumWords::init(mnemonic_language); crypto::ElectrumWords::bytes_to_words(recovery_val, electrum_words); std::string print_electrum = ""; diff --git a/src/simplewallet/simplewallet.h b/src/simplewallet/simplewallet.h index 17affd564..7a21f1cd2 100644 --- a/src/simplewallet/simplewallet.h +++ b/src/simplewallet/simplewallet.h @@ -92,6 +92,7 @@ namespace cryptonote uint64_t get_daemon_blockchain_height(std::string& err); bool try_connect_to_daemon(); bool ask_wallet_create_if_needed(); + std::string get_mnemonic_language(); //----------------- i_wallet2_callback --------------------- virtual void on_new_block(uint64_t height, const cryptonote::block& block);