-
Notifications
You must be signed in to change notification settings - Fork 136
/
YourMoneroRequests.cpp
executable file
·1979 lines (1523 loc) · 65.4 KB
/
YourMoneroRequests.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Created by mwo on 8/01/17.
//
#define MYSQLPP_SSQLS_NO_STATICS 1
#include "YourMoneroRequests.h"
#include "ssqlses.h"
#include "OutputInputIdentification.h"
namespace xmreg
{
handel_::handel_(const fetch_func_t& callback):
request_callback {callback}
{}
void
handel_::operator()(const shared_ptr< Session > session)
{
const auto request = session->get_request( );
size_t content_length = request->get_header( "Content-Length", 0);
session->fetch(content_length, this->request_callback);
}
YourMoneroRequests::YourMoneroRequests(
shared_ptr<MySqlAccounts> _acc,
shared_ptr<CurrentBlockchainStatus> _current_bc_status):
xmr_accounts {_acc}, current_bc_status {_current_bc_status}
{
}
void
YourMoneroRequests::login(const shared_ptr<Session> session, const Bytes & body)
{
json j_response;
json j_request;
vector<string> required_values {"address", "view_key"};
if (!parse_request(body, required_values, j_request, j_response))
{
session_close(session, j_response.dump());
return;
}
string xmr_address;
string view_key;
try
{
xmr_address = j_request["address"];
view_key = j_request["view_key"];
}
catch (json::exception const& e)
{
cerr << "json exception: " << e.what() << '\n';
session_close(session, j_response.dump());
return;
}
// a placeholder for exciting or new account data
XmrAccount acc;
uint64_t acc_id {0};
// marks if this is new account creation or not
bool new_account_created {false};
// first check if new account
// select this account if its existing one
if (!xmr_accounts->select(xmr_address, acc))
{
// account does not exist, so create new one
// for this address
uint64_t current_blockchain_height = get_current_blockchain_height();
// initialize current blockchain timestamp with current time
// in a moment we will try to get last block timestamp
// to replace this value. But if it fails, we just use current
// timestamp
uint64_t current_blockchain_timestamp = std::time(nullptr);
// get last block so we have its timestamp when
// createing the account
block last_blk;
if (current_bc_status->get_block(current_blockchain_height, last_blk))
{
current_blockchain_timestamp = last_blk.timestamp;
}
DateTime blk_timestamp_mysql_format
= XmrTransaction::timestamp_to_DateTime(
current_blockchain_timestamp);
// create new account
XmrAccount new_account(
mysqlpp::null,
xmr_address,
make_hash(view_key),
current_blockchain_height, /* for scanned_block_height */
blk_timestamp_mysql_format,
current_blockchain_height);
// insert the new account into the mysql
if ((acc_id = xmr_accounts->insert(new_account)) == 0)
{
// if creating account failed
j_response = json {{"status", "error"},
{"reason", "Account creation failed"}};
session_close(session, j_response.dump());
return;
}
// set this flag to indicate that we have just created a
// new account in mysql. this information is sent to front-end
// as it can disply some greeting window to new users upon
// their first install
new_account_created = true;
} // if (!xmr_accounts->select(xmr_address, acc))
// so by now new account has been created or it already exists
// so we just login into it.
if (login_and_start_search_thread(xmr_address, view_key, acc, j_response))
{
// if successfuly logged in and created search thread
j_response["status"] = "success";
// we overwrite what ever was sent in login_and_start_search_thread
// for the j_response["new_address"].
j_response["new_address"] = new_account_created;
}
else
{
// some error with loggin in or search thread start
session_close(session, j_response.dump());
return;
} // else if (login_and_start_search_thread(xmr_address,
session_close(session, j_response.dump());
}
void
YourMoneroRequests::get_address_txs(
const shared_ptr< Session > session, const Bytes & body)
{
json j_response;
json j_request;
vector<string> requested_values {"address", "view_key"};
if (!parse_request(body, requested_values, j_request, j_response))
{
session_close(session, j_response.dump());
return;
}
string xmr_address;
string view_key;
try
{
xmr_address = j_request["address"];
view_key = j_request["view_key"];
}
catch (json::exception const& e)
{
cerr << "json exception: " << e.what() << '\n';
session_close(session, j_response.dump());
return;
}
// make hash of the submited viewkey. we only store
// hash of viewkey in database, not acctual viewkey.
string viewkey_hash = make_hash(view_key);
// initialize json response
j_response = json {
{"total_received" , 0}, // calculated in this function
{"total_received_unlocked", 0}, // calculated in this function
{"scanned_height" , 0}, // not used. just to match mymonero
{"scanned_block_height" , 0}, // taken from Accounts table
{"scanned_block_timestamp", 0}, // taken from Accounts table
{"start_height" , 0}, // blockchain height whencreated
{"blockchain_height" , 0}, // current blockchain height
{"transactions" , json::array()}
};
// a placeholder for exciting or new account data
xmreg::XmrAccount acc;
// for this to continue, search thread must have already been
// created and still exisits.
// if (current_bc_status->search_thread_exist(xmr_address))
// {
if (login_and_start_search_thread(xmr_address, view_key, acc, j_response))
{
// populate acc and check view_key
// if (!login_and_start_search_thread(xmr_address, view_key, acc, j_response))
// {
// // some error with loggin in or search thread start
// session_close(session, j_response.dump());
// return;
// }
// before fetching txs, check if provided view key
// is correct. this is simply to ensure that
// we cant fetch an account's txs using only address.
// knowlage of the viewkey is also needed.
uint64_t total_received {0};
uint64_t total_received_unlocked {0};
j_response["total_received"] = total_received;
j_response["start_height"] = acc.start_height;
j_response["scanned_block_height"] = acc.scanned_block_height;
j_response["scanned_block_timestamp"] = static_cast<uint64_t>(
acc.scanned_block_timestamp);
j_response["blockchain_height"] = get_current_blockchain_height();
vector<XmrTransaction> txs;
xmr_accounts->select(acc.id.data, txs);
if (xmr_accounts->select_txs_for_account_spendability_check(
acc.id.data, txs))
{
json j_txs = json::array();
for (XmrTransaction const& tx: txs)
{
json j_tx {
{"id" , tx.blockchain_tx_id},
{"coinbase" , bool {tx.coinbase}},
{"tx_pub_key" , tx.tx_pub_key},
{"hash" , tx.hash},
{"height" , tx.height},
{"mixin" , tx.mixin},
{"payment_id" , tx.payment_id},
{"unlock_time" , tx.unlock_time},
{"total_sent" , 0}, // to be field when checking for spent_outputs below
{"total_received" , std::to_string(tx.total_received)},
{"timestamp" , static_cast<uint64_t>(tx.timestamp)*1000},
{"mempool" , false} // tx in database are never from mempool
};
vector<XmrInput> inputs;
if (xmr_accounts->select_for_tx(tx.id.data, inputs))
{
json j_spent_outputs = json::array();
uint64_t total_spent {0};
for (XmrInput input: inputs)
{
XmrOutput out;
if (xmr_accounts->select_by_primary_id(
input.output_id, out))
{
total_spent += input.amount;
j_spent_outputs.push_back({
{"amount" , std::to_string(input.amount)},
{"key_image" , input.key_image},
{"tx_pub_key" , out.tx_pub_key},
{"out_index" , out.out_index},
{"mixin" , out.mixin}});
}
}
j_tx["total_sent"] = std::to_string(total_spent);
j_tx["spent_outputs"] = j_spent_outputs;
} // if (xmr_accounts->select_inputs_for_tx(tx.id, inputs))
total_received += tx.total_received;
if (bool {tx.spendable})
{
total_received_unlocked += tx.total_received;
}
j_txs.push_back(j_tx);
} // for (XmrTransaction tx: txs)
j_response["total_received"] = std::to_string(total_received);
j_response["total_received_unlocked"] = std::to_string(total_received_unlocked);
j_response["transactions"] = j_txs;
} // if (xmr_accounts->select_txs_for_ac
} // if (current_bc_status->search_thread_exist(xmr_address))
else
{
j_response = json {{"status", "error"},
{"reason", "Search thread does not exist."}};
// some error with loggin in or search thread start
session_close(session, j_response.dump());
return;
}
// append txs found in mempool to the json returned
json j_mempool_tx;
if (current_bc_status->find_txs_in_mempool(
xmr_address, j_mempool_tx))
{
if(!j_mempool_tx.empty())
{
uint64_t total_received_mempool {0};
uint64_t total_sent_mempool {0};
// get last tx id (i.e., index) so that we can
// set some ids for the mempool txs. These ids are
// used for sorting in the frontend. Since we want mempool
// tx to be first, they need to be higher than last_tx_id_db
uint64_t last_tx_id_db {0};
if (!j_response["transactions"].empty())
{
last_tx_id_db = j_response["transactions"].back()["id"];
}
for (json& j_tx: j_mempool_tx)
{
//cout << "mempool j_tx[\"total_received\"]: "
// << j_tx["total_received"] << endl;
j_tx["id"] = ++last_tx_id_db;
total_received_mempool += boost::lexical_cast<uint64_t>(
j_tx["total_received"].get<string>());
total_sent_mempool += boost::lexical_cast<uint64_t>(
j_tx["total_sent"].get<string>());
j_response["transactions"].push_back(j_tx);
}
// we account for mempool txs when providing final
// unlocked and locked balances.
j_response["total_received"]
= std::to_string(
boost::lexical_cast<uint64_t>(
j_response["total_received"].get<string>())
+ total_received_mempool - total_sent_mempool);
j_response["total_received_unlocked"]
= std::to_string(
boost::lexical_cast<uint64_t>(
j_response["total_received_unlocked"].get<string>())
+ total_received_mempool - total_sent_mempool);
}
}
string response_body = j_response.dump();
auto response_headers = make_headers({{ "Content-Length",
to_string(response_body.size())}});
session->close(OK, response_body, response_headers);
}
void
YourMoneroRequests::get_address_info(
const shared_ptr< Session > session, const Bytes & body)
{
json j_response;
json j_request;
vector<string> requested_values {"address" , "view_key"};
if (!parse_request(body, requested_values, j_request, j_response))
{
session_close(session, j_response.dump());
return;
}
string xmr_address;
string view_key;
try
{
xmr_address = j_request["address"];
view_key = j_request["view_key"];
}
catch (json::exception const& e)
{
cerr << "json exception: " << e.what() << '\n';
session_close(session, j_response.dump());
return;
}
// make hash of the submited viewkey. we only store
// hash of viewkey in database, not acctual viewkey.
string viewkey_hash = make_hash(view_key);
j_response = json {
{"locked_funds" , "0"}, // locked xmr (e.g., younger than 10 blocks)
{"total_received" , "0"}, // calculated in this function
{"total_sent" , "0"}, // calculated in this function
{"scanned_height" , 0}, // not used. it is here to match mymonero
{"scanned_block_height" , 0}, // taken from Accounts table
{"scanned_block_timestamp", 0}, // taken from Accounts table
{"start_height" , 0}, // not used, but available in Accounts table.
// it is here to match mymonero
{"blockchain_height" , 0}, // current blockchain height
{"spent_outputs" , nullptr} // list of spent outputs that we think
// user has spent. client side will
// filter out false positives since
// only client has spent key
};
// a placeholder for exciting or new account data
xmreg::XmrAccount acc;
// for this to continue, search thread must have already been
// created and still exisits.
if (login_and_start_search_thread(xmr_address, view_key, acc, j_response))
{
uint64_t total_received {0};
// ping the search thread that we still need it.
// otherwise it will finish after some time.
current_bc_status->ping_search_thread(xmr_address);
uint64_t current_searched_blk_no {0};
if (current_bc_status->get_searched_blk_no(
xmr_address, current_searched_blk_no))
{
// if current_searched_blk_no is higher than what is in mysql, update it
// in the search thread. This may occure when manually editing scanned_block_height
// in Accounts table to import txs or rescan txs.
// we use the minumum difference of 10 blocks, for this update to happen
if (current_searched_blk_no > acc.scanned_block_height + 10)
{
current_bc_status->set_new_searched_blk_no(
xmr_address, acc.scanned_block_height);
}
}
j_response["total_received"] = std::to_string(total_received);
j_response["start_height"] = acc.start_height;
j_response["scanned_block_height"] = acc.scanned_block_height;
j_response["scanned_block_timestamp"]
= static_cast<uint64_t>(acc.scanned_block_timestamp);
j_response["blockchain_height"] = get_current_blockchain_height();
uint64_t total_sent {0};
vector<XmrTransaction> txs;
// get all txs of for the account
xmr_accounts->select(acc.id.data, txs);
// now, filter out or updated transactions from txs vector that no
// longer exisit in the recent blocks. Update is done to check for their
// spendability status.
if (xmr_accounts->select_txs_for_account_spendability_check(
acc.id.data, txs))
{
json j_spent_outputs = json::array();
for (XmrTransaction tx: txs)
{
vector<XmrOutput> outs;
if (xmr_accounts->select_for_tx(tx.id.data, outs))
{
for (XmrOutput &out: outs)
{
// check if the output, has been spend
vector<XmrInput> ins;
if (xmr_accounts->select_inputs_for_out(
out.id.data, ins))
{
for (XmrInput& in: ins)
{
j_spent_outputs.push_back({
{"amount" , std::to_string(in.amount)},
{"key_image" , in.key_image},
{"tx_pub_key" , out.tx_pub_key},
{"out_index" , out.out_index},
{"mixin" , out.mixin},
});
total_sent += in.amount;
}
}
total_received += out.amount;
} // for (XmrOutput &out: outs)
} // if (xmr_accounts->select_outputs_for_tx(tx.id, outs))
} // for (XmrTransaction tx: txs)
j_response["total_received"] = std::to_string(total_received);
j_response["total_sent"] = std::to_string(total_sent);
j_response["spent_outputs"] = j_spent_outputs;
} // if (xmr_accounts->select_txs_for_account_spendability_check(acc.id, txs))
} // if (current_bc_status->search_thread_exist(xmr_address))
else
{
j_response = json {{"status", "error"},
{"reason", "Search thread does not exist."}};
// some error with loggin in or search thread start
session_close(session, j_response.dump());
return;
}
string response_body = j_response.dump();
auto response_headers = make_headers({{ "Content-Length",
to_string(response_body.size())}});
session->close( OK, response_body, response_headers);
}
void
YourMoneroRequests::get_unspent_outs(
const shared_ptr< Session > session,
const Bytes & body)
{
json j_response;
json j_request;
vector<string> requested_values {"address" , "view_key", "mixin",
"use_dust", "dust_threshold", "amount"};
if (!parse_request(body, requested_values, j_request, j_response))
{
session_close(session, j_response.dump());
return;
}
string xmr_address;
string view_key;
uint64_t mixin {4};
bool use_dust {false};
uint64_t dust_threshold {1000000000};
uint64_t amount {0};
try
{
xmr_address = j_request["address"];
view_key = j_request["view_key"];
mixin = j_request["mixin"];
use_dust = j_request["use_dust"];
dust_threshold = boost::lexical_cast<uint64_t>(
j_request["dust_threshold"].get<string>());
amount = boost::lexical_cast<uint64_t>(
j_request["amount"].get<string>());
}
catch (json::exception const& e)
{
cerr << "json exception: " << e.what() << '\n';
session_close(session, j_response.dump());
return;
}
catch (boost::bad_lexical_cast const& e)
{
cerr << "Bed lexical cast" << e.what() << '\n';
session_close(session, j_response.dump());
return;
}
// make hash of the submited viewkey. we only store
// hash of viewkey in database, not acctual viewkey.
string viewkey_hash = make_hash(view_key);
j_response = json {
{"amount" , "0"}, // total value of the outputs
{"outputs", json::array()} // list of outputs
// exclude those without require
// no of confirmation
};
// a placeholder for exciting or new account data
xmreg::XmrAccount acc;
// select this account if its existing one
// for this to continue, search thread must have already been
// created and still exisits.
if (current_bc_status->search_thread_exist(xmr_address))
{
// populate acc and check view_key
if (!login_and_start_search_thread(xmr_address, view_key, acc, j_response))
{
// some error with loggin in or search thread start
session_close(session, j_response.dump());
return;
}
uint64_t total_outputs_amount {0};
// uint64_t current_blockchain_height
// = current_bc_status->get_current_blockchain_height();
vector<XmrTransaction> txs;
// retrieve txs from mysql associated with the given address
if (xmr_accounts->select(acc.id.data, txs))
{
// we found some txs.
json& j_outputs = j_response["outputs"];
for (XmrTransaction& tx: txs)
{
// we skip over locked outputs
// as they cant be spent anyway.
// thus no reason to return them to the frontend
// for constructing a tx.
if (!current_bc_status->is_tx_unlocked(
tx.unlock_time, tx.height))
{
continue;
}
// if (!bool {tx.coinbase})
// {
// continue;
// }
vector<XmrOutput> outs;
if (xmr_accounts->select_for_tx(tx.id.data, outs))
{
for (XmrOutput &out: outs)
{
// skip outputs considered as dust
if (out.amount < dust_threshold)
{
continue;
}
// need to check for rct commintment
// coinbase ringct txs dont have
// rct filed in them. Thus
// we need to make them.
uint64_t global_amount_index = out.global_index;
string rct = out.get_rct();
// coinbase rct txs require speciall treatment
if (tx.coinbase && tx.is_rct)
{
uint64_t amount = (tx.is_rct ? 0 : out.amount);
output_data_t od =
current_bc_status->get_output_key(
amount, global_amount_index);
string rtc_outpk = pod_to_hex(od.commitment);
string rtc_mask = pod_to_hex(rct::identity());
string rtc_amount(64, '0');
rct = rtc_outpk + rtc_mask + rtc_amount;
}
json j_out{
{"amount" , std::to_string(out.amount)},
{"public_key" , out.out_pub_key},
{"index" , out.out_index},
{"global_index" , out.global_index},
{"rct" , rct},
{"tx_id" , out.tx_id},
{"tx_hash" , tx.hash},
{"tx_prefix_hash" , tx.prefix_hash},
{"tx_pub_key" , tx.tx_pub_key},
{"timestamp" , static_cast<uint64_t>(
out.timestamp*1e3)},
{"height" , tx.height},
{"spend_key_images", json::array()}
};
vector<XmrInput> ins;
if (xmr_accounts->select_inputs_for_out(
out.id.data, ins))
{
json& j_ins = j_out["spend_key_images"];
for (XmrInput& in: ins)
{
j_ins.push_back(in.key_image);
}
}
j_outputs.push_back(j_out);
total_outputs_amount += out.amount;
} //for (XmrOutput &out: outs)
} // if (xmr_accounts->select_outputs_for_tx(tx.id, outs))
} // for (XmrTransaction& tx: txs)
} // if (xmr_accounts->select_txs(acc.id, txs))
j_response["amount"] = std::to_string(total_outputs_amount);
// need proper per_kb_fee estimate as
// it is already using dynanamic fees. frontend
// uses old fixed fees.
j_response["per_byte_fee"] = current_bc_status
->get_dynamic_base_fee_estimate();
} // if (current_bc_status->search_thread_exist(xmr_address))
else
{
j_response = json {{"status", "error"},
{"reason", "Search thread does not exist."}};
// some error with loggin in or search thread start
session_close(session, j_response.dump());
return;
}
string response_body = j_response.dump();
auto response_headers = make_headers({{ "Content-Length",
to_string(response_body.size())}});
session->close( OK, response_body, response_headers);
}
void
YourMoneroRequests::get_random_outs(
const shared_ptr< Session > session, const Bytes & body)
{
json j_request;
json j_response;
vector<string> requested_values;
if (!parse_request(body, requested_values, j_request, j_response))
{
session_close(session, j_response.dump());
return;
}
uint64_t count;
try
{
count = j_request["count"];
}
catch (json::exception const& e)
{
cerr << "json exception: " << e.what() << '\n';
session_close(session, j_response.dump());
return;
};
if (count > 41)
{
cerr << "Request ring size too big" << '\n';
j_response["status"] = "error";
j_response["error"] = fmt::format("Request ring size {:d} too large",
count);
session_close(session, j_response.dump());
}
vector<uint64_t> amounts;
try
{
// populate amounts vector so that we can pass it directly to
// daeamon to get random outputs for these amounts
for (json amount: j_request["amounts"])
{
amounts.push_back(boost::lexical_cast<uint64_t>(
amount.get<string>()));
// amounts.push_back(0);
}
}
catch (boost::bad_lexical_cast& e)
{
cerr << "Bed lexical cast" << '\n';
session_close(session, j_response.dump());
return;
}
vector<RandomOutputs::outs_for_amount> found_outputs;
if (current_bc_status->get_random_outputs(amounts, count, found_outputs))
{
json& j_amount_outs = j_response["amount_outs"];
for (const auto& outs: found_outputs)
{
json j_outs {{"amount", std::to_string(outs.amount)},
{"outputs", json::array()}};
json& j_outputs = j_outs["outputs"];
for (auto const& out: outs.outs)
{
tuple<string, string, string>
rct_field = current_bc_status
->construct_output_rct_field(
out.global_amount_index, outs.amount);
string rct = std::get<0>(rct_field) // rct_pk
+ std::get<1>(rct_field) // rct_mask
+ std::get<2>(rct_field); // rct_amount
json out_details {
{"global_index", out.global_amount_index},
{"public_key" , pod_to_hex(out.out_key)},
{"rct" , rct}
};
j_outputs.push_back(out_details);
} // for (const COMMAND_RPC_GET_RANDOM_OUTPUTS_FOR_AM
j_amount_outs.push_back(j_outs);
} // for (const COMMAND_RPC_GET_RANDOM_OUTPUTS_
} // if (current_bc_status->get_random_outputs(amounts,
else
{
j_response["status"] = "error";
j_response["error"] = fmt::format("Error getting random "
"outputs from monero deamon");
}
string response_body = j_response.dump();
auto response_headers = make_headers({{ "Content-Length",
to_string(response_body.size())}});
session->close( OK, response_body, response_headers);
}
void
YourMoneroRequests::submit_raw_tx(
const shared_ptr< Session > session, const Bytes & body)
{
json j_request = body_to_json(body);
string raw_tx_blob = j_request["tx"];
json j_response;
string error_msg;
// before we submit the tx into the deamon, we are going to do a few checks.
// first, we parse the hexbuffer submmited by the frontend into
// binary buffer block
// second, we are going to check if we can construct
// valid transaction object
// fromt that binary buffer.
// third, we are going to check if any key image in this tx is
// in any of the txs
// in the mempool. This allows us to make a clear comment that the tx
// uses outputs just spend. This happens we a users submits few txs,
// one after another
// before previous txs get included in a block and are still
// present in the mempool.
std::string tx_blob;
if(!epee::string_tools::parse_hexstr_to_binbuff(raw_tx_blob, tx_blob))
{
j_response["status"] = "error";
j_response["error"] = "Tx faild parse_hexstr_to_binbuff";
OMERROR << j_response["error"];
session_close(session, j_response.dump());
return;
}
transaction tx_to_be_submitted;
if (!parse_and_validate_tx_from_blob(tx_blob, tx_to_be_submitted))
{
j_response["status"] = "error";
j_response["error"] = "Tx faild parse_and_validate_tx_from_blob";
OMERROR << j_response["error"];
session_close(session, j_response.dump());
return;
}
if (current_bc_status->find_key_images_in_mempool(tx_to_be_submitted))
{
j_response["status"] = "error";
j_response["error"] = "Tx uses your outputs that area already "
"in the mempool. "
"Please wait till your previous tx(s) "
"get mined";
OMERROR << j_response["error"];
session_close(session, j_response.dump());
return;
}
if (!current_bc_status->commit_tx(
raw_tx_blob, error_msg,
current_bc_status->get_bc_setup().do_not_relay))
{
j_response["status"] = "error";
j_response["error"] = error_msg;
OMERROR << j_response["error"];
session_close(session, j_response.dump());
return;
}
j_response["status"] = "success";
string response_body = j_response.dump();
auto response_headers = make_headers({{ "Content-Length",
to_string(response_body.size())}});
session->close( OK, response_body, response_headers);
}
void
YourMoneroRequests::import_wallet_request(
const shared_ptr< Session > session, const Bytes & body)
{
json j_request = body_to_json(body);
string xmr_address = j_request["address"];
json j_response;
j_response["request_fulfilled"] = false;
j_response["import_fee"] = std::to_string(
current_bc_status->get_bc_setup()
.import_fee);
j_response["status"] = "error";
j_response["error"] = "Some error occured";
// if current_bc_status-> is zero, we just import the wallet.
// we dont care about any databases or anything, as importin all
// wallet is free.
// just reset the scanned block height in mysql and finish.