Skip to content

Commit aa6a8a2

Browse files
committed
descriptor: support forcing a key to be generated as x-only
This is to support rawtr() which can be given either an x-only or non-x-only key.
1 parent 90dd9df commit aa6a8a2

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

src/descriptor.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,8 +1112,9 @@ static int generate_number(int64_t number, ms_node *parent,
11121112
return WALLY_OK;
11131113
}
11141114

1115-
static int generate_pk_k(ms_ctx *ctx, ms_node *node,
1116-
unsigned char *script, size_t script_len, size_t *written)
1115+
static int generate_pk_k_impl(ms_ctx *ctx, ms_node *node,
1116+
unsigned char *script, size_t script_len,
1117+
bool force_xonly, size_t *written)
11171118
{
11181119
unsigned char buff[EC_PUBLIC_KEY_UNCOMPRESSED_LEN];
11191120
int ret;
@@ -1126,15 +1127,30 @@ static int generate_pk_k(ms_ctx *ctx, ms_node *node,
11261127
if (*written != EC_PUBLIC_KEY_LEN && *written != EC_XONLY_PUBLIC_KEY_LEN &&
11271128
*written != EC_PUBLIC_KEY_UNCOMPRESSED_LEN)
11281129
return WALLY_EINVAL; /* Invalid pubkey length */
1130+
if (force_xonly) {
1131+
if (*written == EC_PUBLIC_KEY_UNCOMPRESSED_LEN)
1132+
return WALLY_EINVAL; /* Can't make x-only from uncompressed key */
1133+
if (*written == EC_XONLY_PUBLIC_KEY_LEN)
1134+
force_xonly = false; /* Already x-only */
1135+
else
1136+
*written -= 1; /* Account for stripping the lead byte below */
1137+
}
11291138
if (*written <= script_len) {
11301139
script[0] = *written & 0xff; /* push opcode */
1131-
memcpy(script + 1, buff, *written);
1140+
memcpy(script + 1, buff + (force_xonly ? 1 : 0), *written);
11321141
}
11331142
*written += 1;
11341143
}
11351144
return ret;
11361145
}
11371146

1147+
static int generate_pk_k(ms_ctx *ctx, ms_node *node,
1148+
unsigned char *script, size_t script_len, size_t *written)
1149+
{
1150+
const bool force_xonly = false;
1151+
return generate_pk_k_impl(ctx, node, script, script_len, force_xonly, written);
1152+
}
1153+
11381154
static int generate_pk_h(ms_ctx *ctx, ms_node *node,
11391155
unsigned char *script, size_t script_len, size_t *written)
11401156
{

0 commit comments

Comments
 (0)