common: expose json_hex_to_be32/be64
What changed, and why it matters
This commit simply moves two small helper functions that decode hex strings into numbers from two plugin files into a shared library. It is a routine code cleanup/refactoring change with no security relevance visible in the diff or commit message.
No security action needed; this is a benign refactoring change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The change exposes json_hex_to_be32 and json_hex_to_be64 in common/json_parse_simple.c/h and removes duplicate static definitions from plugins/bkpr/blockheights.c and plugins/bkpr/bookkeeper.c. The implementations are identical to the removed static copies: they call hex_decode on the JSON token substring into a fixed-size big-endian output. No logic changes, no new callers, no input validation changes, and no security-related content in the commit message or diff.
Changed components
common/json_parse_simple.ccommon/json_parse_simple.hplugins/bkpr/blockheights.cplugins/bkpr/bookkeeper.cInspect captured patch +21 / −14
diff --git a/common/json_parse_simple.c b/common/json_parse_simple.c
index 348be9ef..fbb2b12c 100644
--- a/common/json_parse_simple.c
+++ b/common/json_parse_simple.c
@@ -2,6 +2,7 @@
#include "config.h"
#include <assert.h>
#include <ccan/mem/mem.h>
+#include <ccan/str/hex/hex.h>
#include <ccan/tal/str/str.h>
#include <common/json_parse_simple.h>
#include <common/utils.h>
@@ -151,6 +152,18 @@ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b)
return false;
}
+bool json_hex_to_be32(const char *buffer, const jsmntok_t *tok, be32 *val)
+{
+ return hex_decode(buffer + tok->start, tok->end - tok->start,
+ val, sizeof(*val));
+}
+
+bool json_hex_to_be64(const char *buffer, const jsmntok_t *tok, be64 *val)
+{
+ return hex_decode(buffer + tok->start, tok->end - tok->start,
+ val, sizeof(*val));
+}
+
bool json_tok_is_num(const char *buffer, const jsmntok_t *tok)
{
diff --git a/common/json_parse_simple.h b/common/json_parse_simple.h
index 0882812d..56f97e2c 100644
--- a/common/json_parse_simple.h
+++ b/common/json_parse_simple.h
@@ -2,6 +2,7 @@
#ifndef LIGHTNING_COMMON_JSON_PARSE_SIMPLE_H
#define LIGHTNING_COMMON_JSON_PARSE_SIMPLE_H
#include "config.h"
+#include <ccan/endian/endian.h>
#include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h>
@@ -51,6 +52,12 @@ bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num);
/* Extract boolean from this */
bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b);
+/* Extract big-endian 32-bit from hex string (for datastore) */
+bool json_hex_to_be32(const char *buffer, const jsmntok_t *tok, be32 *val);
+
+/* Extract big-endian 64-bit from hex string (for datastore) */
+bool json_hex_to_be64(const char *buffer, const jsmntok_t *tok, be64 *val);
+
/* Is this a number? [0..9]+ */
bool json_tok_is_num(const char *buffer, const jsmntok_t *tok);
diff --git a/plugins/bkpr/blockheights.c b/plugins/bkpr/blockheights.c
index 35aa229e..5da70721 100644
--- a/plugins/bkpr/blockheights.c
+++ b/plugins/bkpr/blockheights.c
@@ -98,13 +98,6 @@ u32 find_blockheight(const struct bkpr *bkpr,
return e ? e->height : 0;
}
-static bool json_hex_to_be32(const char *buffer, const jsmntok_t *tok,
- be32 *val)
-{
- return hex_decode(buffer + tok->start, tok->end - tok->start,
- val, sizeof(*val));
-}
-
struct blockheights *init_blockheights(const tal_t *ctx,
struct command *init_cmd)
{
diff --git a/plugins/bkpr/bookkeeper.c b/plugins/bkpr/bookkeeper.c
index 61b6796b..40968e9c 100644
--- a/plugins/bkpr/bookkeeper.c
+++ b/plugins/bkpr/bookkeeper.c
@@ -14,6 +14,7 @@
#include <common/coin_mvt.h>
#include <common/iso4217.h>
#include <common/json_param.h>
+#include <common/json_parse_simple.h>
#include <common/json_stream.h>
#include <common/memleak.h>
#include <common/mkdatastorekey.h>
@@ -1837,13 +1838,6 @@ static const struct plugin_command commands[] = {
},
};
-static bool json_hex_to_be64(const char *buffer, const jsmntok_t *tok,
- be64 *val)
-{
- return hex_decode(buffer + tok->start, tok->end - tok->start,
- val, sizeof(*val));
-}
-
static void memleak_scan_currencyrates(struct htable *memtable,
currencymap_t *currency_rates)
{
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.