Skip to content

Commit 9efe123

Browse files
ZmnSCPxjrustyrussell
authored andcommitted
lightningd/json: Move json helpers specific for lightningd to new module.
1 parent 6391645 commit 9efe123

14 files changed

+204
-168
lines changed

lightningd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ LIGHTNINGD_SRC := \
5959
lightningd/hsm_control.c \
6060
lightningd/htlc_end.c \
6161
lightningd/invoice.c \
62+
lightningd/json.c \
6263
lightningd/jsonrpc.c \
6364
lightningd/lightningd.c \
6465
lightningd/log.c \

lightningd/connect_control.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <common/wireaddr.h>
55
#include <gossipd/gen_gossip_wire.h>
66
#include <lightningd/connect_control.h>
7+
#include <lightningd/json.h>
78
#include <lightningd/jsonrpc.h>
89
#include <lightningd/lightningd.h>
910
#include <lightningd/log.h>

lightningd/dev_ping.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <common/utils.h>
44
#include <gossipd/gen_gossip_wire.h>
55
#include <lightningd/htlc_end.h>
6+
#include <lightningd/json.h>
67
#include <lightningd/jsonrpc.h>
78
#include <lightningd/lightningd.h>
89
#include <lightningd/log.h>

lightningd/gossip_control.c

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <lightningd/connect_control.h>
2222
#include <lightningd/gossip_msg.h>
2323
#include <lightningd/hsm_control.h>
24+
#include <lightningd/json.h>
2425
#include <lightningd/jsonrpc.h>
2526
#include <lightningd/log.h>
2627
#include <sodium/randombytes.h>
@@ -289,7 +290,6 @@ static void json_getroute_reply(struct subd *gossip UNUSED, const u8 *reply, con
289290
{
290291
struct json_result *response;
291292
struct route_hop *hops;
292-
size_t i;
293293

294294
fromwire_gossip_getroute_reply(reply, reply, &hops);
295295

@@ -300,17 +300,7 @@ static void json_getroute_reply(struct subd *gossip UNUSED, const u8 *reply, con
300300

301301
response = new_json_result(cmd);
302302
json_object_start(response, NULL);
303-
json_array_start(response, "route");
304-
for (i = 0; i < tal_count(hops); i++) {
305-
json_object_start(response, NULL);
306-
json_add_pubkey(response, "id", &hops[i].nodeid);
307-
json_add_short_channel_id(response, "channel",
308-
&hops[i].channel_id);
309-
json_add_u64(response, "msatoshi", hops[i].amount);
310-
json_add_num(response, "delay", hops[i].delay);
311-
json_object_end(response);
312-
}
313-
json_array_end(response);
303+
json_add_route(response, "route", hops, tal_count(hops));
314304
json_object_end(response);
315305
command_success(cmd, response);
316306
}

lightningd/invoice.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "invoice.h"
2+
#include "json.h"
23
#include "jsonrpc.h"
34
#include "lightningd.h"
45
#include <bitcoin/address.h>

lightningd/json.c

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#include "json.h"
2+
#include <arpa/inet.h>
3+
#include <ccan/str/hex/hex.h>
4+
#include <common/json.h>
5+
#include <common/type_to_string.h>
6+
#include <common/wireaddr.h>
7+
#include <gossipd/routing.h>
8+
#include <lightningd/options.h>
9+
#include <sys/socket.h>
10+
#include <wallet/wallet.h>
11+
12+
/* Output a route hop */
13+
static void
14+
json_add_route_hop(struct json_result *r, char const *n,
15+
const struct route_hop *h)
16+
{
17+
/* Imitate what getroute/sendpay use */
18+
json_object_start(r, n);
19+
json_add_pubkey(r, "id", &h->nodeid);
20+
json_add_short_channel_id(r, "channel",
21+
&h->channel_id);
22+
json_add_u64(r, "msatoshi", h->amount);
23+
json_add_num(r, "delay", h->delay);
24+
json_object_end(r);
25+
}
26+
27+
/* Output a route */
28+
void
29+
json_add_route(struct json_result *r, char const *n,
30+
const struct route_hop *hops, size_t hops_len)
31+
{
32+
size_t i;
33+
json_array_start(r, n);
34+
for (i = 0; i < hops_len; ++i) {
35+
json_add_route_hop(r, NULL, &hops[i]);
36+
}
37+
json_array_end(r);
38+
}
39+
40+
/* Outputs fields, not a separate object*/
41+
void
42+
json_add_payment_fields(struct json_result *response,
43+
const struct wallet_payment *t)
44+
{
45+
json_add_u64(response, "id", t->id);
46+
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
47+
json_add_pubkey(response, "destination", &t->destination);
48+
json_add_u64(response, "msatoshi", t->msatoshi);
49+
if (deprecated_apis)
50+
json_add_u64(response, "timestamp", t->timestamp);
51+
json_add_u64(response, "created_at", t->timestamp);
52+
53+
switch (t->status) {
54+
case PAYMENT_PENDING:
55+
json_add_string(response, "status", "pending");
56+
break;
57+
case PAYMENT_COMPLETE:
58+
json_add_string(response, "status", "complete");
59+
break;
60+
case PAYMENT_FAILED:
61+
json_add_string(response, "status", "failed");
62+
break;
63+
}
64+
if (t->payment_preimage)
65+
json_add_hex(response, "payment_preimage",
66+
t->payment_preimage,
67+
sizeof(*t->payment_preimage));
68+
}
69+
70+
void json_add_pubkey(struct json_result *response,
71+
const char *fieldname,
72+
const struct pubkey *key)
73+
{
74+
u8 der[PUBKEY_DER_LEN];
75+
76+
pubkey_to_der(der, key);
77+
json_add_hex(response, fieldname, der, sizeof(der));
78+
}
79+
80+
void json_add_txid(struct json_result *result, const char *fieldname,
81+
const struct bitcoin_txid *txid)
82+
{
83+
char hex[hex_str_size(sizeof(*txid))];
84+
85+
bitcoin_txid_to_hex(txid, hex, sizeof(hex));
86+
json_add_string(result, fieldname, hex);
87+
}
88+
89+
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
90+
struct pubkey *pubkey)
91+
{
92+
return pubkey_from_hexstr(buffer + tok->start,
93+
tok->end - tok->start, pubkey);
94+
}
95+
96+
void json_add_short_channel_id(struct json_result *response,
97+
const char *fieldname,
98+
const struct short_channel_id *id)
99+
{
100+
json_add_string(response, fieldname,
101+
type_to_string(response, struct short_channel_id, id));
102+
}
103+
104+
bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok,
105+
struct short_channel_id *scid)
106+
{
107+
return short_channel_id_from_str(buffer + tok->start,
108+
tok->end - tok->start,
109+
scid);
110+
}
111+
112+
void json_add_address(struct json_result *response, const char *fieldname,
113+
const struct wireaddr *addr)
114+
{
115+
/* No need to print padding */
116+
if (addr->type == ADDR_TYPE_PADDING)
117+
return;
118+
119+
json_object_start(response, fieldname);
120+
char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN);
121+
if (addr->type == ADDR_TYPE_IPV4) {
122+
inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN);
123+
json_add_string(response, "type", "ipv4");
124+
json_add_string(response, "address", addrstr);
125+
json_add_num(response, "port", addr->port);
126+
} else if (addr->type == ADDR_TYPE_IPV6) {
127+
inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN);
128+
json_add_string(response, "type", "ipv6");
129+
json_add_string(response, "address", addrstr);
130+
json_add_num(response, "port", addr->port);
131+
}
132+
json_object_end(response);
133+
}
134+

