mirror of
https://github.com/eosswedenorg/antelope-keygen
synced 2026-06-18 04:00: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
|
|
@ -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