From eae378c9d236aa671493b49befe823ed9996a0d7 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Thu, 25 Jul 2024 13:11:57 +0200 Subject: [PATCH] common: Add some more JSON primitives We were missing the `short_channel_id_dir` helpers. --- common/json_parse.c | 20 +++++++++++++++++++- common/json_parse.h | 5 +++++ common/json_stream.c | 11 ++++++++++- common/json_stream.h | 6 ++++++ plugins/offers_offer.c | 19 ------------------- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/common/json_parse.c b/common/json_parse.c index 9fc9882e2..639c35b2e 100644 --- a/common/json_parse.c +++ b/common/json_parse.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include @@ -574,6 +573,25 @@ bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok, tok->end - tok->start, scid)); } +bool json_to_short_channel_id_dir(const char *buffer, const jsmntok_t *tok, + struct short_channel_id_dir *scidd) +{ + jsmntok_t scidtok, numtok; + u32 dir; + + if (!split_tok(buffer, tok, '/', &scidtok, &numtok)) + return false; + + if (!json_to_short_channel_id(buffer, &scidtok, &scidd->scid)) + return false; + + if (!json_to_u32(buffer, &numtok, &dir) || (dir > 1)) + return false; + + scidd->dir = dir; + return true; +} + bool json_to_txid(const char *buffer, const jsmntok_t *tok, struct bitcoin_txid *txid) { diff --git a/common/json_parse.h b/common/json_parse.h index 1569a4a4e..7eca7284c 100644 --- a/common/json_parse.h +++ b/common/json_parse.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_JSON_PARSE_H #define LIGHTNING_COMMON_JSON_PARSE_H #include "config.h" +#include #include #include #include @@ -110,6 +111,10 @@ bool json_to_outpoint(const char *buffer, const jsmntok_t *tok, bool json_to_channel_id(const char *buffer, const jsmntok_t *tok, struct channel_id *cid); +/* Extract a channel id + dir from this */ +bool json_to_short_channel_id_dir(const char *buffer, const jsmntok_t *tok, + struct short_channel_id_dir *scidd); + /* Extract a coin movement 'tag' from this */ bool json_to_coin_mvt_tag(const char *buffer, const jsmntok_t *tok, enum mvt_tag *tag); diff --git a/common/json_stream.c b/common/json_stream.c index f0667b37d..31a5367b1 100644 --- a/common/json_stream.c +++ b/common/json_stream.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include @@ -485,6 +484,16 @@ void json_add_short_channel_id(struct json_stream *response, short_channel_id_outnum(scid)); } +void json_add_short_channel_id_dir(struct json_stream *response, + const char *fieldname, + struct short_channel_id_dir scidd) +{ + json_add_str_fmt(response, fieldname, "%dx%dx%d/%d", + short_channel_id_blocknum(scidd.scid), + short_channel_id_txnum(scidd.scid), + short_channel_id_outnum(scidd.scid), scidd.dir); +} + static void json_add_address_fields(struct json_stream *response, const struct wireaddr *addr, const char *typefield) diff --git a/common/json_stream.h b/common/json_stream.h index b2963dfed..eeca272c3 100644 --- a/common/json_stream.h +++ b/common/json_stream.h @@ -5,6 +5,7 @@ #define LIGHTNING_COMMON_JSON_STREAM_H #include "config.h" +#include #define JSMN_STRICT 1 # include @@ -313,6 +314,11 @@ void json_add_short_channel_id(struct json_stream *response, const char *fieldname, struct short_channel_id id); +/* '"fieldname" : "1234:5:6/1"' */ +void json_add_short_channel_id_dir(struct json_stream *response, + const char *fieldname, + struct short_channel_id_dir idd); + /* JSON serialize a network address for a node */ void json_add_address(struct json_stream *response, const char *fieldname, const struct wireaddr *addr); diff --git a/plugins/offers_offer.c b/plugins/offers_offer.c index 6c75ce17e..d8d75f93e 100644 --- a/plugins/offers_offer.c +++ b/plugins/offers_offer.c @@ -350,25 +350,6 @@ static struct command_result *currency_done(struct command *cmd, return maybe_add_path(cmd, offinfo); } -static bool json_to_short_channel_id_dir(const char *buffer, const jsmntok_t *tok, - struct short_channel_id_dir *scidd) -{ - jsmntok_t scidtok, numtok; - u32 dir; - - if (!split_tok(buffer, tok, '/', &scidtok, &numtok)) - return false; - - if (!json_to_short_channel_id(buffer, &scidtok, &scidd->scid)) - return false; - - if (!json_to_u32(buffer, &numtok, &dir) || (dir > 1)) - return false; - - scidd->dir = dir; - return true; -} - static bool json_to_sciddir_or_pubkey(const char *buffer, const jsmntok_t *tok, struct sciddir_or_pubkey *sciddir_or_pubkey) {