lightningd/json.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/* lightningd/json.h
2+
* Helpers for outputting JSON results that are specific only for
3+
* lightningd.
4+
*/
5+
#ifndef LIGHTNING_LIGHTNINGD_JSON_H
6+
#define LIGHTNING_LIGHTNINGD_JSON_H
7+
#include "config.h"
8+
#include <stdbool.h>
9+
#include <stddef.h>
10+
11+
#define JSMN_STRICT 1
12+
# include <external/jsmn/jsmn.h>
13+
14+
struct bitcoin_txid;
15+
struct json_result;
16+
struct pubkey;
17+
struct route_hop;
18+
struct short_channel_id;
19+
struct wallet_payment;
20+
struct wireaddr;
21+
22+
/* Output a route array. */
23+
void json_add_route(struct json_result *r, char const *n,
24+
const struct route_hop *hops, size_t hops_len);
25+
26+
/* Output the fields of a wallet payment.
27+
* Should be used within an object context. */
28+
void json_add_payment_fields(struct json_result *response,
29+
const struct wallet_payment *t);
30+
31+
/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */
32+
void json_add_pubkey(struct json_result *response,
33+
const char *fieldname,
34+
const struct pubkey *key);
35+
36+
/* '"fieldname" : <hexrev>' or "<hexrev>" if fieldname is NULL */
37+
void json_add_txid(struct json_result *result, const char *fieldname,
38+
const struct bitcoin_txid *txid);
39+
40+
/* Extract a pubkey from this */
41+
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
42+
struct pubkey *pubkey);
43+
44+
/* Extract a short_channel_id from this */
45+
bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok,
46+
struct short_channel_id *scid);
47+
48+
/* '"fieldname" : "1234:5:6"' */
49+
void json_add_short_channel_id(struct json_result *response,
50+
const char *fieldname,
51+
const struct short_channel_id *id);
52+
53+
/* JSON serialize a network address for a node */
54+
void json_add_address(struct json_result *response, const char *fieldname,
55+
const struct wireaddr *addr);
56+
57+
58+
#endif /* !defined (LIGHTNING_LIGHTNINGD_JSON_H) */

