json: add json_add_txid.

I prefer the typesafety of specific functions, rather than having the
caller know that txids are traditionally reversed in bitcoin.

And we already have a bitcoin_txid_to_hex() function for this.

Closes: #411
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2017-12-18 16:50:24 +10:30
parent f792e236eb
commit 553ebc936b
3 changed files with 15 additions and 2 deletions

View File

@ -358,6 +358,15 @@ void json_add_pubkey(struct json_result *response,
json_add_hex(response, fieldname, der, sizeof(der));
}
void json_add_txid(struct json_result *result, const char *fieldname,
const struct sha256_double *txid)
{
char hex[hex_str_size(sizeof(*txid))];
bitcoin_txid_to_hex(txid, hex, sizeof(hex));
json_add_string(result, fieldname, hex);
}
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey)
{

View File

@ -5,6 +5,7 @@
#include <ccan/list/list.h>
#include <common/json.h>
struct sha256_double;
struct wireaddr;
/* Context for a command (from JSON, but might outlive the connection!)
@ -66,6 +67,10 @@ void json_add_pubkey(struct json_result *response,
const char *fieldname,
const struct pubkey *key);
/* '"fieldname" : <hexrev>' or "<hexrev>" if fieldname is NULL */
void json_add_txid(struct json_result *result, const char *fieldname,
const struct sha256_double *txid);
/* Extract a pubkey from this */
bool json_tok_pubkey(const char *buffer, const jsmntok_t *tok,
struct pubkey *pubkey);

View File

@ -456,8 +456,7 @@ static void json_listfunds(struct command *cmd, const char *buffer,
json_array_start(response, "outputs");
for (int i = 0; i < tal_count(utxos); i++) {
json_object_start(response, NULL);
json_add_hex_reversed(response, "txid", &utxos[i]->txid,
sizeof(struct sha256_double));
json_add_txid(response, "txid", &utxos[i]->txid);
json_add_num(response, "output", utxos[i]->outnum);
json_add_u64(response, "value", utxos[i]->amount);
json_object_end(response);