json: Add method to parse a u64 array

This will be used to parse the extratlvs from `listconfigs` in
`keysend`, so we don't accidentally strip values we'd like to keep.
This commit is contained in:
Christian Decker 2023-02-21 13:21:21 +01:00 committed by Alex Myers
parent 1426ac881b
commit 4a38e37b59
2 changed files with 23 additions and 0 deletions

View file

@ -683,6 +683,26 @@ json_to_blinded_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok)
return rpath;
}
bool json_to_uintarr(const char *buffer, const jsmntok_t *tok, u64 **dest)
{
char *str = json_strdup(NULL, buffer, tok);
char *endp, **elements = tal_strsplit(str, str, ",", STR_NO_EMPTY);
unsigned long long l;
u64 u;
for (int i = 0; elements[i] != NULL; i++) {
/* This is how the manpage says to do it. Yech. */
errno = 0;
l = strtoull(elements[i], &endp, 0);
if (*endp || !str[0])
return tal_fmt(NULL, "'%s' is not a number", elements[i]);
u = l;
if (errno || u != l)
return tal_fmt(NULL, "'%s' is out of range", elements[i]);
tal_arr_expand(dest, u);
}
tal_free(str);
return NULL;
}
bool
json_tok_channel_id(const char *buffer, const jsmntok_t *tok,

View file

@ -114,6 +114,9 @@ bool json_to_channel_id(const char *buffer, const jsmntok_t *tok,
bool json_to_coin_mvt_tag(const char *buffer, const jsmntok_t *tok,
enum mvt_tag *tag);
/* Read a JSON value into an array of u64 */
bool json_to_uintarr(const char *buffer, const jsmntok_t *tok, u64 **dest);
/* Extract reply path from this JSON */
struct blinded_path *
json_to_blinded_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok);