forked from UkoeHB/monero
-
Notifications
You must be signed in to change notification settings - Fork 2
/
legacy_knowledge_proofs.cpp
752 lines (686 loc) · 37.3 KB
/
legacy_knowledge_proofs.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
// Copyright (c) 2014-2023, The Monero Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include "legacy_knowledge_proofs.h"
#include <boost/thread/pthread/recursive_mutex.hpp>
#include <cstdint>
#include <string>
#include "common/base58.h"
#include "common/i18n.h"
#include "crypto/crypto.h"
#include "crypto/hash.h"
// #include "net/parse.h"
#include "cryptonote_basic/cryptonote_basic.h"
#include "cryptonote_basic/cryptonote_format_utils.h"
#include "device/device.hpp"
#include "net/http.h"
#include "net/http_client.h"
#include "ringct/rctTypes.h"
#include "rpc/core_rpc_server_commands_defs.h"
#include "rpc/core_rpc_server_error_codes.h"
#include "seraphis_impl/enote_store.h"
#include "seraphis_main/contextual_enote_record_types.h"
#include "seraphis_mocks/enote_finding_context_mocks.h"
#include "storages/http_abstract_invoke.h"
#include "wallet/wallet_errors.h"
//----------------------------------------------------------------------------------------------------
std::string generate_legacy_spend_proof(const std::string &message, const crypto::hash &txid,
const crypto::secret_key &spend_key, const sp::SpEnoteStore &enote_store)
{
// 1. Connection to node and tx query
// CHANGE NODE CONNECTION
// ----------------------
epee::net_utils::http::http_simple_client http_client;
constexpr const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::chrono::seconds(30);
http_client.set_server("127.0.0.1:18081", boost::none);
http_client.connect(rpc_timeout);
// ----------------------
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req = AUTO_VAL_INIT(req);
req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid));
req.decode_as_json = true;
req.prune = true;
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res = AUTO_VAL_INIT(res);
bool r;
r = epee::net_utils::invoke_http_json("/gettransactions", req, res, http_client, rpc_timeout);
THROW_WALLET_EXCEPTION_IF(!r, tools::error::wallet_internal_error, "Failed to get transaction from daemon");
cryptonote::transaction tx;
crypto::hash tx_hash;
THROW_WALLET_EXCEPTION_IF(!get_pruned_tx(res.txs[0], tx, tx_hash), tools::error::wallet_internal_error,
"Failed to get tx from daemon");
// 2. Get signature prefix hash
std::string sig_prefix_data((const char *)&txid, sizeof(crypto::hash));
sig_prefix_data += message;
crypto::hash sig_prefix_hash;
crypto::cn_fast_hash(sig_prefix_data.data(), sig_prefix_data.size(), sig_prefix_hash);
std::vector<std::vector<crypto::signature>> signatures;
// 3. Loop over all inputs
for (size_t i = 0; i < tx.vin.size(); i++)
{
const cryptonote::txin_to_key *const in_key = boost::get<cryptonote::txin_to_key>(std::addressof(tx.vin[i]));
if (in_key == nullptr) continue;
// 3.1 Check if the key image belongs to us
if (!enote_store.has_enote_with_key_image(in_key->k_image))
{
THROW_WALLET_EXCEPTION_IF(true, tools::error::wallet_internal_error,
"This tx wasn't generated by this wallet!");
}
// 3.2 Derive the real output keypair
sp::LegacyContextualEnoteRecordV1 contextual_record;
enote_store.try_get_legacy_enote_record(in_key->k_image, contextual_record);
rct::key pub_key_enote = sp::onetime_address_ref(contextual_record.record.enote);
crypto::secret_key sec_key_enote;
sc_add(to_bytes(sec_key_enote), to_bytes(contextual_record.record.enote_view_extension), to_bytes(spend_key));
THROW_WALLET_EXCEPTION_IF(in_key->k_image != contextual_record.record.key_image,
tools::error::wallet_internal_error, "key image mismatch");
// 3.3 Get output pubkeys in the ring
const std::vector<uint64_t> absolute_offsets =
cryptonote::relative_output_offsets_to_absolute(in_key->key_offsets);
const size_t ring_size = in_key->key_offsets.size();
THROW_WALLET_EXCEPTION_IF(absolute_offsets.size() != ring_size, tools::error::wallet_internal_error,
"absolute offsets size is wrong");
cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::request req = AUTO_VAL_INIT(req);
req.outputs.resize(ring_size);
for (size_t j = 0; j < ring_size; ++j)
{
req.outputs[j].amount = in_key->amount;
req.outputs[j].index = absolute_offsets[j];
}
cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::response res = AUTO_VAL_INIT(res);
{
r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, res, http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "get_outs.bin", tools::error::get_outs_error, res.status);
THROW_WALLET_EXCEPTION_IF(res.outs.size() != ring_size, tools::error::wallet_internal_error,
"daemon returned wrong response for "
"get_outs.bin, wrong amounts count = " +
std::to_string(res.outs.size()) + ", expected " + std::to_string(ring_size));
}
// 3.4 Copy pubkey pointers
std::vector<const crypto::public_key *> p_output_keys;
for (const cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::outkey &out : res.outs) p_output_keys.push_back(&out.key);
// 3.5 Figure out real output index and secret key
size_t sec_index = -1;
for (size_t j = 0; j < ring_size; ++j)
{
if ((rct::key &)res.outs[j].key == pub_key_enote)
{
sec_index = j;
break;
}
}
THROW_WALLET_EXCEPTION_IF(sec_index >= ring_size, tools::error::wallet_internal_error,
"secret index not found");
// 3.6 Generate ring sig for this input
signatures.push_back(std::vector<crypto::signature>());
std::vector<crypto::signature> &sigs = signatures.back();
sigs.resize(in_key->key_offsets.size());
crypto::generate_ring_signature(sig_prefix_hash, in_key->k_image, p_output_keys, sec_key_enote, sec_index,
sigs.data());
}
// 4. Finalize signature
std::string sig_str = "SpendProofV1";
for (const std::vector<crypto::signature> &ring_sig : signatures)
for (const crypto::signature &sig : ring_sig)
sig_str += tools::base58::encode(std::string((const char *)&sig, sizeof(crypto::signature)));
return sig_str;
}
//----------------------------------------------------------------------------------------------------
bool check_legacy_spend_proof(const crypto::hash &txid, const std::string &message, const std::string &sig_str)
{
// 1. Fetch tx from daemon
// CHANGE NODE CONNECTION
// ----------------------
epee::net_utils::http::http_simple_client http_client;
constexpr const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::chrono::seconds(30);
http_client.set_server("127.0.0.1:18081", boost::none);
http_client.connect(rpc_timeout);
// ----------------------
const std::string header = "SpendProofV1";
const size_t header_len = header.size();
THROW_WALLET_EXCEPTION_IF(sig_str.size() < header_len || sig_str.substr(0, header_len) != header,
tools::error::wallet_internal_error, "Signature header check error");
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req = AUTO_VAL_INIT(req);
req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid));
req.decode_as_json = false;
req.prune = true;
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res = AUTO_VAL_INIT(res);
bool r;
{
r = epee::net_utils::invoke_http_json("/gettransactions", req, res, http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR_GENERIC(r, {}, res, "gettransactions");
THROW_WALLET_EXCEPTION_IF(res.txs.size() != 1, tools::error::wallet_internal_error,
"daemon returned wrong response for gettransactions, wrong txs count "
"= " +
std::to_string(res.txs.size()) + ", expected 1");
}
cryptonote::transaction tx;
crypto::hash tx_hash;
THROW_WALLET_EXCEPTION_IF(!get_pruned_tx(res.txs[0], tx, tx_hash), tools::error::wallet_internal_error,
"failed to get tx from daemon");
// 2. Check signature size
size_t num_sigs = 0;
for (size_t i = 0; i < tx.vin.size(); ++i)
{
const cryptonote::txin_to_key *const in_key = boost::get<cryptonote::txin_to_key>(std::addressof(tx.vin[i]));
if (in_key != nullptr) num_sigs += in_key->key_offsets.size();
}
std::vector<std::vector<crypto::signature>> signatures = {std::vector<crypto::signature>(1)};
const size_t sig_len =
tools::base58::encode(std::string((const char *)&signatures[0][0], sizeof(crypto::signature))).size();
if (sig_str.size() != header_len + num_sigs * sig_len)
{
return false;
}
// 3. Decode base58
signatures.clear();
size_t offset = header_len;
for (size_t i = 0; i < tx.vin.size(); ++i)
{
const cryptonote::txin_to_key *const in_key = boost::get<cryptonote::txin_to_key>(std::addressof(tx.vin[i]));
if (in_key == nullptr) continue;
signatures.resize(signatures.size() + 1);
signatures.back().resize(in_key->key_offsets.size());
for (size_t j = 0; j < in_key->key_offsets.size(); ++j)
{
std::string sig_decoded;
THROW_WALLET_EXCEPTION_IF(!tools::base58::decode(sig_str.substr(offset, sig_len), sig_decoded),
tools::error::wallet_internal_error, "Signature decoding error");
THROW_WALLET_EXCEPTION_IF(sizeof(crypto::signature) != sig_decoded.size(),
tools::error::wallet_internal_error, "Signature decoding error");
memcpy(&signatures.back()[j], sig_decoded.data(), sizeof(crypto::signature));
offset += sig_len;
}
}
// 4. Get signature prefix hash
std::string sig_prefix_data((const char *)&txid, sizeof(crypto::hash));
sig_prefix_data += message;
crypto::hash sig_prefix_hash;
crypto::cn_fast_hash(sig_prefix_data.data(), sig_prefix_data.size(), sig_prefix_hash);
// 5. Loop over signatures
std::vector<std::vector<crypto::signature>>::const_iterator sig_iter = signatures.cbegin();
for (size_t i = 0; i < tx.vin.size(); ++i)
{
const cryptonote::txin_to_key *const in_key = boost::get<cryptonote::txin_to_key>(std::addressof(tx.vin[i]));
if (in_key == nullptr) continue;
// 5.1 Get output pubkeys in the ring
cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::request req = AUTO_VAL_INIT(req);
const std::vector<uint64_t> absolute_offsets =
cryptonote::relative_output_offsets_to_absolute(in_key->key_offsets);
req.outputs.resize(absolute_offsets.size());
for (size_t j = 0; j < absolute_offsets.size(); ++j)
{
req.outputs[j].amount = in_key->amount;
req.outputs[j].index = absolute_offsets[j];
}
cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::response res = AUTO_VAL_INIT(res);
bool r;
r = epee::net_utils::invoke_http_bin("/get_outs.bin", req, res, http_client, rpc_timeout);
THROW_ON_RPC_RESPONSE_ERROR(r, {}, res, "get_outs.bin", tools::error::get_outs_error, res.status);
THROW_WALLET_EXCEPTION_IF(res.outs.size() != req.outputs.size(), tools::error::wallet_internal_error,
"daemon returned wrong response for "
"get_outs.bin, wrong amounts count = " +
std::to_string(res.outs.size()) + ", expected " +
std::to_string(req.outputs.size()));
// 5.2 Copy pointers
std::vector<const crypto::public_key *> p_output_keys;
for (const cryptonote::COMMAND_RPC_GET_OUTPUTS_BIN::outkey &out : res.outs) p_output_keys.push_back(&out.key);
// 5.3 Check this ring
if (!crypto::check_ring_signature(sig_prefix_hash, in_key->k_image, p_output_keys, sig_iter->data()))
return false;
++sig_iter;
}
THROW_WALLET_EXCEPTION_IF(sig_iter != signatures.cend(), tools::error::wallet_internal_error,
"Signature iterator didn't reach the end");
return true;
}
//----------------------------------------------------------------------------------------------------
std::string generate_legacy_inproof(const crypto::hash &txid, const rct::key &spend_public_key,
const rct::key &view_public_key, const crypto::secret_key &secret_view_key,
bool is_subaddress, const std::string &message)
{
// 1. Fetch tx from daemon
// CHANGE NODE CONNECTION
// ----------------------
epee::net_utils::http::http_simple_client http_client;
constexpr const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::chrono::seconds(30);
http_client.set_server("127.0.0.1:18081", boost::none);
http_client.connect(rpc_timeout);
// ----------------------
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res;
req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid));
req.decode_as_json = false;
req.prune = true;
bool ok;
{
ok = epee::net_utils::invoke_http_json("/gettransactions", req, res, http_client);
THROW_WALLET_EXCEPTION_IF(!ok || (res.txs.size() != 1 && res.txs_as_hex.size() != 1),
tools::error::wallet_internal_error, "Failed to get transaction from daemon");
}
cryptonote::transaction tx;
crypto::hash tx_hash;
if (res.txs.size() == 1)
{
ok = get_pruned_tx(res.txs.front(), tx, tx_hash);
THROW_WALLET_EXCEPTION_IF(!ok, tools::error::wallet_internal_error, "Failed to parse transaction from daemon");
}
else
{
cryptonote::blobdata tx_data;
ok = epee::string_tools::parse_hexstr_to_binbuff(res.txs_as_hex.front(), tx_data);
THROW_WALLET_EXCEPTION_IF(!ok, tools::error::wallet_internal_error, "Failed to parse transaction from daemon");
THROW_WALLET_EXCEPTION_IF(!cryptonote::parse_and_validate_tx_from_blob(tx_data, tx),
tools::error::wallet_internal_error, "Failed to validate transaction from daemon");
tx_hash = cryptonote::get_transaction_hash(tx);
}
THROW_WALLET_EXCEPTION_IF(tx_hash != txid, tools::error::wallet_internal_error,
"Failed to get the right transaction from daemon");
std::string prefix_data((const char *)&txid, sizeof(crypto::hash));
prefix_data += message;
crypto::hash prefix_hash;
crypto::cn_fast_hash(prefix_data.data(), prefix_data.size(), prefix_hash);
// 2. Prepare device
// CHANGE DEVICE
// ----------------------
hw::device &hwdev = hw::get_device("default");
// ----------------------
rct::key aP;
std::vector<crypto::public_key> shared_secret;
std::vector<crypto::signature> sig;
std::string sig_str;
// 3. Get signature
{
crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx);
THROW_WALLET_EXCEPTION_IF(tx_pub_key == crypto::null_pkey, tools::error::wallet_internal_error,
"Tx pubkey was not found");
std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(tx);
const size_t num_sigs = 1 + additional_tx_pub_keys.size();
shared_secret.resize(num_sigs);
sig.resize(num_sigs);
const crypto::secret_key &a = secret_view_key;
hwdev.scalarmultKey(aP, rct::pk2rct(tx_pub_key), rct::sk2rct(a));
shared_secret[0] = rct2pk(aP);
if (is_subaddress)
{
hwdev.generate_tx_proof(prefix_hash, rct::rct2pk(view_public_key), tx_pub_key,
rct::rct2pk(spend_public_key), shared_secret[0], a, sig[0]);
}
else
{
hwdev.generate_tx_proof(prefix_hash, rct::rct2pk(view_public_key), tx_pub_key, boost::none,
shared_secret[0], a, sig[0]);
}
for (size_t i = 1; i < num_sigs; ++i)
{
hwdev.scalarmultKey(aP, rct::pk2rct(additional_tx_pub_keys[i - 1]), rct::sk2rct(a));
shared_secret[i] = rct2pk(aP);
if (is_subaddress)
{
hwdev.generate_tx_proof(prefix_hash, rct::rct2pk(view_public_key), additional_tx_pub_keys[i - 1],
rct::rct2pk(spend_public_key), shared_secret[i], a, sig[i]);
}
else
{
hwdev.generate_tx_proof(prefix_hash, rct::rct2pk(view_public_key), additional_tx_pub_keys[i - 1],
boost::none, shared_secret[i], a, sig[i]);
}
}
sig_str = std::string("InProofV2");
}
const size_t num_sigs = shared_secret.size();
// 4. Check if this address actually received any funds
crypto::key_derivation derivation;
THROW_WALLET_EXCEPTION_IF(!crypto::generate_key_derivation(shared_secret[0], rct::rct2sk(rct::I), derivation),
tools::error::wallet_internal_error, "Failed to generate key derivation");
std::vector<crypto::key_derivation> additional_derivations(num_sigs - 1);
for (size_t i = 1; i < num_sigs; ++i)
THROW_WALLET_EXCEPTION_IF(
!crypto::generate_key_derivation(shared_secret[i], rct::rct2sk(rct::I), additional_derivations[i - 1]),
tools::error::wallet_internal_error, "Failed to generate key derivation");
uint64_t received;
cryptonote::account_public_address address{rct::rct2pk(spend_public_key), rct::rct2pk(view_public_key)};
check_tx_key_helper(tx, derivation, additional_derivations, address, received);
THROW_WALLET_EXCEPTION_IF(!received, tools::error::wallet_internal_error, tr("No funds received in this tx."));
// 5. Concatenate all signature strings
for (size_t i = 0; i < num_sigs; ++i)
sig_str += tools::base58::encode(std::string((const char *)&shared_secret[i], sizeof(crypto::public_key))) +
tools::base58::encode(std::string((const char *)&sig[i], sizeof(crypto::signature)));
return sig_str;
}
//----------------------------------------------------------------------------------------------------
bool check_tx_proof(const crypto::hash &txid, const cryptonote::account_public_address &address, bool is_subaddress,
const std::string &message, const std::string &sig_str, uint64_t &received, bool &in_pool,
uint64_t &confirmations)
{
// 1. Fetch tx from daemon
// CHANGE NODE CONNECTION
// ----------------------
epee::net_utils::http::http_simple_client http_client;
constexpr const std::chrono::seconds rpc_timeout = std::chrono::minutes(3) + std::chrono::seconds(30);
http_client.set_server("127.0.0.1:18081", boost::none);
http_client.connect(rpc_timeout);
// ----------------------
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::request req;
cryptonote::COMMAND_RPC_GET_TRANSACTIONS::response res;
req.txs_hashes.push_back(epee::string_tools::pod_to_hex(txid));
req.decode_as_json = false;
req.prune = true;
bool ok;
{
ok = epee::net_utils::invoke_http_json("/gettransactions", req, res, http_client);
THROW_WALLET_EXCEPTION_IF(!ok || (res.txs.size() != 1 && res.txs_as_hex.size() != 1),
tools::error::wallet_internal_error, "Failed to get transaction from daemon");
}
cryptonote::transaction tx;
crypto::hash tx_hash;
if (res.txs.size() == 1)
{
ok = get_pruned_tx(res.txs.front(), tx, tx_hash);
THROW_WALLET_EXCEPTION_IF(!ok, tools::error::wallet_internal_error, "Failed to parse transaction from daemon");
}
else
{
cryptonote::blobdata tx_data;
ok = epee::string_tools::parse_hexstr_to_binbuff(res.txs_as_hex.front(), tx_data);
THROW_WALLET_EXCEPTION_IF(!ok, tools::error::wallet_internal_error, "Failed to parse transaction from daemon");
THROW_WALLET_EXCEPTION_IF(!cryptonote::parse_and_validate_tx_from_blob(tx_data, tx),
tools::error::wallet_internal_error, "Failed to validate transaction from daemon");
tx_hash = cryptonote::get_transaction_hash(tx);
}
THROW_WALLET_EXCEPTION_IF(tx_hash != txid, tools::error::wallet_internal_error,
"Failed to get the right transaction from daemon");
// 2. Check proof
if (!check_tx_proof(tx, address, is_subaddress, message, sig_str, received)) return false;
// 3. Get block height
in_pool = res.txs.front().in_pool;
confirmations = 0;
std::uint64_t conf_height = res.txs.front().block_height;
cryptonote::COMMAND_RPC_GET_HEIGHT::request reqh;
cryptonote::COMMAND_RPC_GET_HEIGHT::response resph;
if (!in_pool)
{
epee::net_utils::invoke_http_json("/get_height", reqh, resph, http_client);
uint64_t bc_height = resph.height;
confirmations = bc_height - conf_height;
}
return true;
}
//----------------------------------------------------------------------------------------------------
bool check_tx_proof(const cryptonote::transaction &tx, const cryptonote::account_public_address &address,
bool is_subaddress, const std::string &message, const std::string &sig_str, uint64_t &received)
{
// 1. Get proof type and version
// InProofV1, InProofV2, OutProofV1, OutProofV2
const bool is_out = sig_str.substr(0, 3) == "Out";
const std::string header = is_out ? sig_str.substr(0, 10) : sig_str.substr(0, 9);
int version = 2; // InProofV2
if (is_out && sig_str.substr(8, 2) == "V1")
version = 1; // OutProofV1
else if (is_out)
version = 2; // OutProofV2
else if (sig_str.substr(7, 2) == "V1")
version = 1; // InProofV1
const size_t header_len = header.size();
THROW_WALLET_EXCEPTION_IF(sig_str.size() < header_len || sig_str.substr(0, header_len) != header,
tools::error::wallet_internal_error, "Signature header check error");
// 2. Decode base58
std::vector<crypto::public_key> shared_secret(1);
std::vector<crypto::signature> sig(1);
const size_t pk_len =
tools::base58::encode(std::string((const char *)&shared_secret[0], sizeof(crypto::public_key))).size();
const size_t sig_len = tools::base58::encode(std::string((const char *)&sig[0], sizeof(crypto::signature))).size();
const size_t num_sigs = (sig_str.size() - header_len) / (pk_len + sig_len);
THROW_WALLET_EXCEPTION_IF(sig_str.size() != header_len + num_sigs * (pk_len + sig_len),
tools::error::wallet_internal_error, "Wrong signature size");
shared_secret.resize(num_sigs);
sig.resize(num_sigs);
for (size_t i = 0; i < num_sigs; ++i)
{
std::string pk_decoded;
std::string sig_decoded;
const size_t offset = header_len + i * (pk_len + sig_len);
THROW_WALLET_EXCEPTION_IF(!tools::base58::decode(sig_str.substr(offset, pk_len), pk_decoded),
tools::error::wallet_internal_error, "Signature decoding error");
THROW_WALLET_EXCEPTION_IF(!tools::base58::decode(sig_str.substr(offset + pk_len, sig_len), sig_decoded),
tools::error::wallet_internal_error, "Signature decoding error");
THROW_WALLET_EXCEPTION_IF(
sizeof(crypto::public_key) != pk_decoded.size() || sizeof(crypto::signature) != sig_decoded.size(),
tools::error::wallet_internal_error, "Signature decoding error");
memcpy(&shared_secret[i], pk_decoded.data(), sizeof(crypto::public_key));
memcpy(&sig[i], sig_decoded.data(), sizeof(crypto::signature));
}
crypto::public_key tx_pub_key = get_tx_pub_key_from_extra(tx);
THROW_WALLET_EXCEPTION_IF(tx_pub_key == crypto::null_pkey, tools::error::wallet_internal_error,
"Tx pubkey was not found");
std::vector<crypto::public_key> additional_tx_pub_keys = get_additional_tx_pub_keys_from_extra(tx);
THROW_WALLET_EXCEPTION_IF(additional_tx_pub_keys.size() + 1 != num_sigs, tools::error::wallet_internal_error,
"Signature size mismatch with additional tx pubkeys");
const crypto::hash txid = cryptonote::get_transaction_hash(tx);
std::string prefix_data((const char *)&txid, sizeof(crypto::hash));
prefix_data += message;
crypto::hash prefix_hash;
crypto::cn_fast_hash(prefix_data.data(), prefix_data.size(), prefix_hash);
// 3. Check signature
std::vector<int> good_signature(num_sigs, 0);
if (is_out)
{
good_signature[0] = is_subaddress
? crypto::check_tx_proof(prefix_hash, tx_pub_key, address.m_view_public_key,
address.m_spend_public_key, shared_secret[0], sig[0], version)
: crypto::check_tx_proof(prefix_hash, tx_pub_key, address.m_view_public_key,
boost::none, shared_secret[0], sig[0], version);
for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i)
{
good_signature[i + 1] =
is_subaddress
? crypto::check_tx_proof(prefix_hash, additional_tx_pub_keys[i], address.m_view_public_key,
address.m_spend_public_key, shared_secret[i + 1], sig[i + 1], version)
: crypto::check_tx_proof(prefix_hash, additional_tx_pub_keys[i], address.m_view_public_key,
boost::none, shared_secret[i + 1], sig[i + 1], version);
}
}
else
{
good_signature[0] = is_subaddress
? crypto::check_tx_proof(prefix_hash, address.m_view_public_key, tx_pub_key,
address.m_spend_public_key, shared_secret[0], sig[0], version)
: crypto::check_tx_proof(prefix_hash, address.m_view_public_key, tx_pub_key,
boost::none, shared_secret[0], sig[0], version);
for (size_t i = 0; i < additional_tx_pub_keys.size(); ++i)
{
good_signature[i + 1] =
is_subaddress
? crypto::check_tx_proof(prefix_hash, address.m_view_public_key, additional_tx_pub_keys[i],
address.m_spend_public_key, shared_secret[i + 1], sig[i + 1], version)
: crypto::check_tx_proof(prefix_hash, address.m_view_public_key, additional_tx_pub_keys[i],
boost::none, shared_secret[i + 1], sig[i + 1], version);
}
}
if (std::any_of(good_signature.begin(), good_signature.end(), [](int i) { return i > 0; }))
{
// 4. Obtain key derivation by multiplying scalar 1 to the shared secret
crypto::key_derivation derivation;
if (good_signature[0])
THROW_WALLET_EXCEPTION_IF(
!crypto::generate_key_derivation(shared_secret[0], rct::rct2sk(rct::I), derivation),
tools::error::wallet_internal_error, "Failed to generate key derivation");
std::vector<crypto::key_derivation> additional_derivations(num_sigs - 1);
for (size_t i = 1; i < num_sigs; ++i)
if (good_signature[i])
THROW_WALLET_EXCEPTION_IF(!crypto::generate_key_derivation(shared_secret[i], rct::rct2sk(rct::I),
additional_derivations[i - 1]),
tools::error::wallet_internal_error, "Failed to generate key derivation");
check_tx_key_helper(tx, derivation, additional_derivations, address, received);
return true;
}
return false;
}
//----------------------------------------------------------------------------------------------------
bool get_pruned_tx(const cryptonote::COMMAND_RPC_GET_TRANSACTIONS::entry &entry, cryptonote::transaction &tx,
crypto::hash &tx_hash)
{
cryptonote::blobdata bd;
// easy case if we have the whole tx
if (!entry.as_hex.empty() || (!entry.prunable_as_hex.empty() && !entry.pruned_as_hex.empty()))
{
CHECK_AND_ASSERT_MES(epee::string_tools::parse_hexstr_to_binbuff(
entry.as_hex.empty() ? entry.pruned_as_hex + entry.prunable_as_hex : entry.as_hex, bd),
false, "Failed to parse tx data");
CHECK_AND_ASSERT_MES(cryptonote::parse_and_validate_tx_from_blob(bd, tx), false, "Invalid tx data");
tx_hash = cryptonote::get_transaction_hash(tx);
// if the hash was given, check it matches
CHECK_AND_ASSERT_MES(entry.tx_hash.empty() || epee::string_tools::pod_to_hex(tx_hash) == entry.tx_hash, false,
"Response claims a different hash than the data yields");
return true;
}
// case of a pruned tx with its prunable data hash
if (!entry.pruned_as_hex.empty() && !entry.prunable_hash.empty())
{
crypto::hash ph;
CHECK_AND_ASSERT_MES(epee::string_tools::hex_to_pod(entry.prunable_hash, ph), false,
"Failed to parse prunable hash");
CHECK_AND_ASSERT_MES(epee::string_tools::parse_hexstr_to_binbuff(entry.pruned_as_hex, bd), false,
"Failed to parse pruned data");
CHECK_AND_ASSERT_MES(parse_and_validate_tx_base_from_blob(bd, tx), false, "Invalid base tx data");
// only v2 txes can calculate their txid after pruned
if (bd[0] > 1)
{
tx_hash = cryptonote::get_pruned_transaction_hash(tx, ph);
}
else
{
// for v1, we trust the dameon
CHECK_AND_ASSERT_MES(epee::string_tools::hex_to_pod(entry.tx_hash, tx_hash), false,
"Failed to parse tx hash");
}
return true;
}
return false;
}
//----------------------------------------------------------------------------------------------------
void check_tx_key_helper(const cryptonote::transaction &tx, const crypto::key_derivation &derivation,
const std::vector<crypto::key_derivation> &additional_derivations,
const cryptonote::account_public_address &address, uint64_t &received)
{
received = 0;
for (size_t n = 0; n < tx.vout.size(); ++n)
{
crypto::public_key output_public_key;
if (!get_output_public_key(tx.vout[n], output_public_key)) continue;
crypto::key_derivation found_derivation;
if (is_out_to_acc(address, output_public_key, derivation, additional_derivations, n,
get_output_view_tag(tx.vout[n]), found_derivation))
{
uint64_t amount;
if (tx.version == 1 || tx.rct_signatures.type == rct::RCTTypeNull)
{
amount = tx.vout[n].amount;
}
else
{
crypto::secret_key scalar1;
crypto::derivation_to_scalar(found_derivation, n, scalar1);
rct::ecdhTuple ecdh_info = tx.rct_signatures.ecdhInfo[n];
rct::ecdhDecode(ecdh_info, rct::sk2rct(scalar1),
tx.rct_signatures.type == rct::RCTTypeBulletproof2 ||
tx.rct_signatures.type == rct::RCTTypeCLSAG ||
tx.rct_signatures.type == rct::RCTTypeBulletproofPlus);
const rct::key C = tx.rct_signatures.outPk[n].mask;
rct::key Ctmp;
THROW_WALLET_EXCEPTION_IF(sc_check(ecdh_info.mask.bytes) != 0, tools::error::wallet_internal_error,
"Bad ECDH input mask");
THROW_WALLET_EXCEPTION_IF(sc_check(ecdh_info.amount.bytes) != 0, tools::error::wallet_internal_error,
"Bad ECDH input amount");
rct::addKeys2(Ctmp, ecdh_info.mask, ecdh_info.amount, rct::H);
if (rct::equalKeys(C, Ctmp))
amount = rct::h2d(ecdh_info.amount);
else
amount = 0;
}
received += amount;
}
}
}
//----------------------------------------------------------------------------------------------------
bool is_out_to_acc(const cryptonote::account_public_address &address, const crypto::public_key &out_key,
const crypto::key_derivation &derivation,
const std::vector<crypto::key_derivation> &additional_derivations, const size_t output_index,
const boost::optional<crypto::view_tag> &view_tag_opt, crypto::key_derivation &found_derivation)
{
crypto::public_key derived_out_key;
bool found = false;
bool r;
// first run quick check if output has matching view tag, otherwise output
// should not belong to account
if (cryptonote::out_can_be_to_acc(view_tag_opt, derivation, output_index))
{
// if view tag match, run slower check deriving output pub key and comparing
// to expected
r = crypto::derive_public_key(derivation, output_index, address.m_spend_public_key, derived_out_key);
THROW_WALLET_EXCEPTION_IF(!r, tools::error::wallet_internal_error, "Failed to derive public key");
if (out_key == derived_out_key)
{
found = true;
found_derivation = derivation;
}
}
if (!found && !additional_derivations.empty())
{
const crypto::key_derivation &additional_derivation = additional_derivations[output_index];
if (cryptonote::out_can_be_to_acc(view_tag_opt, additional_derivation, output_index))
{
r = crypto::derive_public_key(additional_derivation, output_index, address.m_spend_public_key,
derived_out_key);
THROW_WALLET_EXCEPTION_IF(!r, tools::error::wallet_internal_error, "Failed to derive public key");
if (out_key == derived_out_key)
{
found = true;
found_derivation = additional_derivation;
}
}
}
return found;
}
//----------------------------------------------------------------------------------------------------
void throw_on_rpc_response_error(bool r, const epee::json_rpc::error &error, const std::string &status,
const char *method)
{
// Treat all RPC payment access errors the same, whether payment is actually required or not
THROW_WALLET_EXCEPTION_IF(error.code == CORE_RPC_ERROR_CODE_INVALID_CLIENT, tools::error::deprecated_rpc_access,
method);
THROW_WALLET_EXCEPTION_IF(error.code, tools::error::wallet_coded_rpc_error, method, error.code,
get_rpc_server_error_message(error.code));
THROW_WALLET_EXCEPTION_IF(!r, tools::error::no_connection_to_daemon, method);
// empty string -> not connection
THROW_WALLET_EXCEPTION_IF(status.empty(), tools::error::no_connection_to_daemon, method);
THROW_WALLET_EXCEPTION_IF(status == CORE_RPC_STATUS_BUSY, tools::error::daemon_busy, method);
THROW_WALLET_EXCEPTION_IF(status == CORE_RPC_STATUS_PAYMENT_REQUIRED, tools::error::deprecated_rpc_access, method);
// Deprecated RPC payment access endpoints would set status to "Client signature does not verify for <method>"
THROW_WALLET_EXCEPTION_IF(status.compare(0, 16, "Client signature") == 0, tools::error::deprecated_rpc_access,
method);
}
//----------------------------------------------------------------------------------------------------