Repository navigation
Expand file tree
/
Copy pathtools.cpp
More file actions
executable file
·1544 lines (1161 loc) · 35.7 KB
/
Copy pathtools.cpp
File metadata and controls
executable file
·1544 lines (1161 loc) · 35.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
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 marcin on 5/11/15.
//
#include "tools.h"
#include <codecvt>
namespace xmreg
{
/**
* Parse key string, e.g., a viewkey in a string
* into crypto::secret_key or crypto::public_key
* depending on the template argument.
*/
template <typename T>
bool
parse_str_secret_key(const string& key_str, T& secret_key)
{
// hash and keys have same structure, so to parse string of
// a key, e.g., a view key, we can first parse it into the hash
// object using parse_hash256 function, and then copy the reslting
// hash data into secret key.
crypto::hash hash_;
if(!parse_hash256(key_str, hash_))
{
cerr << "Cant parse a key (e.g. viewkey): " << key_str << endl;
return false;
}
// crypto::hash and crypto::secret_key have basicly same
// structure. They both keep they key/hash as c-style char array
// of fixed size. Thus we can just copy data from hash
// to key
copy(begin(hash_.data), end(hash_.data), secret_key.data);
return true;
}
// explicit instantiations of get template function
template bool parse_str_secret_key<crypto::secret_key>(const string& key_str, crypto::secret_key& secret_key);
template bool parse_str_secret_key<crypto::public_key>(const string& key_str, crypto::public_key& secret_key);
template bool parse_str_secret_key<crypto::hash>(const string& key_str, crypto::hash& secret_key);
/**
* Get transaction tx using given tx hash. Hash is represent as string here,
* so before we can tap into the blockchain, we need to pare it into
* crypto::hash object.
*/
bool
get_tx_pub_key_from_str_hash(Blockchain& core_storage, const string& hash_str, transaction& tx)
{
crypto::hash tx_hash;
parse_hash256(hash_str, tx_hash);
try
{
// get transaction with given hash
tx = core_storage.get_db().get_tx(tx_hash);
}
catch (const TX_DNE& e)
{
cerr << e.what() << endl;
return false;
}
return true;
}
/**
* Parse monero address in a string form into
* cryptonote::account_public_address object
*/
bool
parse_str_address(const string& address_str,
address_parse_info& address_info,
network_type net_type)
{
if (!get_account_address_from_str(address_info, net_type, address_str))
{
//cerr << "Error getting address: " << address_str << '\n';
return false;
}
return true;
}
string
print_sig (const signature& sig)
{
stringstream ss;
ss << "c: <" << epee::string_tools::pod_to_hex(sig.c) << "> "
<< "r: <" << epee::string_tools::pod_to_hex(sig.r) << ">";
return ss.str();
}
/**
* Check if a character is a path seprator
*/
inline bool
is_separator(char c)
{
// default linux path separator
const char separator = PATH_SEPARARTOR;
return c == separator;
}
/**
* Remove trailinig path separator.
*/
string
remove_trailing_path_separator(const string& in_path)
{
string new_string = in_path;
if (!new_string.empty() && is_separator(new_string[new_string.size() - 1]))
new_string.erase(new_string.size() - 1);
return new_string;
}
bf::path
remove_trailing_path_separator(const bf::path& in_path)
{
string path_str = in_path.native();
return bf::path(remove_trailing_path_separator(path_str));
}
string
timestamp_to_str_gm(time_t timestamp, const char* format)
{
const time_t* t = ×tamp;
const int TIME_LENGTH = 60;
char str_buff[TIME_LENGTH];
std::tm tmp;
gmtime_r(t, &tmp);
size_t len;
len = std::strftime(str_buff, TIME_LENGTH, format, &tmp);
return string(str_buff, len);
}
string
timestamp_to_str_local(time_t timestamp, const char* format)
{
const int TIME_LENGTH = 60;
char str_buff[TIME_LENGTH];
tm *tm_ptr;
tm_ptr = localtime(×tamp);
size_t len;
len = std::strftime(str_buff, TIME_LENGTH, format, tm_ptr);
return string(str_buff, len);
}
ostream&
operator<< (ostream& os, const address_parse_info& addr_info)
{
os << get_account_address_as_str(network_type::MAINNET,
addr_info.is_subaddress,
addr_info.address);
return os;
}
/*
* Generate key_image of foran ith output
*/
bool
generate_key_image(const crypto::key_derivation& derivation,
const std::size_t i,
const crypto::secret_key& sec_key,
const crypto::public_key& pub_key,
crypto::key_image& key_img)
{
cryptonote::keypair in_ephemeral;
if (!crypto::derive_public_key(derivation, i,
pub_key,
in_ephemeral.pub))
{
cerr << "Error generating publick key " << pub_key << endl;
return false;
}
try
{
crypto::derive_secret_key(derivation, i,
sec_key,
in_ephemeral.sec);
}
catch(const std::exception& e)
{
cerr << "Error generate secret image: " << e.what() << endl;
return false;
}
try
{
crypto::generate_key_image(in_ephemeral.pub,
in_ephemeral.sec,
key_img);
}
catch(const std::exception& e)
{
cerr << "Error generate key image: " << e.what() << endl;
return false;
}
return true;
}
string
get_default_lmdb_folder(network_type nettype)
{
// default path to monero folder
// on linux this is /home/<username>/.bitmonero
string default_monero_dir = tools::get_default_data_dir();
if (nettype == cryptonote::network_type::TESTNET)
default_monero_dir += "/testnet";
if (nettype == cryptonote::network_type::STAGENET)
default_monero_dir += "/stagenet";
// the default folder of the lmdb blockchain database
// is therefore as follows
return default_monero_dir + string("/lmdb");
}
/*
* Ge blockchain exception from command line option
*
* If not given, provide default path
*/
bool
get_blockchain_path(bf::path& blockchain_path,
cryptonote::network_type nettype)
{
// the default folder of the lmdb blockchain database
string default_lmdb_dir = xmreg::get_default_lmdb_folder(nettype);
blockchain_path = !blockchain_path.empty()
? blockchain_path
: bf::path(default_lmdb_dir);
if (!bf::is_directory(blockchain_path))
{
cerr << "Given path \"" << blockchain_path << "\" "
<< "is not a folder or does not exist \n";
return false;
}
blockchain_path = xmreg::remove_trailing_path_separator(blockchain_path);
return true;
}
bool
get_blockchain_path(string& blockchain_path,
cryptonote::network_type nettype)
{
bf::path p {blockchain_path};
if (!get_blockchain_path(p, nettype))
return false;
blockchain_path = p.string();
return true;
}
array<uint64_t, 4>
summary_of_in_out_rct(
const transaction& tx,
vector<pair<txout_to_key, uint64_t>>& output_pub_keys,
vector<txin_to_key>& input_key_imgs)
{
uint64_t xmr_outputs {0};
uint64_t xmr_inputs {0};
uint64_t mixin_no {0};
uint64_t num_nonrct_inputs {0};
for (const tx_out& txout: tx.vout)
{
if (txout.target.type() != typeid(txout_to_key))
{
// push empty pair.
output_pub_keys.push_back(pair<txout_to_key, uint64_t>{});
continue;
}
// get tx input key
const txout_to_key& txout_key
= boost::get<cryptonote::txout_to_key>(txout.target);
output_pub_keys.push_back(make_pair(txout_key, txout.amount));
xmr_outputs += txout.amount;
}
size_t input_no = tx.vin.size();
for (size_t i = 0; i < input_no; ++i)
{
if(tx.vin[i].type() != typeid(cryptonote::txin_to_key))
{
continue;
}
// get tx input key
const cryptonote::txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
xmr_inputs += tx_in_to_key.amount;
if (tx_in_to_key.amount != 0)
{
++num_nonrct_inputs;
}
if (mixin_no == 0)
{
mixin_no = tx_in_to_key.key_offsets.size();
}
input_key_imgs.push_back(tx_in_to_key);
} // for (size_t i = 0; i < input_no; ++i)
return {xmr_outputs, xmr_inputs, mixin_no, num_nonrct_inputs};
};
// this version for mempool txs from json
array<uint64_t, 6>
summary_of_in_out_rct(const json& _json)
{
uint64_t xmr_outputs {0};
uint64_t xmr_inputs {0};
uint64_t no_outputs {0};
uint64_t no_inputs {0};
uint64_t mixin_no {0};
uint64_t num_nonrct_inputs {0};
for (const json& vout: _json["vout"])
{
xmr_outputs += vout["amount"].get<uint64_t>();
}
no_outputs = _json["vout"].size();
for (const json& vin: _json["vin"])
{
uint64_t amount = vin["key"]["amount"].get<uint64_t>();
xmr_inputs += amount;
if (amount != 0)
++num_nonrct_inputs;
}
no_inputs = _json["vin"].size();
mixin_no = _json["vin"].at(0)["key"]["key_offsets"].size() - 1;
return {xmr_outputs, xmr_inputs, no_outputs, no_inputs, mixin_no, num_nonrct_inputs};
};
uint64_t
sum_money_in_outputs(const transaction& tx)
{
uint64_t sum_xmr {0};
for (const tx_out& txout: tx.vout)
{
sum_xmr += txout.amount;
}
return sum_xmr;
}
pair<uint64_t, uint64_t>
sum_money_in_outputs(const string& json_str)
{
pair<uint64_t, uint64_t> sum_xmr {0, 0};
json j;
try
{
j = json::parse( json_str);
}
catch (std::invalid_argument& e)
{
cerr << "sum_money_in_outputs: " << e.what() << endl;
return sum_xmr;
}
for (json& vout: j["vout"])
{
sum_xmr.first += vout["amount"].get<uint64_t>();
++sum_xmr.second;
}
return sum_xmr;
};
uint64_t
sum_money_in_inputs(const transaction& tx)
{
uint64_t sum_xmr {0};
size_t input_no = tx.vin.size();
for (size_t i = 0; i < input_no; ++i)
{
if(tx.vin[i].type() != typeid(cryptonote::txin_to_key))
{
continue;
}
// get tx input key
const cryptonote::txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
sum_xmr += tx_in_to_key.amount;
}
return sum_xmr;
}
pair<uint64_t, uint64_t>
sum_money_in_inputs(const string& json_str)
{
pair<uint64_t, uint64_t> sum_xmr {0, 0};
json j;
try
{
j = json::parse( json_str);
}
catch (std::invalid_argument& e)
{
cerr << "sum_money_in_outputs: " << e.what() << endl;
return sum_xmr;
}
for (json& vin: j["vin"])
{
sum_xmr.first += vin["key"]["amount"].get<uint64_t>();
++sum_xmr.second;
}
return sum_xmr;
};
array<uint64_t, 2>
sum_money_in_tx(const transaction& tx)
{
array<uint64_t, 2> sum_xmr;
sum_xmr[0] = sum_money_in_inputs(tx);
sum_xmr[1] = sum_money_in_outputs(tx);
return sum_xmr;
};
array<uint64_t, 2>
sum_money_in_txs(const vector<transaction>& txs)
{
array<uint64_t, 2> sum_xmr {0,0};
for (const transaction& tx: txs)
{
sum_xmr[0] += sum_money_in_inputs(tx);
sum_xmr[1] += sum_money_in_outputs(tx);
}
return sum_xmr;
};
uint64_t
sum_fees_in_txs(const vector<transaction>& txs)
{
uint64_t fees_sum {0};
for (const transaction& tx: txs)
{
fees_sum += get_tx_fee(tx);
}
return fees_sum;
}
vector<pair<txout_to_key, uint64_t>>
get_ouputs(const transaction& tx)
{
vector<pair<txout_to_key, uint64_t>> outputs;
for (const tx_out& txout: tx.vout)
{
if (txout.target.type() != typeid(txout_to_key))
{
continue;
}
// get tx input key
const txout_to_key& txout_key
= boost::get<cryptonote::txout_to_key>(txout.target);
outputs.push_back(make_pair(txout_key, txout.amount));
}
return outputs;
};
vector<outputs_tuple> get_outputs_tuple(const transaction& tx)
{
vector<outputs_tuple> outputs;
for (uint64_t n = 0; n < tx.vout.size(); ++n)
{
// (Loki) per-output unlock time
if (tx.version > cryptonote::txversion::v2_ringct)
{
outputs.push_back(make_tuple(tx.vout[n].target, tx.vout[n].amount, n, tx.output_unlock_times[n]));
}
else
{
outputs.push_back(make_tuple(tx.vout[n].target, tx.vout[n].amount, n, tx.unlock_time));
}
}
return outputs;
};
uint64_t
get_mixin_no(const transaction& tx)
{
uint64_t mixin_no {0};
size_t input_no = tx.vin.size();
for (size_t i = 0; i < input_no; ++i)
{
if(tx.vin[i].type() != typeid(cryptonote::txin_to_key))
{
continue;
}
// get tx input key
const txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
mixin_no = tx_in_to_key.key_offsets.size();
// look for first mixin number.
// all inputs in a single transaction have same number
if (mixin_no > 0)
{
break;
}
}
return mixin_no;
}
vector<uint64_t>
get_mixin_no(const string& json_str)
{
vector<uint64_t> mixin_no;
json j;
try
{
j = json::parse(json_str);
mixin_no.push_back(j["vin"].at(0)["key"]["key_offsets"].size());
}
catch (std::invalid_argument& e)
{
cerr << "get_mixin_no: " << e.what() << endl;
return mixin_no;
}
return mixin_no;
}
vector<uint64_t>
get_mixin_no_in_txs(const vector<transaction>& txs)
{
vector<uint64_t> mixin_no;
for (const transaction& tx: txs)
{
mixin_no.push_back(get_mixin_no(tx));
}
return mixin_no;
}
vector<txin_to_key>
get_key_images(const transaction& tx)
{
vector<txin_to_key> key_images;
size_t input_no = tx.vin.size();
for (size_t i = 0; i < input_no; ++i)
{
if(tx.vin[i].type() != typeid(txin_to_key))
{
continue;
}
// get tx input key
const txin_to_key& tx_in_to_key
= boost::get<cryptonote::txin_to_key>(tx.vin[i]);
key_images.push_back(tx_in_to_key);
}
return key_images;
}
bool
get_payment_id(const vector<uint8_t>& extra,
crypto::hash& payment_id,
crypto::hash8& payment_id8)
{
payment_id = null_hash;
payment_id8 = null_hash8;
std::vector<tx_extra_field> tx_extra_fields;
if(!parse_tx_extra(extra, tx_extra_fields))
{
return false;
}
tx_extra_nonce extra_nonce;
if (find_tx_extra_field_by_type(tx_extra_fields, extra_nonce))
{
// first check for encrypted id and then for normal one
if(get_encrypted_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id8))
{
return true;
}
else if (get_payment_id_from_tx_extra_nonce(extra_nonce.nonce, payment_id))
{
return true;
}
}
return false;
}
bool
get_payment_id(const transaction& tx,
crypto::hash& payment_id,
crypto::hash8& payment_id8)
{
return get_payment_id(tx.extra, payment_id, payment_id8);
}
array<size_t, 5>
timestamp_difference(uint64_t t1, uint64_t t2)
{
uint64_t timestamp_diff = t1 - t2;
// calculate difference of timestamps from current block to the mixin one
if (t2 > t1)
{
timestamp_diff = t2 - t1;
}
uint64_t time_diff_years = timestamp_diff / 31536000;
timestamp_diff -= time_diff_years * 31536000;
uint64_t time_diff_days = timestamp_diff / 86400;
timestamp_diff -= time_diff_days * 86400;
uint64_t time_diff_hours = timestamp_diff / 3600;
timestamp_diff -= time_diff_hours * 3600;
uint64_t time_diff_minutes = timestamp_diff / 60;
timestamp_diff -= time_diff_minutes * 60;
uint64_t time_diff_seconds = timestamp_diff ;
return array<size_t, 5> {time_diff_years, time_diff_days,
time_diff_hours, time_diff_minutes,
time_diff_seconds};
};
string
read(string filename)
{
if (!bf::exists(bf::path(filename)))
{
cerr << "File does not exist: " << filename << endl;
return string();
}
std::ifstream t(filename);
return string(std::istreambuf_iterator<char>(t),
std::istreambuf_iterator<char>());
}
pair<string, double>
timestamps_time_scale(const vector<uint64_t>& timestamps,
uint64_t timeN,
uint64_t resolution,
uint64_t time0)
{
string empty_time = string(resolution, '_');
size_t time_axis_length = empty_time.size();
uint64_t interval_length = timeN-time0;
double scale = double(interval_length) / double(time_axis_length);
for (const auto& timestamp: timestamps)
{
if (timestamp < time0 || timestamp > timeN)
{
cout << "Out of range" << endl;
continue;
}
uint64_t timestamp_place = double(timestamp-time0)
/ double(interval_length)*(time_axis_length - 1);
empty_time[timestamp_place + 1] = '*';
}
return make_pair(empty_time, scale);
}
bool
decode_ringct(const rct::rctSig& rv,
const crypto::public_key &pub,
const crypto::secret_key &sec,
unsigned int i,
rct::key & mask,
uint64_t & amount)
{
crypto::key_derivation derivation;
bool r = crypto::generate_key_derivation(pub, sec, derivation);
if (!r)
{
cerr <<"Failed to generate key derivation to decode rct output " << i << endl;
return false;
}
crypto::secret_key scalar1;
crypto::derivation_to_scalar(derivation, i, scalar1);
try
{
switch (rv.type)
{
case rct::RCTTypeSimple:
case rct::RCTTypeBulletproof:
case rct::RCTTypeBulletproof2:
amount = rct::decodeRctSimple(rv,
rct::sk2rct(scalar1),
i,
mask,
hw::get_device("default"));
break;
case rct::RCTTypeFull:
amount = rct::decodeRct(rv,
rct::sk2rct(scalar1),
i,
mask,
hw::get_device("default"));
break;
default:
cerr << "Unsupported rct type: " << rv.type << endl;
return false;
}
}
catch (const std::exception &e)
{
cerr << "Failed to decode input " << i << endl;
return false;
}
return true;
}
bool
url_decode(const std::string& in, std::string& out)
{
out.clear();
out.reserve(in.size());
for (std::size_t i = 0; i < in.size(); ++i)
{
if (in[i] == '%')
{
if (i + 3 <= in.size())
{
int value = 0;
std::istringstream is(in.substr(i + 1, 2));
if (is >> std::hex >> value)
{
out += static_cast<char>(value);
i += 2;
}
else
{
return false;
}
}
else
{
return false;
}
}
else if (in[i] == '+')
{
out += ' ';
}
else
{
out += in[i];
}
}
return true;
}
map<std::string, std::string>
parse_crow_post_data(const string& req_body)
{
map<std::string, std::string> body;
vector<string> vec;
string tmp;
bool result = url_decode(req_body, tmp);
if (result)
{
boost::algorithm::split(vec, tmp, [](char x) {return x == '&'; });
for(auto &it : vec)
{
auto pos = it.find("=");
if (pos != string::npos)
{
body[it.substr(0, pos)] = it.substr(pos + 1);
}
else
{
break;
}
}
}
return body;
}
// based on
// crypto::public_key wallet2::get_tx_pub_key_from_received_outs(const tools::wallet2::transfer_details &td) const
std::vector<public_key>
get_tx_pub_keys_from_received_outs(const transaction &tx)
{
std::vector<tx_extra_field> tx_extra_fields;
if(!parse_tx_extra(tx.extra, tx_extra_fields))
{
// Extra may only be partially parsed, it's OK if tx_extra_fields contains public key
}
// NOTE(Loki): Loki miner transactions include two pubkeys, one for the miner and
// one for the governance/service node rewards.
tx_extra_pub_key pub_key_field;
std::vector<public_key> tx_pub_keys;
size_t i = 0;
while (find_tx_extra_field_by_type(tx_extra_fields, pub_key_field, i))
{
tx_pub_keys.push_back(pub_key_field.pub_key);
i++;
}
return tx_pub_keys;
}
string
xmr_amount_to_str(const uint64_t& xmr_amount, string format)
{
return fmt::format(format, XMR_AMOUNT(xmr_amount));
}
/**
* Check if given output (specified by output_index)
* belongs is ours based
* on our private view key and public spend key
*/
bool
is_output_ours(const size_t& output_index,
const transaction& tx,
const public_key& pub_tx_key,
const secret_key& private_view_key,
const public_key& public_spend_key)
{
// public transaction key is combined with our viewkey
// to create, so called, derived key.
key_derivation derivation;
if (!generate_key_derivation(pub_tx_key, private_view_key, derivation))
{
cerr << "Cant get dervied key for: " << "\n"
<< "pub_tx_key: " << pub_tx_key << " and "
<< "prv_view_key" << private_view_key << endl;
return false;
}
// get the tx output public key
// that normally would be generated for us,
// if someone had sent us some xmr.
public_key pubkey;
derive_public_key(derivation,
output_index,
public_spend_key,
pubkey);
//cout << "\n" << tx.vout.size() << " " << output_index << endl;
// get tx output public key