Skip to content

Commit eaec226

Browse files
committed
common: json_to_reply_path to helper to handle blindedpath/onion_message hook
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent b8498b6 commit eaec226

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

common/json_helpers.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <common/type_to_string.h>
77
#include <common/wireaddr.h>
88
#include <errno.h>
9+
#include <wire/onion_wire.h>
910
#include <wire/peer_wire.h>
1011

1112
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
@@ -140,6 +141,42 @@ struct wally_psbt *json_to_psbt(const tal_t *ctx, const char *buffer,
140141
return psbt_from_b64(ctx, buffer + tok->start, tok->end - tok->start);
141142
}
142143

144+
struct tlv_onionmsg_payload_reply_path *
145+
json_to_reply_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok)
146+
{
147+
struct tlv_onionmsg_payload_reply_path *rpath;
148+
const jsmntok_t *hops, *t;
149+
size_t i;
150+
const char *err;
151+
152+
rpath = tal(ctx, struct tlv_onionmsg_payload_reply_path);
153+
err = json_scan(tmpctx, buffer, tok, "{blinding:%,first_node_id:%}",
154+
JSON_SCAN(json_to_pubkey, &rpath->blinding),
155+
JSON_SCAN(json_to_pubkey, &rpath->first_node_id),
156+
NULL);
157+
if (err)
158+
return tal_free(rpath);
159+
160+
hops = json_get_member(buffer, tok, "hops");
161+
if (!hops || hops->size < 1)
162+
return tal_free(rpath);
163+
164+
rpath->path = tal_arr(rpath, struct onionmsg_path *, hops->size);
165+
json_for_each_arr(i, t, hops) {
166+
rpath->path[i] = tal(rpath->path, struct onionmsg_path);
167+
err = json_scan(tmpctx, buffer, t, "{id:%,enctlv:%}",
168+
JSON_SCAN(json_to_pubkey,
169+
&rpath->path[i]->node_id),
170+
JSON_SCAN_TAL(rpath->path[i],
171+
json_tok_bin_from_hex,
172+
&rpath->path[i]->enctlv));
173+
if (err)
174+
return tal_free(rpath);
175+
}
176+
177+
return rpath;
178+
}
179+
143180
void json_add_node_id(struct json_stream *response,
144181
const char *fieldname,
145182
const struct node_id *id)

common/json_helpers.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ bool split_tok(const char *buffer, const jsmntok_t *tok,
7979
jsmntok_t *a,
8080
jsmntok_t *b);
8181

82+
/* Extract reply path from this JSON */
83+
struct tlv_onionmsg_payload_reply_path *
84+
json_to_reply_path(const tal_t *ctx, const char *buffer, const jsmntok_t *tok);
85+
8286
/* Helpers for outputting JSON results */
8387

8488
/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */

0 commit comments

Comments
 (0)