forked from bitcoin-core/btcdeb
-
Notifications
You must be signed in to change notification settings - Fork 10
/
value.h
663 lines (643 loc) · 21 KB
/
value.h
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
#ifndef included_value_h_
#define included_value_h_
#include <inttypes.h>
#include <vector>
#include <string>
#include <util/strencodings.h>
#include <debugger/script.h>
#include <tinyformat.h>
#include <crypto/sha256.h>
#include <crypto/ripemd160.h>
#include <base58.h>
#include <bech32.h>
static bool VALUE_WARN = true;
static bool VALUE_EXTENDED = false; // 0bNNNN, etc
extern std::string bech32_hrp;
extern uint8_t bech32_witness_version;
template<typename T1, typename T2>
inline void insert(T1& a, T2&& b) {
a.insert(a.end(), b.begin(), b.end());
}
void DeserializeBool(const char* bv, std::vector<uint8_t>& output);
struct Value {
enum {
T_STRING,
T_INT,
T_DATA,
T_OPCODE,
} type;
int64_t int64;
opcodetype opcode;
std::vector<uint8_t> data;
std::string str;
static std::vector<Value> parse_args(const std::vector<const char*> args) {
std::vector<Value> result;
std::string accum = "";
for (auto& v : args) {
size_t vlen = strlen(v);
if (accum != "") {
accum += std::string(" ") + v;
if (vlen > 0 && v[vlen-1] == ']') {
result.emplace_back(accum.c_str(), accum.length() - 1);
accum = "";
continue;
}
}
if (vlen > 0) {
// brackets embed
if (v[0] == '[' && v[vlen-1] != ']') {
accum = &v[1];
continue;
}
result.emplace_back(v, vlen);
}
}
return result;
}
static std::vector<Value> parse_args(const size_t argc, const char** argv, size_t argidx = 0) {
std::vector<const char*> args;
for (size_t i = argidx; i < argc; i++) args.push_back(argv[i]);
return parse_args(args);
}
static std::vector<Value> parse_args(const char* args_string, size_t args_len = 0) {
if (args_len == 0) args_len = strlen(args_string);
std::vector<const char*> args;
char* args_ptr[args_len];
size_t arg_idx = 0;
size_t start = 0;
for (size_t i = 0; i <= args_len; i++) {
char ch = args_string[i - (i == args_len)];
if (ch == '[') {
// start counting starting brackets, and stop when we hit depth 0
size_t depth = 1;
while ((++i) <= args_len && depth > 0) {
ch = args_string[i];
depth += (ch == '[') - (ch == ']');
}
if (depth > 0) {
fprintf(stderr, "parse error, unclosed [bracket (expected: ']') in \"%s\"\n", args_string);
exit(1);
}
}
if (i == args_len || (ch == ']' || ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '#')) {
if (start == i) {
start++;
} else {
args_ptr[arg_idx] = strndup(&args_string[start], i - start);
args.push_back(args_ptr[arg_idx]);
arg_idx++;
start = i + 1;
}
if (ch == '#') {
// trim out remainder of this line
while (i < args_len && args_string[i] != '\n' && args_string[i] != '\r') {
i++;
}
start = i + 1;
}
}
}
std::vector<Value> result = parse_args(args);
for (size_t i = 0; i < arg_idx; i++) free(args_ptr[i]);
return result;
}
static std::string serialize(const std::vector<Value>& varr) {
CScript s;
for (const Value& v : varr) {
v >> s;
}
return HexStr(s);
}
static Value from_secp256k1_pubkey(const void* secp256k1_pubkey_ptr);
explicit Value(const int64_t i) { int64 = i; type = T_INT; }
explicit Value(const opcodetype o) { opcode = o; type = T_OPCODE; }
explicit Value(const std::vector<uint8_t>& d) { data = d; type = T_DATA; }
Value(const CScript& script) {
data.clear();
insert(data, script);
type = T_DATA;
}
Value(std::vector<Value>&& v, bool fallthrough_single = false) {
if (fallthrough_single && v.size() == 1) {
type = v[0].type;
data = v[0].data;
str = v[0].str;
int64 = v[0].int64;
opcode = v[0].opcode;
return;
}
type = T_DATA;
data.clear();
CScript s;
for (auto& it : v) {
it >> s;
}
insert(data, s);
}
Value(const char* v, size_t vlen = 0, bool non_numeric = false) {
if (!vlen) vlen = strlen(v);
if (vlen == 2 && v[0] == '0' && v[1] == 'x') {
type = T_DATA;
data.clear();
return;
}
str = v;
type = T_STRING;
if (vlen > 1 && v[0] == '[' && v[vlen - 1] == ']') {
CScript s;
// decompile from Bitcoin Script
for (auto& it : parse_args(&v[1], vlen - 2)) {
it >> s;
}
insert(data, s);
type = T_DATA;
return;
}
if (VALUE_EXTENDED && vlen > 1 && v[0] == '0' && v[1] == 'b') {
type = T_DATA;
DeserializeBool(&v[2], data);
return;
}
if (vlen > 3 && v[vlen-1] == ')') {
char fun[30];
size_t i;
for (i = 0; i < 29 && v[i] && v[i] != '('; ++i) {
fun[i] = v[i];
}
if (v[i] == '(') {
fun[i] = 0;
size_t funlen = ++i;
size_t vallen = vlen - i - 1;
char* val = strndup(&v[i], vallen);
*this = Value(val, vallen);
free(val);
if (!do_exec(fun)) {
fprintf(stderr, "unknown function %s: expression left as is\n", fun);
} else return;
}
}
int64 = non_numeric ? 0 : atoll(v);
if (int64 != 0 || !strcmp(v, "0")) {
// verify
char buf[vlen + 1];
snprintf(buf, vlen + 1, "%" PRId64, int64);
if (!strcmp(buf, v)) {
// verified; can it be a hexstring too?
if (!(vlen & 1)) {
std::vector<unsigned char> pushData;
if (TryHex(v, pushData)) {
// it can; warn about using 0x for hex
if (VALUE_WARN) btc_logf("warning: ambiguous input %s is interpreted as a numeric value; use 0x%s to force into hexadecimal interpretation\n", v, v);
}
}
if (int64 >= 1 && int64 <= 16) {
if (VALUE_WARN) btc_logf("warning: ambiguous input %s is interpreted as a numeric value; use OP_%s to force into opcode\n", v, v);
}
type = T_INT;
return;
}
}
// opcode check
opcode = GetOpCode(v);
if (opcode != OP_INVALIDOPCODE) {
type = T_OPCODE;
return;
}
// hex string?
if (!(vlen & 1)) {
if (vlen > 2 && v[0] == '0' && v[1] == 'x') {
vlen -= 2;
v = &v[2];
}
if (TryHex(v, data)) {
type = T_DATA;
return;
}
}
}
const Value& operator>>(CScript& s) const {
switch (type) {
case T_OPCODE:
s << opcode;
break;
case T_INT:
s << int64;
break;
case T_DATA:
if (data.size() < 5) {
// we need to push this as a number
int64_t i = int_value();
s << i;
break;
}
// fall-through
default:
s << data_value();
}
return *this;
}
Value& operator+=(const Value& other) {
data_value();
insert(data, other.data_value());
return *this;
}
bool operator==(const Value& other) const {
return type == other.type &&
(type == T_INT ? int64 == other.int64 :
type == T_STRING ? str == other.str :
type == T_OPCODE ? opcode == other.opcode :
data == other.data);
}
Value& operator=(const Value& other) {
type = other.type;
switch (type) {
case T_INT: int64 = other.int64; break;
case T_STRING: str = other.str; break;
case T_OPCODE: opcode = other.opcode; break;
case T_DATA: data = other.data; break;
}
return *this;
}
std::vector<uint8_t> data_value() const { return const_cast<Value*>(this)->data_value(); }
std::vector<uint8_t> data_value(/*bool script = false*/) {
switch (type) {
case T_DATA:
return data;
case T_OPCODE:
data.clear();
insert(data, CScript() << opcode);
return data;
case T_INT:
// use CScriptNum
data = CScriptNum(int64).getvch();
type = T_DATA;
return data;
default:
// ascii representation
data.resize(str.length());
memcpy(data.data(), str.data(), str.length());
return data;
}
}
std::string& str_value() {
switch (type) {
case T_DATA: {
str = HexStr(data);
break;
}
case T_OPCODE:
str = std::to_string(opcode);
break;
case T_INT:
str = std::to_string(int64);
case T_STRING:
break;
}
type = T_STRING;
return str;
}
std::string hex_str() const {
switch (type) {
case T_OPCODE:
return strprintf("%02x", opcode);
case T_INT:
return HexStr(CScriptNum::serialize(int64));
case T_DATA:
return HexStr(data);
case T_STRING:
return HexStr(data_value());
}
return "<INVALID TYPE>";
}
int64_t int_value() const {
switch (type) {
case T_INT:
return int64;
case T_OPCODE:
return opcode;
case T_DATA:
return CScriptNum(data, false).GetInt64();
default:
fprintf(stderr, "cannot convert string into integer value: %s\n", str.c_str());
return -1;
}
}
void do_reverse() {
std::vector<char> vc;
int64_t j;
switch (type) {
case T_INT:
for (int64_t z = int64; z; z = z / 10) {
vc.push_back(z % 10);
}
j = 0;
for (auto it = vc.rbegin(); it != vc.rend(); ++it) {
j = (j * 10) + *it;
}
int64 = j;
return;
case T_DATA:
std::reverse(std::begin(data), std::end(data));
return;
case T_STRING:
std::reverse(str.begin(), str.end());
return;
default:
fprintf(stderr, "irreversible value type\n");
exit(1);
}
}
void do_sha256() {
data_value();
type = T_DATA;
CSHA256 s;
s.Write(data.data(), data.size());
data.resize(CSHA256::OUTPUT_SIZE);
s.Finalize(data.data());
}
void do_ripemd160() {
data_value();
type = T_DATA;
CRIPEMD160 s;
s.Write(data.data(), data.size());
data.resize(CRIPEMD160::OUTPUT_SIZE);
s.Finalize(data.data());
}
void do_hash256() {
do_sha256();
do_sha256();
}
void do_hash160() {
do_sha256();
do_ripemd160();
}
void do_base58enc() {
data_value();
str = EncodeBase58(data);
type = T_STRING;
}
void do_base58dec() {
if (type != T_STRING) {
fprintf(stderr, "cannot base58-decode non-string value\n");
return;
}
if (!DecodeBase58(str, data, 200)) {
fprintf(stderr, "decode failed\n");
}
type = T_DATA;
}
void do_base58chkenc() {
data_value();
str = EncodeBase58Check(data);
type = T_STRING;
}
void do_base58chkdec() {
if (type != T_STRING) {
fprintf(stderr, "cannot base58-decode non-string value\n");
return;
}
if (!DecodeBase58Check(str, data, 200)) {
fprintf(stderr, "decode failed\n");
}
type = T_DATA;
}
void do_addr_to_spk() {
// addresses are base58-check encoded, so we decode them first
do_base58chkdec();
// they are now prefixed with a 0x00; rip that out
data.erase(data.begin());
// wrap in appropriate script fluff
CScript s;
s << OP_DUP << OP_HASH160 << data << OP_EQUALVERIFY << OP_CHECKSIG;
data.clear();
insert(data, s);
}
void do_spk_to_addr() {
// data should be OP_DUP OP_HASH160 0x14 <20 b hash> OP_EQUALVERIFY OP_CHECKSIG
if (data.size() != 25) {
fprintf(stderr, "wrong length (expected 25 bytes)\n");
return;
}
if (data[0] != OP_DUP ||
data[1] != OP_HASH160 ||
data[2] != 0x14 ||
data[23] != OP_EQUALVERIFY ||
data[24] != OP_CHECKSIG) {
fprintf(stderr, "unknown script (expected DUP H160 0x14 <20b> EQUALVERIFY CHECKSIG\n");
return;
}
data[0] = 0x00; // prefix
data.erase(data.begin() + 1, data.begin() + 3);
data.resize(21);
do_base58chkenc();
}
void do_bech32enc() {
data_value();
std::vector<unsigned char> tmp = {1 /* temporary; this should be configurable (wit ver) */};
ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, data.begin(), data.end());
str = bech32::Encode(bech32::Encoding::BECH32, bech32_hrp, tmp);
type = T_STRING;
}
void do_bech32menc() {
data_value();
std::vector<unsigned char> tmp = {1 /* temporary; this should be configurable (wit ver) */};
ConvertBits<8, 5, true>([&](unsigned char c) { tmp.push_back(c); }, data.begin(), data.end());
str = bech32::Encode(bech32::Encoding::BECH32M, bech32_hrp, tmp);
type = T_STRING;
}
void do_bech32dec() {
if (type != T_STRING) {
fprintf(stderr, "cannot bech32-decode non-string value\n");
return;
}
bech32::DecodeResult result = bech32::Decode(str);
if (result.encoding == bech32::Encoding::INVALID) {
fprintf(stderr, "failed to bech32(m)-decode string\n");
return;
}
auto bech = result.data;
// Bech32(m) decoding
int version = bech[0]; // The first 5 bit symbol is the witness version (0-16)
// data = r.second;
printf("(bech32%s HRP = %s)\n", result.encoding == bech32::Encoding::BECH32M ? "m" : "", result.hrp.c_str());
type = T_DATA;
data.clear();
// The rest of the symbols are converted witness program bytes.
if (ConvertBits<5, 8, false>([&](unsigned char c) { data.push_back(c); }, bech.begin() + 1, bech.end())) {
if (version == 0) {
{
if (data.size() == 20) {
// std::copy(data.begin(), data.end(), keyid.begin());
// return keyid;
return;
}
}
{
// WitnessV0ScriptHash scriptid;
if (data.size() == 32) {
// std::copy(data.begin(), data.end(), scriptid.begin());
// return scriptid;
return;
}
}
fprintf(stderr, "warning: unknown size %zu\n", data.size());
// return CNoDestination();
return;
}
if (version > 16 || data.size() < 2 || data.size() > 40) {
return;
// return CNoDestination();
}
// WitnessUnknown unk;
// unk.version = version;
// std::copy(data.begin(), data.end(), unk.program);
// unk.length = data.size();
// return unk;
return;
}
}
void verify_sig(bool compact);
void do_verify_sig() { verify_sig(false); }
void do_verify_sig_compact() { verify_sig(true); }
void do_combine_pubkeys();
void do_tweak_pubkey();
void do_pubkey_to_xpubkey();
void do_add();
void do_sub();
void do_negate_pubkey();
void do_not_op();
void do_boolify();
void do_tagged_hash();
void do_taproot_tweak_pubkey();
void do_jacobi_symbol();
void do_prefix_compact_size();
void do_len();
#ifdef ENABLE_DANGEROUS
void do_taproot_tweak_seckey();
void do_combine_privkeys();
void do_multiply_privkeys();
void do_negate_privkey();
void do_encode_wif() {
data_value();
data.insert(data.begin(), 0x80); // main net
// data.insert(data.end(), 0x01); // compressed
Value hashed(*this);
hashed.do_hash256();
data.insert(data.end(), hashed.data.begin(), hashed.data.begin() + 4);
do_base58enc();
}
void do_decode_wif() {
if (type != T_STRING) {
fprintf(stderr, "input must be a WIF string; type = %d\n", type);
return;
}
do_base58dec();
if (data.size() < 4) {
fprintf(stderr, "base58 decoding failed\n");
return;
}
std::vector<uint8_t> chksum(data.end() - 4, data.end());
data.resize(data.size() - 4);
if (data[0] != 0x80) {
fprintf(stderr, "unexpected prefix 0x%02x (expected 0x80)\n", data[0]);
}
// check sum validation part before removing prefixes/suffixes
Value hashed(*this);
hashed.do_hash256();
hashed.data.resize(4);
for (int i = 0; i < 4; i++) {
if (hashed.data[i] != chksum[i]) {
fprintf(stderr, "checksum failure for byte %d: 0x%02x != 0x%02x\n", i, chksum[i], hashed.data[i]);
return;
}
}
data = std::vector<uint8_t>(data.begin() + 1, data.end());
}
void sign(bool compact);
void sign_schnorr();
void do_sign() { sign(false); }
void do_sign_compact() { sign(true); }
void do_sign_schnorr() { sign_schnorr(); }
void do_get_pubkey();
void do_get_xpubkey();
#endif // ENABLE_DANGEROUS
bool do_exec(const std::string& fun) {
if (fun == "echo") return true;
if (fun == "hex") { str = hex_str(); type = T_STRING; return true; }
if (fun == "int") { int64 = int_value(); type = T_INT; return true; }
#define DO(s) if (fun == #s) { do_##s(); return true; }
DO(reverse);
DO(sha256);
DO(ripemd160);
DO(hash256);
DO(hash160);
DO(base58chkenc);
DO(base58chkdec);
DO(bech32enc);
DO(bech32dec);
DO(verify_sig);
DO(combine_pubkeys);
DO(tweak_pubkey);
DO(pubkey_to_xpubkey);
DO(addr_to_spk);
DO(spk_to_addr);
DO(add);
DO(sub);
if (fun == "jacobi") { do_jacobi_symbol(); return true; }
DO(tagged_hash);
DO(taproot_tweak_pubkey);
DO(prefix_compact_size);
#ifdef ENABLE_DANGEROUS
// DO(taproot_tweak_seckey);
DO(combine_privkeys);
DO(multiply_privkeys);
DO(negate_privkey);
DO(encode_wif);
DO(decode_wif);
DO(sign);
DO(sign_schnorr);
DO(get_pubkey);
DO(get_xpubkey);
#endif // ENABLE_DANGEROUS
#undef DO
return false;
}
void print() const {
switch (type) {
case T_INT:
printf("%" PRId64, int64);
return;
case T_OPCODE:
printf("%s (%02x)", GetOpName(opcode).c_str(), opcode);
case T_DATA:
for (auto it : data) printf("%02x", it);
return;
case T_STRING:
printf("\"%s\"", str.c_str());
}
}
void println() const {
print(); fputc('\n', stdout);
}
std::string to_string() const {
std::string s = "";
switch (type) {
case T_INT:
return strprintf("%" PRId64, int64);
case T_OPCODE:
return strprintf("%s (%02x)", GetOpName(opcode).c_str(), opcode);
case T_DATA:
for (auto it : data) s = s + strprintf("%02x", it);
return s;
case T_STRING:
return strprintf("\"%s\"", str.c_str());
}
return "???";
}
static Value prepare_extraction(const Value& a, const Value& b);
private:
bool extract_values(std::vector<std::vector<uint8_t>>& values);
};
const std::vector<std::byte>& VecU8ToByte(const std::vector<uint8_t>& u8v);
const std::vector<std::uint8_t>& VecByteToU8(const std::vector<std::byte>& bv);
#endif // included_value_h_