From 43fb2a6ed05c45a316a04831b56ca63ca360bc98 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Fri, 8 Nov 2019 19:10:26 +0100 Subject: [PATCH] json: Add helper to extract a secret from JSON --- common/json.c | 6 ++++++ common/json.h | 4 ++++ 2 files changed, 10 insertions(+) diff --git a/common/json.c b/common/json.c index f800a561d..8cd37cdd0 100644 --- a/common/json.c +++ b/common/json.c @@ -143,6 +143,12 @@ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b) return false; } +bool json_to_secret(const char *buffer, const jsmntok_t *tok, struct secret *dest) +{ + return hex_decode(buffer + tok->start, tok->end - tok->start, + dest->data, sizeof(struct secret)); +} + u8 *json_tok_bin_from_hex(const tal_t *ctx, const char *buffer, const jsmntok_t *tok) { u8 *result; diff --git a/common/json.h b/common/json.h index 3206b3d98..355af0103 100644 --- a/common/json.h +++ b/common/json.h @@ -2,6 +2,7 @@ #define LIGHTNING_COMMON_JSON_H #include "config.h" #include +#include #include #include #include @@ -49,6 +50,9 @@ bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num); /* Extract boolean from this */ bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b); +/* Extract a secret from this. */ +bool json_to_secret(const char *buffer, const jsmntok_t *tok, struct secret *dest); + /* Is this a number? [0..9]+ */ bool json_tok_is_num(const char *buffer, const jsmntok_t *tok);