Skip to content

Commit 4560757

Browse files
cdeckerrustyrussell
authored andcommitted
jsonrpc: Add parsing for parameter of extra parameters
1 parent 473be0f commit 4560757

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

common/json_tok.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,44 @@ struct command_result *param_outpoint_arr(struct command *cmd,
543543
}
544544
return NULL;
545545
}
546+
547+
struct command_result *param_extra_tlvs(struct command *cmd, const char *name,
548+
const char *buffer,
549+
const jsmntok_t *tok,
550+
struct tlv_field **fields)
551+
{
552+
size_t i;
553+
const jsmntok_t *curr;
554+
struct tlv_field *f, *temp;
555+
556+
if (tok->type != JSMN_OBJECT) {
557+
return command_fail(
558+
cmd, JSONRPC2_INVALID_PARAMS,
559+
"Could not decode the TLV object from %s: "
560+
"\"%s\" is not a valid JSON object.",
561+
name, json_strdup(tmpctx, buffer, tok));
562+
}
563+
564+
temp = tal_arr(cmd, struct tlv_field, tok->size);
565+
json_for_each_obj(i, curr, tok) {
566+
f = &temp[i];
567+
if (!json_to_u64(buffer, curr, &f->numtype)) {
568+
return command_fail(
569+
cmd, JSONRPC2_INVALID_PARAMS,
570+
"\"%s\" is not a valid numeric TLV type.",
571+
json_strdup(tmpctx, buffer, curr));
572+
}
573+
f->value = json_tok_bin_from_hex(temp, buffer, curr + 1);
574+
575+
if (f->value == NULL) {
576+
return command_fail(
577+
cmd, JSONRPC2_INVALID_PARAMS,
578+
"\"%s\" is not a valid hex encoded TLV value.",
579+
json_strdup(tmpctx, buffer, curr));
580+
}
581+
f->length = tal_bytelen(f->value);
582+
f->meta = NULL;
583+
}
584+
*fields = temp;
585+
return NULL;
586+
}

common/json_tok.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,9 @@ struct command_result *param_outpoint_arr(struct command *cmd,
189189
const char *buffer,
190190
const jsmntok_t *tok,
191191
struct bitcoin_outpoint **outpoints);
192+
193+
struct command_result *param_extra_tlvs(struct command *cmd, const char *name,
194+
const char *buffer,
195+
const jsmntok_t *tok,
196+
struct tlv_field **fields);
192197
#endif /* LIGHTNING_COMMON_JSON_TOK_H */

0 commit comments

Comments
 (0)