datastore: allow strings.

It's common, and the simplest case.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-08-25 12:20:13 +09:30
committed by Christian Decker
parent dfe2693bbd
commit e711f6c589
11 changed files with 74 additions and 37 deletions

View File

@@ -7,8 +7,12 @@
static void json_add_datastore(struct json_stream *response,
const char *key, const u8 *data)
{
const char *str;
json_add_string(response, "key", key);
json_add_hex(response, "hex", data, tal_bytelen(data));
str = utf8_str(response, data, tal_bytelen(data));
if (str)
json_add_string(response, "string", str);
}
static struct command_result *json_datastore(struct command *cmd,
@@ -17,15 +21,27 @@ static struct command_result *json_datastore(struct command *cmd,
const jsmntok_t *params)
{
struct json_stream *response;
const char *key;
const char *key, *strdata;
u8 *data;
if (!param(cmd, buffer, params,
p_req("key", param_string, &key),
p_req("hex", param_bin_from_hex, &data),
p_opt("string", param_string, &strdata),
p_opt("hex", param_bin_from_hex, &data),
NULL))
return command_param_failed();
if (strdata) {
if (data)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Cannot have both hex and string");
data = tal_dup_arr(cmd, u8, (u8 *)strdata, strlen(strdata), 0);
} else {
if (!data)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Must have either hex or string");
}
if (!wallet_datastore_add(cmd->ld->wallet, key, data))
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Key already exists");
@@ -103,7 +119,7 @@ static const struct json_command datastore_command = {
"datastore",
"utility",
json_datastore,
"Add a {key} and {hex} data to the data store",
"Add a {key} and {hex}/{string} data to the data store",
};
AUTODATA(json_command, &datastore_command);