diff --git a/CMakeLists.txt b/CMakeLists.txt index bfdeb95..fc1bef7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,9 +51,9 @@ set (PROGRAM_SOURCE src/core/isatty.cpp src/crypto/base58.cpp src/crypto/WIF.cpp + src/cli_key_search_result.cpp src/console.cpp src/key_search.cpp - src/key_search_helpers.cpp src/benchmark.cpp src/main.cpp ) diff --git a/include/eoskeygen/key_search.h b/include/eoskeygen/key_search.h index 19a385d..a5c8fc5 100644 --- a/include/eoskeygen/key_search.h +++ b/include/eoskeygen/key_search.h @@ -29,6 +29,8 @@ namespace eoskeygen { +class IKeySearchResult; + class KeySearch { public : @@ -45,14 +47,14 @@ public : // Add a list of words to search for. void addList(const strlist_t& list); - void addDictionary(const Dictionary& dictionary); - // get the list of words to search for. const strlist_t& getList(); // Clears the search list. void clear(); + void setCallback(IKeySearchResult* callback); + #ifdef HAVE_THREADS // Set the number of threads to use while searching. void setThreadCount(size_t num); @@ -79,13 +81,12 @@ protected : // List of words to search for. strlist_t m_words; - // Dictionary to use when we find a search result. - Dictionary m_dict; - #ifdef HAVE_THREADS // Number of threads to use. size_t m_threads; #endif /* HAVE_THREADS */ + + IKeySearchResult* m_callback; }; } // namespace eoskeygen diff --git a/include/eoskeygen/key_search_result.h b/include/eoskeygen/key_search_result.h new file mode 100644 index 0000000..6e48675 --- /dev/null +++ b/include/eoskeygen/key_search_result.h @@ -0,0 +1,40 @@ +/** + * MIT License + * + * Copyright (c) 2019-2020 EOS Sw/eden + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef EOSIOKEYGEN_KEY_SEARCH_RESULT_H +#define EOSIOKEYGEN_KEY_SEARCH_RESULT_H + +#include + +namespace eoskeygen { + +class IKeySearchResult +{ +public : + + virtual void onResult(const struct ec_keypair* key, const struct KeySearch::result& result) = 0; +}; + +} // namespace eoskeygen + +#endif /* EOSIOKEYGEN_KEY_SEARCH_RESULT_H */ diff --git a/src/key_search_helpers.cpp b/src/cli_key_search_result.cpp similarity index 81% rename from src/key_search_helpers.cpp rename to src/cli_key_search_result.cpp index 124d014..b61ea3a 100644 --- a/src/key_search_helpers.cpp +++ b/src/cli_key_search_result.cpp @@ -25,7 +25,7 @@ #include #include #include "console.h" -#include "key_search_helpers.h" +#include "cli_key_search_result.h" namespace eoskeygen { @@ -37,19 +37,24 @@ static size_t highlight(console::Color color, const std::string& str, size_t pos return len; } -void key_search_result(const struct ec_keypair* key, const struct KeySearch::result* result, const Dictionary& dict) { +CliKeySearchResult::CliKeySearchResult(const Dictionary& dict) : +m_dict (dict) +{ +} + +void CliKeySearchResult::onResult(const struct ec_keypair* key, const struct KeySearch::result& result) { std::string pub = wif_pub_encode(key->pub); - Dictionary::search_result_t dict_res = dict.search(pub); + Dictionary::search_result_t dict_res = m_dict.search(pub); std::cout << "----" << std::endl; - std::cout << "Found: " << pub.substr(result->pos, result->len) << std::endl; + std::cout << "Found: " << pub.substr(result.pos, result.len) << std::endl; std::cout << "Public: EOS"; for(size_t i = 3; i < pub.length(); ) { - if (i == result->pos) { - i += highlight(console::red, pub, result->pos, result->len); + if (i == result.pos) { + i += highlight(console::red, pub, result.pos, result.len); continue; } diff --git a/src/key_search_helpers.h b/src/cli_key_search_result.h similarity index 83% rename from src/key_search_helpers.h rename to src/cli_key_search_result.h index d3dcf0d..2645aeb 100644 --- a/src/key_search_helpers.h +++ b/src/cli_key_search_result.h @@ -27,12 +27,23 @@ #include #include #include +#include namespace eoskeygen { class Dictionary; -void key_search_result(const struct ec_keypair* key, const struct KeySearch::result* result, const Dictionary& dict); +class CliKeySearchResult : public IKeySearchResult +{ +public: + CliKeySearchResult(const Dictionary& dict); + + virtual void onResult(const struct ec_keypair* key, const struct KeySearch::result& result); + +protected : + + const Dictionary& m_dict; +}; } // namespace eoskeygen diff --git a/src/key_search.cpp b/src/key_search.cpp index 8adfc2d..72dd714 100644 --- a/src/key_search.cpp +++ b/src/key_search.cpp @@ -24,8 +24,8 @@ #include #include #include +#include #include -#include "key_search_helpers.h" namespace eoskeygen { @@ -43,11 +43,6 @@ void KeySearch::addList(const strlist_t& list) } } -void KeySearch::addDictionary(const Dictionary& dictionary) -{ - m_dict = dictionary; -} - const strlist_t& KeySearch::getList() { return m_words; @@ -58,6 +53,11 @@ void KeySearch::clear() m_words.clear(); } +void KeySearch::setCallback(IKeySearchResult* callback) +{ + m_callback = callback; +} + void KeySearch::_search_linear(size_t n) { size_t count = 0; @@ -67,7 +67,7 @@ void KeySearch::_search_linear(size_t n) { struct result res; ec_generate_key(&pair); if (_contains_word(&pair, res)) { - key_search_result(&pair, &res, m_dict); + m_callback->onResult(&pair, res); count++; } } diff --git a/src/key_search_mt.cpp b/src/key_search_mt.cpp index 1310caa..d9f02c9 100644 --- a/src/key_search_mt.cpp +++ b/src/key_search_mt.cpp @@ -26,8 +26,8 @@ #include #include #include +#include #include -#include "key_search_helpers.h" namespace eoskeygen { @@ -52,7 +52,7 @@ void KeySearch::_thr_proc() { if (_contains_word(&pair, res)) { // Guard output with mutex, so we don't get - // interrupted mid write and can write to g_count safely. + // interrupted mid write and can write to g_count and res safely. const std::lock_guard lock(g_count_mtx); // It is possible g_count was updated by another thread @@ -62,9 +62,9 @@ void KeySearch::_thr_proc() { return; } - // Update count and print result. + // Update count and call result function. g_count++; - key_search_result(&pair, &res, m_dict); + m_callback->onResult(&pair, res); } } } diff --git a/src/main.cpp b/src/main.cpp index bca286c..3b6a206 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,7 @@ #include #include #include +#include "cli_key_search_result.h" #include "core/file.h" #include "console.h" #include "benchmark.h" @@ -47,8 +48,9 @@ int option_num_threads = std::thread::hardware_concurrency(); int cmd_search(const eoskeygen::strlist_t& words, const eoskeygen::Dictionary& dict, int count) { eoskeygen::KeySearch ks; + eoskeygen::CliKeySearchResult rs(dict); - ks.addDictionary(dict); + ks.setCallback(&rs); for(auto it = words.begin(); it != words.end(); it++) { size_t p = eoskeygen::is_base58(*it);