forked from David4860/navcoin-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnavtech.cpp
More file actions
360 lines (292 loc) · 11.7 KB
/
Copy pathnavtech.cpp
File metadata and controls
360 lines (292 loc) · 11.7 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include "navtech.h"
#include "net.h"
#include "util.h"
#include "utilstrencodings.h"
#include <stdexcept>
#include <algorithm>
#include <sstream>
#include "rpc/server.h"
#include <stdio.h>
#include <iterator>
#include <openssl/aes.h>
using namespace std;
CURL *curl;
CURLcode res;
int padding = RSA_PKCS1_PADDING;
int encResultLength = 344;
static size_t CurlWriteResponse(void *contents, size_t size, size_t nmemb, void *userp) {
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
UniValue Navtech::CreateAnonTransaction(string address, CAmount nValue, int nTransactions) {
vector<anonServer> anonServers = this->GetAnonServers();
UniValue serverData = this->FindAnonServer(anonServers, nValue, nTransactions);
UniValue pubKey = find_value(serverData, "public_key");
string encryptedAddress = this->EncryptAddress(address, pubKey.get_str());
bool encryptionSuccess = this->TestEncryption(encryptedAddress, serverData);
if (encryptionSuccess) {
UniValue addresses = find_value(serverData, "nav_addresses");
UniValue addrArray = addresses.get_array();
UniValue navtechData;
navtechData.setObject();
navtechData.pushKV("anondestination", encryptedAddress);
navtechData.pushKV("public_key", pubKey);
navtechData.pushKV("anonaddress", addrArray);
navtechData.pushKV("anonfee", find_value(serverData, "transaction_fee"));
navtechData.pushKV("min_amount", find_value(serverData, "min_amount"));
return navtechData;
} else {
throw runtime_error("Unable to send NAVTech transaction, please try again.");
}
}
vector<anonServer> Navtech::GetAnonServers() {
vector<anonServer> returnServers;
if (vAddedAnonServers.size() < 1 && mapMultiArgs["-addanonserver"].size() < 1) {
throw runtime_error("You must have at least one NAVTech server added to your conf file or by rpc command.");
return returnServers;
}
vector<string> anonServers = {};
vector<string> confAnonServers = mapMultiArgs["-addanonserver"];
BOOST_FOREACH(string confAnonServer, confAnonServers) {
anonServers.push_back(confAnonServer);
}
BOOST_FOREACH(string vAddedAnonServer, vAddedAnonServers) {
anonServers.push_back(vAddedAnonServer);
}
BOOST_FOREACH(string currentServer, anonServers) {
anonServer tempServer;
string::size_type portPos;
portPos = currentServer.find(':');
if (portPos == std::string::npos) {
tempServer.address = currentServer;
tempServer.port = "443";
} else {
tempServer.address = currentServer.substr(0, portPos);
tempServer.port = currentServer.substr(portPos + 1);
}
size_t periodCount = count(tempServer.address.begin(), tempServer.address.end(), '.');
if(periodCount == 3 && stoi(tempServer.port) > 0) {
returnServers.push_back(tempServer);
}
}
if (returnServers.size() < 1) {
throw runtime_error("Please, add some valid NAVTech server to send private payments.");
return returnServers;
}
return returnServers;
}
UniValue Navtech::FindAnonServer(std::vector<anonServer> anonServers, CAmount nValue, int nTransactions) {
if (anonServers.size() < 1) {
throw runtime_error("None of your configured NAVTech nodes are available right now.");
}
int randIndex = rand() % anonServers.size();
string readBuffer;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
string serverURL = "https://" + anonServers[randIndex].address + ":" + anonServers[randIndex].port + "/api/check-node";
string data = "num_addresses="+std::to_string(nTransactions);
curl_easy_setopt(curl, CURLOPT_URL, serverURL.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
LogPrintf("CURL failed to contact server %s:%s\n", anonServers[randIndex].address, anonServers[randIndex].port);
anonServers.erase(anonServers.begin()+randIndex);
curl_easy_cleanup(curl);
curl_global_cleanup();
return this->FindAnonServer(anonServers, nValue, nTransactions);
} else {
UniValue parsedResponse = this->ParseJSONResponse(readBuffer);
UniValue type = find_value(parsedResponse, "type");
UniValue data_obj = find_value(parsedResponse, "data");
if (!data_obj.isObject()) {
anonServers.erase(anonServers.begin()+randIndex);
return this->FindAnonServer(anonServers, nValue);
} else {
UniValue data = data_obj.get_obj();
if (type.get_str() != "SUCCESS") {
LogPrintf("Server retured bad response %s:%s\n", anonServers[randIndex].address, anonServers[randIndex].port);
anonServers.erase(anonServers.begin()+randIndex);
return this->FindAnonServer(anonServers, nValue);
}
if (nValue != -1) {
UniValue maxAmount = find_value(data, "max_amount");
UniValue minAmount = find_value(data, "min_amount");
int satoshiFactor = 100000000;
if ((nValue*nTransactions*2)/satoshiFactor > maxAmount.get_int() || nValue/satoshiFactor < minAmount.get_int()) {
LogPrintf("Transaction amount outside of specified range min_amount=%i max_amount=%i value=%i\n", minAmount.get_int(), maxAmount.get_int(), nValue/satoshiFactor);
anonServers.erase(anonServers.begin()+randIndex);
curl_easy_cleanup(curl);
curl_global_cleanup();
return this->FindAnonServer(anonServers, nValue);
}
curl_easy_cleanup(curl);
curl_global_cleanup();
return data;
} else {
curl_easy_cleanup(curl);
curl_global_cleanup();
return data;
}
}
}
} else {
curl_easy_cleanup(curl);
curl_global_cleanup();
throw runtime_error("CURL unavailable");
}
}
UniValue Navtech::ParseJSONResponse(string readBuffer) {
try {
UniValue valRequest;
if (!valRequest.read(readBuffer))
throw JSONRPCError(RPC_PARSE_ERROR, "Parse error");
if (valRequest.isObject()) {
const UniValue& request = valRequest.get_obj();
return request;
} else {
throw JSONRPCError(RPC_PARSE_ERROR, "Top-level object parse error");
}
} catch (const UniValue& objError) {
throw runtime_error("ParseJSONResponse objError");
} catch (const std::exception& e) {
throw runtime_error("ParseJSONResponse exception");
}
}
string Navtech::EncryptAddress(string address, string pubKeyStr, int nTransactions, int nPiece, long nId) {
address = "{\"n\":\""+address+"\",\"t\":"+std::to_string(GetArg("anon_out_delay",NAVTECH_DEFAULT_OUT_DELAY))+",\"p\":"+std::to_string(nPiece)+",\"o\":"+std::to_string(nTransactions)+",\"u\":"+std::to_string(nId)+"}";
unsigned char pubKeyChar[4098];
memcpy(pubKeyChar, pubKeyStr.c_str(), pubKeyStr.length());
pubKeyChar[pubKeyStr.length()] = 0;
unsigned char addressChar[4098];
memcpy(addressChar, address.c_str(), address.length());
addressChar[address.length()] = 0;
unsigned char encrypted[4098]={};
int encryptedLength = this->PublicEncrypt(addressChar, (int)address.length(), pubKeyChar, encrypted);
if(encryptedLength == -1)
{
throw runtime_error("Encryption failed");
} else {
string encoded = EncodeBase64(encrypted, encryptedLength);
return encoded;
}
}
int Navtech::PublicEncrypt(unsigned char* data, int data_len, unsigned char* key, unsigned char* encrypted)
{
RSA * rsa = this->CreateRSA(key,1);
if(rsa == NULL || rsa == 0)
return -1;
int result = RSA_public_encrypt(data_len, data, encrypted, rsa,padding);
return result;
}
RSA * Navtech::CreateRSA(unsigned char * key,int isPublic)
{
RSA *rsa= NULL;
BIO *keybio ;
keybio = BIO_new_mem_buf(key, -1);
if (keybio==NULL)
{
LogPrintf("Failed to create key BIO\n");
return 0;
}
if(isPublic)
{
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
}
else
{
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
}
if(rsa == NULL)
{
LogPrintf("Failed to create RSA\n");
}
return rsa;
}
bool Navtech::TestEncryption(string encrypted, UniValue serverData) {
UniValue server = find_value(serverData, "server");
UniValue port = find_value(serverData, "server_port");
string readBuffer;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (curl) {
string serverURL = "https://" + server.get_str() + ":" + to_string(port.get_int()) + "/api/test-decryption";
char* escapedEncrypted = curl_easy_escape(curl, encrypted.c_str(), 0);
string encryptedData = string("encrypted_data=");
encryptedData.append(escapedEncrypted);
curl_easy_setopt(curl, CURLOPT_URL, serverURL.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, encryptedData.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
LogPrintf("CURL failed to contact server %s:%s/api/test-decryption\n", server.get_str(), port.get_str());
} else {
UniValue parsedResponse = this->ParseJSONResponse(readBuffer);
UniValue type = find_value(parsedResponse, "type");
if (type.get_str() != "SUCCESS") {
LogPrintf("Server unable to decrypt address %s:%s\n", server.get_str(), port.get_str());
throw runtime_error("Encryption failed");
return false;
} else {
LogPrintf("Decryption test successful\n");
return true;
}
}
curl_easy_cleanup(curl);
} else {
throw runtime_error("CURL unavailable");
}
curl_global_cleanup();
throw runtime_error("TestEncryption End of Function");
}
UniValue Navtech::GetServerInfo(std::string server) {
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
string readBuffer;
if (curl) {
string serverURL = "https://" + server + "/api/check-node";
curl_easy_setopt(curl, CURLOPT_URL, serverURL.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "num_addresses=1");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, CurlWriteResponse);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
throw runtime_error("CURL failed to contact server");
curl_easy_cleanup(curl);
curl_global_cleanup();
} else {
UniValue parsedResponse = this->ParseJSONResponse(readBuffer);
UniValue type = find_value(parsedResponse, "type");
UniValue data_obj = find_value(parsedResponse, "data");
if (!data_obj.isObject()) {
throw runtime_error("Server not available.");
} else {
UniValue data = data_obj.get_obj();
if (type.get_str() != "SUCCESS") {
throw runtime_error("Server returned bad response");
}
UniValue navtechData;
navtechData.setObject();
navtechData.pushKV("min_amount", find_value(data, "min_amount"));
navtechData.pushKV("max_amount", find_value(data, "max_amount"));
navtechData.pushKV("transaction_fee", find_value(data, "transaction_fee"));
curl_easy_cleanup(curl);
curl_global_cleanup();
return navtechData;
}
}
} else {
curl_easy_cleanup(curl);
curl_global_cleanup();
throw runtime_error("CURL unavailable");
}
}