-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
115 lines (91 loc) · 3.13 KB
/
main.cpp
File metadata and controls
115 lines (91 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Bot.h"
#include "DB.h"
#include "version.h"
#include <HexCoding.h>
#include <Coin.h>
#include <Keystore/StoredKey.h>
#include <easylogging++.h>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
#include <openssl/crypto.h>
INITIALIZE_EASYLOGGINGPP
const TWCoinType coin_type = TWCoinType::TWCoinTypeEthereum;
const int PASSWORD_LEN = 100;
boost::asio::io_service io;
nlohmann::json load_config(const std::string& fn)
{
std::ifstream in(fn);
if (!in) {
LOG(ERROR) << "Cannot open config file: " << fn;
exit(1);
}
std::stringstream buffer;
buffer << in.rdbuf();
in.close();
//LOG(DEBUG) << "Contents of " << fn << ": " << buffer.str();
return Bot::parse_json(buffer.str());
}
void store_wallet(const std::string& keystore, const std::string& name, const std::string& private_key, const std::string& password)
{
TW::PrivateKey priv(TW::parse_hex(private_key));
TW::Data pass(password.begin(), password.end());
auto stored_key = TW::Keystore::StoredKey::createWithPrivateKey(name, pass, priv.bytes);
stored_key.store(keystore);
}
TW::PrivateKey load_wallet(const std::string& keystore, const char* password)
{
TW::Data pass(password, password + strlen(password));
OPENSSL_cleanse((void*)password, PASSWORD_LEN);
auto stored_key = TW::Keystore::StoredKey::load(keystore);
return stored_key.privateKey(coin_type, pass);
}
int main(int argc, char* argv[])
{
START_EASYLOGGINGPP(argc, argv);
el::Configurations defaultConf;
defaultConf.setToDefault();
defaultConf.setGlobally(el::ConfigurationType::Format, "%datetime | %msg");
el::Loggers::reconfigureLogger("default", defaultConf);
try {
LOG(INFO) << "=======================";
LOG(INFO) << "compounding-bot version " << VERSION << " started";
if (argc > 1) {
nlohmann::json cfg = load_config(argv[1]);
std::string keystore_pass;
if (cfg["keystore_pass"].is_string() && !std::string(cfg["keystore_pass"]).empty())
keystore_pass = cfg["keystore_pass"];
std::string database_pass = cfg["database"]["pass"];
cfg["database"]["pass"] = "";
cfg["keystore_pass"] = "";
LOG(DEBUG) << "Contents of " << argv[1] << ": " << Bot::pretty_print(cfg, true);
bool keys_present = cfg["secret"].is_string() && !cfg["secret"].empty() && cfg["wallet"].is_string() && !cfg["wallet"].empty();
if (!keys_present) {
if (keystore_pass.empty())
keystore_pass = getpass("Enter password for keystore: ");
auto privateKey = load_wallet(cfg["keystore"], keystore_pass.c_str());
cfg["secret"] = TW::hex(privateKey.bytes);
cfg["wallet"] = TW::deriveAddress(coin_type, privateKey).substr(2);
//LOG(INFO) << cfg["secret"].asString();
//LOG(INFO) << cfg["wallet"].asString();
}
DB db;
db.connect(cfg["database"]["host"], cfg["database"]["user"], database_pass, cfg["database"]["db"]);
Bot bot(cfg, io, &db);
bot.init();
bot.start();
io.run();
}
else {
LOG(ERROR) << "Usage: ./compounding-bot <config.json>";
}
LOG(INFO) << "compounding-bot finished\n\n";
}
catch (TW::Keystore::DecryptionError&) {
LOG(ERROR) << "DecryptionError";
}
catch (std::exception& e) {
LOG(ERROR) << "Exception: " << e.what();
}
return 0;
}