<commit_before>// Copyright (c) 2014, The Monero Project
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
// 
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
// 
// 3. Neither the name of the copyright holder nor the names of its contributors may be
//    used to endorse or promote products derived from this software without specific
//    prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/*!
 * \file electrum-words.cpp
 * 
 * \brief Mnemonic seed generation and wallet restoration from them.
 * 
 * This file and its header file are for translating Electrum-style word lists
 * into their equivalent byte representations for cross-compatibility with
 * that method of "backing up" one's wallet keys.
 */

#include <string>
#include <cassert>
#include <map>
#include <cstdint>
#include <vector>
#include <boost/algorithm/string.hpp>
#include "crypto/crypto.h"  // for declaration of crypto::secret_key
#include <fstream>
#include "mnemonics/electrum-words.h"
#include <stdexcept>
#include <boost/filesystem.hpp>
#include <boost/crc.hpp>
#include <boost/algorithm/string/join.hpp>

namespace
{
  int num_words = 0;
  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";

  /*!
   * \brief Tells if the module hasn't been initialized with a word list file.
   * \return Whether the module hasn't been initialized with a word list file.
   */
  bool is_uninitialized()
  {
    return num_words == 0 ? true : false;
  }

  /*!
   * \brief Create word list map and array data structres for use during inter-conversion between
   * words and secret key.
   * \param word_file Path to the word list file from pwd.
   */
  void create_data_structures(const std::string &word_file)
  {
    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(std::string("Word list file couldn't be opened."));

    std::string word;
    while (input_stream >> word)
    {
      words_array.push_back(word);
      words_map[word] = num_words;
      num_words++;
    }
    input_stream.close();
  }

  /*!
   * \brief Tells if all the words passed in wlist was present in current word list file.
   * \param  wlist List of words to match.
   * \return       Whether they were all present or not.
   */
  bool word_list_file_match(const std::vector<std::string> &wlist)
  {
    for (std::vector<std::string>::const_iterator it = wlist.begin(); it != wlist.end(); it++)
    {
      if (words_map.count(*it) == 0)
      {
        return false;
      }
    }
    return true;
  }

  /*!
   * \brief Creates a checksum index in the word list array on the list of words.
   * \param  words Space separated list of words
   * \return       Checksum index
   */
  uint32_t create_checksum_index(const std::string &words)
  {
    boost::crc_32_type result;
    result.process_bytes(words.data(), words.length());
    return result.checksum() % seed_length;
  }
}

/*!
 * \namespace crypto
 * 
 * \brief crypto namespace.
 */
namespace crypto
{
  /*!
   * \namespace crypto::ElectrumWords
   * 
   * \brief Mnemonic seed word generation and wallet restoration helper functions.
   */
  namespace ElectrumWords
  {
    /*!
     * \brief Called to initialize it work with a word list file.
     * \param language      Language of the word list file.
     * \param old_word_list Whether it is to use the old style word list file.
     */
    void init(const std::string &language, bool old_word_list)
    {
      if (old_word_list)
      {
        // Use the old word list file if told to.<commit_msg>Minor code refactor and comment changes<commit_after>// Copyright (c) 2014, The Monero Project
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
// 
// 1. Redistributions of source code must retain the above copyright notice, this list of
//    conditions and the following disclaimer.
// 
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
//    of conditions and the following disclaimer in the documentation and/or other
//    materials provided with the distribution.
// 
// 3. Neither the name of the copyright holder nor the names of its contributors may be
//    used to endorse or promote products derived from this software without specific
//    prior written permission.
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

/*!
 * \file electrum-words.cpp
 * 
 * \brief Mnemonic seed generation and wallet restoration from them.
 * 
 * This file and its header file are for translating Electrum-style word lists
 * into their equivalent byte representations for cross-compatibility with
 * that method of "backing up" one's wallet keys.
 */

#include <string>
#include <cassert>
#include <map>
#include <cstdint>
#include <vector>
#include <boost/algorithm/string.hpp>
#include "crypto/crypto.h"  // for declaration of crypto::secret_key
#include <fstream>
#include "mnemonics/electrum-words.h"
#include <stdexcept>
#include <boost/filesystem.hpp>
#include <boost/crc.hpp>
#include <boost/algorithm/string/join.hpp>

namespace
{
  int num_words = 0;
  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";

  /*!
   * \brief Tells if the module hasn't been initialized with a word list file.
   * \return true if the module hasn't been initialized with a word list file false otherwise.
   */
  bool is_uninitialized()
  {
    return num_words == 0 ? true : false;
  }

  /*!
   * \brief Create word list map and array data structres for use during inter-conversion between
   * words and secret key.
   * \param word_file Path to the word list file from pwd.
   */
  void create_data_structures(const std::string &word_file)
  {
    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)
    {
      words_array.push_back(word);
      words_map[word] = num_words;
      num_words++;
    }
    input_stream.close();
  }

  /*!
   * \brief Tells if all the words passed in wlist was present in current word list file.
   * \param  wlist List of words to match.
   * \return       true if all the words were present false if not.
   */
  bool word_list_file_match(const std::vector<std::string> &wlist)
  {
    for (std::vector<std::string>::const_iterator it = wlist.begin(); it != wlist.end(); it++)
    {
      if (words_map.count(*it) == 0)
      {
        return false;
      }
    }
    return true;
  }

  /*!
   * \brief Creates a checksum index in the word list array on the list of words.
   * \param  words Space separated list of words
   * \return       Checksum index
   */
  uint32_t create_checksum_index(const std::string &words)
  {
    boost::crc_32_type result;
    result.process_bytes(words.data(), words.length());
    return result.checksum() % seed_length;
  }
}

/*!
 * \namespace crypto
 * 
 * \brief crypto namespace.
 */
namespace crypto
{
  /*!
   * \namespace crypto::ElectrumWords
   * 
   * \brief Mnemonic seed word generation and wallet restoration helper functions.
   */
  namespace ElectrumWords
  {
    /*!
     * \brief Called to initialize it work with a word list file.
     * \param language      Language of the word list file.
     * \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 old_word_list)
    {
      if (old_word_list)
      {
        // Use the old word list file if told to.
{'fraction_non_alphanumeric': 0.0940671350507416, 'fraction_numerical': 0.003708040593286495, 'mean_word_length': 3.282908483075637, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4018940', 'n_tokens_mistral': 3073, 'n_tokens_neox': 2894, 'n_words': 1149}