Skip to content

Commit

Permalink
Rename "key placeholder" with "key expression" where appropriate; add…
Browse files Browse the repository at this point in the history
…ed some comments.

Generalizing to key expressions containing musig() makes it necessary to distinguish
the key expressions in the wallet policy from the actual key placeholders that are
just indexes to the list of key informations (@num in the descriptor template),
whereas the two concepts were often not clearly separated in the code base.

Renaming to "key expressions" makes the distinction more clear.
  • Loading branch information
bigspider committed Feb 23, 2024
1 parent 25c59c6 commit 852355b
Show file tree
Hide file tree
Showing 6 changed files with 333 additions and 363 deletions.
38 changes: 19 additions & 19 deletions src/common/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ static int parse_keyexpr(buffer_t *in_buf,
buffer_t *out_buf) {
char c;
if (!buffer_read_u8(in_buf, (uint8_t *) &c)) {
return WITH_ERROR(-1, "Expected key placeholder");
return WITH_ERROR(-1, "Expected key expression");
}

if (c == '@') {
Expand Down Expand Up @@ -547,12 +547,12 @@ static int parse_keyexpr(buffer_t *in_buf,
|| !buffer_peek(in_buf, &next_character) // we must be able to read the next character
|| !(next_character == '*' || next_character == '<') // and it must be '*' or '<'
) {
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key placeholder");
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key expression");
}

if (next_character == '*') {
if (!consume_characters(in_buf, "**", 2)) {
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key placeholder");
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key expression");
}
out->num_first = 0;
out->num_second = 1;
Expand All @@ -562,26 +562,26 @@ static int parse_keyexpr(buffer_t *in_buf,
out->num_first > 0x80000000u) {
return WITH_ERROR(
-1,
"Expected /** or /<M;N>/* in key placeholder, with unhardened M and N");
"Expected /** or /<M;N>/* in key expression, with unhardened M and N");
}

if (!consume_character(in_buf, ';')) {
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key placeholder");
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key expression");
}

if (parse_unsigned_decimal(in_buf, &out->num_second) == -1 ||
out->num_second > 0x80000000u) {
return WITH_ERROR(
-1,
"Expected /** or /<M;N>/* in key placeholder, with unhardened M and N");
"Expected /** or /<M;N>/* in key expression, with unhardened M and N");
}

if (out->num_first == out->num_second) {
return WITH_ERROR(-1, "M and N must be different in <M;N>/*");
}

if (!consume_characters(in_buf, ">/*", 3)) {
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key placeholder");
return WITH_ERROR(-1, "Expected /** or /<M;N>/* in key expression");
}
}
} else {
Expand Down Expand Up @@ -1477,13 +1477,13 @@ static int parse_script(buffer_t *in_buf,
return WITH_ERROR(-1, "Out of memory");
}

policy_node_keyexpr_t *key_placeholder =
policy_node_keyexpr_t *key_expr =
buffer_alloc(out_buf, sizeof(policy_node_keyexpr_t), true);

if (key_placeholder == NULL) {
if (key_expr == NULL) {
return WITH_ERROR(-1, "Out of memory");
}
i_policy_node_keyexpr(&node->key_placeholder, key_placeholder);
i_policy_node_keyexpr(&node->key, key_expr);

if (token == TOKEN_WPKH) {
if (depth > 0 && ((context_flags & CONTEXT_WITHIN_SH) == 0)) {
Expand All @@ -1496,7 +1496,7 @@ static int parse_script(buffer_t *in_buf,
node->base.type = token;

bool is_taproot = (context_flags & CONTEXT_WITHIN_TR) != 0;
if (0 > parse_keyexpr(in_buf, version, key_placeholder, is_taproot, out_buf)) {
if (0 > parse_keyexpr(in_buf, version, key_expr, is_taproot, out_buf)) {
return WITH_ERROR(-1, "Couldn't parse key placeholder");
}

Expand Down Expand Up @@ -1559,14 +1559,14 @@ static int parse_script(buffer_t *in_buf,
return WITH_ERROR(-1, "Out of memory");
}

policy_node_keyexpr_t *key_placeholder =
policy_node_keyexpr_t *key_expr =
buffer_alloc(out_buf, sizeof(policy_node_keyexpr_t), true);
if (key_placeholder == NULL) {
if (key_expr == NULL) {
return WITH_ERROR(-1, "Out of memory");
}
i_policy_node_keyexpr(&node->key_placeholder, key_placeholder);
i_policy_node_keyexpr(&node->key, key_expr);

if (0 > parse_keyexpr(in_buf, version, key_placeholder, true, out_buf)) {
if (0 > parse_keyexpr(in_buf, version, key_expr, true, out_buf)) {
return WITH_ERROR(-1, "Couldn't parse key placeholder");
}

Expand Down Expand Up @@ -1682,7 +1682,7 @@ static int parse_script(buffer_t *in_buf,
// We allocate the array of key indices at the current position in the output buffer
// (on success)
buffer_alloc(out_buf, 0, true); // ensure alignment of current pointer
i_policy_node_keyexpr(&node->key_placeholders, buffer_get_cur(out_buf));
i_policy_node_keyexpr(&node->keys, buffer_get_cur(out_buf));

node->n = 0;
while (true) {
Expand All @@ -1697,16 +1697,16 @@ static int parse_script(buffer_t *in_buf,
return WITH_ERROR(-1, "Expected ','");
}

policy_node_keyexpr_t *key_placeholder = (policy_node_keyexpr_t *) buffer_alloc(
policy_node_keyexpr_t *key_expr = (policy_node_keyexpr_t *) buffer_alloc(
out_buf,
sizeof(policy_node_keyexpr_t),
true); // we align this pointer, as there's padding in an array of
// structures
if (key_placeholder == NULL) {
if (key_expr == NULL) {
return WITH_ERROR(-1, "Out of memory");
}

if (0 > parse_keyexpr(in_buf, version, key_placeholder, is_taproot, out_buf)) {
if (0 > parse_keyexpr(in_buf, version, key_expr, is_taproot, out_buf)) {
return WITH_ERROR(-1, "Error parsing key placeholder");
}

Expand Down
40 changes: 20 additions & 20 deletions src/common/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,19 +297,6 @@ typedef struct policy_node_ext_info_s {
unsigned int x : 1; // the last opcode is not EQUAL, CHECKSIG, or CHECKMULTISIG
} policy_node_ext_info_t;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcomment"
// The compiler doesn't like /** inside a block comment, so we disable this warning temporarily.

/** Structure representing a key placeholder.
* In V1, it's the index of a key expression in the key informations array, which includes the final
* / ** step.
* In V2, it's the index of a key expression in the key informations array, plus the two
* numbers a, b in the /<NUM_a;NUM_b>/* derivation steps; here, the xpubs in the key informations
* array don't have extra derivation steps.
*/
#pragma GCC diagnostic pop

DEFINE_REL_PTR(uint16, uint16_t)

typedef struct {
Expand All @@ -320,10 +307,23 @@ typedef struct {
DEFINE_REL_PTR(musig_aggr_key_info, musig_aggr_key_info_t)

typedef enum {
KEY_EXPRESSION_NORMAL = 0, // a key expression with a single key placeholder
KEY_EXPRESSION_NORMAL = 0, // a key expression with a single key
KEY_EXPRESSION_MUSIG = 1 // a key expression containing a musig()
} KeyExpressionType;

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcomment"
// The compiler doesn't like /** inside a block comment, so we disable this warning temporarily.

/** Structure representing a key expression.
* In V1, it's the index of a key expression in the key informations array, which includes the final
* / ** step.
* In V2, it's the index of a key expression in the key informations array, plus the two
* numbers a, b in the /<NUM_a;NUM_b>/* derivation steps; here, the xpubs in the key informations
* array don't have extra derivation steps.
* In V2, musig() key expressions are also represented in this struct.
*/
#pragma GCC diagnostic pop
// 12 bytes
typedef struct {
// the following fields are only used in V2
Expand Down Expand Up @@ -370,7 +370,7 @@ typedef policy_node_with_script3_t policy_node_with_scripts_t;
// 4 bytes
typedef struct {
struct policy_node_s base;
rptr_policy_node_keyexpr_t key_placeholder;
rptr_policy_node_keyexpr_t key;
} policy_node_with_key_t;

// 8 bytes
Expand All @@ -381,10 +381,10 @@ typedef struct {

// 12 bytes
typedef struct {
struct policy_node_s base; // type is TOKEN_MULTI or TOKEN_SORTEDMULTI
int16_t k; // threshold
int16_t n; // number of keys
rptr_policy_node_keyexpr_t key_placeholders; // pointer to array of exactly n key placeholders
struct policy_node_s base; // type is TOKEN_MULTI or TOKEN_SORTEDMULTI
int16_t k; // threshold
int16_t n; // number of keys
rptr_policy_node_keyexpr_t keys; // pointer to array of exactly n key expressions
} policy_node_multisig_t;

// 8 bytes
Expand Down Expand Up @@ -434,7 +434,7 @@ typedef struct policy_node_tree_s {

typedef struct {
struct policy_node_s base;
rptr_policy_node_keyexpr_t key_placeholder;
rptr_policy_node_keyexpr_t key;
rptr_policy_node_tree_t tree; // NULL if tr(KP)
} policy_node_tr_t;

Expand Down
Loading

0 comments on commit 852355b

Please sign in to comment.