-
Notifications
You must be signed in to change notification settings - Fork 785
/
wallet.cpp
2043 lines (1910 loc) · 64.9 KB
/
wallet.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/threading.hpp>
#include <nano/lib/utility.hpp>
#include <nano/node/election.hpp>
#include <nano/node/lmdb/lmdb_iterator.hpp>
#include <nano/node/node.hpp>
#include <nano/node/wallet.hpp>
#include <boost/filesystem.hpp>
#include <boost/format.hpp>
#include <boost/polymorphic_cast.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <future>
#include <argon2.h>
nano::uint256_union nano::wallet_store::check (nano::transaction const & transaction_a)
{
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::check_special));
return value.key;
}
nano::uint256_union nano::wallet_store::salt (nano::transaction const & transaction_a)
{
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::salt_special));
return value.key;
}
void nano::wallet_store::wallet_key (nano::raw_key & prv_a, nano::transaction const & transaction_a)
{
nano::lock_guard<std::recursive_mutex> lock (mutex);
nano::raw_key wallet_l;
wallet_key_mem.value (wallet_l);
nano::raw_key password_l;
password.value (password_l);
prv_a.decrypt (wallet_l.data, password_l, salt (transaction_a).owords[0]);
}
void nano::wallet_store::seed (nano::raw_key & prv_a, nano::transaction const & transaction_a)
{
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::seed_special));
nano::raw_key password_l;
wallet_key (password_l, transaction_a);
prv_a.decrypt (value.key, password_l, salt (transaction_a).owords[seed_iv_index]);
}
void nano::wallet_store::seed_set (nano::transaction const & transaction_a, nano::raw_key const & prv_a)
{
nano::raw_key password_l;
wallet_key (password_l, transaction_a);
nano::uint256_union ciphertext;
ciphertext.encrypt (prv_a, password_l, salt (transaction_a).owords[seed_iv_index]);
entry_put_raw (transaction_a, nano::wallet_store::seed_special, nano::wallet_value (ciphertext, 0));
deterministic_clear (transaction_a);
}
nano::public_key nano::wallet_store::deterministic_insert (nano::transaction const & transaction_a)
{
auto index (deterministic_index_get (transaction_a));
auto prv = deterministic_key (transaction_a, index);
nano::public_key result (nano::pub_key (prv));
while (exists (transaction_a, result))
{
++index;
prv = deterministic_key (transaction_a, index);
result = nano::pub_key (prv);
}
uint64_t marker (1);
marker <<= 32;
marker |= index;
entry_put_raw (transaction_a, result, nano::wallet_value (nano::uint256_union (marker), 0));
++index;
deterministic_index_set (transaction_a, index);
return result;
}
nano::public_key nano::wallet_store::deterministic_insert (nano::transaction const & transaction_a, uint32_t const index)
{
auto prv = deterministic_key (transaction_a, index);
nano::public_key result (nano::pub_key (prv));
uint64_t marker (1);
marker <<= 32;
marker |= index;
entry_put_raw (transaction_a, result, nano::wallet_value (nano::uint256_union (marker), 0));
return result;
}
nano::private_key nano::wallet_store::deterministic_key (nano::transaction const & transaction_a, uint32_t index_a)
{
assert (valid_password (transaction_a));
nano::raw_key seed_l;
seed (seed_l, transaction_a);
return nano::deterministic_key (seed_l, index_a);
}
uint32_t nano::wallet_store::deterministic_index_get (nano::transaction const & transaction_a)
{
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::deterministic_index_special));
return static_cast<uint32_t> (value.key.number () & static_cast<uint32_t> (-1));
}
void nano::wallet_store::deterministic_index_set (nano::transaction const & transaction_a, uint32_t index_a)
{
nano::uint256_union index_l (index_a);
nano::wallet_value value (index_l, 0);
entry_put_raw (transaction_a, nano::wallet_store::deterministic_index_special, value);
}
void nano::wallet_store::deterministic_clear (nano::transaction const & transaction_a)
{
nano::uint256_union key (0);
for (auto i (begin (transaction_a)), n (end ()); i != n;)
{
switch (key_type (nano::wallet_value (i->second)))
{
case nano::key_type::deterministic:
{
auto const & key (i->first);
erase (transaction_a, key);
i = begin (transaction_a, key);
break;
}
default:
{
++i;
break;
}
}
}
deterministic_index_set (transaction_a, 0);
}
bool nano::wallet_store::valid_password (nano::transaction const & transaction_a)
{
nano::raw_key zero;
zero.data.clear ();
nano::raw_key wallet_key_l;
wallet_key (wallet_key_l, transaction_a);
nano::uint256_union check_l;
check_l.encrypt (zero, wallet_key_l, salt (transaction_a).owords[check_iv_index]);
bool ok = check (transaction_a) == check_l;
return ok;
}
bool nano::wallet_store::attempt_password (nano::transaction const & transaction_a, std::string const & password_a)
{
bool result = false;
{
nano::lock_guard<std::recursive_mutex> lock (mutex);
nano::raw_key password_l;
derive_key (password_l, transaction_a, password_a);
password.value_set (password_l);
result = !valid_password (transaction_a);
}
if (!result)
{
switch (version (transaction_a))
{
case version_1:
upgrade_v1_v2 (transaction_a);
case version_2:
upgrade_v2_v3 (transaction_a);
case version_3:
upgrade_v3_v4 (transaction_a);
case version_4:
break;
default:
assert (false);
}
}
return result;
}
bool nano::wallet_store::rekey (nano::transaction const & transaction_a, std::string const & password_a)
{
nano::lock_guard<std::recursive_mutex> lock (mutex);
bool result (false);
if (valid_password (transaction_a))
{
nano::raw_key password_new;
derive_key (password_new, transaction_a, password_a);
nano::raw_key wallet_key_l;
wallet_key (wallet_key_l, transaction_a);
nano::raw_key password_l;
password.value (password_l);
password.value_set (password_new);
nano::uint256_union encrypted;
encrypted.encrypt (wallet_key_l, password_new, salt (transaction_a).owords[0]);
nano::raw_key wallet_enc;
wallet_enc.data = encrypted;
wallet_key_mem.value_set (wallet_enc);
entry_put_raw (transaction_a, nano::wallet_store::wallet_key_special, nano::wallet_value (encrypted, 0));
}
else
{
result = true;
}
return result;
}
void nano::wallet_store::derive_key (nano::raw_key & prv_a, nano::transaction const & transaction_a, std::string const & password_a)
{
auto salt_l (salt (transaction_a));
kdf.phs (prv_a, password_a, salt_l);
}
nano::fan::fan (nano::uint256_union const & key, size_t count_a)
{
auto first (std::make_unique<nano::uint256_union> (key));
for (auto i (1); i < count_a; ++i)
{
auto entry (std::make_unique<nano::uint256_union> ());
nano::random_pool::generate_block (entry->bytes.data (), entry->bytes.size ());
*first ^= *entry;
values.push_back (std::move (entry));
}
values.push_back (std::move (first));
}
void nano::fan::value (nano::raw_key & prv_a)
{
nano::lock_guard<std::mutex> lock (mutex);
value_get (prv_a);
}
void nano::fan::value_get (nano::raw_key & prv_a)
{
assert (!mutex.try_lock ());
prv_a.data.clear ();
for (auto & i : values)
{
prv_a.data ^= *i;
}
}
void nano::fan::value_set (nano::raw_key const & value_a)
{
nano::lock_guard<std::mutex> lock (mutex);
nano::raw_key value_l;
value_get (value_l);
*(values[0]) ^= value_l.data;
*(values[0]) ^= value_a.data;
}
// Wallet version number
nano::account const nano::wallet_store::version_special (0);
// Random number used to salt private key encryption
nano::account const nano::wallet_store::salt_special (1);
// Key used to encrypt wallet keys, encrypted itself by the user password
nano::account const nano::wallet_store::wallet_key_special (2);
// Check value used to see if password is valid
nano::account const nano::wallet_store::check_special (3);
// Representative account to be used if we open a new account
nano::account const nano::wallet_store::representative_special (4);
// Wallet seed for deterministic key generation
nano::account const nano::wallet_store::seed_special (5);
// Current key index for deterministic keys
nano::account const nano::wallet_store::deterministic_index_special (6);
int const nano::wallet_store::special_count (7);
size_t const nano::wallet_store::check_iv_index (0);
size_t const nano::wallet_store::seed_iv_index (1);
nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, nano::transaction & transaction_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a, std::string const & json_a) :
password (0, fanout_a),
wallet_key_mem (0, fanout_a),
kdf (kdf_a)
{
init_a = false;
initialize (transaction_a, init_a, wallet_a);
if (!init_a)
{
MDB_val junk;
assert (mdb_get (tx (transaction_a), handle, nano::mdb_val (version_special), &junk) == MDB_NOTFOUND);
boost::property_tree::ptree wallet_l;
std::stringstream istream (json_a);
try
{
boost::property_tree::read_json (istream, wallet_l);
}
catch (...)
{
init_a = true;
}
for (auto i (wallet_l.begin ()), n (wallet_l.end ()); i != n; ++i)
{
nano::account key;
init_a = key.decode_hex (i->first);
if (!init_a)
{
nano::uint256_union value;
init_a = value.decode_hex (wallet_l.get<std::string> (i->first));
if (!init_a)
{
entry_put_raw (transaction_a, key, nano::wallet_value (value, 0));
}
else
{
init_a = true;
}
}
else
{
init_a = true;
}
}
init_a |= mdb_get (tx (transaction_a), handle, nano::mdb_val (version_special), &junk) != 0;
init_a |= mdb_get (tx (transaction_a), handle, nano::mdb_val (wallet_key_special), &junk) != 0;
init_a |= mdb_get (tx (transaction_a), handle, nano::mdb_val (salt_special), &junk) != 0;
init_a |= mdb_get (tx (transaction_a), handle, nano::mdb_val (check_special), &junk) != 0;
init_a |= mdb_get (tx (transaction_a), handle, nano::mdb_val (representative_special), &junk) != 0;
nano::raw_key key;
key.data.clear ();
password.value_set (key);
key.data = entry_get_raw (transaction_a, nano::wallet_store::wallet_key_special).key;
wallet_key_mem.value_set (key);
}
}
nano::wallet_store::wallet_store (bool & init_a, nano::kdf & kdf_a, nano::transaction & transaction_a, nano::account representative_a, unsigned fanout_a, std::string const & wallet_a) :
password (0, fanout_a),
wallet_key_mem (0, fanout_a),
kdf (kdf_a)
{
init_a = false;
initialize (transaction_a, init_a, wallet_a);
if (!init_a)
{
int version_status;
MDB_val version_value;
version_status = mdb_get (tx (transaction_a), handle, nano::mdb_val (version_special), &version_value);
if (version_status == MDB_NOTFOUND)
{
version_put (transaction_a, version_current);
nano::uint256_union salt_l;
random_pool::generate_block (salt_l.bytes.data (), salt_l.bytes.size ());
entry_put_raw (transaction_a, nano::wallet_store::salt_special, nano::wallet_value (salt_l, 0));
// Wallet key is a fixed random key that encrypts all entries
nano::raw_key wallet_key;
random_pool::generate_block (wallet_key.data.bytes.data (), sizeof (wallet_key.data.bytes));
nano::raw_key password_l;
password_l.data.clear ();
password.value_set (password_l);
nano::raw_key zero;
zero.data.clear ();
// Wallet key is encrypted by the user's password
nano::uint256_union encrypted;
encrypted.encrypt (wallet_key, zero, salt_l.owords[0]);
entry_put_raw (transaction_a, nano::wallet_store::wallet_key_special, nano::wallet_value (encrypted, 0));
nano::raw_key wallet_key_enc;
wallet_key_enc.data = encrypted;
wallet_key_mem.value_set (wallet_key_enc);
nano::uint256_union check;
check.encrypt (zero, wallet_key, salt_l.owords[check_iv_index]);
entry_put_raw (transaction_a, nano::wallet_store::check_special, nano::wallet_value (check, 0));
entry_put_raw (transaction_a, nano::wallet_store::representative_special, nano::wallet_value (representative_a, 0));
nano::raw_key seed;
random_pool::generate_block (seed.data.bytes.data (), seed.data.bytes.size ());
seed_set (transaction_a, seed);
entry_put_raw (transaction_a, nano::wallet_store::deterministic_index_special, nano::wallet_value (nano::uint256_union (0), 0));
}
}
nano::raw_key key;
key.data = entry_get_raw (transaction_a, nano::wallet_store::wallet_key_special).key;
wallet_key_mem.value_set (key);
}
std::vector<nano::account> nano::wallet_store::accounts (nano::transaction const & transaction_a)
{
std::vector<nano::account> result;
for (auto i (begin (transaction_a)), n (end ()); i != n; ++i)
{
nano::account const & account (i->first);
result.push_back (account);
}
return result;
}
void nano::wallet_store::initialize (nano::transaction const & transaction_a, bool & init_a, std::string const & path_a)
{
assert (strlen (path_a.c_str ()) == path_a.size ());
auto error (0);
error |= mdb_dbi_open (tx (transaction_a), path_a.c_str (), MDB_CREATE, &handle);
init_a = error != 0;
}
bool nano::wallet_store::is_representative (nano::transaction const & transaction_a)
{
return exists (transaction_a, representative (transaction_a));
}
void nano::wallet_store::representative_set (nano::transaction const & transaction_a, nano::account const & representative_a)
{
entry_put_raw (transaction_a, nano::wallet_store::representative_special, nano::wallet_value (representative_a, 0));
}
nano::account nano::wallet_store::representative (nano::transaction const & transaction_a)
{
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::representative_special));
return reinterpret_cast<nano::account const &> (value.key);
}
nano::public_key nano::wallet_store::insert_adhoc (nano::transaction const & transaction_a, nano::raw_key const & prv)
{
assert (valid_password (transaction_a));
nano::public_key pub (nano::pub_key (prv.as_private_key ()));
nano::raw_key password_l;
wallet_key (password_l, transaction_a);
nano::private_key ciphertext;
ciphertext.encrypt (prv, password_l, pub.owords[0].number ());
entry_put_raw (transaction_a, pub, nano::wallet_value (ciphertext, 0));
return pub;
}
bool nano::wallet_store::insert_watch (nano::transaction const & transaction_a, nano::account const & pub_a)
{
bool error (!valid_public_key (pub_a));
if (!error)
{
entry_put_raw (transaction_a, pub_a, nano::wallet_value (nano::private_key (0), 0));
}
return error;
}
void nano::wallet_store::erase (nano::transaction const & transaction_a, nano::account const & pub)
{
auto status (mdb_del (tx (transaction_a), handle, nano::mdb_val (pub), nullptr));
(void)status;
assert (status == 0);
}
nano::wallet_value nano::wallet_store::entry_get_raw (nano::transaction const & transaction_a, nano::account const & pub_a)
{
nano::wallet_value result;
nano::mdb_val value;
auto status (mdb_get (tx (transaction_a), handle, nano::mdb_val (pub_a), value));
if (status == 0)
{
result = nano::wallet_value (value);
}
else
{
result.key.clear ();
result.work = 0;
}
return result;
}
void nano::wallet_store::entry_put_raw (nano::transaction const & transaction_a, nano::account const & pub_a, nano::wallet_value const & entry_a)
{
auto status (mdb_put (tx (transaction_a), handle, nano::mdb_val (pub_a), nano::mdb_val (sizeof (entry_a), const_cast<nano::wallet_value *> (&entry_a)), 0));
(void)status;
assert (status == 0);
}
nano::key_type nano::wallet_store::key_type (nano::wallet_value const & value_a)
{
auto number (value_a.key.number ());
nano::key_type result;
auto text (number.convert_to<std::string> ());
if (number > std::numeric_limits<uint64_t>::max ())
{
result = nano::key_type::adhoc;
}
else
{
if ((number >> 32).convert_to<uint32_t> () == 1)
{
result = nano::key_type::deterministic;
}
else
{
result = nano::key_type::unknown;
}
}
return result;
}
bool nano::wallet_store::fetch (nano::transaction const & transaction_a, nano::account const & pub, nano::raw_key & prv)
{
auto result (false);
if (valid_password (transaction_a))
{
nano::wallet_value value (entry_get_raw (transaction_a, pub));
if (!value.key.is_zero ())
{
switch (key_type (value))
{
case nano::key_type::deterministic:
{
nano::raw_key seed_l;
seed (seed_l, transaction_a);
uint32_t index (static_cast<uint32_t> (value.key.number () & static_cast<uint32_t> (-1)));
prv.data = deterministic_key (transaction_a, index);
break;
}
case nano::key_type::adhoc:
{
// Ad-hoc keys
nano::raw_key password_l;
wallet_key (password_l, transaction_a);
prv.decrypt (value.key, password_l, pub.owords[0].number ());
break;
}
default:
{
result = true;
break;
}
}
}
else
{
result = true;
}
}
else
{
result = true;
}
if (!result)
{
nano::public_key compare (nano::pub_key (prv.as_private_key ()));
if (!(pub == compare))
{
result = true;
}
}
return result;
}
bool nano::wallet_store::valid_public_key (nano::public_key const & pub)
{
return pub.number () >= special_count;
}
bool nano::wallet_store::exists (nano::transaction const & transaction_a, nano::public_key const & pub)
{
return valid_public_key (pub) && find (transaction_a, pub) != end ();
}
void nano::wallet_store::serialize_json (nano::transaction const & transaction_a, std::string & string_a)
{
boost::property_tree::ptree tree;
for (nano::store_iterator<nano::uint256_union, nano::wallet_value> i (std::make_unique<nano::mdb_iterator<nano::uint256_union, nano::wallet_value>> (transaction_a, handle)), n (nullptr); i != n; ++i)
{
tree.put (i->first.to_string (), i->second.key.to_string ());
}
std::stringstream ostream;
boost::property_tree::write_json (ostream, tree);
string_a = ostream.str ();
}
void nano::wallet_store::write_backup (nano::transaction const & transaction_a, boost::filesystem::path const & path_a)
{
std::ofstream backup_file;
backup_file.open (path_a.string ());
if (!backup_file.fail ())
{
// Set permissions to 600
boost::system::error_code ec;
nano::set_secure_perm_file (path_a, ec);
std::string json;
serialize_json (transaction_a, json);
backup_file << json;
}
}
bool nano::wallet_store::move (nano::transaction const & transaction_a, nano::wallet_store & other_a, std::vector<nano::public_key> const & keys)
{
assert (valid_password (transaction_a));
assert (other_a.valid_password (transaction_a));
auto result (false);
for (auto i (keys.begin ()), n (keys.end ()); i != n; ++i)
{
nano::raw_key prv;
auto error (other_a.fetch (transaction_a, *i, prv));
result = result | error;
if (!result)
{
insert_adhoc (transaction_a, prv);
other_a.erase (transaction_a, *i);
}
}
return result;
}
bool nano::wallet_store::import (nano::transaction const & transaction_a, nano::wallet_store & other_a)
{
assert (valid_password (transaction_a));
assert (other_a.valid_password (transaction_a));
auto result (false);
for (auto i (other_a.begin (transaction_a)), n (end ()); i != n; ++i)
{
nano::raw_key prv;
auto error (other_a.fetch (transaction_a, i->first, prv));
result = result | error;
if (!result)
{
if (!prv.data.is_zero ())
{
insert_adhoc (transaction_a, prv);
}
else
{
insert_watch (transaction_a, i->first);
}
other_a.erase (transaction_a, i->first);
}
}
return result;
}
bool nano::wallet_store::work_get (nano::transaction const & transaction_a, nano::public_key const & pub_a, uint64_t & work_a)
{
auto result (false);
auto entry (entry_get_raw (transaction_a, pub_a));
if (!entry.key.is_zero ())
{
work_a = entry.work;
}
else
{
result = true;
}
return result;
}
void nano::wallet_store::work_put (nano::transaction const & transaction_a, nano::public_key const & pub_a, uint64_t work_a)
{
auto entry (entry_get_raw (transaction_a, pub_a));
assert (!entry.key.is_zero ());
entry.work = work_a;
entry_put_raw (transaction_a, pub_a, entry);
}
unsigned nano::wallet_store::version (nano::transaction const & transaction_a)
{
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::version_special));
auto entry (value.key);
auto result (static_cast<unsigned> (entry.bytes[31]));
return result;
}
void nano::wallet_store::version_put (nano::transaction const & transaction_a, unsigned version_a)
{
nano::uint256_union entry (version_a);
entry_put_raw (transaction_a, nano::wallet_store::version_special, nano::wallet_value (entry, 0));
}
void nano::wallet_store::upgrade_v1_v2 (nano::transaction const & transaction_a)
{
assert (version (transaction_a) == 1);
nano::raw_key zero_password;
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::wallet_key_special));
nano::raw_key kdf;
kdf.data.clear ();
zero_password.decrypt (value.key, kdf, salt (transaction_a).owords[0]);
derive_key (kdf, transaction_a, "");
nano::raw_key empty_password;
empty_password.decrypt (value.key, kdf, salt (transaction_a).owords[0]);
for (auto i (begin (transaction_a)), n (end ()); i != n; ++i)
{
nano::public_key const & key (i->first);
nano::raw_key prv;
if (fetch (transaction_a, key, prv))
{
// Key failed to decrypt despite valid password
nano::wallet_value data (entry_get_raw (transaction_a, key));
prv.decrypt (data.key, zero_password, salt (transaction_a).owords[0]);
nano::public_key compare (nano::pub_key (prv.as_private_key ()));
if (compare == key)
{
// If we successfully decrypted it, rewrite the key back with the correct wallet key
insert_adhoc (transaction_a, prv);
}
else
{
// Also try the empty password
nano::wallet_value data (entry_get_raw (transaction_a, key));
prv.decrypt (data.key, empty_password, salt (transaction_a).owords[0]);
nano::public_key compare (nano::pub_key (prv.as_private_key ()));
if (compare == key)
{
// If we successfully decrypted it, rewrite the key back with the correct wallet key
insert_adhoc (transaction_a, prv);
}
}
}
}
version_put (transaction_a, 2);
}
void nano::wallet_store::upgrade_v2_v3 (nano::transaction const & transaction_a)
{
assert (version (transaction_a) == 2);
nano::raw_key seed;
random_pool::generate_block (seed.data.bytes.data (), seed.data.bytes.size ());
seed_set (transaction_a, seed);
entry_put_raw (transaction_a, nano::wallet_store::deterministic_index_special, nano::wallet_value (nano::uint256_union (0), 0));
version_put (transaction_a, 3);
}
void nano::wallet_store::upgrade_v3_v4 (nano::transaction const & transaction_a)
{
assert (version (transaction_a) == 3);
version_put (transaction_a, 4);
assert (valid_password (transaction_a));
nano::raw_key seed;
nano::wallet_value value (entry_get_raw (transaction_a, nano::wallet_store::seed_special));
nano::raw_key password_l;
wallet_key (password_l, transaction_a);
seed.decrypt (value.key, password_l, salt (transaction_a).owords[0]);
nano::uint256_union ciphertext;
ciphertext.encrypt (seed, password_l, salt (transaction_a).owords[seed_iv_index]);
entry_put_raw (transaction_a, nano::wallet_store::seed_special, nano::wallet_value (ciphertext, 0));
for (auto i (begin (transaction_a)), n (end ()); i != n; ++i)
{
nano::wallet_value value (i->second);
if (!value.key.is_zero ())
{
switch (key_type (i->second))
{
case nano::key_type::adhoc:
{
nano::raw_key key;
if (fetch (transaction_a, nano::public_key (i->first), key))
{
// Key failed to decrypt despite valid password
key.decrypt (value.key, password_l, salt (transaction_a).owords[0]);
nano::uint256_union new_key_ciphertext;
new_key_ciphertext.encrypt (key, password_l, (nano::uint256_union (i->first)).owords[0].number ());
nano::wallet_value new_value (new_key_ciphertext, value.work);
erase (transaction_a, nano::public_key (i->first));
entry_put_raw (transaction_a, nano::public_key (i->first), new_value);
}
}
case nano::key_type::deterministic:
break;
default:
assert (false);
}
}
}
}
void nano::kdf::phs (nano::raw_key & result_a, std::string const & password_a, nano::uint256_union const & salt_a)
{
static nano::network_params network_params;
nano::lock_guard<std::mutex> lock (mutex);
auto success (argon2_hash (1, network_params.kdf_work, 1, password_a.data (), password_a.size (), salt_a.bytes.data (), salt_a.bytes.size (), result_a.data.bytes.data (), result_a.data.bytes.size (), NULL, 0, Argon2_d, 0x10));
assert (success == 0);
(void)success;
}
nano::wallet::wallet (bool & init_a, nano::transaction & transaction_a, nano::wallets & wallets_a, std::string const & wallet_a) :
lock_observer ([](bool, bool) {}),
store (init_a, wallets_a.kdf, transaction_a, wallets_a.node.config.random_representative (), wallets_a.node.config.password_fanout, wallet_a),
wallets (wallets_a)
{
}
nano::wallet::wallet (bool & init_a, nano::transaction & transaction_a, nano::wallets & wallets_a, std::string const & wallet_a, std::string const & json) :
lock_observer ([](bool, bool) {}),
store (init_a, wallets_a.kdf, transaction_a, wallets_a.node.config.random_representative (), wallets_a.node.config.password_fanout, wallet_a, json),
wallets (wallets_a)
{
}
void nano::wallet::enter_initial_password ()
{
nano::raw_key password_l;
{
nano::lock_guard<std::recursive_mutex> lock (store.mutex);
store.password.value (password_l);
}
if (password_l.data.is_zero ())
{
auto transaction (wallets.tx_begin_write ());
if (store.valid_password (transaction))
{
// Newly created wallets have a zero key
store.rekey (transaction, "");
}
else
{
enter_password (transaction, "");
}
}
}
bool nano::wallet::enter_password (nano::transaction const & transaction_a, std::string const & password_a)
{
auto result (store.attempt_password (transaction_a, password_a));
if (!result)
{
auto this_l (shared_from_this ());
wallets.node.background ([this_l]() {
this_l->search_pending ();
});
wallets.node.logger.try_log ("Wallet unlocked");
}
else
{
wallets.node.logger.try_log ("Invalid password, wallet locked");
}
lock_observer (result, password_a.empty ());
return result;
}
nano::public_key nano::wallet::deterministic_insert (nano::transaction const & transaction_a, bool generate_work_a)
{
nano::public_key key (0);
if (store.valid_password (transaction_a))
{
key = store.deterministic_insert (transaction_a);
if (generate_work_a)
{
work_ensure (key, key);
}
auto half_principal_weight (wallets.node.minimum_principal_weight () / 2);
if (wallets.check_rep (key, half_principal_weight))
{
nano::lock_guard<std::mutex> lock (representatives_mutex);
representatives.insert (key);
}
}
return key;
}
nano::public_key nano::wallet::deterministic_insert (uint32_t const index, bool generate_work_a)
{
auto transaction (wallets.tx_begin_write ());
nano::public_key key (0);
if (store.valid_password (transaction))
{
key = store.deterministic_insert (transaction, index);
if (generate_work_a)
{
work_ensure (key, key);
}
}
return key;
}
nano::public_key nano::wallet::deterministic_insert (bool generate_work_a)
{
auto transaction (wallets.tx_begin_write ());
auto result (deterministic_insert (transaction, generate_work_a));
return result;
}
nano::public_key nano::wallet::insert_adhoc (nano::transaction const & transaction_a, nano::raw_key const & key_a, bool generate_work_a)
{
nano::public_key key (0);
if (store.valid_password (transaction_a))
{
key = store.insert_adhoc (transaction_a, key_a);
auto block_transaction (wallets.node.store.tx_begin_read ());
if (generate_work_a)
{
work_ensure (key, wallets.node.ledger.latest_root (block_transaction, key));
}
auto half_principal_weight (wallets.node.minimum_principal_weight () / 2);
if (wallets.check_rep (key, half_principal_weight))
{
nano::lock_guard<std::mutex> lock (representatives_mutex);
representatives.insert (key);
}
}
return key;
}
nano::public_key nano::wallet::insert_adhoc (nano::raw_key const & account_a, bool generate_work_a)
{
auto transaction (wallets.tx_begin_write ());
auto result (insert_adhoc (transaction, account_a, generate_work_a));
return result;
}
bool nano::wallet::insert_watch (nano::transaction const & transaction_a, nano::public_key const & pub_a)
{
return store.insert_watch (transaction_a, pub_a);
}
bool nano::wallet::exists (nano::public_key const & account_a)
{
auto transaction (wallets.tx_begin_read ());
return store.exists (transaction, account_a);
}
bool nano::wallet::import (std::string const & json_a, std::string const & password_a)
{
auto error (false);
std::unique_ptr<nano::wallet_store> temp;
{
auto transaction (wallets.tx_begin_write ());
nano::uint256_union id;
random_pool::generate_block (id.bytes.data (), id.bytes.size ());
temp = std::make_unique<nano::wallet_store> (error, wallets.node.wallets.kdf, transaction, 0, 1, id.to_string (), json_a);
}
if (!error)
{
auto transaction (wallets.tx_begin_write ());
error = temp->attempt_password (transaction, password_a);
}
auto transaction (wallets.tx_begin_write ());
if (!error)
{
error = store.import (transaction, *temp);
}
temp->destroy (transaction);
return error;
}
void nano::wallet::serialize (std::string & json_a)
{
auto transaction (wallets.tx_begin_read ());
store.serialize_json (transaction, json_a);
}
void nano::wallet_store::destroy (nano::transaction const & transaction_a)
{
auto status (mdb_drop (tx (transaction_a), handle, 1));
(void)status;
assert (status == 0);
handle = 0;
}
std::shared_ptr<nano::block> nano::wallet::receive_action (nano::block const & send_a, nano::account const & representative_a, nano::uint128_union const & amount_a, uint64_t work_a, bool generate_work_a)
{
nano::account account;
auto hash (send_a.hash ());
std::shared_ptr<nano::block> block;
if (wallets.node.config.receive_minimum.number () <= amount_a.number ())
{
auto block_transaction (wallets.node.ledger.store.tx_begin_read ());
auto transaction (wallets.tx_begin_read ());
nano::pending_info pending_info;
if (wallets.node.store.block_exists (block_transaction, hash))
{
account = wallets.node.ledger.block_destination (block_transaction, send_a);
if (!wallets.node.ledger.store.pending_get (block_transaction, nano::pending_key (account, hash), pending_info))
{
nano::raw_key prv;
if (!store.fetch (transaction, account, prv))
{
if (work_a == 0)
{
store.work_get (transaction, account, work_a);
}
nano::account_info info;
auto new_account (wallets.node.ledger.store.account_get (block_transaction, account, info));
if (!new_account)
{
block = std::make_shared<nano::state_block> (account, info.head, info.representative, info.balance.number () + pending_info.amount.number (), hash, prv, account, work_a);
}
else
{
block = std::make_shared<nano::state_block> (account, 0, representative_a, pending_info.amount, reinterpret_cast<nano::link const &> (hash), prv, account, work_a);
}
}
else
{
wallets.node.logger.try_log ("Unable to receive, wallet locked");
}
}
else
{
// Ledger doesn't have this marked as available to receive anymore
}
}
else
{
// Ledger doesn't have this block anymore.
}
}
else
{
wallets.node.logger.try_log (boost::str (boost::format ("Not receiving block %1% due to minimum receive threshold") % hash.to_string ()));
// Someone sent us something below the threshold of receiving
}
if (block != nullptr)
{
if (action_complete (block, account, generate_work_a))
{
// Return null block after work generation or ledger process error
block = nullptr;
}
}
return block;
}
std::shared_ptr<nano::block> nano::wallet::change_action (nano::account const & source_a, nano::account const & representative_a, uint64_t work_a, bool generate_work_a)
{
std::shared_ptr<nano::block> block;
{
auto transaction (wallets.tx_begin_read ());
auto block_transaction (wallets.node.store.tx_begin_read ());