From 770789684cb6ea8791713b4b7985eacfa6744991 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Mon, 25 Nov 2019 22:33:19 -0300 Subject: [PATCH] jsonrpc: Add json_add_preimage helper --- lightningd/invoice.c | 4 ++-- lightningd/json.c | 6 ++++++ lightningd/json.h | 4 ++++ lightningd/pay.c | 5 ++--- lightningd/test/run-invoice-select-inchan.c | 8 ++++---- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index 0408b2045..cce329b39 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -138,8 +138,7 @@ invoice_payment_serialize(struct invoice_payment_hook_payload *payload, { json_object_start(stream, "payment"); json_add_escaped_string(stream, "label", payload->label); - json_add_hex(stream, "preimage", - &payload->preimage, sizeof(payload->preimage)); + json_add_preimage(stream, "preimage", &payload->preimage); json_add_string(stream, "msat", type_to_string(tmpctx, struct amount_msat, &payload->msat)); @@ -573,6 +572,7 @@ static void gossipd_incoming_channels_reply(struct subd *gossipd, json_add_sha256(response, "payment_hash", &details->rhash); json_add_u64(response, "expires_at", details->expiry_time); json_add_string(response, "bolt11", details->bolt11); + json_add_preimage(response, "preimage", &details->r); /* Warn if there's not sufficient incoming capacity. */ if (tal_count(info->b11->routes) == 0) { diff --git a/lightningd/json.c b/lightningd/json.c index 734549ae8..0e423a086 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -438,6 +438,12 @@ void json_add_sha256(struct json_stream *result, const char *fieldname, json_add_hex(result, fieldname, hash, sizeof(*hash)); } +void json_add_preimage(struct json_stream *result, const char *fieldname, + const struct preimage *preimage) +{ + json_add_hex(result, fieldname, preimage, sizeof(*preimage)); +} + /** * segwit_addr_net_decode - Try to decode a Bech32 address and detect * testnet/mainnet/regtest/signet diff --git a/lightningd/json.h b/lightningd/json.h index a47fe8e44..da64c384a 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -27,6 +27,7 @@ struct json_stream; struct pubkey; struct node_id; struct sha256; +struct preimage; struct short_channel_id; struct wallet_payment; struct wallet_tx; @@ -206,6 +207,9 @@ void json_add_time(struct json_stream *result, const char *fieldname, void json_add_sha256(struct json_stream *result, const char *fieldname, const struct sha256 *hash); +void json_add_preimage(struct json_stream *result, const char *fieldname, + const struct preimage *preimage); + struct command_result *param_bitcoin_address(struct command *cmd, const char *name, const char *buffer, diff --git a/lightningd/pay.c b/lightningd/pay.c index dc3c9a6c7..cd22cbc1c 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -108,9 +108,8 @@ void json_add_payment_fields(struct json_stream *response, break; } if (t->payment_preimage) - json_add_hex(response, "payment_preimage", - t->payment_preimage, - sizeof(*t->payment_preimage)); + json_add_preimage(response, "payment_preimage", + t->payment_preimage); if (t->label) json_add_string(response, "label", t->label); if (t->bolt11) diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index ba0b9d5d9..8768e22eb 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -170,10 +170,6 @@ void json_add_escaped_string(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, const struct json_escape *esc TAKES UNNEEDED) { fprintf(stderr, "json_add_escaped_string called!\n"); abort(); } -/* Generated stub for json_add_hex */ -void json_add_hex(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, - const void *data UNNEEDED, size_t len UNNEEDED) -{ fprintf(stderr, "json_add_hex called!\n"); abort(); } /* Generated stub for json_add_hex_talarr */ void json_add_hex_talarr(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, @@ -194,6 +190,10 @@ void json_add_node_id(struct json_stream *response UNNEEDED, void json_add_num(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, unsigned int value UNNEEDED) { fprintf(stderr, "json_add_num called!\n"); abort(); } +/* Generated stub for json_add_preimage */ +void json_add_preimage(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, + const struct preimage *preimage UNNEEDED) +{ fprintf(stderr, "json_add_preimage called!\n"); abort(); } /* Generated stub for json_add_secret */ void json_add_secret(struct json_stream *response UNNEEDED, const char *fieldname UNNEEDED,