From 4929034a4047e7b5999b76fdc025b053806a0777 Mon Sep 17 00:00:00 2001 From: trueptolemy Date: Sat, 10 Aug 2019 16:39:04 +0800 Subject: [PATCH] json: Make payment_hash use `json_add_sha256` --- lightningd/invoice.c | 13 +++++-------- lightningd/jsonrpc.c | 2 +- lightningd/pay.c | 2 +- lightningd/peer_control.c | 6 ++---- lightningd/peer_htlcs.c | 6 ++---- lightningd/test/run-invoice-select-inchan.c | 4 ++++ wallet/test/run-wallet.c | 4 ++++ 7 files changed, 19 insertions(+), 18 deletions(-) diff --git a/lightningd/invoice.c b/lightningd/invoice.c index a0f14a9ad..4f393acb4 100644 --- a/lightningd/invoice.c +++ b/lightningd/invoice.c @@ -49,7 +49,7 @@ static void json_add_invoice(struct json_stream *response, { json_add_escaped_string(response, "label", inv->label); json_add_string(response, "bolt11", inv->bolt11); - json_add_hex(response, "payment_hash", &inv->rhash, sizeof(inv->rhash)); + json_add_sha256(response, "payment_hash", &inv->rhash); if (inv->msat) json_add_amount_msat_compat(response, *inv->msat, "msatoshi", "amount_msat"); @@ -514,8 +514,7 @@ static void gossipd_incoming_channels_reply(struct subd *gossipd, details = wallet_invoice_details(info, wallet, invoice); response = json_stream_success(info->cmd); - json_add_hex(response, "payment_hash", &details->rhash, - sizeof(details->rhash)); + json_add_sha256(response, "payment_hash", &details->rhash); json_add_u64(response, "expires_at", details->expiry_time); json_add_string(response, "bolt11", details->bolt11); @@ -1083,9 +1082,8 @@ static struct command_result *json_decodepay(struct command *cmd, json_add_escaped_string(response, "description", take(esc)); } if (b11->description_hash) - json_add_hex(response, "description_hash", - b11->description_hash, - sizeof(*b11->description_hash)); + json_add_sha256(response, "description_hash", + b11->description_hash); json_add_num(response, "min_final_cltv_expiry", b11->min_final_cltv_expiry); if (tal_count(b11->fallbacks)) { @@ -1145,8 +1143,7 @@ static struct command_result *json_decodepay(struct command *cmd, json_array_end(response); } - json_add_hex(response, "payment_hash", - &b11->payment_hash, sizeof(b11->payment_hash)); + json_add_sha256(response, "payment_hash", &b11->payment_hash); json_add_string(response, "signature", type_to_string(cmd, secp256k1_ecdsa_signature, diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 640333119..ba71cf203 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -287,7 +287,7 @@ static struct command_result *json_dev(struct command *cmd UNUSED, /* Hash in place. */ sha256(secret, secret, sizeof(*secret)); response = json_stream_success(cmd); - json_add_hex(response, "rhash", secret, sizeof(*secret)); + json_add_sha256(response, "rhash", secret); return command_success(cmd, response); } } diff --git a/lightningd/pay.c b/lightningd/pay.c index 9b8e445a3..818caec16 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -76,7 +76,7 @@ json_add_payment_fields(struct json_stream *response, 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_sha256(response, "payment_hash", &t->payment_hash); json_add_node_id(response, "destination", &t->destination); json_add_amount_msat_compat(response, t->msatoshi, "msatoshi", "amount_msat"); diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 0419b922a..d8dbead86 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -479,8 +479,7 @@ static void json_add_htlcs(struct lightningd *ld, json_add_amount_msat_compat(response, hin->msat, "msatoshi", "amount_msat"); json_add_u64(response, "expiry", hin->cltv_expiry); - json_add_hex(response, "payment_hash", - &hin->payment_hash, sizeof(hin->payment_hash)); + json_add_sha256(response, "payment_hash", &hin->payment_hash); json_add_string(response, "state", htlc_state_name(hin->hstate)); if (htlc_is_trimmed(REMOTE, hin->msat, local_feerate, @@ -501,8 +500,7 @@ static void json_add_htlcs(struct lightningd *ld, json_add_amount_msat_compat(response, hout->msat, "msatoshi", "amount_msat"); json_add_u64(response, "expiry", hout->cltv_expiry); - json_add_hex(response, "payment_hash", - &hout->payment_hash, sizeof(hout->payment_hash)); + json_add_sha256(response, "payment_hash", &hout->payment_hash); json_add_string(response, "state", htlc_state_name(hout->hstate)); if (htlc_is_trimmed(LOCAL, hout->msat, local_feerate, diff --git a/lightningd/peer_htlcs.c b/lightningd/peer_htlcs.c index 9d94a5f38..1da2d403c 100644 --- a/lightningd/peer_htlcs.c +++ b/lightningd/peer_htlcs.c @@ -734,7 +734,7 @@ static void htlc_accepted_hook_serialize(struct htlc_accepted_hook_payload *p, json_add_amount_msat_only(s, "amount", hin->msat); json_add_u32(s, "cltv_expiry", expiry); json_add_s32(s, "cltv_expiry_relative", expiry - blockheight); - json_add_hex(s, "payment_hash", hin->payment_hash.u.u8, sizeof(hin->payment_hash)); + json_add_sha256(s, "payment_hash", &hin->payment_hash); json_object_end(s); } @@ -2151,9 +2151,7 @@ void json_format_forwarding_object(struct json_stream *response, /* See 6d333f16cc0f3aac7097269bf0985b5fa06d59b4: we may have deleted HTLC. */ if (cur->payment_hash) - json_add_hex(response, "payment_hash", - cur->payment_hash, - sizeof(*cur->payment_hash)); + json_add_sha256(response, "payment_hash", cur->payment_hash); json_add_short_channel_id(response, "in_channel", &cur->channel_in); /* This can be unknown if we failed before channel lookup */ diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index c5e05847d..be54344c9 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -166,6 +166,10 @@ void json_add_escaped_string(struct json_stream *result UNNEEDED, 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_sha256 */ +void json_add_sha256(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, + const struct sha256 *hash UNNEEDED) +{ fprintf(stderr, "json_add_sha256 called!\n"); abort(); } /* Generated stub for json_add_hex_talarr */ void json_add_hex_talarr(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 986396bc6..de6bf2615 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -243,6 +243,10 @@ void json_add_bool(struct json_stream *result UNNEEDED, const char *fieldname UN 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_sha256 */ +void json_add_sha256(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED, + const struct sha256 *hash UNNEEDED) +{ fprintf(stderr, "json_add_sha256 called!\n"); abort(); } /* Generated stub for json_add_hex_talarr */ void json_add_hex_talarr(struct json_stream *result UNNEEDED, const char *fieldname UNNEEDED,