diff --git a/common/json_escaped.c b/common/json_escaped.c index 0780bccc7..d60654b92 100644 --- a/common/json_escaped.c +++ b/common/json_escaped.c @@ -13,9 +13,9 @@ struct json_escaped *json_escaped_string_(const tal_t *ctx, return esc; } -struct json_escaped *json_tok_escaped_string(const tal_t *ctx, - const char *buffer, - const jsmntok_t *tok) +struct json_escaped *json_to_escaped_string(const tal_t *ctx, + const char *buffer, + const jsmntok_t *tok) { if (tok->type != JSMN_STRING) return NULL; diff --git a/common/json_escaped.h b/common/json_escaped.h index 75eecdb18..0f742a86f 100644 --- a/common/json_escaped.h +++ b/common/json_escaped.h @@ -17,9 +17,9 @@ struct json_escaped *json_partial_escape(const tal_t *ctx, const char *str TAKES); /* Extract a JSON-escaped string. */ -struct json_escaped *json_tok_escaped_string(const tal_t *ctx, - const char *buffer, - const jsmntok_t *tok); +struct json_escaped *json_to_escaped_string(const tal_t *ctx, + const char *buffer, + const jsmntok_t *tok); /* Are two escaped json strings identical? */ bool json_escaped_eq(const struct json_escaped *a, diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 57c62b9b1..9163f860d 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -131,10 +131,10 @@ static void json_invoice(struct command *cmd, { struct invoice invoice; const struct invoice_details *details; - const jsmntok_t *desctok, *fallbacks; + const jsmntok_t *fallbacks; const jsmntok_t *preimagetok; u64 *msatoshi_val; - struct json_escaped *label_val, *desc; + struct json_escaped *label_val; const char *desc_val; struct json_result *response = new_json_result(cmd); struct wallet *wallet = cmd->ld->wallet; @@ -147,7 +147,7 @@ static void json_invoice(struct command *cmd, if (!param(cmd, buffer, params, p_req("msatoshi", json_tok_msat, &msatoshi_val), p_req("label", json_tok_label, &label_val), - p_req("description", json_tok_tok, &desctok), + p_req("description", json_tok_escaped_string, &desc_val), p_opt_def("expiry", json_tok_u64, &expiry, 3600), p_opt("fallbacks", json_tok_tok, &fallbacks), p_opt("preimage", json_tok_tok, &preimagetok), @@ -166,23 +166,6 @@ static void json_invoice(struct command *cmd, return; } - desc = json_tok_escaped_string(cmd, buffer, desctok); - if (!desc) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "description '%.*s' not a string", - desctok->end - desctok->start, - buffer + desctok->start); - return; - } - desc_val = json_escaped_unescape(cmd, desc); - if (!desc_val) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "description '%s' is invalid" - " (note: we don't allow \\u)", - desc->s); - return; - } - /* description */ if (strlen(desc_val) >= BOLT11_FIELD_BYTE_LIMIT) { command_fail(cmd, JSONRPC2_INVALID_PARAMS, "Descriptions greater than %d bytes " diff --git a/lightningd/json.c b/lightningd/json.c index f621440f2..75f49dcdf 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -129,11 +129,28 @@ bool json_tok_double(struct command *cmd, const char *name, return false; } +bool json_tok_escaped_string(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + const char **str) +{ + struct json_escaped *esc = json_to_escaped_string(cmd, buffer, tok); + if (esc) + if ((*str = json_escaped_unescape(cmd, esc))) + return true; + + command_fail(cmd, JSONRPC2_INVALID_PARAMS, + "'%s' should be a string, not '%.*s'" + " (note, we don't allow \\u)", + name, + tok->end - tok->start, buffer + tok->start); + return false; +} + bool json_tok_label(struct command *cmd, const char *name, const char * buffer, const jsmntok_t *tok, struct json_escaped **label) { - if ((*label = json_tok_escaped_string(cmd, buffer, tok))) + if ((*label = json_to_escaped_string(cmd, buffer, tok))) return true; /* Allow literal numbers */ diff --git a/lightningd/json.h b/lightningd/json.h index f90da4213..531eef896 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -54,6 +54,11 @@ bool json_tok_double(struct command *cmd, const char *name, const char *buffer, const jsmntok_t *tok, double **num); +/* Extract an escaped string (and unescape it) */ +bool json_tok_escaped_string(struct command *cmd, const char *name, + const char * buffer, const jsmntok_t *tok, + const char **str); + /* Extract a label. It is either an escaped string or a number. */ bool json_tok_label(struct command *cmd, const char *name, const char * buffer, const jsmntok_t *tok, diff --git a/lightningd/pay.c b/lightningd/pay.c index 304b24e3a..64ac0cc63 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -945,19 +945,18 @@ static void json_sendpay_on_resolve(const struct sendpay_result* r, static void json_sendpay(struct command *cmd, const char *buffer, const jsmntok_t *params) { - const jsmntok_t *routetok, *desctok; + const jsmntok_t *routetok; const jsmntok_t *t, *end; size_t n_hops; struct sha256 *rhash; struct route_hop *route; u64 *msatoshi; - const struct json_escaped *desc; const char *description; if (!param(cmd, buffer, params, p_req("route", json_tok_tok, &routetok), p_req("payment_hash", json_tok_sha256, &rhash), - p_opt("description", json_tok_tok, &desctok), + p_opt("description", json_tok_escaped_string, &description), p_opt("msatoshi", json_tok_u64, &msatoshi), NULL)) return; @@ -1017,28 +1016,6 @@ static void json_sendpay(struct command *cmd, } } - if (desctok) { - desc = json_tok_escaped_string(cmd, buffer, desctok); - if (!desc) { - command_fail(cmd, JSONRPC2_INVALID_PARAMS, - "description '%.*s' not a string", - desctok->end - desctok->start, - buffer + desctok->start); - return; - } - description = json_escaped_unescape(cmd, desc); - if (description == NULL) { - command_fail( - cmd, JSONRPC2_INVALID_PARAMS, - "description '%.*s' not a valid escaped string", - desctok->end - desctok->start, - buffer + desctok->start); - return; - } - } else { - description = NULL; - } - if (send_payment(cmd, cmd->ld, rhash, route, msatoshi ? *msatoshi : route[n_hops-1].amount, description,