From 4a38e37b590732322f5bd9a4f6261e43b8a89bd6 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 21 Feb 2023 13:21:21 +0100 Subject: [PATCH] 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. --- common/json_parse.c | 20 ++++++++++++++++++++ common/json_parse.h | 3 +++ 2 files changed, 23 insertions(+) diff --git a/common/json_parse.c b/common/json_parse.c index e4776d87b..14e5d6102 100644 --- a/common/json_parse.c +++ b/common/json_parse.c @@ -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, diff --git a/common/json_parse.h b/common/json_parse.h index 0c45a925b..fef86b7b3 100644 --- a/common/json_parse.h +++ b/common/json_parse.h @@ -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);