forked from moneroexamples/openmonero
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·206 lines (160 loc) · 6.03 KB
/
main.cpp
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include "src/CmdLineOptions.h"
#include "src/MicroCore.h"
#include "src/YourMoneroRequests.h"
#include <iostream>
#include <memory>
#include <cstdlib>
using namespace std;
using namespace restbed;
using boost::filesystem::path;
int
main(int ac, const char* av[])
{
// get command line options
xmreg::CmdLineOptions opts {ac, av};
auto help_opt = opts.get_option<bool>("help");
// if help was chosen, display help text and finish
if (*help_opt)
{
return EXIT_SUCCESS;
}
auto do_not_relay_opt = opts.get_option<bool>("do-not-relay");
auto testnet_opt = opts.get_option<bool>("testnet");
auto port_opt = opts.get_option<string>("port");
auto config_file_opt = opts.get_option<string>("config-file");
bool testnet = *testnet_opt;
bool do_not_relay = *do_not_relay_opt;
// check if config-file provided exist
if (!boost::filesystem::exists(*config_file_opt))
{
cerr << "Config file " << *config_file_opt
<< " does not exist" << endl;
return EXIT_FAILURE;
}
nlohmann::json config_json;
try
{
// try reading and parsing json config file provided
std::ifstream i(*config_file_opt);
i >> config_json;
}
catch (const std::exception& e)
{
cerr << "Error reading confing file "
<< *config_file_opt << ": "
<< e.what() << endl;
return EXIT_FAILURE;
}
//cast port number in string to uint16
uint16_t app_port = boost::lexical_cast<uint16_t>(*port_opt);
// get blockchain path
// if confing.json paths are emtpy, defeault monero
// paths are going to be used
path blockchain_path = testnet
? path(config_json["blockchain-path"]["testnet"].get<string>())
: path(config_json["blockchain-path"]["mainnet"].get<string>());
if (!xmreg::get_blockchain_path(blockchain_path, testnet))
{
cerr << "Error getting blockchain path." << endl;
return EXIT_FAILURE;
}
cout << "Blockchain path: " << blockchain_path.string() << endl;
string deamon_url = testnet
? config_json["daemon-url"]["testnet"]
: config_json["daemon-url"]["mainnet"];
// set mysql/mariadb connection details
xmreg::MySqlConnector::url = config_json["database"]["url"];
xmreg::MySqlConnector::username = config_json["database"]["user"];
xmreg::MySqlConnector::password = config_json["database"]["password"];
xmreg::MySqlConnector::dbname = config_json["database"]["dbname"];
// set blockchain status monitoring thread parameters
xmreg::CurrentBlockchainStatus::blockchain_path
= blockchain_path.string();
xmreg::CurrentBlockchainStatus::testnet
= testnet;
xmreg::CurrentBlockchainStatus::do_not_relay
= do_not_relay;
xmreg::CurrentBlockchainStatus::deamon_url
= deamon_url;
xmreg::CurrentBlockchainStatus::refresh_block_status_every_seconds
= config_json["refresh_block_status_every_seconds"];
xmreg::CurrentBlockchainStatus::search_thread_life_in_seconds
= config_json["search_thread_life_in_seconds"];
xmreg::CurrentBlockchainStatus::import_fee
= config_json["wallet_import"]["fee"];
if (testnet)
{
xmreg::CurrentBlockchainStatus::import_payment_address
= config_json["wallet_import"]["testnet"]["address"];
xmreg::CurrentBlockchainStatus::import_payment_viewkey
= config_json["wallet_import"]["testnet"]["viewkey"];
}
else
{
xmreg::CurrentBlockchainStatus::import_payment_address
= config_json["wallet_import"]["mainnet"]["address"];
xmreg::CurrentBlockchainStatus::import_payment_viewkey
= config_json["wallet_import"]["mainnet"]["viewkey"];
}
// since CurrentBlockchainStatus class monitors current status
// of the blockchain (e.g., current height), its seems logical to
// make static objects for accessing the blockchain in this class.
// this way monero accessing blockchain variables (i.e. mcore and core_storage)
// are not passed around like crazy everywhere. Uri( "file:///tmp/dh2048.pem"
// There are here, and this is the only class that
// has direct access to blockchain and talks (using rpc calls)
// with the deamon.
if (!xmreg::CurrentBlockchainStatus::init_monero_blockchain())
{
cerr << "Error accessing blockchain." << endl;
return EXIT_FAILURE;
}
// launch the status monitoring thread so that it keeps track of blockchain
// info, e.g., current height. Information from this thread is used
// by tx searching threads that are launched for each user independently,
// when they log back or create new account.
xmreg::CurrentBlockchainStatus::start_monitor_blockchain_thread();
// create REST JSON API services
xmreg::YourMoneroRequests open_monero(
shared_ptr<xmreg::MySqlAccounts>(new xmreg::MySqlAccounts{}));
// create Open Monero APIs
MAKE_RESOURCE(login);
MAKE_RESOURCE(get_address_txs);
MAKE_RESOURCE(get_address_info);
MAKE_RESOURCE(get_unspent_outs);
MAKE_RESOURCE(get_random_outs);
MAKE_RESOURCE(submit_raw_tx);
MAKE_RESOURCE(import_wallet_request);
MAKE_RESOURCE(get_version);
// restbed service
Service service;
// Publish the Open Monero API created so that front end can use it
service.publish(login);
service.publish(get_address_txs);
service.publish(get_address_info);
service.publish(get_unspent_outs);
service.publish(get_random_outs);
service.publish(submit_raw_tx);
service.publish(import_wallet_request);
service.publish(get_version);
auto settings = make_shared<Settings>( );
if (config_json["ssl"]["enable"])
{
auto ssl_settings = make_shared<SSLSettings>( );
ssl_settings->set_http_disabled( true );
ssl_settings->set_port(app_port);
ssl_settings->set_private_key( Uri( "file:///tmp/mwo.key" ) );
ssl_settings->set_certificate( Uri( "file:///tmp/mwo.crt" ) );
ssl_settings->set_temporary_diffie_hellman( Uri( "file:///tmp/dh2048.pem" ) );
settings->set_ssl_settings(ssl_settings);
cout << "Start the service at https://localhost:" << app_port << endl;
}
else
{
settings->set_port(app_port);
settings->set_default_header( "Connection", "close" );
cout << "Start the service at http://localhost:" << app_port << endl;
}
service.start(settings);
return EXIT_SUCCESS;
}