Browse Source

json: add a param parser for a txid

htlc_accepted_hook
lisa neigut 6 years ago
committed by Rusty Russell
parent
commit
a4a193840f
  1. 7
      common/json_helpers.c
  2. 4
      common/json_helpers.h
  3. 15
      lightningd/json.c
  4. 3
      lightningd/json.h
  5. 4
      lightningd/test/run-jsonrpc.c

7
common/json_helpers.c

@ -68,3 +68,10 @@ bool json_to_short_channel_id(const char *buffer, const jsmntok_t *tok,
tok->end - tok->start, scid,
may_be_deprecated_form));
}
bool json_to_txid(const char *buffer, const jsmntok_t *tok,
struct bitcoin_txid *txid)
{
return bitcoin_txid_from_hex(buffer + tok->start,
tok->end - tok->start, txid);
}

4
common/json_helpers.h

@ -2,6 +2,7 @@
#ifndef LIGHTNING_COMMON_JSON_HELPERS_H
#define LIGHTNING_COMMON_JSON_HELPERS_H
#include "config.h"
#include <bitcoin/tx.h>
#include <common/json.h>
struct amount_msat;
@ -35,4 +36,7 @@ bool json_to_sat(const char *buffer, const jsmntok_t *tok,
bool json_to_msat(const char *buffer, const jsmntok_t *tok,
struct amount_msat *msat);
/* Extract a bitcoin txid from this */
bool json_to_txid(const char *buffer, const jsmntok_t *tok,
struct bitcoin_txid *txid);
#endif /* LIGHTNING_COMMON_JSON_HELPERS_H */

15
lightningd/json.c

@ -108,6 +108,21 @@ struct command_result *param_pubkey(struct command *cmd, const char *name,
json_tok_full(buffer, tok));
}
struct command_result *param_txid(struct command *cmd,
const char *name,
const char *buffer,
const jsmntok_t *tok,
struct bitcoin_txid **txid)
{
*txid = tal(cmd, struct bitcoin_txid);
if (json_to_txid(buffer, tok, *txid))
return NULL;
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"'%s' should be txid, not '%.*s'",
name, json_tok_full_len(tok),
json_tok_full(buffer, tok));
}
void json_add_short_channel_id(struct json_stream *response,
const char *fieldname,
const struct short_channel_id *scid)

3
lightningd/json.h

@ -54,6 +54,9 @@ struct command_result *param_pubkey(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct pubkey **pubkey);
struct command_result *param_txid(struct command *cmd, const char *name,
const char *buffer, const jsmntok_t *tok,
struct bitcoin_txid **txid);
/* Makes sure *id is valid. */
struct command_result *param_node_id(struct command *cmd,
const char *name,

4
lightningd/test/run-jsonrpc.c

@ -34,6 +34,10 @@ bool json_to_short_channel_id(const char *buffer UNNEEDED, const jsmntok_t *tok
struct short_channel_id *scid UNNEEDED,
bool may_be_deprecated_form UNNEEDED)
{ fprintf(stderr, "json_to_short_channel_id called!\n"); abort(); }
/* Generated stub for json_to_txid */
bool json_to_txid(const char *buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
struct bitcoin_txid *txid UNNEEDED)
{ fprintf(stderr, "json_to_txid called!\n"); abort(); }
/* Generated stub for log_ */
void log_(struct log *log UNNEEDED, enum log_level level UNNEEDED, const char *fmt UNNEEDED, ...)

Loading…
Cancel
Save