plugins: use json_scan.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-01-06 16:11:19 +10:30
committed by Christian Decker
parent 35e8949df3
commit b61da8c5a9
4 changed files with 74 additions and 113 deletions

View File

@@ -2,6 +2,7 @@
#include <ccan/array_size/array_size.h>
#include <ccan/tal/str/str.h>
#include <common/gossmap.h>
#include <common/type_to_string.h>
#include <plugins/libplugin-pay.h>
#include <plugins/libplugin.h>
#include <wire/onion_wire.h>
@@ -244,13 +245,11 @@ static struct command_result *htlc_accepted_call(struct command *cmd,
const char *buf,
const jsmntok_t *params)
{
const jsmntok_t *payloadt = json_delve(buf, params, ".onion.payload");
const jsmntok_t *payment_hash_tok = json_delve(buf, params, ".htlc.payment_hash");
const u8 *rawpayload;
struct sha256 payment_hash;
size_t max;
struct tlv_tlv_payload *payload;
struct tlv_field *preimage_field = NULL;
char *hexpreimage, *hexpaymenthash;
bigsize_t s;
bool unknown_even_type = false;
struct tlv_field *field;
@@ -258,10 +257,11 @@ static struct command_result *htlc_accepted_call(struct command *cmd,
struct out_req *req;
struct timeabs now = time_now();
if (!payloadt)
if (!json_scan(buf, params, "{onion:{payload:%},htlc:{payment_hash:%}}",
JSON_SCAN_TAL(cmd, json_tok_bin_from_hex, &rawpayload),
JSON_SCAN(json_to_sha256, &payment_hash)))
return htlc_accepted_continue(cmd, NULL);
rawpayload = json_tok_bin_from_hex(cmd, buf, payloadt);
max = tal_bytelen(rawpayload);
payload = tlv_tlv_payload_new(cmd);
@@ -272,7 +272,8 @@ static struct command_result *htlc_accepted_call(struct command *cmd,
if (!fromwire_tlv_payload(&rawpayload, &max, payload)) {
plugin_log(
cmd->plugin, LOG_UNUSUAL, "Malformed TLV payload %.*s",
payloadt->end - payloadt->start, buf + payloadt->start);
json_tok_full_len(params),
json_tok_full(buf, params));
return htlc_accepted_continue(cmd, NULL);
}
@@ -318,20 +319,18 @@ static struct command_result *htlc_accepted_call(struct command *cmd,
ki->payload = tal_steal(ki, payload);
ki->preimage_field = preimage_field;
hexpreimage = tal_hex(cmd, preimage_field->value);
/* If the preimage doesn't hash to the payment_hash we must continue,
* maybe someone else knows how to handle these. */
sha256(&ki->payment_hash, preimage_field->value, preimage_field->length);
hexpaymenthash = tal_hexstr(cmd, &ki->payment_hash, sizeof(ki->payment_hash));
if (!json_tok_streq(buf, payment_hash_tok, hexpaymenthash)) {
if (!sha256_eq(&ki->payment_hash, &payment_hash)) {
plugin_log(
cmd->plugin, LOG_UNUSUAL,
"Preimage provided by the sender does not match the "
"payment_hash: SHA256(%s)=%s != %.*s. Ignoring keysend.",
hexpreimage, hexpaymenthash,
payment_hash_tok->end - payment_hash_tok->start,
buf + payment_hash_tok->start);
"payment_hash: SHA256(%s)=%s != %s. Ignoring keysend.",
tal_hexstr(tmpctx,
preimage_field->value, preimage_field->length),
type_to_string(tmpctx, struct sha256, &ki->payment_hash),
type_to_string(tmpctx, struct sha256, &payment_hash));
tal_free(ki);
return htlc_accepted_continue(cmd, NULL);
}
@@ -348,7 +347,7 @@ static struct command_result *htlc_accepted_call(struct command *cmd,
&htlc_accepted_invoice_created,
ki);
plugin_log(cmd->plugin, LOG_INFORM, "Inserting a new invoice for keysend with payment_hash %s", hexpaymenthash);
plugin_log(cmd->plugin, LOG_INFORM, "Inserting a new invoice for keysend with payment_hash %s", type_to_string(tmpctx, struct sha256, &payment_hash));
json_add_string(req->js, "msatoshi", "any");
json_add_string(req->js, "label", ki->label);
json_add_string(req->js, "description", "Spontaneous incoming payment through keysend");