Skip to content

Commit f8a0a5e

Browse files
committed
descriptor: simplify analyze_pubkey_hex args before refactoring for taproot
1 parent 346be3e commit f8a0a5e

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/descriptor.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2022,35 +2022,40 @@ static int analyze_address(ms_ctx *ctx, const char *str, size_t str_len,
20222022
return ret;
20232023
}
20242024

2025-
static int analyze_pubkey_hex(ms_ctx *ctx, const char *str, size_t str_len,
2026-
uint32_t flags, ms_node *node, bool *is_hex)
2025+
/* take the possible hex data in node->data, if it is a valid pubkey
2026+
* convert it to an allocated binary buffer and make this node a key node
2027+
*/
2028+
static int analyze_pubkey_hex(ms_ctx *ctx, ms_node *node,
2029+
uint32_t flags, bool *is_hex)
20272030
{
20282031
unsigned char pubkey[EC_PUBLIC_KEY_UNCOMPRESSED_LEN + 1];
20292032
size_t offset = flags & WALLY_MINISCRIPT_TAPSCRIPT ? 1 : 0;
20302033
size_t written;
20312034

20322035
*is_hex = false;
20332036
if (offset) {
2034-
if (str_len != EC_XONLY_PUBLIC_KEY_LEN * 2)
2037+
if (node->data_len != EC_XONLY_PUBLIC_KEY_LEN * 2)
20352038
return WALLY_OK; /* Only X-only pubkeys allowed under tapscript */
20362039
pubkey[0] = 2; /* Non-X-only pubkey prefix, for validation below */
20372040
} else {
2038-
if (str_len != EC_PUBLIC_KEY_LEN * 2 && str_len != EC_PUBLIC_KEY_UNCOMPRESSED_LEN * 2)
2041+
if (node->data_len != EC_PUBLIC_KEY_LEN * 2 &&
2042+
node->data_len != EC_PUBLIC_KEY_UNCOMPRESSED_LEN * 2)
20392043
return WALLY_OK; /* Unknown public key size */
20402044
}
20412045

2042-
if (wally_hex_n_to_bytes(str, str_len, pubkey + offset, sizeof(pubkey) - offset, &written) != WALLY_OK ||
2046+
if (wally_hex_n_to_bytes(node->data, node->data_len,
2047+
pubkey + offset, sizeof(pubkey) - offset, &written) != WALLY_OK ||
20432048
wally_ec_public_key_verify(pubkey, written + offset) != WALLY_OK)
20442049
return WALLY_OK; /* Not hex, or not a pubkey */
20452050

20462051
if (!clone_bytes((unsigned char **)&node->data, pubkey + offset, written))
20472052
return WALLY_ENOMEM;
2048-
node->data_len = str_len / 2;
2049-
if (str_len == EC_PUBLIC_KEY_UNCOMPRESSED_LEN * 2) {
2053+
node->data_len = node->data_len / 2;
2054+
if (node->data_len == EC_PUBLIC_KEY_UNCOMPRESSED_LEN) {
20502055
node->flags |= WALLY_MS_IS_UNCOMPRESSED;
20512056
ctx->features |= WALLY_MS_IS_UNCOMPRESSED;
20522057
}
2053-
if (str_len == EC_XONLY_PUBLIC_KEY_LEN * 2) {
2058+
if (node->data_len == EC_XONLY_PUBLIC_KEY_LEN) {
20542059
node->flags |= WALLY_MS_IS_X_ONLY;
20552060
ctx->features |= WALLY_MS_IS_X_ONLY;
20562061
}
@@ -2108,7 +2113,7 @@ static int analyze_miniscript_key(ms_ctx *ctx, uint32_t flags,
21082113
}
21092114

21102115
/* check key (public key) */
2111-
ret = analyze_pubkey_hex(ctx, node->data, node->data_len, flags, node, &is_hex);
2116+
ret = analyze_pubkey_hex(ctx, node, flags, &is_hex);
21122117
if (ret == WALLY_OK && is_hex)
21132118
return WALLY_OK;
21142119

0 commit comments

Comments
 (0)