common: add mkdatastorekey helper to make string arrays.
What changed, and why it matters
This commit is a routine code cleanup: it introduces a small helper function that builds lists of text strings more conveniently, and updates three existing functions to use it. There is no security-relevant change visible in the diff.
No security action required; treat as normal refactoring.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds common/mkdatastorekey.c/h, a variadic tal-allocated const char ** builder, and refactors wallet_datastore_save_utxo_description, wallet_datastore_save_payment_description, and migrate_datastore_commando_runes to use it. The generated keys are semantically identical to the previous hand-built arrays; the helper merely reduces boilerplate. No new inputs, parsers, cryptographic operations, or trust boundaries are introduced.
Changed components
common/mkdatastorekey.ccommon/mkdatastorekey.hwallet/wallet.cInspect captured patch +42 / −19
diff --git a/common/Makefile b/common/Makefile
index 62ceb252..7c762487 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -68,6 +68,7 @@ COMMON_SRC_NOGEN := \
common/keyset.c \
common/lease_rates.c \
common/memleak.c \
+ common/mkdatastorekey.c \
common/msg_queue.c \
common/node_id.c \
common/onion_decode.c \
diff --git a/common/mkdatastorekey.c b/common/mkdatastorekey.c
new file mode 100644
index 00000000..3f331a5c
--- /dev/null
+++ b/common/mkdatastorekey.c
@@ -0,0 +1,18 @@
+#include "config.h"
+#include <ccan/tal/str/str.h>
+#include <common/mkdatastorekey.h>
+#include <common/utils.h>
+
+const char **mkdatastorekey_(const tal_t *ctx, ...)
+{
+ va_list ap;
+ const char *s;
+ const char **key = tal_arr(ctx, const char *, 0);
+
+ va_start(ap, ctx);
+ while ((s = va_arg(ap, const char *)) != NULL)
+ tal_arr_expand(&key, tal_strdup(key, s));
+ va_end(ap);
+
+ return key;
+}
diff --git a/common/mkdatastorekey.h b/common/mkdatastorekey.h
new file mode 100644
index 00000000..01f9dac5
--- /dev/null
+++ b/common/mkdatastorekey.h
@@ -0,0 +1,15 @@
+#ifndef LIGHTNING_COMMON_MKDATASTOREKEY_H
+#define LIGHTNING_COMMON_MKDATASTOREKEY_H
+#include "config.h"
+#include <ccan/compiler/compiler.h>
+#include <ccan/tal/tal.h>
+
+/* Generate an array of strings from these values: great for making
+ * keys for datastore operations */
+#define mkdatastorekey(ctx, ...) \
+ mkdatastorekey_(ctx, __VA_ARGS__, NULL)
+
+LAST_ARG_NULL
+const char **mkdatastorekey_(const tal_t *ctx, ... TAKES);
+
+#endif /* LIGHTNING_COMMON_MKDATASTOREKEY_H */
diff --git a/wallet/wallet.c b/wallet/wallet.c
index 2b080247..d2a3e215 100644
--- a/wallet/wallet.c
+++ b/wallet/wallet.c
@@ -8,6 +8,7 @@
#include <channeld/channeld_wiregen.h>
#include <common/clock_time.h>
#include <common/memleak.h>
+#include <common/mkdatastorekey.h>
#include <common/onionreply.h>
#include <common/randbytes.h>
#include <common/trace.h>
@@ -6364,13 +6365,9 @@ void wallet_datastore_save_utxo_description(struct db *db,
const struct bitcoin_outpoint *outpoint,
const char *desc)
{
- const char **key;
-
- key = tal_arr(tmpctx, const char *, 4);
- key[0] = "bookkeeper";
- key[1] = "description";
- key[2] = "utxo";
- key[3] = fmt_bitcoin_outpoint(key, outpoint);
+ const char **key = mkdatastorekey(tmpctx,
+ "bookkeeper", "description", "utxo",
+ take(fmt_bitcoin_outpoint(NULL, outpoint)));
/* In case it's a duplicate, remove first */
db_datastore_remove(db, key);
@@ -6381,13 +6378,9 @@ void wallet_datastore_save_payment_description(struct db *db,
const struct sha256 *payment_hash,
const char *desc)
{
- const char **key;
-
- key = tal_arr(tmpctx, const char *, 4);
- key[0] = "bookkeeper";
- key[1] = "description";
- key[2] = "payment";
- key[3] = fmt_sha256(key, payment_hash);
+ const char **key = mkdatastorekey(tmpctx,
+ "bookkeeper", "description", "payment",
+ take(fmt_sha256(NULL, payment_hash)));
/* In case it's a duplicate, remove first */
db_datastore_remove(db, key);
@@ -6740,12 +6733,8 @@ void migrate_datastore_commando_runes(struct lightningd *ld, struct db *db)
{
const char **startkey;
- /* datastore routines expect a tal_arr */
- startkey = tal_arr(tmpctx, const char *, 2);
-
/* We deleted this from the datastore on migration. */
- startkey[0] = "commando";
- startkey[1] = "rune_counter";
+ startkey = mkdatastorekey(tmpctx, "commando", "rune_counter");
if (db_datastore_get(tmpctx, db, startkey, NULL))
db_fatal(db, "Commando runes still present? Migration removed in v25.02: call Rusty!");
}
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.