mirror of
https://github.com/eosswedenorg/antelope-keygen
synced 2026-06-17 03:50:03 +02:00
Decouple key_search_result() and KeySearch class with an interface.
This commit is contained in:
parent
a417390bb7
commit
d9634f334b
8 changed files with 84 additions and 25 deletions
|
|
@ -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
|
||||
)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
40
include/eoskeygen/key_search_result.h
Normal file
40
include/eoskeygen/key_search_result.h
Normal file
|
|
@ -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 <eoskeygen/key_search.h>
|
||||
|
||||
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 */
|
||||
|
|
@ -25,7 +25,7 @@
|
|||
#include <eoskeygen/core/dictionary.h>
|
||||
#include <eoskeygen/crypto/WIF.h>
|
||||
#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;
|
||||
}
|
||||
|
||||
|
|
@ -27,12 +27,23 @@
|
|||
#include <eoskeygen/core/string.h>
|
||||
#include <eoskeygen/crypto/types.h>
|
||||
#include <eoskeygen/key_search.h>
|
||||
#include <eoskeygen/key_search_result.h>
|
||||
|
||||
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
|
||||
|
||||
|
|
@ -24,8 +24,8 @@
|
|||
#include <string>
|
||||
#include <eoskeygen/crypto/ec.h>
|
||||
#include <eoskeygen/crypto/WIF.h>
|
||||
#include <eoskeygen/key_search_result.h>
|
||||
#include <eoskeygen/key_search.h>
|
||||
#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++;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -26,8 +26,8 @@
|
|||
#include <mutex>
|
||||
#include <vector>
|
||||
#include <eoskeygen/crypto/ec.h>
|
||||
#include <eoskeygen/key_search_result.h>
|
||||
#include <eoskeygen/key_search.h>
|
||||
#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<std::mutex> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@
|
|||
#include <eoskeygen/crypto/ec.h>
|
||||
#include <eoskeygen/crypto/WIF.h>
|
||||
#include <eoskeygen/key_search.h>
|
||||
#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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue