2018-03-16 04:45:08 +01:00
|
|
|
#include <arpa/inet.h>
|
2018-08-21 15:58:55 +02:00
|
|
|
#include <ccan/mem/mem.h>
|
2018-08-30 15:48:36 +02:00
|
|
|
#include <ccan/str/hex/hex.h>
|
2018-05-07 06:29:21 +02:00
|
|
|
#include <ccan/tal/str/str.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <common/json.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/json_command.h>
|
|
|
|
#include <common/json_escaped.h>
|
|
|
|
#include <common/jsonrpc_errors.h>
|
2018-10-19 03:17:48 +02:00
|
|
|
#include <common/memleak.h>
|
2018-12-08 01:39:28 +01:00
|
|
|
#include <common/param.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <common/type_to_string.h>
|
|
|
|
#include <common/wireaddr.h>
|
|
|
|
#include <gossipd/routing.h>
|
2018-11-20 02:46:32 +01:00
|
|
|
#include <lightningd/json.h>
|
|
|
|
#include <lightningd/json_stream.h>
|
2018-08-10 22:16:22 +02:00
|
|
|
#include <lightningd/jsonrpc.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
#include <lightningd/options.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <wallet/wallet.h>
|
2018-04-30 14:54:39 +02:00
|
|
|
#include <wire/wire.h>
|
2018-03-16 04:45:08 +01:00
|
|
|
|
|
|
|
/* Output a route hop */
|
|
|
|
static void
|
2018-10-19 03:17:49 +02:00
|
|
|
json_add_route_hop(struct json_stream *r, char const *n,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct route_hop *h)
|
|
|
|
{
|
|
|
|
/* Imitate what getroute/sendpay use */
|
|
|
|
json_object_start(r, n);
|
|
|
|
json_add_pubkey(r, "id", &h->nodeid);
|
|
|
|
json_add_short_channel_id(r, "channel",
|
|
|
|
&h->channel_id);
|
|
|
|
json_add_u64(r, "msatoshi", h->amount);
|
|
|
|
json_add_num(r, "delay", h->delay);
|
|
|
|
json_object_end(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Output a route */
|
|
|
|
void
|
2018-10-19 03:17:49 +02:00
|
|
|
json_add_route(struct json_stream *r, char const *n,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct route_hop *hops, size_t hops_len)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
json_array_start(r, n);
|
|
|
|
for (i = 0; i < hops_len; ++i) {
|
|
|
|
json_add_route_hop(r, NULL, &hops[i]);
|
|
|
|
}
|
|
|
|
json_array_end(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Outputs fields, not a separate object*/
|
|
|
|
void
|
2018-10-19 03:17:49 +02:00
|
|
|
json_add_payment_fields(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct wallet_payment *t)
|
|
|
|
{
|
|
|
|
json_add_u64(response, "id", t->id);
|
|
|
|
json_add_hex(response, "payment_hash", &t->payment_hash, sizeof(t->payment_hash));
|
|
|
|
json_add_pubkey(response, "destination", &t->destination);
|
|
|
|
json_add_u64(response, "msatoshi", t->msatoshi);
|
2018-03-22 14:21:18 +01:00
|
|
|
json_add_u64(response, "msatoshi_sent", t->msatoshi_sent);
|
2018-03-16 04:45:08 +01:00
|
|
|
json_add_u64(response, "created_at", t->timestamp);
|
|
|
|
|
|
|
|
switch (t->status) {
|
|
|
|
case PAYMENT_PENDING:
|
|
|
|
json_add_string(response, "status", "pending");
|
|
|
|
break;
|
|
|
|
case PAYMENT_COMPLETE:
|
|
|
|
json_add_string(response, "status", "complete");
|
|
|
|
break;
|
|
|
|
case PAYMENT_FAILED:
|
|
|
|
json_add_string(response, "status", "failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (t->payment_preimage)
|
|
|
|
json_add_hex(response, "payment_preimage",
|
|
|
|
t->payment_preimage,
|
|
|
|
sizeof(*t->payment_preimage));
|
2018-07-23 15:32:47 +02:00
|
|
|
if (t->description)
|
|
|
|
json_add_string(response, "description", t->description);
|
2018-03-16 04:45:08 +01:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_pubkey(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const char *fieldname,
|
|
|
|
const struct pubkey *key)
|
|
|
|
{
|
|
|
|
u8 der[PUBKEY_DER_LEN];
|
|
|
|
|
|
|
|
pubkey_to_der(der, key);
|
|
|
|
json_add_hex(response, fieldname, der, sizeof(der));
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_txid(struct json_stream *result, const char *fieldname,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct bitcoin_txid *txid)
|
|
|
|
{
|
|
|
|
char hex[hex_str_size(sizeof(*txid))];
|
|
|
|
|
|
|
|
bitcoin_txid_to_hex(txid, hex, sizeof(hex));
|
|
|
|
json_add_string(result, fieldname, hex);
|
|
|
|
}
|
|
|
|
|
2018-08-14 23:19:31 +02:00
|
|
|
bool json_to_pubkey(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct pubkey *pubkey)
|
2018-03-16 04:45:08 +01:00
|
|
|
{
|
|
|
|
return pubkey_from_hexstr(buffer + tok->start,
|
|
|
|
tok->end - tok->start, pubkey);
|
|
|
|
}
|
|
|
|
|
2018-08-14 23:19:31 +02:00
|
|
|
bool json_tok_pubkey(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct pubkey **pubkey)
|
|
|
|
{
|
|
|
|
*pubkey = tal(cmd, struct pubkey);
|
|
|
|
if (json_to_pubkey(buffer, tok, *pubkey))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be a pubkey, not '%.*s'",
|
2018-12-16 05:48:06 +01:00
|
|
|
name, json_tok_full_len(tok), json_tok_full(buffer, tok));
|
2018-08-14 23:19:31 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_short_channel_id(struct json_stream *response,
|
2018-03-16 04:45:08 +01:00
|
|
|
const char *fieldname,
|
|
|
|
const struct short_channel_id *id)
|
|
|
|
{
|
|
|
|
json_add_string(response, fieldname,
|
|
|
|
type_to_string(response, struct short_channel_id, id));
|
|
|
|
}
|
|
|
|
|
2018-08-15 16:20:40 +02:00
|
|
|
bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct short_channel_id *scid)
|
2018-03-16 04:45:08 +01:00
|
|
|
{
|
2018-08-15 16:20:40 +02:00
|
|
|
return (short_channel_id_from_str(buffer + tok->start,
|
|
|
|
tok->end - tok->start, scid));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool json_tok_short_channel_id(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct short_channel_id **scid)
|
|
|
|
{
|
|
|
|
*scid = tal(cmd, struct short_channel_id);
|
2018-08-21 15:58:55 +02:00
|
|
|
if (json_to_short_channel_id(buffer, tok, *scid))
|
2018-08-15 16:20:40 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be a short channel id, not '%.*s'",
|
2018-12-16 05:48:06 +01:00
|
|
|
name, json_tok_full_len(tok), json_tok_full(buffer, tok));
|
2018-08-15 16:20:40 +02:00
|
|
|
return false;
|
2018-03-16 04:45:08 +01:00
|
|
|
}
|
|
|
|
|
2018-08-27 07:11:39 +02:00
|
|
|
const char *json_feerate_style_name(enum feerate_style style)
|
|
|
|
{
|
|
|
|
switch (style) {
|
|
|
|
case FEERATE_PER_KBYTE:
|
|
|
|
return "perkb";
|
|
|
|
case FEERATE_PER_KSIPA:
|
|
|
|
return "perkw";
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool json_tok_feerate_style(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
enum feerate_style **style)
|
|
|
|
{
|
|
|
|
*style = tal(cmd, enum feerate_style);
|
|
|
|
if (json_tok_streq(buffer, tok,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA))) {
|
|
|
|
**style = FEERATE_PER_KSIPA;
|
|
|
|
return true;
|
|
|
|
} else if (json_tok_streq(buffer, tok,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE))) {
|
|
|
|
**style = FEERATE_PER_KBYTE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' should be '%s' or '%s', not '%.*s'",
|
|
|
|
name,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA),
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE),
|
2018-12-16 05:48:06 +01:00
|
|
|
json_tok_full_len(tok), json_tok_full(buffer, tok));
|
2018-08-27 07:11:39 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-08-28 22:46:32 +02:00
|
|
|
bool json_tok_feerate(struct command *cmd, const char *name,
|
|
|
|
const char *buffer, const jsmntok_t *tok,
|
|
|
|
u32 **feerate)
|
|
|
|
{
|
|
|
|
jsmntok_t base = *tok, suffix = *tok;
|
|
|
|
enum feerate_style style;
|
|
|
|
unsigned int num;
|
|
|
|
|
2018-08-28 22:46:34 +02:00
|
|
|
for (size_t i = 0; i < NUM_FEERATES; i++) {
|
|
|
|
if (json_tok_streq(buffer, tok, feerate_name(i)))
|
|
|
|
return json_feerate_estimate(cmd, feerate, i);
|
|
|
|
}
|
|
|
|
|
2018-08-28 22:46:32 +02:00
|
|
|
/* We have to split the number and suffix. */
|
|
|
|
suffix.start = suffix.end;
|
|
|
|
while (suffix.start > base.start && !isdigit(buffer[suffix.start-1])) {
|
|
|
|
suffix.start--;
|
|
|
|
base.end--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_to_number(buffer, &base, &num)) {
|
|
|
|
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' prefix should be an integer, not '%.*s'",
|
|
|
|
name, base.end - base.start, buffer + base.start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (json_tok_streq(buffer, &suffix, "")
|
|
|
|
|| json_tok_streq(buffer, &suffix,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE))) {
|
|
|
|
style = FEERATE_PER_KBYTE;
|
|
|
|
} else if (json_tok_streq(buffer, &suffix,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA))) {
|
|
|
|
style = FEERATE_PER_KSIPA;
|
|
|
|
} else {
|
|
|
|
command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
|
|
|
"'%s' suffix should be '%s' or '%s', not '%.*s'",
|
|
|
|
name,
|
|
|
|
json_feerate_style_name(FEERATE_PER_KSIPA),
|
|
|
|
json_feerate_style_name(FEERATE_PER_KBYTE),
|
|
|
|
suffix.end - suffix.start, buffer + suffix.start);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*feerate = tal(cmd, u32);
|
|
|
|
**feerate = feerate_from_style(num, style);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-30 14:54:39 +02:00
|
|
|
bool
|
|
|
|
json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
|
|
|
|
struct channel_id *cid)
|
|
|
|
{
|
|
|
|
return hex_decode(buffer + tok->start, tok->end - tok->start,
|
|
|
|
cid, sizeof(*cid));
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_address(struct json_stream *response, const char *fieldname,
|
2018-03-16 04:45:08 +01:00
|
|
|
const struct wireaddr *addr)
|
|
|
|
{
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
char *addrstr = tal_arr(response, char, INET6_ADDRSTRLEN);
|
|
|
|
if (addr->type == ADDR_TYPE_IPV4) {
|
|
|
|
inet_ntop(AF_INET, addr->addr, addrstr, INET_ADDRSTRLEN);
|
|
|
|
json_add_string(response, "type", "ipv4");
|
|
|
|
json_add_string(response, "address", addrstr);
|
|
|
|
json_add_num(response, "port", addr->port);
|
|
|
|
} else if (addr->type == ADDR_TYPE_IPV6) {
|
|
|
|
inet_ntop(AF_INET6, addr->addr, addrstr, INET6_ADDRSTRLEN);
|
|
|
|
json_add_string(response, "type", "ipv6");
|
|
|
|
json_add_string(response, "address", addrstr);
|
|
|
|
json_add_num(response, "port", addr->port);
|
2018-05-10 01:18:19 +02:00
|
|
|
} else if (addr->type == ADDR_TYPE_TOR_V2) {
|
|
|
|
json_add_string(response, "type", "torv2");
|
|
|
|
json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr));
|
|
|
|
json_add_num(response, "port", addr->port);
|
|
|
|
} else if (addr->type == ADDR_TYPE_TOR_V3) {
|
|
|
|
json_add_string(response, "type", "torv3");
|
|
|
|
json_add_string(response, "address", fmt_wireaddr_without_port(tmpctx, addr));
|
|
|
|
json_add_num(response, "port", addr->port);
|
2018-03-16 04:45:08 +01:00
|
|
|
}
|
|
|
|
json_object_end(response);
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_address_internal(struct json_stream *response,
|
2018-05-07 06:29:21 +02:00
|
|
|
const char *fieldname,
|
|
|
|
const struct wireaddr_internal *addr)
|
|
|
|
{
|
|
|
|
switch (addr->itype) {
|
|
|
|
case ADDR_INTERNAL_SOCKNAME:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "local socket");
|
|
|
|
json_add_string(response, "socket", addr->u.sockname);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-07 06:29:22 +02:00
|
|
|
case ADDR_INTERNAL_ALLPROTO:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "any protocol");
|
|
|
|
json_add_num(response, "port", addr->u.port);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-10 01:18:24 +02:00
|
|
|
case ADDR_INTERNAL_AUTOTOR:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "Tor generated address");
|
|
|
|
json_add_address(response, "service", &addr->u.torservice);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-10 05:44:23 +02:00
|
|
|
case ADDR_INTERNAL_FORPROXY:
|
|
|
|
json_object_start(response, fieldname);
|
|
|
|
json_add_string(response, "type", "unresolved");
|
|
|
|
json_add_string(response, "name", addr->u.unresolved.name);
|
|
|
|
json_add_num(response, "port", addr->u.unresolved.port);
|
|
|
|
json_object_end(response);
|
|
|
|
return;
|
2018-05-07 06:29:21 +02:00
|
|
|
case ADDR_INTERNAL_WIREADDR:
|
|
|
|
json_add_address(response, fieldname, &addr->u.wireaddr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
abort();
|
|
|
|
}
|
2018-08-10 17:00:34 +02:00
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_num(struct json_stream *result, const char *fieldname, unsigned int value)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
json_add_member(result, fieldname, "%u", value);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_double(struct json_stream *result, const char *fieldname, double value)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
json_add_member(result, fieldname, "%f", value);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_u64(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
uint64_t value)
|
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
json_add_member(result, fieldname, "%"PRIu64, value);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_literal(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *literal, int len)
|
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
json_add_member(result, fieldname, "%.*s", len, literal);
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_string(struct json_stream *result, const char *fieldname, const char *value)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
|
|
|
struct json_escaped *esc = json_partial_escape(NULL, value);
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
json_add_member(result, fieldname, "\"%s\"", esc->s);
|
2018-10-19 03:17:48 +02:00
|
|
|
tal_free(esc);
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_bool(struct json_stream *result, const char *fieldname, bool value)
|
2018-10-19 03:17:48 +02:00
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
json_add_member(result, fieldname, value ? "true" : "false");
|
2018-10-19 03:17:48 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_hex(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
const void *data, size_t len)
|
|
|
|
{
|
|
|
|
char *hex = tal_arr(NULL, char, hex_str_size(len));
|
|
|
|
|
|
|
|
hex_encode(data, len, hex, hex_str_size(len));
|
|
|
|
json_add_string(result, fieldname, hex);
|
|
|
|
tal_free(hex);
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_hex_talarr(struct json_stream *result,
|
2018-10-19 03:17:48 +02:00
|
|
|
const char *fieldname,
|
|
|
|
const tal_t *data)
|
|
|
|
{
|
|
|
|
json_add_hex(result, fieldname, data, tal_bytelen(data));
|
|
|
|
}
|
|
|
|
|
2018-10-19 03:17:49 +02:00
|
|
|
void json_add_escaped_string(struct json_stream *result, const char *fieldname,
|
2018-10-19 03:17:48 +02:00
|
|
|
const struct json_escaped *esc TAKES)
|
|
|
|
{
|
2018-11-20 02:46:32 +01:00
|
|
|
json_add_member(result, fieldname, "\"%s\"", esc->s);
|
2018-10-19 03:17:48 +02:00
|
|
|
if (taken(esc))
|
|
|
|
tal_free(esc);
|
|
|
|
}
|