forked from goatpig/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBIP32_Serialization.cpp
More file actions
184 lines (152 loc) · 4.97 KB
/
BIP32_Serialization.cpp
File metadata and controls
184 lines (152 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
////////////////////////////////////////////////////////////////////////////////
// //
// Copyright (C) 2017, goatpig //
// Distributed under the MIT license //
// See LICENSE-MIT or https://opensource.org/licenses/MIT //
// //
////////////////////////////////////////////////////////////////////////////////
#include "BIP32_Serialization.h"
#include "BlockDataManagerConfig.h"
////////////////////////////////////////////////////////////////////////////////
BinaryData BIP32_Serialization::computeFingerprint(const SecureBinaryData& key)
{
auto compute_fingerprint = [](const SecureBinaryData& key)->BinaryData
{
return BtcUtils::hash160(key).getSliceCopy(0, 4);
};
bool ispriv = false;
if (key.getSize() == 32)
{
ispriv = true;
}
else if (key.getSize() == 33)
{
if (key.getPtr()[0] == 0)
ispriv = true;
}
if (ispriv)
{
BinaryDataRef privkey = key.getRef();
if (privkey.getSize() == 33)
privkey = key.getSliceRef(1, 32);
auto&& pubkey = CryptoECDSA().ComputePublicKey(privkey);
auto&& compressed_pub = CryptoECDSA().CompressPoint(pubkey);
return compute_fingerprint(compressed_pub);
}
else
{
if (key.getSize() != 33)
{
auto&& compressed_pub = CryptoECDSA().CompressPoint(key);
return compute_fingerprint(compressed_pub);
}
else
{
return compute_fingerprint(key);
}
}
}
////////////////////////////////////////////////////////////////////////////////
void BIP32_Serialization::encode()
{
bool ispriv = false;
if (key_.getSize() == 32)
{
ispriv = true;
}
else if (key_.getSize() == 33)
{
if (key_.getPtr()[0] == 0)
ispriv = true;
}
if (chaincode_.getSize() != 32)
throw runtime_error("invalid chaincode for BIP32 ser");
fingerprint_ = move(computeFingerprint(key_));
BinaryWriter bw;
if (BlockDataManagerConfig::getPubkeyHashPrefix() ==
SCRIPT_PREFIX_HASH160)
{
if (ispriv)
bw.put_uint32_t(BIP32_SER_VERSION_MAIN_PRV, BE);
else
bw.put_uint32_t(BIP32_SER_VERSION_MAIN_PUB, BE);
}
else if (BlockDataManagerConfig::getPubkeyHashPrefix() ==
SCRIPT_PREFIX_HASH160_TESTNET)
{
if (ispriv)
bw.put_uint32_t(BIP32_SER_VERSION_TEST_PRV, BE);
else
bw.put_uint32_t(BIP32_SER_VERSION_TEST_PUB, BE);
}
else
{
throw runtime_error("invalid network");
}
bw.put_uint8_t(depth_);
bw.put_BinaryData(fingerprint_);
bw.put_uint32_t(leaf_id_, BE);
bw.put_BinaryData(chaincode_);
if (key_.getSize() == 32 && ispriv)
bw.put_uint8_t(0);
else
throw runtime_error("invalid key for BIP32 ser");
bw.put_BinaryData(key_);
auto&& hash = BtcUtils::getSha256(bw.getData()).getSliceCopy(0, 4);
bw.put_BinaryData(hash);
auto&& b58_data = BtcUtils::base58_encode(bw.getData());
b58_string_ = b58_data.toBinStr();
}
////////////////////////////////////////////////////////////////////////////////
void BIP32_Serialization::decode()
{
//b58 decode
auto&& bin_data = BtcUtils::base58_decode(b58_string_);
if (bin_data.getSize() != 82)
throw runtime_error("invalid bip32 serialized string");
//checksum
BinaryRefReader brr(bin_data);
auto bdr_val = brr.get_BinaryDataRef(bin_data.getSize() - 4);
auto bdr_chksum = brr.get_BinaryDataRef(4);
auto&& hash = BtcUtils::getSha256(bdr_val);
if (hash.getSliceRef(0, 4) != bdr_chksum)
throw runtime_error("bip32 checksum failure");
////
BinaryRefReader brr_val(bdr_val);
//version
auto verbytes = brr_val.get_uint32_t(BE);
bool ispriv = false;
switch (verbytes)
{
case BIP32_SER_VERSION_MAIN_PRV:
ispriv = true;
case BIP32_SER_VERSION_MAIN_PUB:
{
if (BlockDataManagerConfig::getPubkeyHashPrefix() !=
SCRIPT_PREFIX_HASH160)
throw runtime_error("bip32 string is for wrong network");
break;
}
case BIP32_SER_VERSION_TEST_PRV:
ispriv = true;
case BIP32_SER_VERSION_TEST_PUB:
{
if (BlockDataManagerConfig::getPubkeyHashPrefix() !=
SCRIPT_PREFIX_HASH160_TESTNET)
throw runtime_error("bip32 string is for wrong network");
break;
}
default:
throw runtime_error("unexpected bip32 string version");
}
depth_ = brr_val.get_uint8_t();
fingerprint_ = brr_val.get_BinaryData(4);
leaf_id_ = brr_val.get_uint32_t(BE);
chaincode_ = brr_val.get_BinaryData(32);
key_ = brr_val.get_BinaryData(33);
if (ispriv && key_.getPtr()[0] != 0)
throw runtime_error("bip32 string invalid key type");
auto&& fingerprint = computeFingerprint(key_);
if (fingerprint != fingerprint_)
throw runtime_error("bip32 string fingerprint mismatch");
}