Restructured language sources to be singletons

This commit is contained in:
Oran Juice 2014-10-02 18:15:18 +05:30
parent 6c3b85de21
commit 4517bac7f3
No known key found for this signature in database
GPG key ID: 71C5AF46CCB28124
13 changed files with 9801 additions and 9878 deletions

View file

@ -41,6 +41,7 @@
#include <map> #include <map>
#include <cstdint> #include <cstdint>
#include <vector> #include <vector>
#include <unordered_map>
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include "crypto/crypto.h" // for declaration of crypto::secret_key #include "crypto/crypto.h" // for declaration of crypto::secret_key
#include <fstream> #include <fstream>
@ -55,98 +56,94 @@
#include "portuguese.h" #include "portuguese.h"
#include "japanese.h" #include "japanese.h"
#include "old_english.h" #include "old_english.h"
#include "language_base.h"
#include "singleton.h"
namespace namespace
{ {
int num_words = 0;
const int seed_length = 24; const int seed_length = 24;
std::map<std::string,uint32_t> words_map;
std::vector<std::string> words_array;
bool is_old_style_word_list = false;
const std::string WORD_LISTS_DIRECTORY = "wordlists";
const std::string LANGUAGES_DIRECTORY = "languages";
const std::string OLD_WORD_FILE = "old-word-list";
const int unique_prefix_length = 4;
/*! /*!
* \brief Tells if the module hasn't been initialized with a word list file. * \brief Finds the word list that contains the seed words and puts the indices
* \return true if the module hasn't been initialized with a word list file false otherwise. * where matches occured in matched_indices.
* \param seed List of words to match.
* \param has_checksum If word list passed checksum test, we need to only do a prefix check.
* \param matched_indices The indices where the seed words were found are added to this.
* \return true if all the words were present in some language false if not.
*/ */
bool is_uninitialized() bool find_seed_language(const std::vector<std::string> &seed,
bool has_checksum, std::vector<uint32_t> &matched_indices, uint32_t &word_list_length,
std::string &language_name)
{ {
return num_words == 0 ? true : false; // If there's a new language added, add an instance of it here.
} std::vector<Language::Base*> language_instances({
Language::Singleton<Language::English>::instance(),
/*! Language::Singleton<Language::Spanish>::instance(),
* \brief Create word list map and array data structres for use during inter-conversion between Language::Singleton<Language::Portuguese>::instance(),
* words and secret key. Language::Singleton<Language::Japanese>::instance(),
* \param word_file Path to the word list file from pwd. Language::Singleton<Language::OldEnglish>::instance()
* \param has_checksum True if checksum was supplied false if not. });
*/ // To hold trimmed seed words in case of a checksum being present.
void create_data_structures(const std::string &word_file, bool has_checksum) std::vector<std::string> trimmed_seed;
{
words_array.clear();
words_map.clear();
num_words = 0;
std::ifstream input_stream;
input_stream.open(word_file.c_str(), std::ifstream::in);
if (!input_stream)
throw std::runtime_error("Word list file couldn't be opened.");
std::string word;
while (input_stream >> word)
{
if (word.length() == 0 || word[0] == '#')
{
// Skip empty and comment lines
continue;
}
words_array.push_back(word);
if (has_checksum) if (has_checksum)
{ {
// Only if checksum was passed should we stick to just 4 char checks to be lenient about typos. // If it had a checksum, we'll just compare the unique prefix
words_map[word.substr(0, unique_prefix_length)] = num_words; // So we create a list of trimmed seed words
} for (std::vector<std::string>::const_iterator it = seed.begin(); it != seed.end(); it++)
else
{ {
words_map[word] = num_words; trimmed_seed.push_back(it->length() > Language::unique_prefix_length ?
it->substr(0, Language::unique_prefix_length) : *it);
} }
num_words++;
}
input_stream.close();
} }
std::unordered_map<std::string, uint32_t> word_map;
std::unordered_map<std::string, uint32_t> trimmed_word_map;
// Iterate through all the languages and find a match
for (std::vector<Language::Base*>::iterator it1 = language_instances.begin();
it1 != language_instances.end(); it1++)
{
word_map = (*it1)->get_word_map();
trimmed_word_map = (*it1)->get_trimmed_word_map();
// To iterate through seed words
std::vector<std::string>::const_iterator it2;
// To iterate through trimmed seed words
std::vector<std::string>::iterator it3;
bool full_match = true;
/*! // Iterate through all the words and see if they're all present
* \brief Tells if all the words passed in wlist was present in current word list file. for (it2 = seed.begin(), it3 = trimmed_seed.begin();
* \param wlist List of words to match. it2 != seed.end() && it3 != trimmed_seed.end(); it2++, it3++)
* \param has_checksum If word list passed checksum test, we need to only do a 4 char check.
* \return true if all the words were present false if not.
*/
bool word_list_file_match(const std::vector<std::string> &wlist, bool has_checksum)
{
for (std::vector<std::string>::const_iterator it = wlist.begin(); it != wlist.end(); it++)
{ {
if (has_checksum) if (has_checksum)
{ {
if (words_map.count(it->substr(0, unique_prefix_length)) == 0) // Use the trimmed words and map
if (trimmed_word_map.count(*it3) == 0)
{ {
return false; full_match = false;
break;
} }
matched_indices.push_back(trimmed_word_map[*it3]);
} }
else else
{ {
if (words_map.count(*it) == 0) if (word_map.count(*it2) == 0)
{ {
return false; full_match = false;
} break;
}
matched_indices.push_back(word_map[*it2]);
} }
} }
if (full_match)
{
word_list_length = (*it1)->get_word_list().size();
language_name = (*it1)->get_language_name();
return true; return true;
} }
// Some didn't match. Clear the index array.
matched_indices.clear();
}
return false;
}
/*! /*!
* \brief Creates a checksum index in the word list array on the list of words. * \brief Creates a checksum index in the word list array on the list of words.
@ -155,16 +152,43 @@ namespace
*/ */
uint32_t create_checksum_index(const std::vector<std::string> &word_list) uint32_t create_checksum_index(const std::vector<std::string> &word_list)
{ {
std::string four_char_words = ""; std::string trimmed_words = "";
for (std::vector<std::string>::const_iterator it = word_list.begin(); it != word_list.end(); it++) for (std::vector<std::string>::const_iterator it = word_list.begin(); it != word_list.end(); it++)
{ {
four_char_words += it->substr(0, unique_prefix_length); if (it->length() > 4)
{
trimmed_words += it->substr(0, Language::unique_prefix_length);
}
else
{
trimmed_words += *it;
}
} }
boost::crc_32_type result; boost::crc_32_type result;
result.process_bytes(four_char_words.data(), four_char_words.length()); result.process_bytes(trimmed_words.data(), trimmed_words.length());
return result.checksum() % seed_length; return result.checksum() % seed_length;
} }
/*!
* \brief Does the checksum test on the seed passed.
* \param seed Vector of seed words
* \return True if the test passed false if not.
*/
bool checksum_test(std::vector<std::string> seed)
{
// The last word is the checksum.
std::string last_word = seed.back();
seed.pop_back();
std::string checksum = seed[create_checksum_index(seed)];
std::string trimmed_checksum = checksum.length() > 4 ? checksum.substr(0, Language::unique_prefix_length) :
checksum;
std::string trimmed_last_word = checksum.length() > 4 ? last_word.substr(0, Language::unique_prefix_length) :
last_word;
return trimmed_checksum == trimmed_last_word;
}
} }
/*! /*!
@ -181,115 +205,63 @@ namespace crypto
*/ */
namespace ElectrumWords namespace ElectrumWords
{ {
/*!
* \brief Called to initialize it to work with a word list file.
* \param language Language of the word list file.
* \param has_checksum True if the checksum was passed false if not.
* \param old_word_list true it is to use the old style word list file false if not.
*/
void init(const std::string &language, bool has_checksum, bool old_word_list)
{
if (old_word_list)
{
// Use the old word list file if told to.
create_data_structures(WORD_LISTS_DIRECTORY + '/' + OLD_WORD_FILE, has_checksum);
is_old_style_word_list = true;
}
else
{
create_data_structures(WORD_LISTS_DIRECTORY + '/' + LANGUAGES_DIRECTORY + '/' + language, has_checksum);
is_old_style_word_list = false;
}
if (num_words == 0)
{
throw std::runtime_error(std::string("Word list file is empty: ") +
(old_word_list ? OLD_WORD_FILE : (LANGUAGES_DIRECTORY + '/' + language)));
}
}
/*! /*!
* \brief Converts seed words to bytes (secret key). * \brief Converts seed words to bytes (secret key).
* \param words String containing the words separated by spaces. * \param words String containing the words separated by spaces.
* \param dst To put the secret key restored from the words. * \param dst To put the secret key restored from the words.
* \param language_name Language of the seed as found gets written here.
* \return false if not a multiple of 3 words, or if word is not in the words list * \return false if not a multiple of 3 words, or if word is not in the words list
*/ */
bool words_to_bytes(const std::string& words, crypto::secret_key& dst) bool words_to_bytes(const std::string& words, crypto::secret_key& dst,
std::string &language_name)
{ {
std::vector<std::string> wlist; std::vector<std::string> seed;
boost::split(wlist, words, boost::is_any_of(" ")); boost::split(seed, words, boost::is_any_of(" "));
// error on non-compliant word list
if (seed.size() != seed_length/2 && seed.size() != seed_length &&
seed.size() != seed_length + 1)
{
return false;
}
// If it is seed with a checksum. // If it is seed with a checksum.
bool has_checksum = (wlist.size() == seed_length + 1); bool has_checksum = seed.size() == (seed_length + 1);
if (has_checksum) if (has_checksum)
{ {
// The last word is the checksum. if (!checksum_test(seed))
std::string last_word = wlist.back();
wlist.pop_back();
std::string checksum = wlist[create_checksum_index(wlist)];
if (checksum.substr(0, unique_prefix_length) != last_word.substr(0, unique_prefix_length))
{ {
// Checksum fail // Checksum fail
return false; return false;
} }
} }
// Try to find a word list file that contains all the words in the word list.
std::vector<std::string> languages;
get_language_list(languages);
std::vector<std::string>::iterator it; std::vector<uint32_t> matched_indices;
for (it = languages.begin(); it != languages.end(); it++) uint32_t word_list_length;
{ if (!find_seed_language(seed, has_checksum, matched_indices, word_list_length, language_name))
init(*it, has_checksum);
if (word_list_file_match(wlist, has_checksum))
{
break;
}
}
// If no such file was found, see if the old style word list file has them all.
if (it == languages.end())
{
init("", has_checksum, true);
if (!word_list_file_match(wlist, has_checksum))
{ {
return false; return false;
} }
}
int n = num_words;
// error on non-compliant word list for (unsigned int i=0; i < seed.size() / 3; i++)
if (wlist.size() != 12 && wlist.size() != 24) return false;
for (unsigned int i=0; i < wlist.size() / 3; i++)
{ {
uint32_t val; uint32_t val;
uint32_t w1, w2, w3; uint32_t w1, w2, w3;
w1 = matched_indices[i*3];
w2 = matched_indices[i*3 + 1];
w3 = matched_indices[i*3 + 2];
if (has_checksum) val = w1 + word_list_length * (((word_list_length - w1) + w2) % word_list_length) +
{ word_list_length * word_list_length * (((word_list_length - w2) + w3) % word_list_length);
w1 = words_map.at(wlist[i*3].substr(0, unique_prefix_length));
w2 = words_map.at(wlist[i*3 + 1].substr(0, unique_prefix_length));
w3 = words_map.at(wlist[i*3 + 2].substr(0, unique_prefix_length));
}
else
{
w1 = words_map.at(wlist[i*3]);
w2 = words_map.at(wlist[i*3 + 1]);
w3 = words_map.at(wlist[i*3 + 2]);
}
val = w1 + n * (((n - w1) + w2) % n) + n * n * (((n - w2) + w3) % n); if (!(val % word_list_length == w1)) return false;
if (!(val % n == w1)) return false;
memcpy(dst.data + i * 4, &val, 4); // copy 4 bytes to position memcpy(dst.data + i * 4, &val, 4); // copy 4 bytes to position
} }
std::string wlist_copy = words; std::string wlist_copy = words;
if (wlist.size() == 12) if (seed.size() == seed_length/2)
{ {
memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate memcpy(dst.data, dst.data + 16, 16); // if electrum 12-word seed, duplicate
wlist_copy += ' '; wlist_copy += ' ';
@ -303,21 +275,42 @@ namespace crypto
* \brief Converts bytes (secret key) to seed words. * \brief Converts bytes (secret key) to seed words.
* \param src Secret key * \param src Secret key
* \param words Space delimited concatenated words get written here. * \param words Space delimited concatenated words get written here.
* \param language_name Seed language name
* \return true if successful false if not. Unsuccessful if wrong key size. * \return true if successful false if not. Unsuccessful if wrong key size.
*/ */
bool bytes_to_words(const crypto::secret_key& src, std::string& words) bool bytes_to_words(const crypto::secret_key& src, std::string& words,
const std::string &language_name)
{ {
if (is_uninitialized())
{
init("english", true);
}
// To store the words for random access to add the checksum word later.
std::vector<std::string> words_store;
int n = num_words;
if (sizeof(src.data) % 4 != 0 || sizeof(src.data) == 0) return false; if (sizeof(src.data) % 4 != 0 || sizeof(src.data) == 0) return false;
std::vector<std::string> word_list;
Language::Base *language;
if (language_name == "English")
{
language = Language::Singleton<Language::English>::instance();
}
else if (language_name == "Spanish")
{
language = Language::Singleton<Language::Spanish>::instance();
}
else if (language_name == "Portuguese")
{
language = Language::Singleton<Language::Portuguese>::instance();
}
else if (language_name == "Japanese")
{
language = Language::Singleton<Language::Japanese>::instance();
}
else
{
return false;
}
word_list = language->get_word_list();
// To store the words for random access to add the checksum word later.
std::vector<std::string> words_store;
uint32_t word_list_length = word_list.size();
// 8 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626 // 8 bytes -> 3 words. 8 digits base 16 -> 3 digits base 1626
for (unsigned int i=0; i < sizeof(src.data)/4; i++, words += ' ') for (unsigned int i=0; i < sizeof(src.data)/4; i++, words += ' ')
{ {
@ -327,19 +320,19 @@ namespace crypto
memcpy(&val, (src.data) + (i * 4), 4); memcpy(&val, (src.data) + (i * 4), 4);
w1 = val % n; w1 = val % word_list_length;
w2 = ((val / n) + w1) % n; w2 = ((val / word_list_length) + w1) % word_list_length;
w3 = (((val / n) / n) + w2) % n; w3 = (((val / word_list_length) / word_list_length) + w2) % word_list_length;
words += words_array[w1]; words += word_list[w1];
words += ' '; words += ' ';
words += words_array[w2]; words += word_list[w2];
words += ' '; words += ' ';
words += words_array[w3]; words += word_list[w3];
words_store.push_back(words_array[w1]); words_store.push_back(word_list[w1]);
words_store.push_back(words_array[w2]); words_store.push_back(word_list[w2]);
words_store.push_back(words_array[w3]); words_store.push_back(word_list[w3]);
} }
words.pop_back(); words.pop_back();
@ -353,31 +346,18 @@ namespace crypto
*/ */
void get_language_list(std::vector<std::string> &languages) void get_language_list(std::vector<std::string> &languages)
{ {
languages.clear(); std::vector<Language::Base*> language_instances({
boost::filesystem::path languages_directory("wordlists/languages"); Language::Singleton<Language::English>::instance(),
if (!boost::filesystem::exists(languages_directory) || Language::Singleton<Language::Spanish>::instance(),
!boost::filesystem::is_directory(languages_directory)) Language::Singleton<Language::Portuguese>::instance(),
Language::Singleton<Language::Japanese>::instance(),
Language::Singleton<Language::OldEnglish>::instance()
});
for (std::vector<Language::Base*>::iterator it = language_instances.begin();
it != language_instances.end(); it++)
{ {
throw std::runtime_error("Word list languages directory is missing."); languages.push_back((*it)->get_language_name());
} }
boost::filesystem::directory_iterator end;
for (boost::filesystem::directory_iterator it(languages_directory); it != end; it++)
{
languages.push_back(it->path().filename().string());
}
}
/*!
* \brief Tells if the module is currenly using an old style word list.
* \return true if it is currenly using an old style word list false if not.
*/
bool get_is_old_style_word_list()
{
if (is_uninitialized())
{
throw std::runtime_error("ElectrumWords hasn't been initialized with a word list yet.");
}
return is_old_style_word_list;
} }
/*! /*!
@ -387,9 +367,9 @@ namespace crypto
*/ */
bool get_is_old_style_seed(const std::string &seed) bool get_is_old_style_seed(const std::string &seed)
{ {
std::vector<std::string> wlist; std::vector<std::string> word_list;
boost::split(wlist, seed, boost::is_any_of(" ")); boost::split(word_list, seed, boost::is_any_of(" "));
return wlist.size() != (seed_length + 1); return word_list.size() != (seed_length + 1);
} }
} }

View file

@ -36,6 +36,9 @@
* that method of "backing up" one's wallet keys. * that method of "backing up" one's wallet keys.
*/ */
#ifndef ELECTRUM_WORDS_H
#define ELECTRUM_WORDS_H
#include <string> #include <string>
#include <cstdint> #include <cstdint>
#include <map> #include <map>
@ -55,29 +58,26 @@ namespace crypto
*/ */
namespace ElectrumWords namespace ElectrumWords
{ {
/*!
* \brief Called to initialize it to work with a word list file.
* \param language Language of the word list file.
* \param has_checksum True if the checksum was passed false if not.
* \param old_word_list true it is to use the old style word list file false if not.
*/
void init(const std::string &language, bool has_checksum=true, bool old_word_list=false);
/*! /*!
* \brief Converts seed words to bytes (secret key). * \brief Converts seed words to bytes (secret key).
* \param words String containing the words separated by spaces. * \param words String containing the words separated by spaces.
* \param dst To put the secret key restored from the words. * \param dst To put the secret key restored from the words.
* \param language_name Language of the seed as found gets written here.
* \return false if not a multiple of 3 words, or if word is not in the words list * \return false if not a multiple of 3 words, or if word is not in the words list
*/ */
bool words_to_bytes(const std::string& words, crypto::secret_key& dst); bool words_to_bytes(const std::string& words, crypto::secret_key& dst,
std::string &language_name);
/*! /*!
* \brief Converts bytes (secret key) to seed words. * \brief Converts bytes (secret key) to seed words.
* \param src Secret key * \param src Secret key
* \param words Space delimited concatenated words get written here. * \param words Space delimited concatenated words get written here.
* \param language_name Seed language name
* \return true if successful false if not. Unsuccessful if wrong key size. * \return true if successful false if not. Unsuccessful if wrong key size.
*/ */
bool bytes_to_words(const crypto::secret_key& src, std::string& words); bool bytes_to_words(const crypto::secret_key& src, std::string& words,
const std::string &language_name);
/*! /*!
* \brief Gets a list of seed languages that are supported. * \brief Gets a list of seed languages that are supported.
@ -85,12 +85,6 @@ namespace crypto
*/ */
void get_language_list(std::vector<std::string> &languages); void get_language_list(std::vector<std::string> &languages);
/*!
* \brief If the module is currenly using an old style word list.
* \return true if it is currenly using an old style word list false if not.
*/
bool get_is_old_style_word_list();
/*! /*!
* \brief Tells if the seed passed is an old style seed or not. * \brief Tells if the seed passed is an old style seed or not.
* \param seed The seed to check (a space delimited concatenated word list) * \param seed The seed to check (a space delimited concatenated word list)
@ -99,3 +93,5 @@ namespace crypto
bool get_is_old_style_seed(const std::string &seed); bool get_is_old_style_seed(const std::string &seed);
} }
} }
#endif

View file

@ -1,9 +1,19 @@
#ifndef ENGLISH_H
#define ENGLISH_H
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "language_base.h"
#include <string>
std::vector<std::string>& word_list_english() namespace Language
{ {
static std::vector<std::string> word_list( class English: public Base
{
public:
English()
{
word_list = new std::vector<std::string>({
"abandon", "abandon",
"ability", "ability",
"able", "able",
@ -2052,47 +2062,13 @@ std::vector<std::string>& word_list_english()
"zero", "zero",
"zone", "zone",
"zoo" "zoo"
); });
return word_list; word_map = new std::unordered_map<std::string, uint32_t>;
trimmed_word_map = new std::unordered_map<std::string, uint32_t>;
language_name = "English";
populate_maps();
}
};
} }
std::unordered_map<std::string, unsigned int>& word_map_english() #endif
{
static std::unordered_map<std::string, unsigned int> word_map;
if (word_map.size() > 0)
{
return word_map;
}
std::vector<std::string> word_list = word_list_english();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
word_map[*it] = ii;
}
return word_map;
}
std::unordered_map<std::string, unsigned int>& trimmed_word_map_english()
{
static std::unordered_map<std::string, unsigned int> trimmed_word_map;
if (trimmed_word_map.size() > 0)
{
return trimmed_word_map;
}
std::vector<std::string> word_list = word_list_english();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
if (it->length() > 4)
{
trimmed_word_map[it->substr(0, 4)] = ii;
}
else
{
trimmed_word_map[*it] = ii;
}
}
return trimmed_word_map;
}

View file

@ -1,9 +1,19 @@
#ifndef JAPANESE_H
#define JAPANESE_H
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "language_base.h"
#include <string>
std::vector<std::string>& word_list_japanese() namespace Language
{ {
static std::vector<std::string> word_list( class Japanese: public Base
{
public:
Japanese()
{
word_list = new std::vector<std::string>({
"あい", "あい",
"あいこくしん", "あいこくしん",
"あう", "あう",
@ -2052,47 +2062,13 @@ std::vector<std::string>& word_list_japanese()
"ろんぶん", "ろんぶん",
"ろんり", "ろんり",
"わじまし" "わじまし"
); });
return word_list; word_map = new std::unordered_map<std::string, uint32_t>;
trimmed_word_map = new std::unordered_map<std::string, uint32_t>;
language_name = "Japanese";
populate_maps();
}
};
} }
std::unordered_map<std::string, unsigned int>& word_map_japanese() #endif
{
static std::unordered_map<std::string, unsigned int> word_map;
if (word_map.size() > 0)
{
return word_map;
}
std::vector<std::string> word_list = word_list_japanese();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
word_map[*it] = ii;
}
return word_map;
}
std::unordered_map<std::string, unsigned int>& trimmed_word_map_japanese()
{
static std::unordered_map<std::string, unsigned int> trimmed_word_map;
if (trimmed_word_map.size() > 0)
{
return trimmed_word_map;
}
std::vector<std::string> word_list = word_list_japanese();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
if (it->length() > 4)
{
trimmed_word_map[it->substr(0, 4)] = ii;
}
else
{
trimmed_word_map[*it] = ii;
}
}
return trimmed_word_map;
}

View file

@ -0,0 +1,61 @@
#ifndef LANGUAGE_BASE_H
#define LANGUAGE_BASE_H
#include <vector>
#include <unordered_map>
#include <string>
namespace Language
{
const int unique_prefix_length = 4;
class Base
{
protected:
std::vector<std::string> *word_list;
std::unordered_map<std::string, uint32_t> *word_map;
std::unordered_map<std::string, uint32_t> *trimmed_word_map;
std::string language_name;
void populate_maps()
{
int ii;
std::vector<std::string>::iterator it;
for (it = word_list->begin(), ii = 0; it != word_list->end(); it++, ii++)
{
(*word_map)[*it] = ii;
if (it->length() > 4)
{
(*trimmed_word_map)[it->substr(0, 4)] = ii;
}
else
{
(*trimmed_word_map)[*it] = ii;
}
}
}
public:
Base()
{
word_list = new std::vector<std::string>;
word_map = new std::unordered_map<std::string, uint32_t>;
trimmed_word_map = new std::unordered_map<std::string, uint32_t>;
}
const std::vector<std::string>& get_word_list() const
{
return *word_list;
}
const std::unordered_map<std::string, uint32_t>& get_word_map() const
{
return *word_map;
}
const std::unordered_map<std::string, uint32_t>& get_trimmed_word_map() const
{
return *trimmed_word_map;
}
std::string get_language_name() const
{
return language_name;
}
};
}
#endif

View file

@ -1,9 +1,19 @@
#ifndef OLD_ENGLISH_H
#define OLD_ENGLISH_H
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "language_base.h"
#include <string>
std::vector<std::string>& word_list_old_english() namespace Language
{ {
static std::vector<std::string> word_list( class OldEnglish: public Base
{
public:
OldEnglish()
{
word_list = new std::vector<std::string>({
"like", "like",
"just", "just",
"love", "love",
@ -1630,47 +1640,13 @@ std::vector<std::string>& word_list_old_english()
"unseen", "unseen",
"weapon", "weapon",
"weary" "weary"
); });
return word_list; word_map = new std::unordered_map<std::string, uint32_t>;
trimmed_word_map = new std::unordered_map<std::string, uint32_t>;
language_name = "OldEnglish";
populate_maps();
}
};
} }
std::unordered_map<std::string, unsigned int>& word_map_old_english() #endif
{
static std::unordered_map<std::string, unsigned int> word_map;
if (word_map.size() > 0)
{
return word_map;
}
std::vector<std::string> word_list = word_list_old_english();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
word_map[*it] = ii;
}
return word_map;
}
std::unordered_map<std::string, unsigned int>& trimmed_word_map_old_english()
{
static std::unordered_map<std::string, unsigned int> trimmed_word_map;
if (trimmed_word_map.size() > 0)
{
return trimmed_word_map;
}
std::vector<std::string> word_list = word_list_old_english();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
if (it->length() > 4)
{
trimmed_word_map[it->substr(0, 4)] = ii;
}
else
{
trimmed_word_map[*it] = ii;
}
}
return trimmed_word_map;
}

View file

@ -1,9 +1,19 @@
#ifndef PORTUGUESE_H
#define PORTUGUESE_H
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "language_base.h"
#include <string>
std::vector<std::string>& word_list_portuguese() namespace Language
{ {
static std::vector<std::string> word_list( class Portuguese: public Base
{
public:
Portuguese()
{
word_list = new std::vector<std::string>({
"abaular", "abaular",
"abdominal", "abdominal",
"abeto", "abeto",
@ -1630,48 +1640,13 @@ std::vector<std::string>& word_list_portuguese()
"zeloso", "zeloso",
"zenite", "zenite",
"zumbi" "zumbi"
});
); word_map = new std::unordered_map<std::string, uint32_t>;
return word_list; trimmed_word_map = new std::unordered_map<std::string, uint32_t>;
language_name = "Portuguese";
populate_maps();
}
};
} }
std::unordered_map<std::string, unsigned int>& word_map_portuguese() #endif
{
static std::unordered_map<std::string, unsigned int> word_map;
if (word_map.size() > 0)
{
return word_map;
}
std::vector<std::string> word_list = word_list_portuguese();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
word_map[*it] = ii;
}
return word_map;
}
std::unordered_map<std::string, unsigned int>& trimmed_word_map_portuguese()
{
static std::unordered_map<std::string, unsigned int> trimmed_word_map;
if (trimmed_word_map.size() > 0)
{
return trimmed_word_map;
}
std::vector<std::string> word_list = word_list_portuguese();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
if (it->length() > 4)
{
trimmed_word_map[it->substr(0, 4)] = ii;
}
else
{
trimmed_word_map[*it] = ii;
}
}
return trimmed_word_map;
}

16
src/mnemonics/singleton.h Normal file
View file

@ -0,0 +1,16 @@
namespace Language
{
template <class T>
class Singleton
{
Singleton() {}
Singleton(Singleton &s) {}
Singleton& operator=(const Singleton&) {}
public:
static T* instance()
{
static T* obj = new T;
return obj;
}
};
}

View file

@ -1,9 +1,19 @@
#ifndef SPANISH_H
#define SPANISH_H
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#include "language_base.h"
#include <string>
std::vector<std::string>& word_list_spanish() namespace Language
{ {
static std::vector<std::string> word_list( class Spanish: public Base
{
public:
Spanish()
{
word_list = new std::vector<std::string>({
"ábaco", "ábaco",
"abdomen", "abdomen",
"abeja", "abeja",
@ -2052,47 +2062,13 @@ std::vector<std::string>& word_list_spanish()
"zorro", "zorro",
"zumo", "zumo",
"zurdo" "zurdo"
); });
return word_list; word_map = new std::unordered_map<std::string, uint32_t>;
trimmed_word_map = new std::unordered_map<std::string, uint32_t>;
language_name = "Spanish";
populate_maps();
}
};
} }
std::unordered_map<std::string, unsigned int>& word_map_spanish() #endif
{
static std::unordered_map<std::string, unsigned int> word_map;
if (word_map.size() > 0)
{
return word_map;
}
std::vector<std::string> word_list = word_list_spanish();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
word_map[*it] = ii;
}
return word_map;
}
std::unordered_map<std::string, unsigned int>& trimmed_word_map_spanish()
{
static std::unordered_map<std::string, unsigned int> trimmed_word_map;
if (trimmed_word_map.size() > 0)
{
return trimmed_word_map;
}
std::vector<std::string> word_list = word_list_spanish();
int ii;
std::vector<std::string>::iterator it;
for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
{
if (it->length() > 4)
{
trimmed_word_map[it->substr(0, 4)] = ii;
}
else
{
trimmed_word_map[*it] = ii;
}
}
return trimmed_word_map;
}

View file

@ -368,6 +368,7 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
{ {
if (m_wallet_file.empty()) m_wallet_file = m_generate_new; // alias for simplicity later if (m_wallet_file.empty()) m_wallet_file = m_generate_new; // alias for simplicity later
std::string old_language;
// check for recover flag. if present, require electrum word list (only recovery option for now). // check for recover flag. if present, require electrum word list (only recovery option for now).
if (m_restore_deterministic_wallet) if (m_restore_deterministic_wallet)
{ {
@ -387,21 +388,14 @@ bool simple_wallet::init(const boost::program_options::variables_map& vm)
} }
} }
try if (!crypto::ElectrumWords::words_to_bytes(m_electrum_seed, m_recovery_key, old_language))
{
if (!crypto::ElectrumWords::words_to_bytes(m_electrum_seed, m_recovery_key))
{ {
fail_msg_writer() << "electrum-style word list failed verification"; fail_msg_writer() << "electrum-style word list failed verification";
return false; return false;
} }
} }
catch (std::runtime_error &e) bool r = new_wallet(m_wallet_file, pwd_container.password(), m_recovery_key, m_restore_deterministic_wallet,
{ m_non_deterministic, testnet, old_language);
fail_msg_writer() << e.what() << std::endl;
return false;
}
}
bool r = new_wallet(m_wallet_file, pwd_container.password(), m_recovery_key, m_restore_deterministic_wallet, m_non_deterministic, testnet);
CHECK_AND_ASSERT_MES(r, false, "account creation failed"); CHECK_AND_ASSERT_MES(r, false, "account creation failed");
} }
else else
@ -471,7 +465,7 @@ std::string simple_wallet::get_mnemonic_language()
try try
{ {
language_number = std::stoi(language_choice); language_number = std::stoi(language_choice);
if (!((language_number >= 0) && (static_cast<uint>(language_number) < language_list.size()))) if (!((language_number >= 0) && (static_cast<unsigned int>(language_number) < language_list.size())))
{ {
language_number = -1; language_number = -1;
fail_msg_writer() << "Invalid language choice passed. Please try again.\n"; fail_msg_writer() << "Invalid language choice passed. Please try again.\n";
@ -486,7 +480,8 @@ std::string simple_wallet::get_mnemonic_language()
} }
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
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) 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)
{ {
m_wallet_file = wallet_file; m_wallet_file = wallet_file;
@ -512,10 +507,10 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas
// convert rng value to electrum-style word list // convert rng value to electrum-style word list
std::string electrum_words; std::string electrum_words;
bool was_deprecated_wallet = m_restore_deterministic_wallet && bool was_deprecated_wallet = (old_language == "OldEnglish") ||
(crypto::ElectrumWords::get_is_old_style_word_list() || crypto::ElectrumWords::get_is_old_style_seed(m_electrum_seed);
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 // 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. // that was earlier used before this restore.
if (!m_restore_deterministic_wallet || was_deprecated_wallet) if (!m_restore_deterministic_wallet || was_deprecated_wallet)
@ -526,26 +521,10 @@ bool simple_wallet::new_wallet(const string &wallet_file, const std::string& pas
message_writer(epee::log_space::console_color_green, false) << "\nYou had been using " << 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"; "a deprecated version of the wallet. Please use the new seed that we provide.\n";
} }
std::string mnemonic_language = get_mnemonic_language(); mnemonic_language = get_mnemonic_language();
try
{
crypto::ElectrumWords::init(mnemonic_language);
}
catch (std::runtime_error &e)
{
fail_msg_writer() << e.what() << std::endl;
return false;
}
}
try
{
crypto::ElectrumWords::bytes_to_words(recovery_val, electrum_words);
}
catch (std::runtime_error &e)
{
fail_msg_writer() << e.what() << std::endl;
return false;
} }
crypto::ElectrumWords::bytes_to_words(recovery_val, electrum_words, mnemonic_language);
m_wallet->set_seed_language(mnemonic_language);
std::string print_electrum = ""; std::string print_electrum = "";

View file

@ -74,7 +74,8 @@ namespace cryptonote
bool run_console_handler(); bool run_console_handler();
bool new_wallet(const std::string &wallet_file, const std::string& password, const crypto::secret_key& recovery_key = crypto::secret_key(), bool recover = false, bool two_random = false, bool testnet = false); bool 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 open_wallet(const std::string &wallet_file, const std::string& password, bool testnet); bool open_wallet(const std::string &wallet_file, const std::string& password, bool testnet);
bool close_wallet(); bool close_wallet();

View file

@ -88,7 +88,7 @@ void wallet2::init(const std::string& daemon_address, uint64_t upper_transaction
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
bool wallet2::get_seed(std::string& electrum_words) bool wallet2::get_seed(std::string& electrum_words)
{ {
crypto::ElectrumWords::bytes_to_words(get_account().get_keys().m_spend_secret_key, electrum_words); crypto::ElectrumWords::bytes_to_words(get_account().get_keys().m_spend_secret_key, electrum_words, seed_language);
crypto::secret_key second; crypto::secret_key second;
keccak((uint8_t *)&get_account().get_keys().m_spend_secret_key, sizeof(crypto::secret_key), (uint8_t *)&second, sizeof(crypto::secret_key)); keccak((uint8_t *)&get_account().get_keys().m_spend_secret_key, sizeof(crypto::secret_key), (uint8_t *)&second, sizeof(crypto::secret_key));
@ -97,6 +97,13 @@ bool wallet2::get_seed(std::string& electrum_words)
return memcmp(second.data,get_account().get_keys().m_view_secret_key.data, sizeof(crypto::secret_key)) == 0; return memcmp(second.data,get_account().get_keys().m_view_secret_key.data, sizeof(crypto::secret_key)) == 0;
} }
/*!
* \brief Sets the seed language
*/
void wallet2::set_seed_language(const std::string &language)
{
seed_language = language;
}
//---------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------
void wallet2::process_new_transaction(const cryptonote::transaction& tx, uint64_t height) void wallet2::process_new_transaction(const cryptonote::transaction& tx, uint64_t height)
{ {

View file

@ -152,7 +152,10 @@ namespace tools
void callback(i_wallet2_callback* callback) { m_callback = callback; } void callback(i_wallet2_callback* callback) { m_callback = callback; }
bool get_seed(std::string& electrum_words); bool get_seed(std::string& electrum_words);
/*!
* \brief Sets the seed language
*/
void set_seed_language(const std::string &language);
void refresh(); void refresh();
void refresh(uint64_t start_height, size_t & blocks_fetched); void refresh(uint64_t start_height, size_t & blocks_fetched);
void refresh(uint64_t start_height, size_t & blocks_fetched, bool& received_money); void refresh(uint64_t start_height, size_t & blocks_fetched, bool& received_money);
@ -236,6 +239,7 @@ namespace tools
i_wallet2_callback* m_callback; i_wallet2_callback* m_callback;
bool m_testnet; bool m_testnet;
std::string seed_language;
}; };
} }
BOOST_CLASS_VERSION(tools::wallet2, 7) BOOST_CLASS_VERSION(tools::wallet2, 7)