lightningd/jsonrpc.c

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <errno.h>
1818
#include <fcntl.h>
1919
#include <lightningd/chaintopology.h>
20+
#include <lightningd/json.h>
2021
#include <lightningd/jsonrpc.h>
2122
#include <lightningd/jsonrpc_errors.h>
2223
#include <lightningd/lightningd.h>
@@ -316,71 +317,6 @@ struct json_result *null_response(const tal_t *ctx)
316317
return response;
317318
}
318319

319-
void json_add_pubkey(struct json_result *response,
320-
const char *fieldname,
321-
const struct pubkey *key)
322-
{
323-
u8 der[PUBKEY_DER_LEN];
324-
325-
pubkey_to_der(der, key);
326-
json_add_hex(response, fieldname, der, sizeof(der));
327-
}
328-
329-
void json_add_txid(struct json_result *result, const char *fieldname,
330-
const struct bitcoin_txid *txid)
331-
{
332-
char hex[hex_str_size(sizeof(*txid))];
333-
334-
bitcoin_txid_to_hex(txid, hex, sizeof(hex));
335-
json_add_string(result, fieldname, hex);
336-
}
337-
338-
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
339-
struct pubkey *pubkey)
340-
{
341-
return pubkey_from_hexstr(buffer + tok->start,
342-
tok->end - tok->start, pubkey);
343-
}
344-
345-
void json_add_short_channel_id(struct json_result *response,
346-
const char *fieldname,
347-
const struct short_channel_id *id)
348-
{
349-
json_add_string(response, fieldname,
350-
type_to_string(response, struct short_channel_id, id));
351-
}
352-
353-
bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok,
354-
struct short_channel_id *scid)
355-
{
356-
return short_channel_id_from_str(buffer + tok->start,
357-
tok->end - tok->start,
358-
scid);
359-
}
360-
361-
void json_add_address(struct json_result *response, const char *fieldname,
362-
const struct wireaddr *addr)
363-
{
364-
/* No need to print padding */
365-
if (addr->type == ADDR_TYPE_PADDING)
366-
return;
367-
368-
json_object_start(response, fieldname);
369-
char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN);
370-
if (addr->type == ADDR_TYPE_IPV4) {
371-
inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN);
372-
json_add_string(response, "type", "ipv4");
373-
json_add_string(response, "address", addrstr);
374-
json_add_num(response, "port", addr->port);
375-
} else if (addr->type == ADDR_TYPE_IPV6) {
376-
inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN);
377-
json_add_string(response, "type", "ipv6");
378-
json_add_string(response, "address", addrstr);
379-
json_add_num(response, "port", addr->port);
380-
}
381-
json_object_end(response);
382-
}
383-
384320
static bool cmd_in_jcon(const struct json_connection *jcon,
385321
const struct command *cmd)
386322
{

lightningd/jsonrpc.h

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,6 @@ void PRINTF_FMT(4, 5) command_fail_detailed(struct command *cmd,
8080
/* Mainly for documentation, that we plan to close this later. */
8181
void command_still_pending(struct command *cmd);
8282

83-
/* '"fieldname" : "0289abcdef..."' or "0289abcdef..." if fieldname is NULL */
84-
void json_add_pubkey(struct json_result *response,
85-
const char *fieldname,
86-
const struct pubkey *key);
87-
88-
/* '"fieldname" : <hexrev>' or "<hexrev>" if fieldname is NULL */
89-
void json_add_txid(struct json_result *result, const char *fieldname,
90-
const struct bitcoin_txid *txid);
91-
92-
/* Extract a pubkey from this */
93-
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
94-
struct pubkey *pubkey);
95-
96-
/* Extract a short_channel_id from this */
97-
bool json_tok_short_channel_id(const char *buffer, const jsmntok_t *tok,
98-
struct short_channel_id *scid);
99-
100-
/* '"fieldname" : "1234:5:6"' */
101-
void json_add_short_channel_id(struct json_result *response,
102-
const char *fieldname,
103-
const struct short_channel_id *id);
104-
105-
/* JSON serialize a network address for a node */
106-
void json_add_address(struct json_result *response, const char *fieldname,
107-
const struct wireaddr *addr);
108-
10983

11084
/* For initialization */
11185
void setup_jsonrpc(struct lightningd *ld, const char *rpc_filename);

lightningd/opening_control.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <lightningd/channel_control.h>
1414
#include <lightningd/closing_control.h>
1515
#include <lightningd/hsm_control.h>
16+
#include <lightningd/json.h>
1617
#include <lightningd/jsonrpc.h>
1718
#include <lightningd/lightningd.h>
1819
#include <lightningd/log.h>

0 commit comments

Comments
 (0)