|
87 | 87 | #define KIND_DESCRIPTOR_ADDR (0x00040000 | KIND_DESCRIPTOR) |
88 | 88 | #define KIND_DESCRIPTOR_RAW (0x00050000 | KIND_DESCRIPTOR) |
89 | 89 | #define KIND_DESCRIPTOR_RAW_TR (0x00100000 | KIND_DESCRIPTOR) |
| 90 | +#define KIND_DESCRIPTOR_TR (0x00200000 | KIND_DESCRIPTOR) |
90 | 91 |
|
91 | 92 | /* miniscript */ |
92 | 93 | #define KIND_MINISCRIPT_PK (0x00000100 | KIND_MINISCRIPT) |
@@ -702,6 +703,18 @@ static int verify_raw_tr(ms_ctx *ctx, ms_node *node) |
702 | 703 | return WALLY_OK; |
703 | 704 | } |
704 | 705 |
|
| 706 | +static int verify_tr(ms_ctx *ctx, ms_node *node) |
| 707 | +{ |
| 708 | + const uint32_t child_count = node_get_child_count(node); |
| 709 | + if (child_count != 1u) |
| 710 | + return WALLY_EINVAL; /* FIXME: Support script paths */ |
| 711 | + if (node->child->builtin || !(node->child->kind & KIND_KEY) || |
| 712 | + node_has_uncompressed_key(ctx, node)) |
| 713 | + return WALLY_EINVAL; |
| 714 | + node->type_properties = builtin_get(node)->type_properties; |
| 715 | + return WALLY_OK; |
| 716 | +} |
| 717 | + |
705 | 718 | static int verify_delay(ms_ctx *ctx, ms_node *node) |
706 | 719 | { |
707 | 720 | (void)ctx; |
@@ -1404,6 +1417,35 @@ static int generate_raw_tr(ms_ctx *ctx, ms_node *node, |
1404 | 1417 | return ret; |
1405 | 1418 | } |
1406 | 1419 |
|
| 1420 | +static int generate_tr(ms_ctx *ctx, ms_node *node, |
| 1421 | + unsigned char *script, size_t script_len, size_t *written) |
| 1422 | +{ |
| 1423 | + unsigned char tweaked[EC_PUBLIC_KEY_LEN]; |
| 1424 | + unsigned char pubkey[EC_PUBLIC_KEY_UNCOMPRESSED_LEN + 1]; |
| 1425 | + size_t pubkey_len; |
| 1426 | + int ret; |
| 1427 | + |
| 1428 | + /* Generate a push of the x-only public key of our child */ |
| 1429 | + const bool force_xonly = true; |
| 1430 | + ret = generate_pk_k_impl(ctx, node, pubkey, sizeof(pubkey), force_xonly, &pubkey_len); |
| 1431 | + if (pubkey_len != EC_XONLY_PUBLIC_KEY_LEN + 1) |
| 1432 | + return WALLY_EINVAL; /* Should be PUSH_32 [x-only pubkey] */ |
| 1433 | + |
| 1434 | + /* Tweak it into a compressed pubkey */ |
| 1435 | + ret = wally_ec_public_key_bip341_tweak(pubkey + 1, pubkey_len - 1, |
| 1436 | + NULL, 0, 0, /* FIXME: Support script path */ |
| 1437 | + tweaked, sizeof(tweaked)); |
| 1438 | + |
| 1439 | + if (ret == WALLY_OK && script_len >= WALLY_SCRIPTPUBKEY_P2TR_LEN) { |
| 1440 | + /* Generate the script using the x-only part of the tweaked key */ |
| 1441 | + script[0] = OP_1; |
| 1442 | + script[1] = sizeof(tweaked) - 1; |
| 1443 | + memcpy(script + 2, tweaked + 1, sizeof(tweaked) - 1); |
| 1444 | + } |
| 1445 | + *written = WALLY_SCRIPTPUBKEY_P2TR_LEN; |
| 1446 | + return ret; |
| 1447 | +} |
| 1448 | + |
1407 | 1449 | static int generate_delay(ms_ctx *ctx, ms_node *node, |
1408 | 1450 | unsigned char *script, size_t script_len, size_t *written) |
1409 | 1451 | { |
@@ -1817,6 +1859,11 @@ static const struct ms_builtin_t g_builtins[] = { |
1817 | 1859 | KIND_DESCRIPTOR_RAW_TR, |
1818 | 1860 | TYPE_NONE, |
1819 | 1861 | 1, verify_raw_tr, generate_raw_tr |
| 1862 | + }, { |
| 1863 | + I_NAME("tr"), |
| 1864 | + KIND_DESCRIPTOR_TR, |
| 1865 | + TYPE_NONE, |
| 1866 | + 0xffffffff, verify_tr, generate_tr |
1820 | 1867 | }, |
1821 | 1868 | /* miniscript */ |
1822 | 1869 | { |
@@ -2087,7 +2134,9 @@ static int analyze_pubkey_hex(ms_ctx *ctx, ms_node *node, |
2087 | 2134 | wally_ec_xonly_public_key_verify(pubkey, pubkey_len) != WALLY_OK) |
2088 | 2135 | return WALLY_OK; /* Not a valid pubkey */ |
2089 | 2136 |
|
2090 | | - make_xonly = node->parent && (node->parent->kind == KIND_DESCRIPTOR_RAW_TR); |
| 2137 | + make_xonly = node->parent && |
| 2138 | + (node->parent->kind == KIND_DESCRIPTOR_RAW_TR || |
| 2139 | + node->parent->kind == KIND_DESCRIPTOR_TR); |
2091 | 2140 | allow_xonly = make_xonly || flags & WALLY_MINISCRIPT_TAPSCRIPT; |
2092 | 2141 | if (pubkey_len == EC_PUBLIC_KEY_UNCOMPRESSED_LEN && allow_xonly) |
2093 | 2142 | return WALLY_OK; /* Uncompressed key not allowed here */ |
@@ -2502,6 +2551,7 @@ static int node_generation_size(const ms_node *node, size_t *total) |
2502 | 2551 | /* No-op */ |
2503 | 2552 | break; |
2504 | 2553 | case KIND_DESCRIPTOR_RAW_TR: |
| 2554 | + case KIND_DESCRIPTOR_TR: |
2505 | 2555 | *total += WALLY_SCRIPTPUBKEY_P2TR_LEN; |
2506 | 2556 | break; |
2507 | 2557 | case KIND_MINISCRIPT_PK_K: |
|
0 commit comments