1
0
Fork 0
mirror of https://github.com/eosswedenorg/antelope-keygen synced 2026-06-18 04:00:03 +02:00

cli: move source files.

This commit is contained in:
Henrik Hautakoski 2020-04-21 10:06:10 +02:00
parent 54e493de1f
commit 884f8a6125
12 changed files with 7 additions and 7 deletions

54
cli/src/benchmark.cpp Normal file
View file

@ -0,0 +1,54 @@
/**
* 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.
*/
#include <chrono>
#include <libeosio/ec.hpp>
#include "benchmark.hpp"
using std::chrono::steady_clock;
using std::chrono::duration;
using std::chrono::time_point;
namespace eoskeygen {
void benchmark(size_t num_keys, struct benchmark_result* res) {
time_point<steady_clock> start;
if (num_keys < 1) {
res->sec = res->kps = 0;
return;
}
start = steady_clock::now();
for(size_t i = 0; i < num_keys; i++) {
struct libeosio::ec_keypair k;
libeosio::ec_generate_key(&k);
}
res->sec = duration<float>(steady_clock::now() - start).count();
res->kps = static_cast<float>(num_keys) / res->sec;
}
} // namespace eoskeygen

40
cli/src/benchmark.hpp Normal file
View 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_BENCHMARK_H
#define EOSIOKEYGEN_BENCHMARK_H
#include <ctime>
namespace eoskeygen {
struct benchmark_result {
float sec; // elapsed seconds.
float kps; // keys per second.
};
void benchmark(size_t num_keys, struct benchmark_result* res);
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_BENCHMARK_H */

View file

@ -0,0 +1,74 @@
/**
* 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.
*/
#include <iostream>
#include <libeosio/WIF.hpp>
#include <eoskeygen/core/dictionary.hpp>
#include "console.hpp"
#include "cli_key_search_result.hpp"
namespace eoskeygen {
static size_t highlight(console::Color color, const std::string& str, size_t pos, size_t len) {
std::cout << console::fg(color, console::bold)
<< str.substr(pos, len)
<< console::reset;
return len;
}
CliKeySearchResult::CliKeySearchResult(const Dictionary& dict) :
m_dict (dict)
{
}
void CliKeySearchResult::onResult(const struct libeosio::ec_keypair* key, const struct KeySearch::result& result) {
std::string pub = libeosio::wif_pub_encode(key->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 << "Public: EOS";
for(size_t i = 3; i < pub.length(); ) {
if (i == result.pos) {
i += highlight(console::red, pub, result.pos, result.len);
continue;
}
auto p = dict_res.find(i);
if (p != dict_res.end()) {
i += highlight(console::blue, pub, p->first, p->second);
continue;
}
std::cout << pub[i++];
}
std::cout << std::endl
<< "Private: " << libeosio::wif_priv_encode(key->secret) << std::endl;
}
} // namespace eoskeygen

View file

@ -0,0 +1,50 @@
/**
* 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_HELPERS_H
#define EOSIOKEYGEN_KEY_SEARCH_HELPERS_H
#include <libeosio/types.hpp>
#include <eoskeygen/core/string.hpp>
#include <eoskeygen/key_search.hpp>
#include <eoskeygen/key_search_result.hpp>
namespace eoskeygen {
class Dictionary;
class CliKeySearchResult : public IKeySearchResult
{
public:
CliKeySearchResult(const Dictionary& dict);
virtual void onResult(const struct libeosio::ec_keypair* key, const struct KeySearch::result& result);
protected :
const Dictionary& m_dict;
};
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_KEY_SEARCH_HELPERS_H */

48
cli/src/console.cpp Normal file
View file

@ -0,0 +1,48 @@
/**
* 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.
*/
#include <iostream>
#include "isatty.hpp"
#include "console.hpp"
namespace eoskeygen { namespace console {
bool disable_color = false;
FILE* _getFileFromStream(const std::ostream& os) {
if (&os == &std::cout) {
return stdout;
} else if (&os == &std::cerr) {
return stderr;
}
return NULL;
}
bool isColorsSupported(const std::ostream& os) {
FILE* fd = _getFileFromStream(os);
return disable_color == false && isatty(fd);
}
} } // namespace eoskeygen::console

91
cli/src/console.hpp Normal file
View file

@ -0,0 +1,91 @@
/**
* 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_CONSOLE_H
#define EOSIOKEYGEN_CONSOLE_H
#include <ostream>
namespace eoskeygen {
namespace console {
extern bool disable_color;
// enum for all supported colors.
enum Color {
default_fg,
black,
white,
red,
green,
blue,
yellow,
magenta,
cyan,
// Light colors.
light_grey,
light_red,
light_green,
light_blue,
light_yellow,
light_magenta,
light_cyan,
// Dark colors
dark_grey
};
enum Attribute {
normal,
bold,
italic
};
bool isColorsSupported(const std::ostream& os);
// Resets all colors/attributes
std::ostream& reset(std::ostream& os);
// Foreground color
// Defined as a class with overloaded "<<" operator so you can write:
// std::cout << fg(red) << "Text";
class fg
{
public:
fg(Color color, Attribute attribute = normal)
: _color(color), _attr(attribute) {}
friend std::ostream& operator<<(std::ostream& os, const fg& obj);
protected :
Color _color;
Attribute _attr;
};
} // namespace console
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_CONSOLE_H */

83
cli/src/console_ansi.cpp Normal file
View file

@ -0,0 +1,83 @@
/**
* 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.
*/
#include <iostream>
#include "console.hpp"
namespace eoskeygen {
namespace console {
std::ostream& reset(std::ostream& os) {
if (!isColorsSupported(os)) {
return os;
}
return os << "\033[0m";
}
// Foreground
std::ostream& operator<<(std::ostream& os, const fg& obj) {
int attr;
int code;
if (!isColorsSupported(os)) {
return os;
}
switch(obj._color) {
case black : code = 30; break;
case red : code = 31; break;
case green : code = 32; break;
case yellow : code = 33; break;
case blue : code = 34; break;
case magenta : code = 35; break;
case cyan : code = 36; break;
case light_grey : code = 37; break;
case dark_grey : code = 90; break;
case light_red : code = 91; break;
case light_green : code = 92; break;
case light_yellow : code = 93; break;
case light_blue : code = 94; break;
case light_magenta : code = 95; break;
case light_cyan : code = 96; break;
case white : code = 97; break;
case default_fg : default :
code = 39;
}
switch(obj._attr) {
case bold : attr = 1; break;
case italic : attr = 2; break;
case normal : default :
attr = 0;
}
return os << "\033[" << attr << ";" << code << "m";
}
} // namespace console
} // namespace eoskeygen

108
cli/src/console_win32.cpp Normal file
View file

@ -0,0 +1,108 @@
/**
* 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.
*/
#include <windows.h>
#include <iostream>
#include "console.hpp"
namespace eoskeygen {
// WinAPI colors
#define FG_BLACK 0
#define FG_BLUE FOREGROUND_BLUE
#define FG_GREEN FOREGROUND_GREEN
#define FG_RED FOREGROUND_RED
#define FG_CYAN (FOREGROUND_GREEN | FOREGROUND_BLUE)
#define FG_YELLOW (FOREGROUND_RED | FOREGROUND_GREEN)
#define FG_MAGENTA (FOREGROUND_RED | FOREGROUND_BLUE)
#define FG_DARKGREY FOREGROUND_INTENSITY
#define FG_GREY (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define FG_LIGHTBLUE (FOREGROUND_INTENSITY | FOREGROUND_BLUE)
#define FG_LIGHTGREEN (FOREGROUND_INTENSITY | FOREGROUND_GREEN)
#define FG_LIGHTCYAN (FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define FG_LIGHTRED (FOREGROUND_INTENSITY | FOREGROUND_RED)
#define FG_LIGHTMAGENTA (FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE)
#define FG_LIGHTYELLOW (FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN)
#define FG_WHITE (FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
#define FG_DEFAULT FG_GREY
namespace console {
HANDLE _getNativeHandle(const std::ostream& os) {
if (&os == &std::cout) {
return ::GetStdHandle(STD_OUTPUT_HANDLE);
} else if (&os == &std::cerr) {
return ::GetStdHandle(STD_ERROR_HANDLE);
}
return NULL;
}
std::ostream& reset(std::ostream& os) {
if (isColorsSupported(os)) {
::SetConsoleTextAttribute(_getNativeHandle(os), FG_DEFAULT);
}
return os;
}
// Foreground
std::ostream& operator<<(std::ostream& os, const fg& obj) {
int code;
if (isColorsSupported(os)) {
switch(obj._color) {
case black : code = FG_BLACK; break;
case red : code = FG_RED; break;
case green : code = FG_GREEN; break;
case blue : code = FG_BLUE; break;
case yellow : code = FG_YELLOW; break;
case magenta : code = FG_MAGENTA; break;
case cyan : code = FG_CYAN; break;
case light_grey : code = FG_GREY; break;
case light_red : code = FG_LIGHTRED; break;
case light_green : code = FG_LIGHTGREEN; break;
case light_yellow : code = FG_LIGHTYELLOW; break;
case light_blue : code = FG_LIGHTBLUE; break;
case light_magenta : code = FG_LIGHTMAGENTA; break;
case light_cyan : code = FG_LIGHTCYAN; break;
case dark_grey : code = FG_DARKGREY; break;
case white : code = FG_WHITE; break;
case default_fg : default :
code = FG_DEFAULT;
}
::SetConsoleTextAttribute(_getNativeHandle(os), code);
}
// WinAPI does not support text attributes in the console.
// so we ignore those.
return os;
}
} // namespace console
} // namespace eoskeygen

21
cli/src/isatty.cpp Normal file
View file

@ -0,0 +1,21 @@
#if _WIN32
#include <io.h>
#else
#include <unistd.h>
#define _isatty isatty
#define _fileno fileno
#endif
#include "isatty.hpp"
namespace eoskeygen {
bool isatty(int fd) {
return ::_isatty(fd);
}
bool isatty(FILE* fd) {
// fileno() segfaults if fd is null.
return fd ? isatty(_fileno(fd)) : false;
}
} // namespace eoskeygen

37
cli/src/isatty.hpp Normal file
View file

@ -0,0 +1,37 @@
/**
* 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_CORE_ISATTY_H
#define EOSIOKEYGEN_CORE_ISATTY_H
#include <stdio.h>
namespace eoskeygen {
bool isatty(int fd);
bool isatty(FILE* fd);
} // namespace eoskeygen
#endif /* EOSIOKEYGEN_CORE_ISATTY_H */

293
cli/src/main.cpp Normal file
View file

@ -0,0 +1,293 @@
/**
* 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.
*/
#include <iostream>
#include <cstring>
#include <libeosio/base58.hpp>
#include <libeosio/ec.hpp>
#include <libeosio/WIF.hpp>
#include <eoskeygen/config.hpp>
#include <eoskeygen/core/file.hpp>
#include <eoskeygen/core/string.hpp>
#include <eoskeygen/core/dictionary.hpp>
#include <eoskeygen/core/leet.hpp>
#include <eoskeygen/key_search.hpp>
#include "cli_key_search_result.hpp"
#include "console.hpp"
#include "benchmark.hpp"
#include "config.hpp"
// Command line options.
bool option_l33t = false;
#ifdef EOSIOKEYGEN_HAVE_THREADS
size_t option_num_threads = eoskeygen::KeySearch::max_threads();
#endif /* EOSIOKEYGEN_HAVE_THREADS */
int cmd_search(const eoskeygen::strlist_t& words, const eoskeygen::Dictionary& dict, int count) {
eoskeygen::KeySearch ks;
eoskeygen::CliKeySearchResult rs(dict);
ks.setCallback(&rs);
for(auto it = words.begin(); it != words.end(); it++) {
size_t p = libeosio::is_base58(*it);
if (p != std::string::npos) {
std::cerr << "The word '"
<< *it << "' contains an invalid non-base58 character '"
<< (*it)[p] << "'" << std::endl;
return 1;
}
}
if (option_l33t) {
for(std::size_t i = 0; i < words.size(); i++) {
ks.addList(eoskeygen::l33twords(words[i]));
}
} else {
ks.addList(words);
}
#ifdef EOSIOKEYGEN_HAVE_THREADS
ks.setThreadCount(option_num_threads);
#endif /* EOSIOKEYGEN_HAVE_THREADS */
std::cout << "Searching for " << count
<< " keys containing: " << eoskeygen::strlist::join(ks.getList(), ",")
#ifdef EOSIOKEYGEN_HAVE_THREADS
<< ", Using: " << option_num_threads << " threads"
#endif /* EOSIOKEYGEN_HAVE_THREADS */
<< std::endl;
ks.find(count);
return 0;
}
void usage(const char *name) {
std::cout << name
<< " [ -h | --help | -v | search [ -m | --l33t"
#ifdef EOSIOKEYGEN_HAVE_THREADS
<< " | --threads=<num>"
#endif /* EOSIOKEYGEN_HAVE_THREADS */
<< " | --dict=<file> ... "
<< " | --lang=<value> ... ] <word_list>|file:<filename> [ <count:10> ]"
<< " | benchmark [ <num:1000> ]"
<< " ]"
<< std::endl << std::endl;
std::cout << " - Output one EOSIO key pair if no arguments are given" << std::endl << std::endl;
// Options
std::cout
<< " Options:" << std::endl
<< " -h, --help Shows this help text."
<< std::endl << std::endl
<< " -v Shows version."
<< std::endl << std::endl;
std::cout << " search: " << std::endl
<< " performs a search, finding <count> public keys containing" << std::endl
<< " one or more words from <word_list> (separated with ',')." << std::endl
<< std::endl
<< " Instead of a list it is possible to specify a file with words" << std::endl
<< " (separated with newline '\\n') using file:<filename>"
<< std::endl << std::endl
<< " -m: Monochrome, disables all color output."
<< std::endl << std::endl
<< " --l33t: Takes each word in <word_list> and find all l33tspeak" << std::endl
<< " combinations of that word and uses the new list for the search."
#ifdef EOSIOKEYGEN_HAVE_THREADS
<< std::endl << std::endl
<< " --threads=<num>: Use <num> of parallel threads for searching." << std::endl
<< " Default is what the operating system recomend."
#endif /* EOSIOKEYGEN_HAVE_THREADS */
<< std::endl << std::endl
<< " --dict=<file>: Use words found in <file> (separated by newline) to" << std::endl
<< " highlight words in the keys found (note that the words in this" << std::endl
<< " file are not used for search. only for highlight output)." << std::endl
<< " There can be more then one --dict flag. In that case contents" << std::endl
<< " of all files are merged into one dictionary." << std::endl
<< std::endl << std::endl
<< " --lang=<value>: Same as --dict but will use <value>" << std::endl
<< " to find a file in " << CONFIG_SHARE_FULL_PATH << "/dict." << std::endl
<< " There can be more then one --lang flag. In that case contents" << std::endl
<< " of all files are merged into one dictionary." << std::endl
<< std::endl;
std::cout << " Benchmark: " << std::endl
<< " performs a benchmark test, generating <num> keys and measuring the time." << std::endl
<< std::endl;
}
void cmd_benchmark(size_t num_keys) {
struct eoskeygen::benchmark_result res;
std::cout << "Benchmark: Generating "
<< num_keys << " keys" << std::endl;
eoskeygen::benchmark(num_keys, &res);
std::cout << "Result: Took " << res.sec << " seconds, "
<< res.kps << " keys per second." << std::endl;
}
int main(int argc, char **argv) {
// current position in argv
// when parsing command line.
int p = 1;
// No args, just print a key.
if (argc <= 1) {
struct libeosio::ec_keypair pair;
libeosio::ec_generate_key(&pair);
libeosio::wif_print_key(&pair);
return 0;
}
if (!strcmp(argv[p], "-h") || !strcmp(argv[p], "--help")) {
usage(argv[0]);
return 0;
}
if (!strcmp(argv[p], "-v")) {
std::cout << PROGRAM_NAME << " v" << PROGRAM_VERSION << std::endl;
return 0;
}
if (!strcmp(argv[p], "search")) {
int count = 10;
eoskeygen::strlist_t words;
eoskeygen::Dictionary dict;
while(p++ < argc - 1) {
if (!strcmp(argv[p], "-m")) {
eoskeygen::console::disable_color = true;
} else if (!strcmp(argv[p], "--l33t")) {
option_l33t = true;
} else if (!memcmp(argv[p], "--threads=", 10)) {
#ifdef EOSIOKEYGEN_HAVE_THREADS
option_num_threads = atoi(argv[p] + 10);
if (option_num_threads < 2) {
std::cerr << "NOTICE: Number of threads less than 2 does not make sense."
<< " So eosio-keygen will use 2." << std::endl;
option_num_threads = 2;
}
#else
// Even if we dont have threads. we consume the flag.
// otherwise we might break scripts. Print a nice message instead.
std::cerr << "NOTICE: eosio-keygen is not compiled with"
<< " thread support. this option is ignored." << std::endl;
#endif /* EOSIOKEYGEN_HAVE_THREADS */
}
// Dictionary.
else if (!memcmp(argv[p], "--dict=", 7)) {
eoskeygen::Dictionary d;
std::string filename(argv[p] + 7);
if (d.loadFromFile(filename)) {
dict.add(d);
} else {
std::cerr << "Could not load dictionary from file: " << filename << std::endl;
}
}
// Language (dictionary, but we find the file in <CONFIG_SHARE_FULL_PATH>/dict/<lang>)
else if (!memcmp(argv[p], "--lang=", 7)) {
eoskeygen::Dictionary d;
std::string lang(argv[p] + 7);
std::string filename(std::string(CONFIG_SHARE_FULL_PATH) + "/dict/" + lang);
if (d.loadFromFile(filename)) {
dict.add(d);
} else {
std::cerr << "Could not load language " << lang << " (" << filename << ")" << std::endl;
}
}
// Error out on any flag we don't support.
else if (argv[p][0] == '-') {
std::cerr << "Unrecognized flag: " << argv[p] << std::endl;
usage(argv[0]);
return 0;
}
// wordlist and count
else if (words.size() < 1) {
std::string arg = std::string(argv[p]);
if (arg.rfind("file:", 0) == 0) {
std::string filename = arg.substr(5);
if (!eoskeygen::readLines(filename, words)) {
std::cerr << "Could not read file: " << filename << std::endl;
return 0;
}
if (words.size() < 1) {
std::cerr << filename << " did not contain any words" << std::endl;
return 0;
}
}
// List
else {
words = eoskeygen::strlist::splitw(arg);
}
if (p + 1 < argc) {
count = atoi(argv[++p]);
if (count < 1) {
count = 1;
}
}
}
}
if (words.size() < 1) {
std::cerr << "You must specify a word list." << std::endl;
usage(argv[0]);
return 1;
}
return cmd_search(words, dict, count);
}
// Benchmark
else if (!strcmp(argv[p], "benchmark")) {
int num_keys = 1000;
if (++p < argc) {
num_keys = atoi(argv[p]);
if (num_keys < 1) {
num_keys = 1;
}
}
cmd_benchmark(num_keys);
} else {
std::cerr << "Unrecogniced command: " << argv[1] << std::endl;
usage(argv[0]);
return 1;
}
return 0;
}