From 1b9b160108ac7480ab473d282e8ef43bce42a8ac Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 9 May 2024 13:05:15 +0930 Subject: [PATCH] common: move json_to_blinded_path into its own file. It's fairly obscure, and let's not make everyone link it. Signed-off-by: Rusty Russell --- common/Makefile | 1 + common/json_blinded_path.c | 41 ++++++++++++++++++++++++++++++++++++++ common/json_blinded_path.h | 10 ++++++++++ common/json_parse.c | 36 --------------------------------- common/json_parse.h | 4 ---- plugins/Makefile | 2 +- plugins/offers.c | 1 + 7 files changed, 54 insertions(+), 41 deletions(-) create mode 100644 common/json_blinded_path.c create mode 100644 common/json_blinded_path.h diff --git a/common/Makefile b/common/Makefile index c850ac597..30c458b58 100644 --- a/common/Makefile +++ b/common/Makefile @@ -54,6 +54,7 @@ COMMON_SRC_NOGEN := \ common/initial_commit_tx.c \ common/invoice_path_id.c \ common/iso4217.c \ + common/json_blinded_path.c \ common/json_channel_type.c \ common/json_filter.c \ common/json_param.c \ diff --git a/common/json_blinded_path.c b/common/json_blinded_path.c new file mode 100644 index 000000000..f499efba2 --- /dev/null +++ b/common/json_blinded_path.c @@ -0,0 +1,41 @@ +#include "config.h" +#include +#include +#include + +struct blinded_path * +json_to_blinded_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok) +{ + struct blinded_path *rpath; + const jsmntok_t *hops, *t; + size_t i; + const char *err; + + rpath = tal(ctx, struct blinded_path); + err = json_scan(tmpctx, buffer, tok, "{blinding:%,first_node_id:%}", + JSON_SCAN(json_to_pubkey, &rpath->blinding), + JSON_SCAN(json_to_pubkey, &rpath->first_node_id), + NULL); + if (err) + return tal_free(rpath); + + hops = json_get_member(buffer, tok, "hops"); + if (!hops || hops->size < 1) + return tal_free(rpath); + + rpath->path = tal_arr(rpath, struct onionmsg_hop *, hops->size); + json_for_each_arr(i, t, hops) { + rpath->path[i] = tal(rpath->path, struct onionmsg_hop); + err = json_scan(tmpctx, buffer, t, "{blinded_node_id:%,encrypted_recipient_data:%}", + JSON_SCAN(json_to_pubkey, + &rpath->path[i]->blinded_node_id), + JSON_SCAN_TAL(rpath->path[i], + json_tok_bin_from_hex, + &rpath->path[i]->encrypted_recipient_data)); + if (err) + return tal_free(rpath); + } + + return rpath; +} + diff --git a/common/json_blinded_path.h b/common/json_blinded_path.h new file mode 100644 index 000000000..739d47153 --- /dev/null +++ b/common/json_blinded_path.h @@ -0,0 +1,10 @@ +#ifndef LIGHTNING_COMMON_JSON_BLINDED_PATH_H +#define LIGHTNING_COMMON_JSON_BLINDED_PATH_H +#include "config.h" +#include + +/* Extract reply path from this JSON */ +struct blinded_path * +json_to_blinded_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); + +#endif /* LIGHTNING_COMMON_JSON_BLINDED_PATH_H */ diff --git a/common/json_parse.c b/common/json_parse.c index 0791ce2e1..9fc9882e2 100644 --- a/common/json_parse.c +++ b/common/json_parse.c @@ -649,42 +649,6 @@ struct wally_psbt *json_to_psbt(const tal_t *ctx, const char *buffer, return psbt_from_b64(ctx, buffer + tok->start, tok->end - tok->start); } -struct blinded_path * -json_to_blinded_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok) -{ - struct blinded_path *rpath; - const jsmntok_t *hops, *t; - size_t i; - const char *err; - - rpath = tal(ctx, struct blinded_path); - err = json_scan(tmpctx, buffer, tok, "{blinding:%,first_node_id:%}", - JSON_SCAN(json_to_pubkey, &rpath->blinding), - JSON_SCAN(json_to_pubkey, &rpath->first_node_id), - NULL); - if (err) - return tal_free(rpath); - - hops = json_get_member(buffer, tok, "hops"); - if (!hops || hops->size < 1) - return tal_free(rpath); - - rpath->path = tal_arr(rpath, struct onionmsg_hop *, hops->size); - json_for_each_arr(i, t, hops) { - rpath->path[i] = tal(rpath->path, struct onionmsg_hop); - err = json_scan(tmpctx, buffer, t, "{blinded_node_id:%,encrypted_recipient_data:%}", - JSON_SCAN(json_to_pubkey, - &rpath->path[i]->blinded_node_id), - JSON_SCAN_TAL(rpath->path[i], - json_tok_bin_from_hex, - &rpath->path[i]->encrypted_recipient_data)); - if (err) - return tal_free(rpath); - } - - return rpath; -} - bool json_tok_channel_id(const char *buffer, const jsmntok_t *tok, struct channel_id *cid) diff --git a/common/json_parse.h b/common/json_parse.h index 27cae148a..1569a4a4e 100644 --- a/common/json_parse.h +++ b/common/json_parse.h @@ -114,10 +114,6 @@ 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); -/* Extract reply path from this JSON */ -struct blinded_path * -json_to_blinded_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); - bool json_tok_channel_id(const char *buffer, const jsmntok_t *tok, struct channel_id *cid); diff --git a/plugins/Makefile b/plugins/Makefile index 1cf7452e5..930c4d5ad 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -212,7 +212,7 @@ $(PLUGIN_KEYSEND_OBJS): $(PLUGIN_PAY_LIB_HEADER) plugins/spenderp: bitcoin/block.o bitcoin/preimage.o bitcoin/psbt.o common/psbt_open.o common/json_channel_type.o common/channel_type.o common/features.o wire/peer_wiregen.o $(PLUGIN_SPENDER_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS) -plugins/offers: $(PLUGIN_OFFERS_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) common/addr.o common/bolt12.o common/bolt12_merkle.o common/bolt11_json.o common/iso4217.o $(WIRE_OBJS) $(WIRE_BOLT12_OBJS) bitcoin/block.o common/channel_id.o bitcoin/preimage.o common/blindedpath.o common/invoice_path_id.o common/blinding.o common/hmac.o $(JSMN_OBJS) +plugins/offers: $(PLUGIN_OFFERS_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) common/addr.o common/bolt12.o common/bolt12_merkle.o common/bolt11_json.o common/iso4217.o $(WIRE_OBJS) $(WIRE_BOLT12_OBJS) bitcoin/block.o common/channel_id.o bitcoin/preimage.o common/blindedpath.o common/invoice_path_id.o common/blinding.o common/hmac.o common/json_blinded_path.o $(JSMN_OBJS) plugins/fetchinvoice: $(PLUGIN_FETCHINVOICE_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) common/bolt12.o common/bolt12_merkle.o common/iso4217.o $(WIRE_OBJS) $(WIRE_BOLT12_OBJS) bitcoin/block.o common/channel_id.o bitcoin/preimage.o $(JSMN_OBJS) common/gossmap.o common/fp16.o common/dijkstra.o common/route.o common/blindedpath.o common/hmac.o common/blinding.o common/gossmods_listpeerchannels.o diff --git a/plugins/offers.c b/plugins/offers.c index 22d1c097a..9cb1ffb42 100644 --- a/plugins/offers.c +++ b/plugins/offers.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include