Skip to content

Commit

Permalink
common: move json_to_blinded_path into its own file.
Browse files Browse the repository at this point in the history
It's fairly obscure, and let's not make everyone link it.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed May 13, 2024
1 parent 7eb72c5 commit 1b9b160
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 41 deletions.
1 change: 1 addition & 0 deletions common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
41 changes: 41 additions & 0 deletions common/json_blinded_path.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "config.h"
#include <common/json_blinded_path.h>
#include <common/json_parse.h>
#include <wire/onion_wire.h>

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;
}

10 changes: 10 additions & 0 deletions common/json_blinded_path.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef LIGHTNING_COMMON_JSON_BLINDED_PATH_H
#define LIGHTNING_COMMON_JSON_BLINDED_PATH_H
#include "config.h"
#include <common/json_parse_simple.h>

/* 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 */
36 changes: 0 additions & 36 deletions common/json_parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 0 additions & 4 deletions common/json_parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions plugins/offers.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <common/bolt12_merkle.h>
#include <common/invoice_path_id.h>
#include <common/iso4217.h>
#include <common/json_blinded_path.h>
#include <common/json_param.h>
#include <common/json_stream.h>
#include <plugins/offers.h>
Expand Down

0 comments on commit 1b9b160

Please sign in to comment.