forked from ElementsProject/libwally-core
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdescriptor.c
3112 lines (2826 loc) · 114 KB
/
descriptor.c
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 "internal.h"
#include "script.h"
#include "script_int.h"
#include <include/wally_address.h>
#include <include/wally_bip32.h>
#include <include/wally_crypto.h>
#include <include/wally_descriptor.h>
#include <include/wally_map.h>
#include <include/wally_script.h>
#include <limits.h>
#include <stdbool.h>
#include <stdlib.h>
#define NUM_ELEMS(a) (sizeof(a) / sizeof(a[0]))
#define MS_FLAGS_ALL (WALLY_MINISCRIPT_TAPSCRIPT | \
WALLY_MINISCRIPT_ONLY | \
WALLY_MINISCRIPT_REQUIRE_CHECKSUM | \
WALLY_MINISCRIPT_POLICY_TEMPLATE | \
WALLY_MINISCRIPT_UNIQUE_KEYPATHS)
#define MS_FLAGS_CANONICALIZE (WALLY_MINISCRIPT_REQUIRE_CHECKSUM | \
WALLY_MINISCRIPT_POLICY_TEMPLATE)
/* Properties and expressions definition */
#define TYPE_NONE 0x00
#define TYPE_B 0x01 /* Base expressions */
#define TYPE_V 0x02 /* Verify expressions */
#define TYPE_K 0x04 /* Key expressions */
#define TYPE_W 0x08 /* Wrapped expressions */
#define TYPE_MASK 0x0F /* expressions mask */
#define PROP_Z 0x00000100 /* Zero-arg property */
#define PROP_O 0x00000200 /* One-arg property */
#define PROP_N 0x00000400 /* Nonzero arg property */
#define PROP_D 0x00000800 /* Dissatisfiable property */
#define PROP_U 0x00001000 /* Unit property */
#define PROP_E 0x00002000 /* Expression property */
#define PROP_F 0x00004000 /* Forced property */
#define PROP_S 0x00008000 /* Safe property */
#define PROP_M 0x00010000 /* Nonmalleable property */
#define PROP_X 0x00020000 /* Expensive verify */
#define PROP_G 0x00040000 /* Relative time timelock */
#define PROP_H 0x00080000 /* Relative height timelock */
#define PROP_I 0x00100000 /* Absolute time timelock */
#define PROP_J 0x00200000 /* Absolute time heightlock */
#define PROP_K 0x00400000 /* No timelock mixing allowed */
/* OP_0 properties: Bzudemsxk */
#define PROP_OP_0 (TYPE_B | PROP_Z | PROP_U | PROP_D | PROP_E | PROP_M | PROP_S | PROP_X | PROP_K)
/* OP_1 properties: Bzufmxk */
#define PROP_OP_1 (TYPE_B | PROP_Z | PROP_U | PROP_F | PROP_M | PROP_X | PROP_K)
#define KIND_MINISCRIPT 0x01
#define KIND_DESCRIPTOR 0x02 /* Output Descriptor */
#define KIND_RAW 0x04
#define KIND_NUMBER 0x08
#define KIND_ADDRESS 0x10
#define KIND_KEY 0x20
#define KIND_BASE58 (0x0100 | KIND_ADDRESS)
#define KIND_BECH32 (0x0200 | KIND_ADDRESS)
#define KIND_PUBLIC_KEY (0x001000 | KIND_KEY)
#define KIND_PRIVATE_KEY (0x002000 | KIND_KEY)
#define KIND_BIP32 (0x004000 | KIND_KEY)
#define KIND_BIP32_PRIVATE_KEY (0x010000 | KIND_BIP32)
#define KIND_BIP32_PUBLIC_KEY (0x020000 | KIND_BIP32)
#define DESCRIPTOR_MIN_SIZE 20
#define MINISCRIPT_MULTI_MAX 20
#define REDEEM_SCRIPT_MAX_SIZE 520
#define WITNESS_SCRIPT_MAX_SIZE 10000
#define DESCRIPTOR_SEQUENCE_LOCKTIME_TYPE_FLAG 0x00400000
#define DESCRIPTOR_LOCKTIME_THRESHOLD 500000000
#define DESCRIPTOR_CHECKSUM_LENGTH 8
/* output descriptor */
#define KIND_DESCRIPTOR_PK (0x00000100 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_PKH (0x00000200 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_MULTI (0x00000300 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_MULTI_S (0x00000400 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_SH (0x00000500 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_WPKH (0x00010000 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_WSH (0x00020000 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_COMBO (0x00030000 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_ADDR (0x00040000 | KIND_DESCRIPTOR)
#define KIND_DESCRIPTOR_RAW (0x00050000 | KIND_DESCRIPTOR)
/* miniscript */
#define KIND_MINISCRIPT_PK (0x00000100 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_PKH (0x00000200 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_MULTI (0x00000300 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_PK_K (0x00001000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_PK_H (0x00002000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_OLDER (0x00010000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_AFTER (0x00020000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_SHA256 (0x00030000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_HASH256 (0x00040000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_RIPEMD160 (0x00050000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_HASH160 (0x00060000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_THRESH (0x00070000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_ANDOR (0x01000000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_AND_V (0x02000000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_AND_B (0x03000000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_AND_N (0x04000000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_OR_B (0x05000000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_OR_C (0x06000000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_OR_D (0x07000000 | KIND_MINISCRIPT)
#define KIND_MINISCRIPT_OR_I (0x08000000 | KIND_MINISCRIPT)
struct addr_ver_t {
const unsigned char network;
const unsigned char version_p2pkh;
const unsigned char version_p2sh;
const unsigned char version_wif;
const char family[8];
};
static const struct addr_ver_t g_address_versions[] = {
{
WALLY_NETWORK_BITCOIN_MAINNET,
WALLY_ADDRESS_VERSION_P2PKH_MAINNET,
WALLY_ADDRESS_VERSION_P2SH_MAINNET,
WALLY_ADDRESS_VERSION_WIF_MAINNET,
{ 'b', 'c', '\0', '\0', '\0', '\0', '\0', '\0' }
},
{
WALLY_NETWORK_BITCOIN_TESTNET,
WALLY_ADDRESS_VERSION_P2PKH_TESTNET,
WALLY_ADDRESS_VERSION_P2SH_TESTNET,
WALLY_ADDRESS_VERSION_WIF_TESTNET,
{ 't', 'b', '\0', '\0', '\0', '\0', '\0', '\0' }
},
{ /* Bitcoin regtest. This must remain immediately after WALLY_NETWORK_BITCOIN_TESTNET */
WALLY_NETWORK_BITCOIN_REGTEST,
WALLY_ADDRESS_VERSION_P2PKH_TESTNET,
WALLY_ADDRESS_VERSION_P2SH_TESTNET,
WALLY_ADDRESS_VERSION_WIF_TESTNET,
{ 'b', 'c', 'r', 't', '\0', '\0', '\0', '\0' }
},
{
WALLY_NETWORK_LIQUID,
WALLY_ADDRESS_VERSION_P2PKH_LIQUID,
WALLY_ADDRESS_VERSION_P2SH_LIQUID,
WALLY_ADDRESS_VERSION_WIF_MAINNET,
{ 'e', 'x', '\0', '\0', '\0', '\0', '\0', '\0' }
},
{
WALLY_NETWORK_LIQUID_TESTNET,
WALLY_ADDRESS_VERSION_P2PKH_LIQUID_TESTNET,
WALLY_ADDRESS_VERSION_P2SH_LIQUID_TESTNET,
WALLY_ADDRESS_VERSION_WIF_TESTNET,
{ 't', 'e', 'x', '\0', '\0', '\0', '\0', '\0' }
},
{
WALLY_NETWORK_LIQUID_REGTEST,
WALLY_ADDRESS_VERSION_P2PKH_LIQUID_REGTEST,
WALLY_ADDRESS_VERSION_P2SH_LIQUID_REGTEST,
WALLY_ADDRESS_VERSION_WIF_TESTNET,
{ 'e', 'r', 't', '\0', '\0', '\0', '\0', '\0' }
},
};
/* A node in a parsed miniscript expression */
typedef struct ms_node_t {
struct ms_node_t *next;
struct ms_node_t *child;
struct ms_node_t *parent;
uint32_t kind;
uint32_t type_properties;
int64_t number;
const char *child_path;
const char *data;
uint32_t data_len;
uint32_t child_path_len;
char wrapper_str[12];
unsigned char builtin;
unsigned char flags; /* WALLY_MS_IS_ flags */
} ms_node;
typedef struct wally_descriptor {
char *src; /* The canonical source script */
size_t src_len; /* Length of src */
ms_node *top_node; /* The first node of the parse tree */
const struct addr_ver_t *addr_ver;
uint32_t features; /* Features present in the parsed tree */
uint32_t num_variants; /* Number of script variants in the expression */
uint32_t num_multipaths; /* Number of multi-path items in the expression */
size_t script_len; /* Max script length generatable from this expression */
/* User modified for generation */
uint32_t variant; /* Variant for derivation of multi-type expressions */
uint32_t child_num; /* BIP32 child number for derivation */
uint32_t multi_index; /* Multi-path index for derivation */
uint32_t *path_buff; /* Path buffer for deriving keys */
uint32_t max_path_elems; /* Max path length seen in the descriptor */
struct wally_map keys;
} ms_ctx;
static int ctx_add_key_node(ms_ctx *ctx, ms_node *node)
{
const char *v = (char *)node;
return map_add(&ctx->keys, NULL, ctx->keys.num_items,
(unsigned char *)v, 1, true, false);
}
static int ensure_unique_policy_keys(const ms_ctx *ctx);
/* Built-in miniscript expressions */
typedef int (*node_verify_fn_t)(ms_ctx *ctx, ms_node *node);
typedef int (*node_gen_fn_t)(ms_ctx *ctx, ms_node *node,
unsigned char *script, size_t script_len, size_t *written);
struct ms_builtin_t {
const char *name;
const uint32_t name_len;
const uint32_t kind;
const uint32_t type_properties;
const uint32_t child_count; /* Number of expected children */
const node_verify_fn_t verify_fn;
const node_gen_fn_t generate_fn;
};
/* FIXME: the max is actually 20 in a witness script */
#define CHECKMULTISIG_NUM_KEYS_MAX 15
struct multisig_sort_data_t {
size_t pubkey_len;
unsigned char pubkey[EC_PUBLIC_KEY_UNCOMPRESSED_LEN];
};
static const struct addr_ver_t *addr_ver_from_network(uint32_t network)
{
size_t i;
if (network != WALLY_NETWORK_NONE) {
for (i = 0; i < NUM_ELEMS(g_address_versions); ++i) {
if (network == g_address_versions[i].network)
return g_address_versions + i;
}
}
return NULL; /* Not found */
}
static const struct addr_ver_t *addr_ver_from_version(
uint32_t version, const struct addr_ver_t *expected, bool *is_p2sh)
{
size_t i;
for (i = 0; i < NUM_ELEMS(g_address_versions); ++i) {
const struct addr_ver_t *addr_ver = g_address_versions + i;
if (version == addr_ver->version_p2pkh || version == addr_ver->version_p2sh) {
/* Found a matching network based on base58 address version */
if (expected && addr_ver->network != expected->network) {
/* Mismatch on caller provided network */
if (addr_ver->network == WALLY_NETWORK_BITCOIN_TESTNET &&
expected->network == WALLY_NETWORK_BITCOIN_REGTEST)
++addr_ver; /* testnet/regtest use the same versions; use regtest */
else
return NULL; /* Mismatch on provided network: Not found */
}
*is_p2sh = version == addr_ver->version_p2sh;
return addr_ver; /* Found */
}
}
return NULL; /* Not found */
}
static const struct addr_ver_t *addr_ver_from_family(
const char *family, size_t family_len, uint32_t network)
{
const struct addr_ver_t *addr_ver = addr_ver_from_network(network);
if (!addr_ver || !family || strlen(addr_ver->family) != family_len ||
memcmp(family, addr_ver->family, family_len))
return NULL; /* Not found or mismatched address version */
return addr_ver; /* Found */
}
/* Function prototype */
static const struct ms_builtin_t *builtin_get(const ms_node *node);
static int generate_script(ms_ctx *ctx, ms_node *node,
unsigned char *script, size_t script_len, size_t *written);
static bool is_valid_policy_map(const struct wally_map *map_in);
/* Wrapper for strtoll */
static bool strtoll_n(const char *str, size_t str_len, int64_t *v)
{
char buf[21]; /* from -9223372036854775808 to 9223372036854775807 */
char *end = NULL;
if (!str_len || str_len > sizeof(buf) - 1u ||
(str[0] != '-' && (str[0] < '0' || str[0] > '9')))
return false; /* Too short/long, or invalid format */
memcpy(buf, str, str_len);
buf[str_len] = '\0';
*v = strtoll(buf, &end, 10);
return end == buf + str_len && *v != LLONG_MIN && *v != LLONG_MAX;
}
/*
* Checksum code adapted from bitcoin core: bitcoin/src/script/descriptor.cpp DescriptorChecksum()
*/
/* The character set for the checksum itself (same as bech32). */
static const char *checksum_charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
static const unsigned char checksum_positions[] = {
0x5f, 0x3c, 0x5d, 0x5c, 0x1d, 0x1e, 0x33, 0x10, 0x0b, 0x0c, 0x12, 0x34, 0x0f, 0x35, 0x36, 0x11,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x1c, 0x37, 0x38, 0x39, 0x3a, 0x3b,
0x1b, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x0d, 0x5e, 0x0e, 0x3d, 0x3e,
0x5b, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x1f, 0x3f, 0x20, 0x40
};
static inline size_t checksum_get_position(char c)
{
return c < ' ' || c > '~' ? 0 : checksum_positions[(unsigned char)(c - ' ')];
}
static uint64_t poly_mod_descriptor_checksum(uint64_t c, int val)
{
uint8_t c0 = c >> 35;
c = ((c & 0x7ffffffff) << 5) ^ val;
if (c0 & 1) c ^= 0xf5dee51989;
if (c0 & 2) c ^= 0xa9fdca3312;
if (c0 & 4) c ^= 0x1bab10e32d;
if (c0 & 8) c ^= 0x3706b1677a;
if (c0 & 16) c ^= 0x644d626ffd;
return c;
}
static int generate_checksum(const char *str, size_t str_len, char *checksum_out)
{
uint64_t c = 1;
int cls = 0;
int clscount = 0;
size_t pos;
size_t i;
for (i = 0; i < str_len; ++i) {
if ((pos = checksum_get_position(str[i])) == 0)
return WALLY_EINVAL; /* Invalid character */
--pos;
/* Emit a symbol for the position inside the group, for every character. */
c = poly_mod_descriptor_checksum(c, pos & 31);
/* Accumulate the group numbers */
cls = cls * 3 + (int)(pos >> 5);
if (++clscount == 3) {
/* Emit an extra symbol representing the group numbers, for every 3 characters. */
c = poly_mod_descriptor_checksum(c, cls);
cls = 0;
clscount = 0;
}
}
if (clscount > 0)
c = poly_mod_descriptor_checksum(c, cls);
for (i = 0; i < DESCRIPTOR_CHECKSUM_LENGTH; ++i)
c = poly_mod_descriptor_checksum(c, 0);
c ^= 1;
for (i = 0; i < DESCRIPTOR_CHECKSUM_LENGTH; ++i)
checksum_out[i] = checksum_charset[(c >> (5 * (7 - i))) & 31];
checksum_out[DESCRIPTOR_CHECKSUM_LENGTH] = '\0';
return WALLY_OK;
}
typedef bool (*is_identifer_fn)(char c);
static bool is_identifer_char(char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
}
static bool is_policy_start_char(char c) { return c == '@'; }
static bool is_policy_identifer_char(char c) { return c >= '0' && c <= '9'; }
static int canonicalize(const char *descriptor,
const struct wally_map *vars_in, uint32_t flags,
char **output, size_t *num_substitutions)
{
const size_t VAR_MAX_NAME_LEN = 16;
is_identifer_fn is_id_start = is_identifer_char, is_id_char = is_identifer_char;
size_t required_len = 0;
int key_index_hwm = -1;
const char *p = descriptor, *start;
char *out;
bool found_policy_single = false, found_policy_multi = false;;
*output = NULL;
*num_substitutions = 0;
if (!descriptor || (flags & ~MS_FLAGS_CANONICALIZE))
return WALLY_EINVAL;
if (flags & WALLY_MINISCRIPT_POLICY_TEMPLATE) {
if (!is_valid_policy_map(vars_in))
return WALLY_EINVAL; /* Invalid policy variables given */
is_id_start = is_policy_start_char;
is_id_char = is_policy_identifer_char;
}
/* First, find the length of the canonicalized descriptor */
while (*p && *p != '#') {
while (*p && *p != '#' && !is_id_start(*p)) {
++required_len;
++p;
}
if (!is_id_start(*p))
break;
start = p++;
while (is_id_char(*p))
++p;
if (p != start) {
const bool starts_with_digit = *start >= '0' && *start <= '9';
const size_t lookup_len = p - start;
if (!vars_in || lookup_len > VAR_MAX_NAME_LEN || starts_with_digit) {
required_len += lookup_len; /* Too long/wrong format for an identifier */
} else {
/* Lookup the potential identifier */
const struct wally_map_item *item;
item = wally_map_get(vars_in, (unsigned char*)start, lookup_len);
if (!item) {
required_len += lookup_len;
continue;
}
required_len += item->value_len;
++*num_substitutions;
if (flags & WALLY_MINISCRIPT_POLICY_TEMPLATE) {
int key_index = (int)(item - vars_in->items);
if (key_index > key_index_hwm + 1)
return WALLY_EINVAL; /* Must be ordered with no gaps */
if (key_index > key_index_hwm)
key_index_hwm = key_index;
if (*p++ != '/')
return WALLY_EINVAL;
++required_len;
if (*p == '<') {
found_policy_multi = true;
continue;
}
if (*p++ != '*')
return WALLY_EINVAL;
if (*p == '*') {
found_policy_multi = true;
++p;
required_len += strlen("<0;1>/*");
} else {
found_policy_single = true;
required_len += 1;
}
}
}
}
}
if (!*p && (flags & WALLY_MINISCRIPT_REQUIRE_CHECKSUM))
return WALLY_EINVAL; /* Checksum required but not present */
if (flags & WALLY_MINISCRIPT_POLICY_TEMPLATE) {
if (found_policy_single && found_policy_multi)
return WALLY_EINVAL; /* Cannot mix cardinality of policy keys */
if (key_index_hwm == -1 || key_index_hwm != (int)vars_in->num_items - 1)
return WALLY_EINVAL; /* One or more keys wasn't substituted */
}
if (!(*output = wally_malloc(required_len + 1 + DESCRIPTOR_CHECKSUM_LENGTH + 1)))
return WALLY_ENOMEM;
p = descriptor;
out = *output;
while (*p && *p != '#') {
while (*p && *p != '#' && !is_id_start(*p)) {
*out++ = *p++;
}
if (!is_id_start(*p))
break;
start = p++;
while (is_id_char(*p))
++p;
if (p != start) {
const bool is_number = *start >= '0' && *start <= '9';
size_t lookup_len = p - start;
if (!vars_in || lookup_len > VAR_MAX_NAME_LEN || is_number)
memcpy(out, start, lookup_len);
else {
/* Lookup the potential identifier */
const struct wally_map_item *item;
item = wally_map_get(vars_in, (unsigned char*)start, lookup_len);
lookup_len = item ? item->value_len : lookup_len;
memcpy(out, item ? (char *)item->value : start, lookup_len);
if (item && flags & WALLY_MINISCRIPT_POLICY_TEMPLATE) {
if (p[1] == '*' && p[2] == '*') {
out += lookup_len;
lookup_len = strlen("/<0;1>/*");
memcpy(out, "/<0;1>/*", lookup_len);
p += strlen("/**");
}
}
}
out += lookup_len;
}
}
*out++ = '#';
out[DESCRIPTOR_CHECKSUM_LENGTH] = '\0';
if (generate_checksum(*output, required_len, out) != WALLY_OK ||
(*p == '#' && strcmp(p + 1, out))) {
/* Invalid character in input or failed to match passed in checksum */
clear_and_free(*output, required_len + 1 + DESCRIPTOR_CHECKSUM_LENGTH + 1);
*output = NULL;
return WALLY_EINVAL;
}
return WALLY_OK;
}
static uint32_t node_get_child_count(const ms_node *node)
{
int32_t ret = 0;
const ms_node *child;
for (child = node->child; child; child = child->next)
++ret;
return ret;
}
static bool node_has_uncompressed_key(const ms_ctx *ctx, const ms_node *node)
{
if (ctx->features & WALLY_MS_IS_UNCOMPRESSED) {
const ms_node *child;
for (child = node->child; child; child = child->next)
if ((child->flags & WALLY_MS_IS_UNCOMPRESSED) || node_has_uncompressed_key(ctx, child))
return true;
}
return false;
}
static bool node_is_root(const ms_node *node)
{
/* True if this is a (possibly temporary) top level node, or an argument of a builtin */
return !node->parent || node->parent->builtin;
}
static void node_free(ms_node *node)
{
if (node) {
ms_node *child = node->child;
while (child) {
ms_node *next = child->next;
node_free(child);
child = next;
}
if (node->kind & (KIND_RAW | KIND_ADDRESS) || node->kind == KIND_PUBLIC_KEY || node->kind == KIND_PRIVATE_KEY)
clear_and_free((void*)node->data, node->data_len);
clear_and_free(node, sizeof(*node));
}
}
static bool has_two_different_lock_states(uint32_t primary, uint32_t secondary)
{
return ((primary & PROP_G) && (secondary & PROP_H)) ||
((primary & PROP_H) && (secondary & PROP_G)) ||
((primary & PROP_I) && (secondary & PROP_J)) ||
((primary & PROP_J) && (secondary & PROP_I));
}
int wally_descriptor_free(ms_ctx *ctx)
{
if (ctx) {
/* Just clear the item storage, the actual items are owned by
* the tree of nodes */
clear_and_free(ctx->keys.items,
ctx->keys.num_items * sizeof(*ctx->keys.items));
wally_free_string(ctx->src);
node_free(ctx->top_node);
clear_and_free(ctx, sizeof(*ctx));
}
return WALLY_OK;
}
static int verify_sh(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
if (node->parent || !node->child->builtin)
return WALLY_EINVAL;
node->type_properties = node->child->type_properties;
return WALLY_OK;
}
static int verify_wsh(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
if (node->parent && node->parent->kind != KIND_DESCRIPTOR_SH)
return WALLY_EINVAL;
if (!node->child->builtin || node_has_uncompressed_key(ctx, node))
return WALLY_EINVAL;
node->type_properties = node->child->type_properties;
return WALLY_OK;
}
static int verify_pk(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
if (node->child->builtin || !(node->child->kind & KIND_KEY))
return WALLY_EINVAL;
if (node->parent && node_has_uncompressed_key(ctx, node) &&
node->parent->kind != KIND_DESCRIPTOR_SH &&
node->parent->kind != KIND_DESCRIPTOR_WSH)
return WALLY_EINVAL;
node->type_properties = builtin_get(node)->type_properties;
return WALLY_OK;
}
static int verify_wpkh(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
ms_node *parent = node->parent;
if (parent && (!parent->builtin || parent->kind & KIND_MINISCRIPT))
return WALLY_EINVAL;
if (node->child->builtin || !(node->child->kind & KIND_KEY))
return WALLY_EINVAL;
for (/* no-op */; parent; parent = parent->parent)
if (parent->kind == KIND_DESCRIPTOR_WSH)
return WALLY_EINVAL;
return node_has_uncompressed_key(ctx, node) ? WALLY_EINVAL : WALLY_OK;
}
static int verify_combo(ms_ctx *ctx, ms_node *node)
{
const bool has_uncompressed_key = node_has_uncompressed_key(ctx, node);
int ret;
if (node->parent)
return WALLY_EINVAL;
if (has_uncompressed_key) {
ctx->num_variants = 2; /* p2pk and p2pkh */
} else {
ctx->num_variants = 4; /* p2pk, p2pkh, p2wpkh and p2sh-p2wpkh */
}
ret = verify_pk(ctx, node);
/* pkh is the same verification as pk, so skipped */
if (ret == WALLY_OK && !has_uncompressed_key) {
ret = verify_wpkh(ctx, node);
/* p2sh, i.e. p2sh-wpkh is valid if pk and wpkh are */
}
/* Take our properties from the combo builtin; this means
* you can't really say anything about combo validity.
*/
node->type_properties = builtin_get(node)->type_properties;
return ret;
}
static int verify_multi(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
const int64_t count = node_get_child_count(node);
ms_node *top, *key;
if (count < 2 || count - 1 > MINISCRIPT_MULTI_MAX)
return WALLY_EINVAL;
top = node->child;
if (!top->next || top->builtin || top->kind != KIND_NUMBER ||
top->number <= 0 || count < top->number)
return WALLY_EINVAL;
key = top->next;
while (key) {
if (key->builtin || !(key->kind & KIND_KEY))
return WALLY_EINVAL;
key = key->next;
}
node->type_properties = builtin_get(node)->type_properties;
return WALLY_OK;
}
static int verify_addr(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
if (node->parent || node->child->builtin || !(node->child->kind & KIND_ADDRESS))
return WALLY_EINVAL;
return WALLY_OK;
}
static int verify_raw(ms_ctx *ctx, ms_node *node)
{
const uint32_t child_count = node_get_child_count(node);
(void)ctx;
if (node->parent || child_count > 1)
return WALLY_EINVAL;
if (child_count && (node->child->builtin || !(node->child->kind & KIND_RAW)))
return WALLY_EINVAL;
return WALLY_OK;
}
static int verify_delay(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
if (node->child->builtin || node->child->kind != KIND_NUMBER ||
node->child->number <= 0 || node->child->number > 0x7fffffff)
return WALLY_EINVAL;
node->type_properties = builtin_get(node)->type_properties;
if (builtin_get(node)->kind == KIND_MINISCRIPT_OLDER) {
if (node->child->number & DESCRIPTOR_SEQUENCE_LOCKTIME_TYPE_FLAG)
node->type_properties |= PROP_G;
else
node->type_properties |= PROP_H;
} else {
/* KIND_MINISCRIPT_AFTER */
if (node->child->number >= DESCRIPTOR_LOCKTIME_THRESHOLD)
node->type_properties |= PROP_I;
else
node->type_properties |= PROP_J;
}
return WALLY_OK;
}
static int verify_hash_type(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
if (node->child->builtin || !(node->child->kind & KIND_RAW))
return WALLY_EINVAL;
node->type_properties = builtin_get(node)->type_properties;
return WALLY_OK;
}
static uint32_t verify_andor_property(uint32_t x_prop, uint32_t y_prop, uint32_t z_prop)
{
/* Y and Z are both B, K, or V */
uint32_t prop = PROP_X;
uint32_t need_x = TYPE_B | PROP_D | PROP_U;
uint32_t need_yz = TYPE_B | TYPE_K | TYPE_V;
if (!(x_prop & TYPE_B) || !(x_prop & need_x))
return 0;
if (!(y_prop & z_prop & need_yz))
return 0;
prop |= y_prop & z_prop & need_yz;
prop |= x_prop & y_prop & z_prop & PROP_Z;
prop |= (x_prop | (y_prop & z_prop)) & PROP_O;
prop |= y_prop & z_prop & PROP_U;
prop |= z_prop & PROP_D;
prop |= (x_prop | y_prop | z_prop) & (PROP_G | PROP_H | PROP_I | PROP_J);
if (x_prop & PROP_S || y_prop & PROP_F) {
prop |= z_prop & PROP_F;
prop |= x_prop & z_prop & PROP_E;
}
if (x_prop & PROP_E &&
(x_prop | y_prop | z_prop) & PROP_S) {
prop |= x_prop & y_prop & z_prop & PROP_M;
}
prop |= z_prop & (x_prop | y_prop) & PROP_S;
if ((x_prop & y_prop & z_prop & PROP_K) &&
!has_two_different_lock_states(x_prop, y_prop))
prop |= PROP_K;
return prop;
}
static int verify_andor(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
node->type_properties = verify_andor_property(node->child->type_properties,
node->child->next->type_properties,
node->child->next->next->type_properties);
return node->type_properties ? WALLY_OK : WALLY_EINVAL;
}
static uint32_t verify_and_v_property(uint32_t x_prop, uint32_t y_prop)
{
uint32_t prop = 0;
prop |= x_prop & PROP_N;
prop |= y_prop & (PROP_U | PROP_X);
prop |= x_prop & y_prop & (PROP_D | PROP_M | PROP_Z);
prop |= (x_prop | y_prop) & PROP_S;
prop |= (x_prop | y_prop) & (PROP_G | PROP_H | PROP_I | PROP_J);
if (x_prop & TYPE_V)
prop |= y_prop & (TYPE_K | TYPE_V | TYPE_B);
if (x_prop & PROP_Z)
prop |= y_prop & PROP_N;
if ((x_prop | y_prop) & PROP_Z)
prop |= (x_prop | y_prop) & PROP_O;
if (y_prop & PROP_F || x_prop & PROP_S)
prop |= PROP_F;
if ((x_prop & y_prop & PROP_K) &&
!has_two_different_lock_states(x_prop, y_prop))
prop |= PROP_K;
return prop & TYPE_MASK ? prop : 0;
}
static int verify_and_v(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
node->type_properties = verify_and_v_property(
node->child->type_properties,
node->child->next->type_properties);
return node->type_properties ? WALLY_OK : WALLY_EINVAL;
}
static int verify_and_b(ms_ctx *ctx, ms_node *node)
{
const uint32_t x_prop = node->child->type_properties;
const uint32_t y_prop = node->child->next->type_properties;
(void)ctx;
node->type_properties = PROP_U | PROP_X;
node->type_properties |= x_prop & y_prop & (PROP_D | PROP_Z | PROP_M);
node->type_properties |= (x_prop | y_prop) & PROP_S;
node->type_properties |= (x_prop | y_prop) & (PROP_G | PROP_H | PROP_I | PROP_J);
node->type_properties |= x_prop & PROP_N;
if (y_prop & TYPE_W)
node->type_properties |= x_prop & TYPE_B;
if ((x_prop | y_prop) & PROP_Z)
node->type_properties |= (x_prop | y_prop) & PROP_O;
if (x_prop & PROP_Z)
node->type_properties |= y_prop & PROP_N;
if ((x_prop & y_prop) & PROP_S)
node->type_properties |= x_prop & y_prop & PROP_E;
if (((x_prop & y_prop) & PROP_F) ||
!(~x_prop & (PROP_S | PROP_F)) ||
!(~y_prop & (PROP_S | PROP_F)))
node->type_properties |= PROP_F;
if ((x_prop & y_prop & PROP_K) &&
!has_two_different_lock_states(x_prop, y_prop))
node->type_properties |= PROP_K;
return WALLY_OK;
}
static int verify_and_n(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
node->type_properties = verify_andor_property(node->child->type_properties,
node->child->next->type_properties,
PROP_OP_0);
return node->type_properties ? WALLY_OK : WALLY_EINVAL;
}
static int verify_or_b(ms_ctx *ctx, ms_node *node)
{
const uint32_t x_prop = node->child->type_properties;
const uint32_t y_prop = node->child->next->type_properties;
(void)ctx;
node->type_properties = PROP_D | PROP_U | PROP_X;
node->type_properties |= x_prop & y_prop & (PROP_Z | PROP_S | PROP_E);
node->type_properties |= (x_prop | y_prop) & (PROP_G | PROP_H | PROP_I | PROP_J);
node->type_properties |= (x_prop & y_prop) & PROP_K;
if (!(~x_prop & (TYPE_B | PROP_D)) &&
!(~y_prop & (TYPE_W | PROP_D)))
node->type_properties |= TYPE_B;
if ((x_prop | y_prop) & PROP_Z)
node->type_properties |= (x_prop | y_prop) & PROP_O;
if (((x_prop | y_prop) & PROP_S) &&
((x_prop & y_prop) & PROP_E))
node->type_properties |= x_prop & y_prop & PROP_M;
return WALLY_OK;
}
static int verify_or_c(ms_ctx *ctx, ms_node *node)
{
const uint32_t x_prop = node->child->type_properties;
const uint32_t y_prop = node->child->next->type_properties;
(void)ctx;
node->type_properties = PROP_F | PROP_X;
node->type_properties |= x_prop & y_prop & (PROP_Z | PROP_S);
node->type_properties |= (x_prop | y_prop) & (PROP_G | PROP_H | PROP_I | PROP_J);
node->type_properties |= (x_prop & y_prop) & PROP_K;
if (!(~x_prop & (TYPE_B | PROP_D | PROP_U)))
node->type_properties |= y_prop & TYPE_V;
if (y_prop & PROP_Z)
node->type_properties |= x_prop & PROP_O;
if (x_prop & PROP_E && ((x_prop | y_prop) & PROP_S))
node->type_properties |= x_prop & y_prop & PROP_M;
return WALLY_OK;
}
static int verify_or_d(ms_ctx *ctx, ms_node *node)
{
const uint32_t x_prop = node->child->type_properties;
const uint32_t y_prop = node->child->next->type_properties;
(void)ctx;
node->type_properties = PROP_X;
node->type_properties |= x_prop & y_prop & (PROP_Z | PROP_E | PROP_S);
node->type_properties |= y_prop & (PROP_U | PROP_F | PROP_D);
node->type_properties |= (x_prop | y_prop) & (PROP_G | PROP_H | PROP_I | PROP_J);
node->type_properties |= (x_prop & y_prop) & PROP_K;
if (!(~x_prop & (TYPE_B | PROP_D | PROP_U)))
node->type_properties |= y_prop & TYPE_B;
if (y_prop & PROP_Z)
node->type_properties |= x_prop & PROP_O;
if (x_prop & PROP_E && ((x_prop | y_prop) & PROP_S))
node->type_properties |= x_prop & y_prop & PROP_M;
return WALLY_OK;
}
static uint32_t verify_or_i_property(uint32_t x_prop, uint32_t y_prop)
{
uint32_t prop = PROP_X;
prop |= x_prop & y_prop & (TYPE_V | TYPE_B | TYPE_K | PROP_U | PROP_F | PROP_S);
prop |= (x_prop | y_prop) & (PROP_G | PROP_H | PROP_I | PROP_J);
prop |= (x_prop & y_prop) & PROP_K;
if (!(prop & TYPE_MASK))
return 0;
prop |= (x_prop | y_prop) & PROP_D;
if ((x_prop & y_prop) & PROP_Z)
prop |= PROP_O;
if ((x_prop | y_prop) & PROP_F)
prop |= (x_prop | y_prop) & PROP_E;
if ((x_prop | y_prop) & PROP_S)
prop |= x_prop & y_prop & PROP_M;
return prop;
}
static int verify_or_i(ms_ctx *ctx, ms_node *node)
{
(void)ctx;
node->type_properties = verify_or_i_property(node->child->type_properties,
node->child->next->type_properties);
return node->type_properties ? WALLY_OK : WALLY_EINVAL;
}
static int verify_thresh(ms_ctx *ctx, ms_node *node)
{
ms_node *top = node->child, *child;
int64_t count = 0, num_s = 0, args = 0;
uint32_t acc_tl = PROP_K, tmp_acc_tl;
bool all_e = true, all_m = true;
(void)ctx;
if (!top || top->builtin || top->kind != KIND_NUMBER)
return WALLY_EINVAL;
for (child = top->next; child; child = child->next) {
const uint32_t expected_type = count ? TYPE_W : TYPE_B;
if (!child->builtin || (~child->type_properties & (expected_type | PROP_D | PROP_U)))
return WALLY_EINVAL;
if (~child->type_properties & PROP_E)
all_e = false;
if (~child->type_properties & PROP_M)
all_m = false;
if (child->type_properties & PROP_S)
++num_s;
if (child->type_properties & PROP_Z)
args += (~child->type_properties & PROP_O) ? 2 : 1;
tmp_acc_tl = ((acc_tl | child->type_properties) & (PROP_G | PROP_H | PROP_I | PROP_J));
if ((acc_tl & child->type_properties) & PROP_K) {
if (top->number <= 1 || (top->number > 1 &&
!has_two_different_lock_states(acc_tl, child->type_properties)))
tmp_acc_tl |= PROP_K;
}
acc_tl = tmp_acc_tl;
++count;
}
if (top->number < 1 || top->number > count)
return WALLY_EINVAL;
node->type_properties = TYPE_B | PROP_D | PROP_U;
if (args == 0)
node->type_properties |= PROP_Z;
else if (args == 1)
node->type_properties |= PROP_O;
if (all_e && num_s == count)
node->type_properties |= PROP_E;
if (all_e && all_m && num_s >= count - top->number)
node->type_properties |= PROP_M;
if (num_s >= count - top->number + 1)
node->type_properties |= PROP_S;
node->type_properties |= acc_tl;
return WALLY_OK;
}
static int node_verify_wrappers(ms_node *node)
{
uint32_t *properties = &node->type_properties;
size_t i;
if (node->wrapper_str[0] == '\0')
return WALLY_OK; /* No wrappers */
/* Validate the nodes wrappers in reverse order */
for (i = strlen(node->wrapper_str); i != 0; --i) {
const uint32_t x_prop = *properties;
#define PROP_REQUIRE(props) if ((x_prop & (props)) != (props)) return WALLY_EINVAL
#define PROP_CHANGE_TYPE(clr, set) *properties &= ~(clr); *properties |= set
#define PROP_CHANGE(keep, set) *properties &= (TYPE_MASK | keep); *properties |= set
switch(node->wrapper_str[i - 1]) {
case 'a':
PROP_REQUIRE(TYPE_B);