-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpy_monero_wallet_rpc.cpp
More file actions
1732 lines (1471 loc) · 73.5 KB
/
py_monero_wallet_rpc.cpp
File metadata and controls
1732 lines (1471 loc) · 73.5 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
#include "py_monero_wallet_rpc.h"
PyMoneroWalletPoller::~PyMoneroWalletPoller() {
set_is_polling(false);
}
void PyMoneroWalletPoller::set_is_polling(bool is_polling) {
if (is_polling == m_is_polling) return;
m_is_polling = is_polling;
if (m_is_polling) {
m_thread = std::thread([this]() {
loop();
});
m_thread.detach();
} else {
if (m_thread.joinable()) m_thread.join();
}
}
void PyMoneroWalletPoller::set_period_in_ms(uint64_t period_ms) {
m_poll_period_ms = period_ms;
}
void PyMoneroWalletPoller::poll() {
if (m_num_polling > 1) return;
m_num_polling++;
boost::lock_guard<boost::recursive_mutex> lock(m_mutex);
try {
// skip if wallet is closed
if (m_wallet->is_closed()) {
m_num_polling--;
return;
}
if (m_prev_balances == boost::none) {
m_prev_height = m_wallet->get_height();
monero::monero_tx_query tx_query;
tx_query.m_is_locked = true;
m_prev_locked_txs = m_wallet->get_txs(tx_query);
m_prev_balances = m_wallet->get_balances(boost::none, boost::none);
m_num_polling--;
return;
}
// announce height changes
uint64_t height = m_wallet->get_height();
if (m_prev_height.get() != height) {
for (uint64_t i = m_prev_height.get(); i < height; i++) {
on_new_block(i);
}
m_prev_height = height;
}
// get locked txs for comparison to previous
uint64_t min_height = 0; // only monitor recent txs
if (height > 70) min_height = height - 70;
monero::monero_tx_query tx_query;
tx_query.m_is_locked = true;
tx_query.m_min_height = min_height;
tx_query.m_include_outputs = true;
auto locked_txs = m_wallet->get_txs(tx_query);
// collect hashes of txs no longer locked
std::vector<std::string> no_longer_locked_hashes;
for (const auto &prev_locked_tx : m_prev_locked_txs) {
if (get_tx(locked_txs, prev_locked_tx->m_hash.get()) == nullptr) {
no_longer_locked_hashes.push_back(prev_locked_tx->m_hash.get());
}
}
m_prev_locked_txs = locked_txs;
std::vector<std::shared_ptr<monero::monero_tx_wallet>> unlocked_txs;
if (!no_longer_locked_hashes.empty()) {
monero_tx_query tx_query;
tx_query.m_is_locked = false;
tx_query.m_min_height = min_height;
tx_query.m_hashes = no_longer_locked_hashes;
tx_query.m_include_outputs = true;
unlocked_txs = m_wallet->get_txs(tx_query);
}
// announce new unconfirmed and confirmed txs
for (const auto &locked_tx : locked_txs) {
if (locked_tx->m_is_confirmed) {
m_prev_confirmed_notifications.push_back(locked_tx->m_hash.get());
notify_outputs(locked_tx);
}
else {
m_prev_unconfirmed_notifications.push_back(locked_tx->m_hash.get());
}
}
// announce new unlocked outputs
for (const auto &unlocked_tx : unlocked_txs) {
std::string tx_hash = unlocked_tx->m_hash.get();
m_prev_confirmed_notifications.erase(std::remove_if(m_prev_confirmed_notifications.begin(), m_prev_confirmed_notifications.end(), [&tx_hash](const std::string& iter){ return iter == tx_hash; }), m_prev_confirmed_notifications.end());
m_prev_unconfirmed_notifications.erase(std::remove_if(m_prev_unconfirmed_notifications.begin(), m_prev_unconfirmed_notifications.end(), [&tx_hash](const std::string& iter){ return iter == tx_hash; }), m_prev_unconfirmed_notifications.end());
notify_outputs(unlocked_tx);
}
check_for_changed_balances();
m_num_polling--;
}
catch (const std::exception &e) {
m_num_polling--;
if (m_is_polling) {
std::cout << "Failed to background poll wallet " << m_wallet->get_path() << ": " << e.what() << std::endl;
}
}
}
std::shared_ptr<monero::monero_tx_wallet> PyMoneroWalletPoller::get_tx(const std::vector<std::shared_ptr<monero::monero_tx_wallet>>& txs, const std::string& tx_hash) {
for (auto tx : txs) {
if (tx->m_hash == tx_hash) return tx;
}
return nullptr;
}
void PyMoneroWalletPoller::loop() {
while (m_is_polling) {
try {
poll();
} catch (const std::exception& e) {
std::cout << "ERROR " << e.what() << std::endl;
}
std::this_thread::sleep_for(std::chrono::milliseconds(m_poll_period_ms));
}
}
void PyMoneroWalletPoller::on_new_block(uint64_t height) {
m_wallet->announce_new_block(height);
}
void PyMoneroWalletPoller::notify_outputs(const std::shared_ptr<monero::monero_tx_wallet> &tx) {
if (tx->m_outgoing_transfer != boost::none) {
auto outgoing_transfer = tx->m_outgoing_transfer.get();
if (!tx->m_inputs.empty()) throw std::runtime_error("Tx inputs should be empty");
auto output = std::make_shared<monero::monero_output_wallet>();
output->m_amount = outgoing_transfer->m_amount.get() + tx->m_fee.get();
output->m_account_index = outgoing_transfer->m_account_index;
output->m_tx = tx;
if (outgoing_transfer->m_subaddress_indices.size() == 1) {
output->m_subaddress_index = outgoing_transfer->m_subaddress_indices[0];
}
tx->m_inputs.push_back(output);
m_wallet->announce_output_spent(output);
}
if (tx->m_incoming_transfers.size() > 0) {
if (!tx->m_outputs.empty()) {
for(const auto &output : tx->get_outputs_wallet()) {
m_wallet->announce_output_received(output);
}
}
else {
for (const auto &transfer : tx->m_incoming_transfers) {
auto output = std::make_shared<monero::monero_output_wallet>();
output->m_account_index = transfer->m_account_index;
output->m_subaddress_index = transfer->m_subaddress_index;
output->m_amount = transfer->m_amount.get();
output->m_tx = tx;
tx->m_outputs.push_back(output);
}
for (const auto &output : tx->get_outputs_wallet()) {
m_wallet->announce_output_received(output);
}
}
}
}
bool PyMoneroWalletPoller::check_for_changed_balances() {
auto balances = m_wallet->get_balances(boost::none, boost::none);
if (balances->m_balance != m_prev_balances.get()->m_balance || balances->m_unlocked_balance != m_prev_balances.get()->m_unlocked_balance) {
m_prev_balances = balances;
m_wallet->announce_balances_changed(balances->m_balance, balances->m_unlocked_balance);
return true;
}
return false;
}
PyMoneroWalletRpc::~PyMoneroWalletRpc() {
}
boost::optional<monero::monero_rpc_connection> PyMoneroWalletRpc::get_rpc_connection() const {
if (m_rpc == nullptr) return boost::none;
return boost::optional<monero::monero_rpc_connection>(*m_rpc);
}
PyMoneroWalletRpc* PyMoneroWalletRpc::open_wallet(const std::shared_ptr<PyMoneroWalletConfig> &config) {
if (config == nullptr) throw std::runtime_error("Must provide configuration of wallet to open");
if (config->m_path == boost::none || config->m_path->empty()) throw std::runtime_error("Filename is not initialized");
std::string path = config->m_path.get();
std::string password = std::string("");
if (config->m_password != boost::none) password = config->m_password.get();
auto params = std::make_shared<PyMoneroCreateOpenWalletParams>(path, password);
PyMoneroJsonRequest request("open_wallet", params);
m_rpc->send_json_request(request);
clear();
if (config->m_connection_manager != boost::none) {
if (config->m_server != boost::none) throw std::runtime_error("Wallet can be opened with a server or connection manager but not both");
set_connection_manager(config->m_connection_manager.get());
}
else if (config->m_server != boost::none) {
set_daemon_connection(config->m_server);
}
return this;
}
PyMoneroWalletRpc* PyMoneroWalletRpc::open_wallet(const std::string& name, const std::string& password) {
auto config = std::make_shared<PyMoneroWalletConfig>();
config->m_path = name;
config->m_password = password;
return open_wallet(config);
}
void handle_create_wallet_error(const PyMoneroRpcError& ex, const std::string& path) {
std::string msg = ex.what();
std::transform(msg.begin(), msg.end(), msg.begin(), [](unsigned char c){ return std::tolower(c); });
if (msg.find("already exists") != std::string::npos) throw PyMoneroRpcError(ex.code, std::string("Wallet already exists: ") + path);
if (msg == std::string("electrum-style word list failed verification")) throw PyMoneroRpcError(ex.code, std::string("Invalid mnemonic"));
throw ex;
}
PyMoneroWalletRpc* PyMoneroWalletRpc::create_wallet(const std::shared_ptr<PyMoneroWalletConfig> &config) {
if (config == nullptr) throw std::runtime_error("Must specify config to create wallet");
if (config->m_network_type != boost::none) throw std::runtime_error("Cannot specify network type when creating RPC wallet");
if (config->m_seed != boost::none && (config->m_primary_address != boost::none || config->m_private_view_key != boost::none || config->m_private_spend_key != boost::none)) {
throw std::runtime_error("Wallet can be initialized with a seed or keys but not both");
}
if (config->m_account_lookahead != boost::none || config->m_subaddress_lookahead != boost::none) throw std::runtime_error("monero-wallet-rpc does not support creating wallets with subaddress lookahead over rpc");
if (config->m_connection_manager != boost::none) {
if (config->m_server != boost::none) throw std::runtime_error("Wallet can be opened with a server or connection manager but not both");
auto cm = config->m_connection_manager.get();
if (cm != nullptr) {
auto connection = cm->get_connection();
if (connection) {
config->m_server = *connection;
}
}
}
if (config->m_seed != boost::none) create_wallet_from_seed(config);
else if (config->m_private_spend_key != boost::none || config->m_primary_address != boost::none) create_wallet_from_keys(config);
else create_wallet_random(config);
if (config->m_connection_manager != boost::none) {
set_connection_manager(config->m_connection_manager.get());
}
else if (config->m_server != boost::none) {
set_daemon_connection(config->m_server);
}
return this;
}
std::vector<std::string> PyMoneroWalletRpc::get_seed_languages() const {
PyMoneroJsonRequest request("get_languages");
std::shared_ptr<PyMoneroJsonResponse> response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
std::vector<std::string> languages;
for (auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("languages")) {
auto languages_node = it->second;
for (auto it2 = languages_node.begin(); it2 != languages_node.end(); ++it2) {
languages.push_back(it2->second.data());
}
}
}
return languages;
}
void PyMoneroWalletRpc::stop() {
PyMoneroJsonRequest request("stop_wallet");
m_rpc->send_json_request(request);
}
bool PyMoneroWalletRpc::is_view_only() const {
try {
std::string key = "mnemonic";
query_key(key);
return false;
}
catch (const PyMoneroRpcError& e) {
if (e.code == -29) return true;
if (e.code == -1) return false;
throw;
}
}
boost::optional<monero::monero_rpc_connection> PyMoneroWalletRpc::get_daemon_connection() const {
if (m_daemon_connection == nullptr) return boost::none;
return boost::optional<monero::monero_rpc_connection>(*m_daemon_connection);
}
void PyMoneroWalletRpc::set_daemon_connection(const std::string& uri, const std::string& username, const std::string& password, const std::string& proxy_uri) {
if (uri.empty()) {
set_daemon_connection(boost::none);
return;
}
boost::optional<monero_rpc_connection> rpc = monero_rpc_connection(uri, username, password, proxy_uri);
set_daemon_connection(rpc);
}
void PyMoneroWalletRpc::set_daemon_connection(const boost::optional<monero_rpc_connection>& connection, bool is_trusted, const boost::optional<PyMoneroSslOptions>& ssl_options) {
auto params = std::make_shared<PyMoneroSetDaemonParams>();
if (connection == boost::none) {
params->m_address = "placeholder";
params->m_username = "";
params->m_password = "";
}
else {
params->m_address = connection->m_uri;
params->m_username = connection->m_username;
params->m_password = connection->m_password;
}
params->m_trusted = is_trusted;
params->m_ssl_support = "autodetect";
if (ssl_options != boost::none) {
params->m_ssl_private_key_path = ssl_options->m_ssl_private_key_path;
params->m_ssl_certificate_path = ssl_options->m_ssl_certificate_path;
params->m_ssl_ca_file = ssl_options->m_ssl_ca_file;
params->m_ssl_allowed_fingerprints = ssl_options->m_ssl_allowed_fingerprints;
params->m_ssl_allow_any_cert = ssl_options->m_ssl_allow_any_cert;
}
PyMoneroJsonRequest request("set_daemon", params);
m_rpc->send_json_request(request);
if (connection == boost::none || connection->m_uri == boost::none || connection->m_uri->empty()) {
m_daemon_connection = nullptr;
}
else {
m_daemon_connection = std::make_shared<PyMoneroRpcConnection>(connection.get());
}
}
void PyMoneroWalletRpc::set_daemon_connection(const boost::optional<monero_rpc_connection>& connection) {
set_daemon_connection(connection, false, boost::none);
}
bool PyMoneroWalletRpc::is_connected_to_daemon() const {
try {
check_reserve_proof(get_primary_address(), "", "");
return false;
}
catch (const PyMoneroRpcError& e) {
if (e.code == -13) throw; // no wallet file
return e.message.find("Failed to connect to daemon") == std::string::npos;
}
}
monero::monero_version PyMoneroWalletRpc::get_version() const {
PyMoneroJsonRequest request("get_version");
std::shared_ptr<PyMoneroJsonResponse> response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto res = response->m_result.get();
std::shared_ptr<PyMoneroVersion> info = std::make_shared<PyMoneroVersion>();
PyMoneroVersion::from_property_tree(res, info);
return *info;
}
std::string PyMoneroWalletRpc::get_path() const {
return m_path;
}
std::string PyMoneroWalletRpc::get_seed() const {
std::string key = "mnemonic";
return query_key(key);
}
std::string PyMoneroWalletRpc::get_seed_language() const {
throw std::runtime_error("MoneroWalletRpc::get_seed_language() not supported");
}
std::string PyMoneroWalletRpc::get_public_view_key() const {
std::string key = "public_view_key";
return query_key(key);
}
std::string PyMoneroWalletRpc::get_private_view_key() const {
std::string key = "view_key";
return query_key(key);
}
std::string PyMoneroWalletRpc::get_public_spend_key() const {
std::string key = "public_spend_key";
return query_key(key);
}
std::string PyMoneroWalletRpc::get_private_spend_key() const {
std::string key = "spend_key";
return query_key(key);
}
std::string PyMoneroWalletRpc::get_address(const uint32_t account_idx, const uint32_t subaddress_idx) const {
auto it = m_address_cache.find(account_idx);
if (it == m_address_cache.end()) {
std::vector<uint32_t> empty_indices;
get_subaddresses(account_idx, empty_indices, true);
return get_address(account_idx, subaddress_idx);
}
auto subaddress_map = it->second;
auto it2 = subaddress_map.find(subaddress_idx);
if (it2 == subaddress_map.end()) {
std::vector<uint32_t> empty_indices;
get_subaddresses(account_idx, empty_indices, true);
auto it3 = m_address_cache.find(account_idx);
if (it3 == m_address_cache.end()) throw std::runtime_error("Could not find account address at index (" + std::to_string(account_idx) + ", " + std::to_string(subaddress_idx) + ")" );
auto it4 = it3->second.find(subaddress_idx);
if (it4 == it3->second.end()) throw std::runtime_error("Could not find address at index (" + std::to_string(account_idx) + ", " + std::to_string(subaddress_idx) + ")" );
return it4->second;
}
return it2->second;
}
monero_subaddress PyMoneroWalletRpc::get_address_index(const std::string& address) const {
auto params = std::make_shared<PyMoneroGetAddressIndexParams>(address);
PyMoneroJsonRequest request("get_address_index", params);
std::shared_ptr<PyMoneroJsonResponse> response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto res = response->m_result.get();
auto tmplt = std::make_shared<monero::monero_subaddress>();
PyMoneroSubaddress::from_property_tree(res, tmplt);
return *tmplt;
}
monero_integrated_address PyMoneroWalletRpc::get_integrated_address(const std::string& standard_address, const std::string& payment_id) const {
auto params = std::make_shared<PyMoneroMakeIntegratedAddressParams>(standard_address, payment_id);
PyMoneroJsonRequest request("make_integrated_address", params);
std::shared_ptr<PyMoneroJsonResponse> response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto res = response->m_result.get();
auto tmplt = std::make_shared<monero::monero_integrated_address>();
PyMoneroIntegratedAddress::from_property_tree(res, tmplt);
return decode_integrated_address(tmplt->m_integrated_address);
}
monero_integrated_address PyMoneroWalletRpc::decode_integrated_address(const std::string& integrated_address) const {
auto params = std::make_shared<PyMoneroSplitIntegratedAddressParams>(integrated_address);
PyMoneroJsonRequest request("split_integrated_address", params);
std::shared_ptr<PyMoneroJsonResponse> response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto res = response->m_result.get();
auto tmplt = std::make_shared<monero::monero_integrated_address>();
PyMoneroIntegratedAddress::from_property_tree(res, tmplt);
tmplt->m_integrated_address = integrated_address;
return *tmplt;
}
uint64_t PyMoneroWalletRpc::get_height() const {
PyMoneroJsonRequest request("get_height");
std::shared_ptr<PyMoneroJsonResponse> response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto res = response->m_result.get();
return PyMoneroWalletGetHeightResponse::from_property_tree(res);
}
uint64_t PyMoneroWalletRpc::get_daemon_height() const {
throw std::runtime_error("monero-wallet-rpc does not support getting the chain height");
}
uint64_t PyMoneroWalletRpc::get_height_by_date(uint16_t year, uint8_t month, uint8_t day) const {
throw std::runtime_error("monero-wallet-rpc does not support getting a height by date");
}
monero_sync_result PyMoneroWalletRpc::sync() {
auto params = std::make_shared<PyMoneroRefreshWalletParams>();
boost::lock_guard<boost::recursive_mutex> lock(m_sync_mutex);
try {
PyMoneroJsonRequest request("refresh", params);
auto response = m_rpc->send_json_request(request);
poll();
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
monero_sync_result sync_result(0, false);
for (auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("blocks_fetched")) sync_result.m_num_blocks_fetched = it->second.get_value<uint64_t>();
else if (key == std::string("received_money")) sync_result.m_received_money = it->second.get_value<bool>();
}
return sync_result;
} catch (const PyMoneroRpcError& ex) {
if (ex.message == std::string("no connection to daemon")) throw PyMoneroError("Wallet is not connected to daemon");
throw;
}
}
monero_sync_result PyMoneroWalletRpc::sync(monero_wallet_listener& listener) {
throw std::runtime_error("Monero Wallet RPC does not support reporting sync progress");
}
monero_sync_result PyMoneroWalletRpc::sync(uint64_t start_height, monero_wallet_listener& listener) {
throw std::runtime_error("Monero Wallet RPC does not support reporting sync progress");
}
monero_sync_result PyMoneroWalletRpc::sync(uint64_t start_height) {
auto params = std::make_shared<PyMoneroRefreshWalletParams>(start_height);
boost::lock_guard<boost::recursive_mutex> lock(m_sync_mutex);
try {
PyMoneroJsonRequest request("refresh", params);
auto response = m_rpc->send_json_request(request);
poll();
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
monero_sync_result sync_result(0, false);
for (auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("blocks_fetched")) sync_result.m_num_blocks_fetched = it->second.get_value<uint64_t>();
else if (key == std::string("received_money")) sync_result.m_received_money = it->second.get_value<bool>();
}
return sync_result;
} catch (const PyMoneroRpcError& ex) {
if (ex.message == std::string("no connection to daemon")) throw PyMoneroError("Wallet is not connected to daemon");
throw;
}
}
void PyMoneroWalletRpc::start_syncing(uint64_t sync_period_in_ms) {
// convert ms to seconds for rpc parameter
uint64_t sync_period_in_seconds = sync_period_in_ms / 1000;
// send rpc request
auto params = std::make_shared<PyMoneroRefreshWalletParams>(true, sync_period_in_seconds);
PyMoneroJsonRequest request("auto_refresh", params);
auto response = m_rpc->send_json_request(request);
// update sync period for poller
m_sync_period_in_ms = sync_period_in_seconds * 1000;
if (m_poller != nullptr) m_poller->set_period_in_ms(m_sync_period_in_ms.get());
// poll if listening
poll();
}
void PyMoneroWalletRpc::stop_syncing() {
auto params = std::make_shared<PyMoneroAutoRefreshParams>(false);
PyMoneroJsonRequest request("auto_refresh", params);
m_rpc->send_json_request(request);
}
void PyMoneroWalletRpc::scan_txs(const std::vector<std::string>& tx_hashes) {
if (tx_hashes.empty()) throw std::runtime_error("No tx hashes given to scan");
auto params = std::make_shared<PyMoneroScanTxParams>(tx_hashes);
PyMoneroJsonRequest request("scan_tx", params);
m_rpc->send_json_request(request);
poll();
}
void PyMoneroWalletRpc::rescan_spent() {
PyMoneroJsonRequest request("rescan_spent");
m_rpc->send_json_request(request);
}
void PyMoneroWalletRpc::rescan_blockchain() {
PyMoneroJsonRequest request("rescan_blockchain");
m_rpc->send_json_request(request);
}
uint64_t PyMoneroWalletRpc::get_balance() const {
auto wallet_balance = get_balances(boost::none, boost::none);
return wallet_balance->m_balance;
}
uint64_t PyMoneroWalletRpc::get_balance(uint32_t account_index) const {
auto wallet_balance = get_balances(account_index, boost::none);
return wallet_balance->m_balance;
}
uint64_t PyMoneroWalletRpc::get_balance(uint32_t account_idx, uint32_t subaddress_idx) const {
auto wallet_balance = get_balances(account_idx, subaddress_idx);
return wallet_balance->m_balance;
}
uint64_t PyMoneroWalletRpc::get_unlocked_balance() const {
auto wallet_balance = get_balances(boost::none, boost::none);
return wallet_balance->m_unlocked_balance;
}
uint64_t PyMoneroWalletRpc::get_unlocked_balance(uint32_t account_index) const {
auto wallet_balance = get_balances(account_index, boost::none);
return wallet_balance->m_unlocked_balance;
}
uint64_t PyMoneroWalletRpc::get_unlocked_balance(uint32_t account_idx, uint32_t subaddress_idx) const {
auto wallet_balance = get_balances(account_idx, subaddress_idx);
return wallet_balance->m_unlocked_balance;
}
monero_account PyMoneroWalletRpc::get_account(const uint32_t account_idx, bool include_subaddresses) const {
return get_account(account_idx, include_subaddresses, false);
}
monero_account PyMoneroWalletRpc::get_account(const uint32_t account_idx, bool include_subaddresses, bool skip_balances) const {
for(auto& account : monero::monero_wallet::get_accounts()) {
if (account.m_index.get() == account_idx) {
if (include_subaddresses) {
std::vector<uint32_t> empty_indices;
account.m_subaddresses = get_subaddresses(account_idx, empty_indices, skip_balances);
}
return account;
}
}
throw std::runtime_error("Account with index " + std::to_string(account_idx) + " does not exist");
}
std::vector<monero_account> PyMoneroWalletRpc::get_accounts(bool include_subaddresses, const std::string& tag) const {
return get_accounts(include_subaddresses, tag, false);
}
std::vector<monero_account> PyMoneroWalletRpc::get_accounts(bool include_subaddresses, const std::string& tag, bool skip_balances) const {
auto params = std::make_shared<PyMoneroGetAccountsParams>(tag);
PyMoneroJsonRequest request("get_accounts", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
std::vector<monero_account> accounts;
PyMoneroAccount::from_property_tree(node, accounts);
if (include_subaddresses) {
for (auto &account : accounts) {
std::vector<uint32_t> empty_indices;
account.m_subaddresses = get_subaddresses(account.m_index.get(), empty_indices, true);
if (!skip_balances) {
for (auto &subaddress : account.m_subaddresses) {
subaddress.m_balance = 0;
subaddress.m_unlocked_balance = 0;
subaddress.m_num_unspent_outputs = 0;
subaddress.m_num_blocks_to_unlock = 0;
}
}
}
if (!skip_balances) {
auto params2 = std::make_shared<PyMoneroGetBalanceParams>(true);
PyMoneroJsonRequest request2("get_balance", params2);
auto response2 = m_rpc->send_json_request(request2);
if (response2->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node2 = response2->m_result.get();
auto bal_res = std::make_shared<PyMoneroGetBalanceResponse>();
PyMoneroGetBalanceResponse::from_property_tree(node2, bal_res);
for (const auto &subaddress : bal_res->m_per_subaddress) {
// merge info
auto account = &accounts[subaddress->m_account_index.get()];
if (account->m_index != subaddress->m_account_index) throw std::runtime_error("RPC accounts are out of order");
auto tgt_subaddress = &account->m_subaddresses[subaddress->m_account_index.get()];
if (tgt_subaddress->m_index != subaddress->m_index) throw std::runtime_error("RPC subaddresses are out of order");
if (subaddress->m_balance != boost::none) tgt_subaddress->m_balance = subaddress->m_balance;
if (subaddress->m_unlocked_balance != boost::none) tgt_subaddress->m_unlocked_balance = subaddress->m_unlocked_balance;
if (subaddress->m_num_unspent_outputs != boost::none) tgt_subaddress->m_num_unspent_outputs = subaddress->m_num_unspent_outputs;
if (subaddress->m_num_blocks_to_unlock != boost::none) tgt_subaddress->m_num_blocks_to_unlock = subaddress->m_num_blocks_to_unlock;
}
}
}
return accounts;
}
monero_account PyMoneroWalletRpc::create_account(const std::string& label) {
auto params = std::make_shared<PyMoneroCreateAccountParams>(label);
PyMoneroJsonRequest request("create_account", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
monero_account res;
res.m_balance = 0;
res.m_unlocked_balance = 0;
bool found_index = false;
bool address_found = false;
for (auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("account_index")) {
found_index = true;
res.m_index = it->second.get_value<uint32_t>();
}
else if (key == std::string("address")) {
address_found = true;
res.m_primary_address = it->second.data();
}
}
if (!found_index || !address_found) throw std::runtime_error("Could not create account");
return res;
}
std::vector<monero_subaddress> PyMoneroWalletRpc::get_subaddresses(const uint32_t account_idx, const std::vector<uint32_t>& subaddress_indices, bool skip_balances) const {
// fetch subaddresses
auto params = std::make_shared<PyMoneroGetAddressParams>(account_idx, subaddress_indices);
PyMoneroJsonRequest request("get_address", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
std::vector<monero_subaddress> subaddresses;
// initialize subaddresses
for (auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("addresses")) {
auto node2 = it->second;
for (auto it2 = node2.begin(); it2 != node2.end(); ++it2) {
auto subaddress = std::make_shared<monero::monero_subaddress>();
PyMoneroSubaddress::from_rpc_property_tree(it2->second, subaddress);
subaddress->m_account_index = account_idx;
subaddresses.push_back(*subaddress);
}
break;
}
}
// fetch and initialize subaddress balances
if (!skip_balances) {
// these fields are not initialized if subaddress is unused and therefore not returned from `get_balance`
for (auto &subaddress : subaddresses) {
subaddress.m_balance = 0;
subaddress.m_unlocked_balance = 0;
subaddress.m_num_unspent_outputs = 0;
subaddress.m_num_blocks_to_unlock = 0;
}
// fetch and initialize balances
PyMoneroJsonRequest request2("get_balance", params);
auto response2 = m_rpc->send_json_request(request);
if (response2->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node2 = response2->m_result.get();
std::vector<std::shared_ptr<monero::monero_subaddress>> subaddresses2;
PyMoneroSubaddress::from_rpc_property_tree(node2, subaddresses2);
for (auto &tgt_subaddress: subaddresses) {
for (const auto &rpc_subaddress : subaddresses2) {
if (rpc_subaddress->m_index != tgt_subaddress.m_index) continue; // skip to subaddress with same index
if (rpc_subaddress->m_balance != boost::none) tgt_subaddress.m_balance = rpc_subaddress->m_balance;
if (rpc_subaddress->m_unlocked_balance != boost::none) tgt_subaddress.m_unlocked_balance = rpc_subaddress->m_unlocked_balance;
if (rpc_subaddress->m_num_unspent_outputs != boost::none) tgt_subaddress.m_num_unspent_outputs = rpc_subaddress->m_num_unspent_outputs;
if (rpc_subaddress->m_num_blocks_to_unlock != boost::none) tgt_subaddress.m_num_blocks_to_unlock = rpc_subaddress->m_num_blocks_to_unlock;
}
}
}
// cache addresses
auto it = m_address_cache.find(account_idx);
if (it == m_address_cache.end()) {
m_address_cache[account_idx] = serializable_unordered_map<uint32_t, std::string>();
}
for (const auto& subaddress : subaddresses) {
m_address_cache[account_idx][subaddress.m_index.get()] = subaddress.m_address.get();
}
// return results
return subaddresses;
}
std::vector<monero_subaddress> PyMoneroWalletRpc::get_subaddresses(uint32_t account_idx, const std::vector<uint32_t>& subaddress_indices) const {
return get_subaddresses(account_idx, subaddress_indices, false);
}
std::vector<monero_subaddress> PyMoneroWalletRpc::get_subaddresses(const uint32_t account_idx) const {
std::vector<uint32_t> empty_indices;
return get_subaddresses(account_idx, empty_indices);
}
monero_subaddress PyMoneroWalletRpc::get_subaddress(const uint32_t account_idx, const uint32_t subaddress_idx) const {
std::vector<uint32_t> subaddress_indices;
subaddress_indices.push_back(subaddress_idx);
auto subaddresses = get_subaddresses(account_idx, subaddress_indices);
if (subaddresses.empty()) throw std::runtime_error("Subaddress is not initialized");
if (subaddresses.size() != 1) throw std::runtime_error("Only 1 subaddress should be returned");
return subaddresses[0];
}
monero_subaddress PyMoneroWalletRpc::create_subaddress(uint32_t account_idx, const std::string& label) {
auto params = std::make_shared<PyMoneroCreateSubaddressParams>(account_idx, label);
PyMoneroJsonRequest request("create_address", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
monero_subaddress sub;
sub.m_account_index = account_idx;
if (!label.empty()) sub.m_label = label;
sub.m_balance = 0;
sub.m_unlocked_balance = 0;
sub.m_num_unspent_outputs = 0;
sub.m_is_used = false;
sub.m_num_blocks_to_unlock = 0;
for(auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("address_index")) sub.m_index = it->second.get_value<uint32_t>();
else if (key == std::string("address")) sub.m_address = it->second.data();
}
return sub;
}
void PyMoneroWalletRpc::set_subaddress_label(uint32_t account_idx, uint32_t subaddress_idx, const std::string& label) {
auto params = std::make_shared<PyMoneroSetSubaddressLabelParams>(account_idx, subaddress_idx, label);
PyMoneroJsonRequest request("label_address", params);
m_rpc->send_json_request(request);
}
std::string PyMoneroWalletRpc::export_outputs(bool all) const {
auto params = std::make_shared<PyMoneroImportExportOutputsParams>(all);
PyMoneroJsonRequest request("export_outputs", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
for (auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("outputs_data_hex")) return it->second.data();
}
throw std::runtime_error("Could not get outputs hex");
}
int PyMoneroWalletRpc::import_outputs(const std::string& outputs_hex) {
auto params = std::make_shared<PyMoneroImportExportOutputsParams>(outputs_hex);
PyMoneroJsonRequest request("import_outputs", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
for (auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("num_imported")) return it->second.get_value<int>();
}
return 0;
}
std::vector<std::shared_ptr<monero_key_image>> PyMoneroWalletRpc::export_key_images(bool all) const {
auto params = std::make_shared<PyMoneroImportExportKeyImagesParams>(all);
PyMoneroJsonRequest request("export_key_images", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
std::vector<std::shared_ptr<monero::monero_key_image>> key_images;
PyMoneroKeyImage::from_property_tree(node, key_images);
return key_images;
}
std::shared_ptr<monero_key_image_import_result> PyMoneroWalletRpc::import_key_images(const std::vector<std::shared_ptr<monero_key_image>>& key_images) {
auto params = std::make_shared<PyMoneroImportExportKeyImagesParams>(key_images);
PyMoneroJsonRequest request("import_key_images", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
auto import_result = std::make_shared<monero_key_image_import_result>();
PyMoneroKeyImageImportResult::from_property_tree(node, import_result);
return import_result;
}
std::vector<std::shared_ptr<monero_key_image>> PyMoneroWalletRpc::get_new_key_images_from_last_import() {
throw std::runtime_error("get_new_key_images_from_last_import(): not implemented");
}
void PyMoneroWalletRpc::freeze_output(const std::string& key_image) {
auto params = std::make_shared<PyMoneroQueryOutputParams>(key_image);
PyMoneroJsonRequest request("freeze", params);
m_rpc->send_json_request(request);
}
void PyMoneroWalletRpc::thaw_output(const std::string& key_image) {
auto params = std::make_shared<PyMoneroQueryOutputParams>(key_image);
PyMoneroJsonRequest request("thaw", params);
m_rpc->send_json_request(request);
}
bool PyMoneroWalletRpc::is_output_frozen(const std::string& key_image) {
auto params = std::make_shared<PyMoneroQueryOutputParams>(key_image);
PyMoneroJsonRequest request("frozen", params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
for(auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("frozen")) return it->second.get_value<bool>();
}
throw std::runtime_error("Could not get output");
}
monero_tx_priority PyMoneroWalletRpc::get_default_fee_priority() const {
PyMoneroJsonRequest request("get_default_fee_priority");
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
for(auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (key == std::string("priority")) {
int priority = it->second.get_value<int>();
if (priority == 0) return monero_tx_priority::DEFAULT;
else if (priority == 1) return monero_tx_priority::UNIMPORTANT;
else if (priority == 2) return monero_tx_priority::NORMAL;
else if (priority == 3) return monero_tx_priority::ELEVATED;
}
}
throw std::runtime_error("Could not get default fee priority");
}
std::vector<std::shared_ptr<monero_tx_wallet>> PyMoneroWalletRpc::create_txs(const monero_tx_config& conf) {
// validate, copy, and normalize request
monero_tx_config config = conf;
if (config.m_destinations.empty()) throw std::runtime_error("Destinations cannot be empty");
if (config.m_sweep_each_subaddress != boost::none) throw std::runtime_error("Sweep each subaddress not supported");
if (config.m_below_amount != boost::none) throw std::runtime_error("Below amount not supported");
if (config.m_can_split == boost::none) {
config = config.copy();
config.m_can_split = true;
}
if (config.m_relay == true && is_multisig()) throw std::runtime_error("Cannot relay multisig transaction until co-signed");
// determine account and subaddresses to send from
if (config.m_account_index == boost::none) throw std::runtime_error("Must specify the account index to send from");
auto account_idx = config.m_account_index.get();
// cannot apply subtractFeeFrom with `transfer_split` call
if (config.m_can_split && config.m_subtract_fee_from.size() > 0) {
throw std::runtime_error("subtractfeefrom transfers cannot be split over multiple transactions yet");
}
// build request parameters
auto params = std::make_shared<PyMoneroTransferParams>(config);
std::string request_path = "transfer";
if (config.m_can_split) request_path = "transfer_split";
PyMoneroJsonRequest request(request_path, params);
auto response = m_rpc->send_json_request(request);
if (response->m_result == boost::none) throw std::runtime_error("Invalid Monero JSONRPC response");
auto node = response->m_result.get();
// pre-initialize txs iff present. multisig and view-only wallets will have tx set without transactions
std::vector<std::shared_ptr<monero_tx_wallet>> txs;
int num_txs = 0;
for(auto it = node.begin(); it != node.end(); ++it) {
std::string key = it->first;
if (config.m_can_split && key == std::string("fee_list")) {
auto fee_list_node = it->second;
for(auto it2 = fee_list_node.begin(); it2 != fee_list_node.end(); ++it2) {
num_txs++;
}
}
}
bool copy_destinations = num_txs == 1;
for (int i = 0; i < num_txs; i++) {
auto tx = std::make_shared<monero::monero_tx_wallet>();
PyMoneroTxWallet::init_sent(config, tx, copy_destinations);