-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcfdcore_hdwallet.cpp
1424 lines (1264 loc) · 47.8 KB
/
cfdcore_hdwallet.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
// Copyright 2020 CryptoGarage
/**
* @file cfdcore_hdwallet.cpp
*
* @brief implementation of BIP32/BIP39/BIP44 classes
*/
#include "cfdcore/cfdcore_hdwallet.h"
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <string>
#include <vector>
#include "cfdcore/cfdcore_bytedata.h"
#include "cfdcore/cfdcore_exception.h"
#include "cfdcore/cfdcore_logger.h"
#include "cfdcore/cfdcore_schnorrsig.h"
#include "cfdcore/cfdcore_util.h"
#include "cfdcore_wally_util.h" // NOLINT
namespace cfd {
namespace core {
using logger::warn;
// ----------------------------------------------------------------------------
// Definitions in the file
// ----------------------------------------------------------------------------
/// empty seed string (64byte)
static constexpr const char* kEmptySeedStr =
"00000000000000000000000000000000000000000000000000000000000000000000000"
"000000000000000000000000000000000000000000000000000000000"; // NOLINT
/**
* @brief Bip32 Analyze key information.
* @param[in] extkey extkey
* @param[in] base58 base58 data
* @param[in,out] serialize_data serialize data
* @param[out] version version
* @param[out] depth depth
* @param[out] child child
* @param[out] chaincode chaincode
* @param[out] privkey privkey
* @param[out] pubkey pubkey
* @param[out] fingerprint finger print
*/
static void AnalyzeBip32KeyData(
const void* extkey, const std::string* base58,
std::vector<uint8_t>* serialize_data, uint32_t* version, uint8_t* depth,
uint32_t* child, ByteData256* chaincode, Privkey* privkey, Pubkey* pubkey,
uint32_t* fingerprint) {
struct ext_key output = {};
std::string clsname = (privkey != nullptr) ? "ExtPrivkey" : "ExtPubkey";
const std::vector<uint8_t>* serialize_bytes = nullptr;
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN + BASE58_CHECKSUM_LEN);
int ret;
if (extkey != nullptr) {
memcpy(&output, extkey, sizeof(output));
} else if (base58 != nullptr) {
size_t written = 0;
ret = wally_base58_to_bytes(
base58->c_str(), BASE58_FLAG_CHECKSUM, data.data(), data.size(),
&written);
if (ret != WALLY_OK) {
warn(
CFD_LOG_SOURCE, "{} wally_base58_to_bytes error. ret={}", clsname,
ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
clsname + " base58 decode error.");
}
data.resize(written);
serialize_bytes = &data;
if (serialize_data != nullptr) {
*serialize_data = data;
}
} else {
serialize_bytes = serialize_data;
}
if (serialize_bytes != nullptr) {
// unserialize
ret = bip32_key_unserialize(
serialize_bytes->data(), serialize_bytes->size(), &output);
if (ret != WALLY_OK) {
warn(
CFD_LOG_SOURCE, "{} bip32_key_unserialize error. ret={}", clsname,
ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, clsname + " unserialize error.");
}
}
*version = output.version;
*depth = output.depth;
*child = output.child_num;
if (fingerprint) {
memcpy(fingerprint, output.parent160, sizeof(*fingerprint));
}
std::vector<uint8_t> chaincode_bytes(kByteData256Length);
memcpy(chaincode_bytes.data(), output.chain_code, chaincode_bytes.size());
*chaincode = ByteData256(chaincode_bytes);
if (privkey != nullptr) {
if (output.priv_key[0] != BIP32_FLAG_KEY_PRIVATE) {
warn(CFD_LOG_SOURCE, "{} privkey disabled.", clsname);
throw CfdException(
CfdError::kCfdIllegalStateError, clsname + " keytype error.");
}
// privkey
std::vector<uint8_t> privkey_bytes(kByteData256Length);
memcpy(privkey_bytes.data(), &output.priv_key[1], privkey_bytes.size());
*privkey = Privkey(ByteData256(privkey_bytes));
} else if (pubkey != nullptr) {
if (output.priv_key[0] == BIP32_FLAG_KEY_PRIVATE) {
warn(CFD_LOG_SOURCE, "{} privkey enabled.", clsname);
throw CfdException(
CfdError::kCfdIllegalStateError, clsname + " keytype error.");
}
std::vector<uint8_t> pubkey_bytes(Pubkey::kCompressedPubkeySize);
memcpy(pubkey_bytes.data(), output.pub_key, pubkey_bytes.size());
*pubkey = Pubkey(pubkey_bytes);
}
}
/**
* @brief Perform Base58 conversion.
* @param[in] serialize_data serialize data
* @param[in] caller_name caller class name
* @return base58 string
*/
static std::string ToBase58String(
const ByteData& serialize_data, const std::string& caller_name) {
char* output = nullptr;
if (serialize_data.GetDataSize() != BIP32_SERIALIZED_LEN) {
warn(
CFD_LOG_SOURCE, "{} serialize_data size illegal. size={}", caller_name,
serialize_data.GetDataSize());
throw CfdException(
CfdError::kCfdIllegalStateError,
caller_name + " serialize_data size error.");
}
int ret = wally_base58_from_bytes(
serialize_data.GetBytes().data(), BIP32_SERIALIZED_LEN,
BASE58_FLAG_CHECKSUM, &output);
if (ret != WALLY_OK) {
warn(
CFD_LOG_SOURCE, "{}, wally_base58_from_bytes error. ret={}",
caller_name, ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
caller_name + " base58 encode error.");
}
return WallyUtil::ConvertStringAndFree(output);
}
/**
* @brief Get an array from a string path.
* @param[in] string_path child number string path
* @param[in] caller_name caller class name
* @param[in] depth current key depth
* @return uint32_t array
*/
static std::vector<uint32_t> ToArrayFromString(
const std::string& string_path, const std::string& caller_name,
uint8_t depth) {
std::vector<uint32_t> result;
std::vector<std::string> list = StringUtil::Split(string_path, "/");
for (size_t index = 0; index < list.size(); ++index) {
std::string str = list[index];
bool hardened = false;
if (str.size() <= 1) {
// do nothing
} else if (
(str.back() == '\'') || (str.back() == 'h') || (str.back() == 'H')) {
str = str.substr(0, str.size() - 1);
hardened = true;
}
if ((str == "m") || (str == "M")) {
if (depth != 0) {
warn(
CFD_LOG_SOURCE, "{} bip32 path fail. this key is not master key.",
caller_name);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
caller_name + " bip32 path fail. this key is not master key.");
}
continue; // master key
}
if (str.empty()) {
if (index == 0) {
// start slash pattern
continue;
} else {
warn(
CFD_LOG_SOURCE, "{} bip32 string path fail. empty item.",
caller_name);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
caller_name + " bip32 string path fail. empty item.");
}
}
// Conversion by strtol function
char* p_str_end = nullptr;
uint32_t value;
if ((str.size() > 2) && (str[0] == '0') && (str[1] == 'x')) {
value = std::strtoul(str.c_str(), &p_str_end, 16);
} else {
value = std::strtoul(str.c_str(), &p_str_end, 10);
}
if (str.empty() || ((p_str_end != nullptr) && (*p_str_end != '\0'))) {
warn(CFD_LOG_SOURCE, "{} bip32 string path fail.", caller_name);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
caller_name + " bip32 string path fail.");
}
if (hardened) value |= 0x80000000;
result.push_back(static_cast<uint32_t>(value));
}
if (result.empty()) {
warn(CFD_LOG_SOURCE, "{} bip32 string path empty.", caller_name);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
caller_name + " bip32 string path empty.");
}
return result;
}
// ----------------------------------------------------------------------------
// HDWallet
// ----------------------------------------------------------------------------
HDWallet::HDWallet() : seed_(ByteData(kEmptySeedStr)) {
// do nothing
}
HDWallet::HDWallet(const ByteData& seed) : seed_(seed) {
if ((seed.GetDataSize() != HDWallet::kSeed128Size) &&
(seed.GetDataSize() != HDWallet::kSeed256Size) &&
(seed.GetDataSize() != HDWallet::kSeed512Size)) {
throw CfdException(
CfdError::kCfdIllegalArgumentError, "Seed length error.");
}
}
HDWallet::HDWallet(
std::vector<std::string> mnemonic, std::string passphrase,
bool use_ideographic_space)
: seed_(ByteData(kEmptySeedStr)) {
seed_ = ConvertMnemonicToSeed(mnemonic, passphrase, use_ideographic_space);
if ((seed_.GetDataSize() != HDWallet::kSeed128Size) &&
(seed_.GetDataSize() != HDWallet::kSeed256Size) &&
(seed_.GetDataSize() != HDWallet::kSeed512Size)) {
throw CfdException(
CfdError::kCfdIllegalArgumentError, "Seed length error.");
}
}
ByteData HDWallet::GetSeed() const { return seed_; }
ExtPrivkey HDWallet::GeneratePrivkey(NetType network_type) const {
return ExtPrivkey(seed_, network_type);
}
ExtPrivkey HDWallet::GeneratePrivkey(
NetType network_type, uint32_t child_num) const {
std::vector<uint32_t> path = {child_num};
return GeneratePrivkey(network_type, path);
}
ExtPrivkey HDWallet::GeneratePrivkey(
NetType network_type, const std::vector<uint32_t>& path) const {
ExtPrivkey privkey(seed_, network_type);
return privkey.DerivePrivkey(path);
}
ExtPrivkey HDWallet::GeneratePrivkey(
NetType network_type, const std::string& string_path) const {
ExtPrivkey privkey(seed_, network_type);
return privkey.DerivePrivkey(string_path);
}
KeyData HDWallet::GeneratePrivkeyData(
NetType network_type, const std::vector<uint32_t>& path) const {
ExtPrivkey privkey(seed_, network_type);
ExtPrivkey key = privkey.DerivePrivkey(path);
auto fingerprint = privkey.GetPrivkey().GeneratePubkey().GetFingerprint();
return KeyData(key, path, fingerprint);
}
KeyData HDWallet::GeneratePrivkeyData(
NetType network_type, const std::string& string_path) const {
ExtPrivkey privkey(seed_, network_type);
ExtPrivkey key = privkey.DerivePrivkey(string_path);
auto fingerprint = privkey.GetPrivkey().GeneratePubkey().GetFingerprint();
return KeyData(key, string_path, fingerprint);
}
ExtPubkey HDWallet::GeneratePubkey(NetType network_type) const {
ExtPrivkey privkey(seed_, network_type);
return privkey.GetExtPubkey();
}
ExtPubkey HDWallet::GeneratePubkey(
NetType network_type, uint32_t child_num) const {
std::vector<uint32_t> path = {child_num};
return GeneratePubkey(network_type, path);
}
ExtPubkey HDWallet::GeneratePubkey(
NetType network_type, const std::vector<uint32_t>& path) const {
ExtPrivkey privkey(seed_, network_type);
return privkey.DerivePubkey(path);
}
ExtPubkey HDWallet::GeneratePubkey(
NetType network_type, const std::string& string_path) const {
ExtPrivkey privkey(seed_, network_type);
return privkey.DerivePubkey(string_path);
}
KeyData HDWallet::GeneratePubkeyData(
NetType network_type, const std::vector<uint32_t>& path) const {
ExtPrivkey privkey(seed_, network_type);
ExtPrivkey key = privkey.DerivePrivkey(path);
auto fingerprint = privkey.GetPrivkey().GeneratePubkey().GetFingerprint();
return KeyData(key.GetExtPubkey(), path, fingerprint);
}
KeyData HDWallet::GeneratePubkeyData(
NetType network_type, const std::string& string_path) const {
ExtPrivkey privkey(seed_, network_type);
ExtPrivkey key = privkey.DerivePrivkey(string_path);
auto fingerprint = privkey.GetPrivkey().GeneratePubkey().GetFingerprint();
return KeyData(key.GetExtPubkey(), string_path, fingerprint);
}
std::vector<std::string> HDWallet::GetMnemonicWordlist(
const std::string& language) {
if (!CheckSupportedLanguages(language)) {
warn(
CFD_LOG_SOURCE, "Not support language passed. language=[{}]",
language);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "Not support language passed.");
}
return WallyUtil::GetMnemonicWordlist(language);
}
std::vector<std::string> HDWallet::ConvertEntropyToMnemonic(
const ByteData& entropy, const std::string& language) {
if (!CheckSupportedLanguages(language)) {
warn(
CFD_LOG_SOURCE, "Not support language passed. language=[{}]",
language);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "Not support language passed.");
}
return WallyUtil::ConvertEntropyToMnemonic(entropy, language);
}
ByteData HDWallet::ConvertMnemonicToEntropy(
const std::vector<std::string>& mnemonic, const std::string& language) {
if (!CheckSupportedLanguages(language)) {
warn(
CFD_LOG_SOURCE, "Not support language passed. language=[{}]",
language);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "Not support language passed.");
}
return WallyUtil::ConvertMnemonicToEntropy(mnemonic, language);
}
bool HDWallet::CheckValidMnemonic(
const std::vector<std::string>& mnemonic, const std::string& language) {
if (!CheckSupportedLanguages(language)) {
warn(
CFD_LOG_SOURCE, "Not support language passed. language=[{}]",
language);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "Not support language passed.");
}
return WallyUtil::CheckValidMnemonic(mnemonic, language);
}
bool HDWallet::CheckSupportedLanguages(const std::string& language) {
std::vector<std::string> slangs = WallyUtil::GetSupportedMnemonicLanguages();
return (
std::find(slangs.cbegin(), slangs.cend(), language) != slangs.cend());
}
ByteData HDWallet::ConvertMnemonicToSeed(
const std::vector<std::string>& mnemonic, const std::string& passphrase,
bool use_ideographic_space) {
return WallyUtil::ConvertMnemonicToSeed(
mnemonic, passphrase, use_ideographic_space);
}
// ----------------------------------------------------------------------------
// ExtPrivkey
// ----------------------------------------------------------------------------
ExtPrivkey::ExtPrivkey() {
// do nothing
}
ExtPrivkey::ExtPrivkey(const ByteData& seed, NetType network_type) {
std::vector<uint8_t> seed_byte = seed.GetBytes();
if ((seed_byte.size() != HDWallet::kSeed128Size) &&
(seed_byte.size() != HDWallet::kSeed256Size) &&
(seed_byte.size() != HDWallet::kSeed512Size)) {
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey Seed length error.");
}
uint32_t version = kVersionTestnetPrivkey;
if ((network_type == NetType::kMainnet) ||
(network_type == NetType::kLiquidV1)) {
version = kVersionMainnetPrivkey;
}
struct ext_key extkey;
int ret = bip32_key_from_seed(
seed_byte.data(), seed_byte.size(), version, 0, &extkey);
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_from_seed error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey gen from seed error.");
}
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN);
ret = bip32_key_serialize(
&extkey, BIP32_FLAG_KEY_PRIVATE, data.data(), data.size());
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_serialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey serialize error.");
}
serialize_data_ = ByteData(data);
AnalyzeBip32KeyData(
&extkey, nullptr, nullptr, &version_, &depth_, &child_num_, &chaincode_,
&privkey_, nullptr, &fingerprint_);
}
ExtPrivkey::ExtPrivkey(const ByteData& serialize_data)
: ExtPrivkey(serialize_data, ByteData256()) {
// do nothing
}
ExtPrivkey::ExtPrivkey(
const ByteData& serialize_data, const ByteData256& tweak_sum) {
// unserialize
tweak_sum_ = tweak_sum;
serialize_data_ = serialize_data;
std::vector<uint8_t> data = serialize_data.GetBytes();
AnalyzeBip32KeyData(
nullptr, nullptr, &data, &version_, &depth_, &child_num_, &chaincode_,
&privkey_, nullptr, &fingerprint_);
}
ExtPrivkey::ExtPrivkey(const std::string& base58_data)
: ExtPrivkey(base58_data, ByteData256()) {
// do nothing
}
ExtPrivkey::ExtPrivkey(
const std::string& base58_data, const ByteData256& tweak_sum) {
std::vector<uint8_t> data;
AnalyzeBip32KeyData(
nullptr, &base58_data, &data, &version_, &depth_, &child_num_,
&chaincode_, &privkey_, nullptr, &fingerprint_);
serialize_data_ = ByteData(data);
tweak_sum_ = tweak_sum;
}
ExtPrivkey::ExtPrivkey(
NetType network_type, const Privkey& parent_key,
const ByteData256& parent_chain_code, uint8_t parent_depth,
uint32_t child_num) {
if (!parent_key.IsValid()) {
warn(CFD_LOG_SOURCE, "invalid privkey.");
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"Failed to privkey. ExtPrivkey invalid privkey.");
}
// create simple parent data
struct ext_key parent = {};
memset(&parent, 0, sizeof(parent));
parent.version = kVersionTestnetPrivkey;
if ((network_type == NetType::kMainnet) ||
(network_type == NetType::kLiquidV1)) {
parent.version = kVersionMainnetPrivkey;
}
parent.depth = parent_depth;
Pubkey pubkey = parent_key.GeneratePubkey(true);
std::vector<uint8_t> privkey_bytes = parent_key.GetData().GetBytes();
std::vector<uint8_t> pubkey_bytes = pubkey.GetData().GetBytes();
std::vector<uint8_t> pubkey_hash = HashUtil::Hash160(pubkey).GetBytes();
std::vector<uint8_t> chain_bytes = parent_chain_code.GetData().GetBytes();
parent.priv_key[0] = BIP32_FLAG_KEY_PRIVATE;
memcpy(&parent.priv_key[1], privkey_bytes.data(), privkey_bytes.size());
memcpy(parent.pub_key, pubkey_bytes.data(), pubkey_bytes.size());
memcpy(parent.hash160, pubkey_hash.data(), pubkey_hash.size());
memcpy(parent.chain_code, chain_bytes.data(), chain_bytes.size());
struct ext_key extkey = {};
memset(&extkey, 0, sizeof(extkey));
int ret = bip32_key_from_parent(
&parent, child_num, BIP32_FLAG_KEY_PRIVATE, &extkey);
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_from_parent error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPubkey generatekey error.");
}
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN);
ret = bip32_key_serialize(
&extkey, BIP32_FLAG_KEY_PRIVATE, data.data(), data.size());
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_serialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey serialize error.");
}
serialize_data_ = ByteData(data);
tweak_sum_ = ByteData256();
AnalyzeBip32KeyData(
&extkey, nullptr, nullptr, &version_, &depth_, &child_num_, &chaincode_,
&privkey_, nullptr, &fingerprint_);
}
ExtPrivkey::ExtPrivkey(
NetType network_type, const Privkey& parent_key, const Privkey& privkey,
const ByteData256& chain_code, uint8_t depth, uint32_t child_num)
: ExtPrivkey(
network_type,
HashUtil::Hash160(parent_key.GeneratePubkey()).GetData(), privkey,
chain_code, depth, child_num) {
if (!parent_key.IsValid()) {
warn(CFD_LOG_SOURCE, "invalid privkey.");
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"Failed to privkey. ExtPrivkey invalid privkey.");
}
}
ExtPrivkey::ExtPrivkey(
NetType network_type, const ByteData& parent_fingerprint,
const Privkey& privkey, const ByteData256& chain_code, uint8_t depth,
uint32_t child_num) {
if (!privkey.IsValid()) {
warn(CFD_LOG_SOURCE, "invalid privkey.");
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"Failed to privkey. ExtPrivkey invalid privkey.");
}
// create simple parent data
struct ext_key extkey = {};
memset(&extkey, 0, sizeof(extkey));
extkey.version = kVersionTestnetPrivkey;
if ((network_type == NetType::kMainnet) ||
(network_type == NetType::kLiquidV1)) {
extkey.version = kVersionMainnetPrivkey;
}
extkey.depth = depth;
extkey.child_num = child_num;
Pubkey pubkey = privkey.GeneratePubkey(true);
std::vector<uint8_t> privkey_bytes = privkey.GetData().GetBytes();
std::vector<uint8_t> pubkey_bytes = pubkey.GetData().GetBytes();
std::vector<uint8_t> pubkey_hash = HashUtil::Hash160(pubkey).GetBytes();
std::vector<uint8_t> fingerprint_bytes = parent_fingerprint.GetBytes();
std::vector<uint8_t> chain_bytes = chain_code.GetData().GetBytes();
extkey.priv_key[0] = BIP32_FLAG_KEY_PRIVATE;
memcpy(&extkey.priv_key[1], privkey_bytes.data(), privkey_bytes.size());
memcpy(extkey.pub_key, pubkey_bytes.data(), pubkey_bytes.size());
// parent160: use top 4-byte only
fingerprint_bytes.resize(4);
memcpy(extkey.parent160, fingerprint_bytes.data(), fingerprint_bytes.size());
memcpy(extkey.hash160, pubkey_hash.data(), pubkey_hash.size());
memcpy(extkey.chain_code, chain_bytes.data(), chain_bytes.size());
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN);
int ret = bip32_key_serialize(
&extkey, BIP32_FLAG_KEY_PRIVATE, data.data(), data.size());
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_serialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey serialize error.");
}
serialize_data_ = ByteData(data);
tweak_sum_ = ByteData256();
AnalyzeBip32KeyData(
&extkey, nullptr, nullptr, &version_, &depth_, &child_num_, &chaincode_,
&privkey_, nullptr, &fingerprint_);
}
ByteData ExtPrivkey::GetData() const { return serialize_data_; }
std::string ExtPrivkey::ToString() const {
return ToBase58String(serialize_data_, "ExtPrivkey");
}
Privkey ExtPrivkey::GetPrivkey() const { return privkey_; }
ExtPrivkey ExtPrivkey::DerivePrivkey(uint32_t child_num) const {
std::vector<uint32_t> path = {child_num};
return DerivePrivkey(path);
}
ExtPrivkey ExtPrivkey::DerivePrivkey(const std::vector<uint32_t>& path) const {
struct ext_key extkey;
struct ext_key child_key;
const std::vector<uint8_t>& serialize_data = serialize_data_.GetBytes();
int ret = bip32_key_unserialize(
serialize_data.data(), serialize_data.size(), &extkey);
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_unserialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey unserialize error.");
}
#ifndef CFD_DISABLE_ELEMENTS
// write pub_key_tweak_sum to ext_key
memcpy(
extkey.pub_key_tweak_sum, tweak_sum_.GetBytes().data(),
sizeof(extkey.pub_key_tweak_sum));
#endif // CFD_DISABLE_ELEMENTS
uint32_t flag = BIP32_FLAG_KEY_PRIVATE;
ret = bip32_key_from_parent_path(
&extkey, path.data(), path.size(), flag | BIP32_FLAG_KEY_TWEAK_SUM,
&child_key);
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_from_parent_path error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey derive error.");
}
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN);
ret = bip32_key_serialize(&child_key, flag, data.data(), data.size());
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_serialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey serialize error.");
}
ByteData256 tweak_sum_data;
#ifndef CFD_DISABLE_ELEMENTS
// collect pub_key_tweak_sum from ext_key
std::vector<uint8_t> tweak_sum(sizeof(extkey.pub_key_tweak_sum));
memcpy(tweak_sum.data(), extkey.pub_key_tweak_sum, tweak_sum.size());
tweak_sum_data = ByteData256(tweak_sum);
#endif // CFD_DISABLE_ELEMENTS
return ExtPrivkey(ByteData(data), tweak_sum_data);
}
ExtPrivkey ExtPrivkey::DerivePrivkey(const std::string& string_path) const {
std::vector<uint32_t> path =
ToArrayFromString(string_path, "ExtPrivkey", depth_);
return DerivePrivkey(path);
}
KeyData ExtPrivkey::DerivePrivkeyData(
const std::vector<uint32_t>& path) const {
ExtPrivkey key = DerivePrivkey(path);
auto fingerprint = privkey_.GeneratePubkey().GetFingerprint();
return KeyData(key, path, fingerprint);
}
KeyData ExtPrivkey::DerivePrivkeyData(const std::string& string_path) const {
ExtPrivkey key = DerivePrivkey(string_path);
auto fingerprint = privkey_.GeneratePubkey().GetFingerprint();
return KeyData(key, string_path, fingerprint);
}
ExtPubkey ExtPrivkey::GetExtPubkey() const {
struct ext_key extkey;
const std::vector<uint8_t>& serialize_data = serialize_data_.GetBytes();
int ret = bip32_key_unserialize(
serialize_data.data(), serialize_data.size(), &extkey);
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_unserialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey unserialize error.");
}
// copy data
extkey.priv_key[0] = BIP32_FLAG_KEY_PUBLIC;
// その他設定の上書きはlibwallyに任せる
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN);
ret = bip32_key_serialize(
&extkey, BIP32_FLAG_KEY_PUBLIC, data.data(), data.size());
if (ret != WALLY_OK) {
warn(
CFD_LOG_SOURCE, "GetExtPubkey bip32_key_serialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"ExtPrivkey Pubkey serialize error.");
}
return ExtPubkey(ByteData(data), tweak_sum_);
}
ExtPubkey ExtPrivkey::DerivePubkey(uint32_t child_num) const {
std::vector<uint32_t> path = {child_num};
return DerivePubkey(path);
}
ExtPubkey ExtPrivkey::DerivePubkey(const std::vector<uint32_t>& path) const {
ExtPrivkey privkey = DerivePrivkey(path);
return privkey.GetExtPubkey();
}
ExtPubkey ExtPrivkey::DerivePubkey(const std::string& string_path) const {
ExtPrivkey privkey = DerivePrivkey(string_path);
return privkey.GetExtPubkey();
}
KeyData ExtPrivkey::DerivePubkeyData(const std::vector<uint32_t>& path) const {
ExtPubkey key = DerivePubkey(path);
auto fingerprint = privkey_.GeneratePubkey().GetFingerprint();
return KeyData(key, path, fingerprint);
}
KeyData ExtPrivkey::DerivePubkeyData(const std::string& string_path) const {
ExtPubkey key = DerivePubkey(string_path);
auto fingerprint = privkey_.GeneratePubkey().GetFingerprint();
return KeyData(key, string_path, fingerprint);
}
bool ExtPrivkey::IsValid() const { return privkey_.IsValid(); }
ByteData256 ExtPrivkey::GetChainCode() const { return chaincode_; }
uint32_t ExtPrivkey::GetVersion() const { return version_; }
uint8_t ExtPrivkey::GetDepth() const { return depth_; }
uint32_t ExtPrivkey::GetChildNum() const { return child_num_; }
ByteData ExtPrivkey::GetVersionData() const {
std::vector<uint8_t> byte_data(4);
byte_data[0] = (version_ >> 24) & 0xff;
byte_data[1] = (version_ >> 16) & 0xff;
byte_data[2] = (version_ >> 8) & 0xff;
byte_data[3] = version_ & 0xff;
return ByteData(byte_data);
}
uint32_t ExtPrivkey::GetFingerprint() const { return fingerprint_; }
ByteData ExtPrivkey::GetFingerprintData() const {
std::vector<uint8_t> byte_data(4);
byte_data[3] = (fingerprint_ >> 24) & 0xff;
byte_data[2] = (fingerprint_ >> 16) & 0xff;
byte_data[1] = (fingerprint_ >> 8) & 0xff;
byte_data[0] = fingerprint_ & 0xff;
return ByteData(byte_data);
}
ByteData256 ExtPrivkey::GetPubTweakSum() const { return tweak_sum_; }
NetType ExtPrivkey::GetNetworkType() const {
if (version_ == ExtPrivkey::kVersionMainnetPrivkey) {
return NetType::kMainnet;
}
return NetType::kTestnet;
}
// ----------------------------------------------------------------------------
// ExtPubkey
// ----------------------------------------------------------------------------
ExtPubkey::ExtPubkey() {
// do nothing
}
ExtPubkey::ExtPubkey(const ByteData& serialize_data)
: ExtPubkey(serialize_data, ByteData256()) {
// do nothing
}
ExtPubkey::ExtPubkey(
const ByteData& serialize_data, const ByteData256& tweak_sum) {
// unserialize
tweak_sum_ = tweak_sum;
serialize_data_ = serialize_data;
std::vector<uint8_t> data = serialize_data.GetBytes();
AnalyzeBip32KeyData(
nullptr, nullptr, &data, &version_, &depth_, &child_num_, &chaincode_,
nullptr, &pubkey_, &fingerprint_);
}
ExtPubkey::ExtPubkey(const std::string& base58_data)
: ExtPubkey(base58_data, ByteData256()) {
// do nothing
}
ExtPubkey::ExtPubkey(
const std::string& base58_data, const ByteData256& tweak_sum) {
std::vector<uint8_t> data;
AnalyzeBip32KeyData(
nullptr, &base58_data, &data, &version_, &depth_, &child_num_,
&chaincode_, nullptr, &pubkey_, &fingerprint_);
serialize_data_ = ByteData(data);
tweak_sum_ = tweak_sum;
}
ExtPubkey::ExtPubkey(
NetType network_type, const Pubkey& parent_key,
const ByteData256& parent_chain_code, uint8_t parent_depth,
uint32_t child_num) {
if (!parent_key.IsValid()) {
warn(CFD_LOG_SOURCE, "invalid pubkey.");
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"Failed to pubkey. ExtPubkey invalid pubkey.");
}
Pubkey key = parent_key;
if (!key.IsCompress()) {
key = key.Compress();
}
// create simple parent data
struct ext_key parent = {};
memset(&parent, 0, sizeof(parent));
parent.version = kVersionTestnetPubkey;
if ((network_type == NetType::kMainnet) ||
(network_type == NetType::kLiquidV1)) {
parent.version = kVersionMainnetPubkey;
}
parent.depth = parent_depth;
parent.priv_key[0] = BIP32_FLAG_KEY_PUBLIC;
std::vector<uint8_t> pubkey_bytes = key.GetData().GetBytes();
std::vector<uint8_t> pubkey_hash = HashUtil::Hash160(key).GetBytes();
std::vector<uint8_t> chain_bytes = parent_chain_code.GetData().GetBytes();
memcpy(parent.pub_key, pubkey_bytes.data(), pubkey_bytes.size());
memcpy(parent.hash160, pubkey_hash.data(), pubkey_hash.size());
memcpy(parent.chain_code, chain_bytes.data(), chain_bytes.size());
struct ext_key extkey = {};
memset(&extkey, 0, sizeof(extkey));
int ret = bip32_key_from_parent(
&parent, child_num, BIP32_FLAG_KEY_PUBLIC, &extkey);
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_from_parent error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPubkey generatekey error.");
}
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN);
ret = bip32_key_serialize(
&extkey, BIP32_FLAG_KEY_PUBLIC, data.data(), data.size());
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_serialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey serialize error.");
}
serialize_data_ = ByteData(data);
tweak_sum_ = ByteData256();
AnalyzeBip32KeyData(
&extkey, nullptr, nullptr, &version_, &depth_, &child_num_, &chaincode_,
nullptr, &pubkey_, &fingerprint_);
}
ExtPubkey::ExtPubkey(
NetType network_type, const Pubkey& parent_key, const Pubkey& pubkey,
const ByteData256& chain_code, uint8_t depth, uint32_t child_num)
: ExtPubkey(
network_type, HashUtil::Hash160(parent_key).GetData(), pubkey,
chain_code, depth, child_num) {
if (!parent_key.IsValid()) {
warn(CFD_LOG_SOURCE, "invalid pubkey.");
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"Failed to pubkey. ExtPubkey invalid pubkey.");
}
}
ExtPubkey::ExtPubkey(
NetType network_type, const ByteData& parent_fingerprint,
const Pubkey& pubkey, const ByteData256& chain_code, uint8_t depth,
uint32_t child_num) {
if (!pubkey.IsValid()) {
warn(CFD_LOG_SOURCE, "invalid pubkey.");
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"Failed to pubkey. ExtPubkey invalid pubkey.");
}
Pubkey key = pubkey;
if (!key.IsCompress()) {
key = key.Compress();
}
// create simple parent data
struct ext_key extkey = {};
memset(&extkey, 0, sizeof(extkey));
extkey.version = kVersionTestnetPubkey;
if ((network_type == NetType::kMainnet) ||
(network_type == NetType::kLiquidV1)) {
extkey.version = kVersionMainnetPubkey;
}
extkey.depth = depth;
extkey.child_num = child_num;
extkey.priv_key[0] = BIP32_FLAG_KEY_PUBLIC;
std::vector<uint8_t> pubkey_bytes = key.GetData().GetBytes();
std::vector<uint8_t> pubkey_hash = HashUtil::Hash160(key).GetBytes();
std::vector<uint8_t> fingerprint_bytes = parent_fingerprint.GetBytes();
std::vector<uint8_t> chain_bytes = chain_code.GetData().GetBytes();
memcpy(extkey.pub_key, pubkey_bytes.data(), pubkey_bytes.size());
// parent160: use top 4-byte only
fingerprint_bytes.resize(4);
memcpy(extkey.parent160, fingerprint_bytes.data(), fingerprint_bytes.size());
memcpy(extkey.hash160, pubkey_hash.data(), pubkey_hash.size());
memcpy(extkey.chain_code, chain_bytes.data(), chain_bytes.size());
std::vector<uint8_t> data(BIP32_SERIALIZED_LEN);
int ret = bip32_key_serialize(
&extkey, BIP32_FLAG_KEY_PUBLIC, data.data(), data.size());
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_serialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPrivkey serialize error.");
}
serialize_data_ = ByteData(data);
tweak_sum_ = ByteData256();
AnalyzeBip32KeyData(
&extkey, nullptr, nullptr, &version_, &depth_, &child_num_, &chaincode_,
nullptr, &pubkey_, &fingerprint_);
#ifndef CFD_DISABLE_ELEMENTS
// collect pub_key_tweak_sum from ext_key
std::vector<uint8_t> tweak_sum(sizeof(extkey.pub_key_tweak_sum));
memcpy(tweak_sum.data(), extkey.pub_key_tweak_sum, tweak_sum.size());
tweak_sum_ = ByteData256(tweak_sum);
#endif // CFD_DISABLE_ELEMENTS
}
ByteData ExtPubkey::GetData() const { return serialize_data_; }
std::string ExtPubkey::ToString() const {
return ToBase58String(serialize_data_, "ExtPubkey");
}
Pubkey ExtPubkey::GetPubkey() const { return pubkey_; }
ExtPubkey ExtPubkey::DerivePubkey(uint32_t child_num) const {
std::vector<uint32_t> path = {child_num};
return DerivePubkey(path);
}
ExtPubkey ExtPubkey::DerivePubkey(const std::vector<uint32_t>& path) const {
struct ext_key extkey;
struct ext_key child_key;
const std::vector<uint8_t>& serialize_data = serialize_data_.GetBytes();
int ret = bip32_key_unserialize(
serialize_data.data(), serialize_data.size(), &extkey);
if (ret != WALLY_OK) {
warn(CFD_LOG_SOURCE, "bip32_key_unserialize error. ret={}", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError, "ExtPubkey unserialize error.");
}
#ifndef CFD_DISABLE_ELEMENTS
// write pub_key_tweak_sum to ext_key
memcpy(
extkey.pub_key_tweak_sum, tweak_sum_.GetBytes().data(),
sizeof(extkey.pub_key_tweak_sum));
#endif // CFD_DISABLE_ELEMENTS
uint32_t flag = BIP32_FLAG_KEY_PUBLIC;
ret = bip32_key_from_parent_path(
&extkey, path.data(), path.size(), flag | BIP32_FLAG_KEY_TWEAK_SUM,
&child_key);
if (ret != WALLY_OK) {
// hardened check
for (const auto& value : path) {
if ((value & ExtPrivkey::kHardenedKey) != 0) {
warn(
CFD_LOG_SOURCE,
"bip32_key_from_parent_path error. ret={} hardened=true", ret);
throw CfdException(
CfdError::kCfdIllegalArgumentError,
"ExtPubkey hardened derive error.");
}
}
warn(CFD_LOG_SOURCE, "bip32_key_from_parent_path error. ret={}", ret);