From 415c2bfe3ca6d0ac38f9a2ecc33c2a80ee6b48c0 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 18 Jun 2021 17:21:41 +0200 Subject: [PATCH] jsonrpc: Add parsers for routehint-arrays We'll start passing routehints manually to keysend to reach non-public nodes as well. --- common/json_tok.c | 70 +++++++++++++++++++++++++++++++++++++++++ common/json_tok.h | 10 ++++++ common/test/run-param.c | 4 +++ 3 files changed, 84 insertions(+) diff --git a/common/json_tok.c b/common/json_tok.c index 8f6b8e4f3..4c598dade 100644 --- a/common/json_tok.c +++ b/common/json_tok.c @@ -584,3 +584,73 @@ struct command_result *param_extra_tlvs(struct command *cmd, const char *name, *fields = temp; return NULL; } + +struct command_result *param_routehint(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct route_info **ri) +{ + size_t i; + const jsmntok_t *curr; + const char *err; + + if (tok->type != JSMN_ARRAY) { + return command_fail( + cmd, JSONRPC2_INVALID_PARAMS, + "Routehint %s (\"%s\") is not an array of hop objects", + name, json_strdup(tmpctx, buffer, tok)); + } + + *ri = tal_arr(cmd, struct route_info, tok->size); + json_for_each_arr(i, curr, tok) { + struct route_info *e = &(*ri)[i]; + struct amount_msat temp; + + err = json_scan(tmpctx, buffer, curr, + "{id:%,scid:%,feebase:%,feeprop:%,expirydelta:%}", + JSON_SCAN(json_to_node_id, &e->pubkey), + JSON_SCAN(json_to_short_channel_id, &e->short_channel_id), + JSON_SCAN(json_to_msat, &temp), + JSON_SCAN(json_to_u32, &e->fee_proportional_millionths), + JSON_SCAN(json_to_u16, &e->cltv_expiry_delta) + ); + e->fee_base_msat = + temp.millisatoshis; /* Raw: internal conversion. */ + if (err != NULL) { + return command_fail( + cmd, JSONRPC2_INVALID_PARAMS, + "Error parsing routehint %s[%zu]: %s", name, i, + err); + } + } + return NULL; +} + +struct command_result * +param_routehint_array(struct command *cmd, const char *name, const char *buffer, + const jsmntok_t *tok, struct route_info ***ris) +{ + size_t i; + const jsmntok_t *curr; + char *element_name; + struct command_result *err; + if (tok->type != JSMN_ARRAY) { + return command_fail( + cmd, JSONRPC2_INVALID_PARAMS, + "Routehint array %s (\"%s\") is not an array", + name, json_strdup(tmpctx, buffer, tok)); + } + + *ris = tal_arr(cmd, struct route_info *, 0); + json_for_each_arr(i, curr, tok) { + struct route_info *element; + element_name = tal_fmt(cmd, "%s[%zu]", name, i); + err = param_routehint(cmd, element_name, buffer, curr, &element); + if (err != NULL) { + return err; + } + tal_arr_expand(ris, element); + + tal_free(element_name); + } + return NULL; +} diff --git a/common/json_tok.h b/common/json_tok.h index d3b539bf8..4b57e9beb 100644 --- a/common/json_tok.h +++ b/common/json_tok.h @@ -3,6 +3,7 @@ #define LIGHTNING_COMMON_JSON_TOK_H #include "config.h" #include +#include #include #include #include @@ -194,4 +195,13 @@ struct command_result *param_extra_tlvs(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, struct tlv_field **fields); + +struct command_result *param_routehint(struct command *cmd, const char *name, + const char *buffer, const jsmntok_t *tok, + struct route_info **ri); + +struct command_result * +param_routehint_array(struct command *cmd, const char *name, const char *buffer, + const jsmntok_t *tok, struct route_info ***ris); + #endif /* LIGHTNING_COMMON_JSON_TOK_H */ diff --git a/common/test/run-param.c b/common/test/run-param.c index b644397c1..b4a577c9a 100644 --- a/common/test/run-param.c +++ b/common/test/run-param.c @@ -49,6 +49,10 @@ bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED, bool json_to_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct channel_id *cid UNNEEDED) { fprintf(stderr, "json_to_channel_id called!\n"); abort(); } +/* Generated stub for json_to_msat */ +bool json_to_msat(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, + struct amount_msat *msat UNNEEDED) +{ fprintf(stderr, "json_to_msat called!\n"); abort(); } /* Generated stub for json_to_node_id */ bool json_to_node_id(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED, struct node_id *id UNNEEDED)