-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathdstencode.cpp
More file actions
318 lines (264 loc) · 9.06 KB
/
dstencode.cpp
File metadata and controls
318 lines (264 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright (c) 2017 The Bitcoin developers
// Copyright (c) 2019 DeVault developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <dstencode.h>
#include <testaddr.h> // mostly for test functions / unit tests
#include <chainparams.h>
#include <config.h>
#include <script/standard.h>
#include <cashaddr.h>
#include <chainparams.h>
#include <key.h>
#include <pubkey.h>
#include <script/script.h>
#include <primitives/transaction.h>
#include <util/strencodings.h>
#include <variant>
#include <algorithm>
// Now include previous cashaddrenc to make some EncodeCashAddr, etc functions private
// and not for general use
CTxDestination DecodeCashAddrDestination(const CashAddrContent &content);
namespace {
// Convert the data part to a 5 bit representation.
template <class T>
std::vector<uint8_t> PackAddrData(const T &id, uint8_t type) {
uint8_t version_byte(type << 3);
size_t size = id.size();
uint8_t encoded_size = 0;
switch (size * 8) {
case 160:
encoded_size = 0;
break;
case 192:
encoded_size = 1;
break;
case 224:
encoded_size = 2;
break;
case 256:
encoded_size = 3;
break;
case 320:
encoded_size = 4;
break;
case 384:
encoded_size = 5;
break;
case 448:
encoded_size = 6;
break;
case 512:
encoded_size = 7;
break;
default:
throw std::runtime_error(
"Error packing cashaddr: invalid address length");
}
version_byte |= encoded_size;
std::vector<uint8_t> data = {version_byte};
data.insert(data.end(), std::begin(id), std::end(id));
std::vector<uint8_t> converted;
// Reserve the number of bytes required for a 5-bit packed version of a
// hash, with version byte. Add half a byte(4) so integer math provides
// the next multiple-of-5 that would fit all the data.
converted.reserve(((size + 1) * 8 + 4) / 5);
ConvertBits<8, 5, true>(converted, std::begin(data), std::end(data));
return converted;
}
// Implements encoding of CTxDestination using cashaddr.
class CashAddrEncoder : public std::variant<std::string> {
public:
CashAddrEncoder(const CChainParams &p) : params(p) {}
std::string operator()(const CKeyID &id) const {
std::vector<uint8_t> data = PackAddrData(id, PUBKEY_TYPE);
return cashaddr::Encode(params.CashAddrPrefix(), data);
}
std::string operator()(const BKeyID &id) const {
std::vector<uint8_t> data = PackAddrData(id, BLSPUBKEY_TYPE);
return cashaddr::Encode(params.BLSAddrPrefix(), data);
}
std::string operator()(const CScriptID &id) const {
std::vector<uint8_t> data = PackAddrData(id, SCRIPT_TYPE);
return cashaddr::Encode(params.CashAddrPrefix(), data);
}
std::string operator()(const CNoDestination &) const { return ""; }
private:
const CChainParams ¶ms;
};
} // namespace
std::string EncodeCashAddr(const CTxDestination &dst, const CChainParams ¶ms) {
return std::visit(CashAddrEncoder(params), dst);
}
std::string EncodeCashAddr(const std::string &prefix, const CashAddrContent &content) {
std::vector<uint8_t> data = PackAddrData(content.hash, content.type);
return cashaddr::Encode(prefix, data);
}
CTxDestination DecodeCashAddr(const std::string &addr, const CChainParams ¶ms) {
CashAddrContent content = DecodeCashAddrContent(addr, params.CashAddrPrefix());
if (content.hash.size() == 0) { // this is how we differentiate between EC and BLS Addresses
content = DecodeCashAddrContent(addr, params.BLSAddrPrefix());
}
if (content.hash.size() == 0) {
return CNoDestination{};
}
return DecodeCashAddrDestination(content);
}
CashAddrContent DecodeCashAddrContent(const std::string &addr,
const std::string &expectedPrefix) {
std::string prefix;
std::vector<uint8_t> payload;
std::tie(prefix, payload) = cashaddr::Decode(addr, expectedPrefix);
if (prefix != expectedPrefix) {
return {};
}
if (payload.empty()) {
return {};
}
// Check that the padding is zero.
size_t extrabits = payload.size() * 5 % 8;
if (extrabits >= 5) {
// We have more padding than allowed.
return {};
}
uint8_t last = payload.back();
uint8_t mask = (1 << extrabits) - 1;
if (last & mask) {
// We have non zero bits as padding.
return {};
}
std::vector<uint8_t> data;
data.reserve(payload.size() * 5 / 8);
ConvertBits<5, 8, false>(data, begin(payload), end(payload));
// Decode type and size from the version.
uint8_t version = data[0];
if (version & 0x80) {
// First bit is reserved.
return {};
}
auto type = CashAddrType((version >> 3) & 0x1f);
uint32_t hash_size = 20 + 4 * (version & 0x03);
if (version & 0x04) {
hash_size *= 2;
}
// Check that we decoded the exact number of bytes we expected.
if (data.size() != hash_size + 1) {
return {};
}
// Pop the version.
data.erase(data.begin());
return {type, std::move(data)};
}
CTxDestination DecodeCashAddrDestination(const CashAddrContent &content) {
if (content.hash.size() != 20) {
// Only 20 bytes hash are supported now.
return CNoDestination{};
}
uint160 hash;
std::copy(begin(content.hash), end(content.hash), hash.begin());
switch (content.type) {
case PUBKEY_TYPE:
return CKeyID(hash);
case BLSPUBKEY_TYPE:
return BKeyID(hash);
case SCRIPT_TYPE:
return CScriptID(hash);
default:
return CNoDestination{};
}
}
// PackCashAddrContent allows for testing PackAddrData in unittests due to
// template definitions.
std::vector<uint8_t> PackCashAddrContent(const CashAddrContent &content) {
return PackAddrData(content.hash, content.type);
}
std::string EncodeSecret(const CKey& key) {
assert(key.IsValid());
auto type = SECRET_TYPE;
uint8_t version_byte(type << 3);
std::vector<uint8_t> data = {version_byte};
size_t size = key.size();
data.insert(data.end(), std::begin(key), std::end(key));
std::vector<uint8_t> copydata;
copydata.insert(copydata.end(), std::begin(key), std::end(key));
std::vector<uint8_t> converted;
converted.reserve(((size + 1) * 8 + 4) / 5);
ConvertBits<8, 5, true>(converted, std::begin(data), std::end(data));
return cashaddr::Encode(Params().CashAddrSecretPrefix(), converted);
}
bool CheckSecretIsValid(const std::string &addr) {
CKey k = DecodeSecret(addr);
return k.IsValid();
}
CKey DecodeSecret(const std::string &addr) {
CKey key;
std::string prefix;
std::vector<uint8_t> payload;
std::tie(prefix, payload) = cashaddr::Decode(addr, Params().CashAddrSecretPrefix());
if (payload.empty()) {
return key;
}
// Check that the padding is zero.
size_t extrabits = payload.size() * 5 % 8;
if (extrabits >= 5) {
// We have more padding than allowed.
return key;
}
uint8_t last = payload.back();
uint8_t mask = (1 << extrabits) - 1;
if (last & mask) {
// We have non zero bits as padding.
return key;
}
std::vector<uint8_t> data;
data.reserve(payload.size() * 5 / 8);
ConvertBits<5, 8, false>(data, begin(payload), end(payload));
// Decode type and size from the version.
uint8_t version = data[0];
if (version & 0x80) {
// First bit is reserved.
return key;
}
auto type = CashAddrType((version >> 3) & 0x1f);
// must be Secret Type
if (type != SECRET_TYPE) {
return key;
}
// Check that we decoded the exact number of bytes we expected.
if (data.size() != 33) {
return key;
}
// Pop the version.
data.erase(data.begin());
key.Set(data.begin(), data.end());
return key;
}
std::string EncodeDestination(const CTxDestination &dest,
const Config &config) {
const CChainParams ¶ms = config.GetChainParams();
return EncodeCashAddr(dest, params);
}
CTxDestination DecodeDestination(const std::string &addr,
const CChainParams ¶ms) {
CTxDestination dst = DecodeCashAddr(addr, params);
return dst;
}
bool IsValidDestinationString(const std::string &addr,
const CChainParams ¶ms) {
return IsValidDestination(DecodeDestination(addr, params));
}
std::string EncodeDestination(const CTxDestination &dst) {
return EncodeDestination(dst, GetConfig());
}
std::string GetAddrFromTxOut(const CTxOut& out) {
CTxDestination dest;
ExtractDestination(out.scriptPubKey, dest);
std::string SDest = EncodeDestination(dest);
return SDest;
}
CTxDestination GetDestFromTxOut(const CTxOut& out) {
CTxDestination dest;
// Assume always a valid scriptkey
ExtractDestination(out.scriptPubKey, dest);
return dest;
}