diff --git a/common/json.c b/common/json.c index 678f1b5de..4324aef9b 100644 --- a/common/json.c +++ b/common/json.c @@ -128,6 +128,26 @@ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b) return false; } +u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t *tok) +{ + u8 *result; + size_t hexlen, rawlen; + hexlen = tok->end - tok->start; + rawlen = hex_data_size(hexlen); + + result = tal_arr(ctx, u8, rawlen); + if (!hex_decode(buffer + tok->start, hexlen, result, rawlen)) + return tal_free(result); + + return result; +} + +bool json_to_preimage(const char *buffer, const jsmntok_t *tok, struct preimage *preimage) +{ + size_t hexlen = tok->end - tok->start; + return hex_decode(buffer + tok->start, hexlen, preimage->r, sizeof(preimage->r)); +} + bool json_tok_is_num(const char *buffer, const jsmntok_t *tok) { if (tok->type != JSMN_PRIMITIVE) diff --git a/common/json.h b/common/json.h index d0e5f985b..e024e9e5f 100644 --- a/common/json.h +++ b/common/json.h @@ -1,6 +1,7 @@ #ifndef LIGHTNING_COMMON_JSON_H #define LIGHTNING_COMMON_JSON_H #include "config.h" +#include #include #include #include @@ -21,6 +22,12 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str); /* Allocate a tal string copy */ char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); +/* Decode a hex-encoded binary */ +u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); + +/* Decode a hex-encoded payment preimage */ +bool json_to_preimage(const char *buffer, const jsmntok_t *tok, struct preimage *preimage); + /* Extract number from this (may be a string, or a number literal) */ bool json_to_number(const char *buffer, const jsmntok_t *tok, unsigned int *num);