JSON-RPC: add interfaces to access datastore.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Changelog-Added: JSON-RPC: `datastore`, `deldatastore` and `listdatastore` for plugins to store simple persistent key/value data.
This commit is contained in:
Rusty Russell
2021-08-25 12:20:12 +09:30
committed by Christian Decker
parent 2fb8735f04
commit 1b48545e5b
3 changed files with 128 additions and 0 deletions

View File

@@ -38,8 +38,10 @@ c-lightning Documentation
lightning-connect <lightning-connect.7.md>
lightning-createinvoice <lightning-createinvoice.7.md>
lightning-createonion <lightning-createonion.7.md>
lightning-datastore <lightning-datastore.7.md>
lightning-decode <lightning-decode.7.md>
lightning-decodepay <lightning-decodepay.7.md>
lightning-deldatastore <lightning-deldatastore.7.md>
lightning-delexpiredinvoice <lightning-delexpiredinvoice.7.md>
lightning-delinvoice <lightning-delinvoice.7.md>
lightning-delpay <lightning-delpay.7.md>
@@ -63,6 +65,7 @@ c-lightning Documentation
lightning-keysend <lightning-keysend.7.md>
lightning-listchannels <lightning-listchannels.7.md>
lightning-listconfigs <lightning-listconfigs.7.md>
lightning-listdatastore <lightning-listdatastore.7.md>
lightning-listforwards <lightning-listforwards.7.md>
lightning-listfunds <lightning-listfunds.7.md>
lightning-listinvoices <lightning-listinvoices.7.md>

View File

@@ -41,6 +41,7 @@ LIGHTNINGD_SRC := \
LIGHTNINGD_SRC_NOHDR := \
lightningd/datastore.c \
lightningd/offer.c \
lightningd/signmessage.c

124
lightningd/datastore.c Normal file
View File

@@ -0,0 +1,124 @@
#include <common/param.h>
#include <lightningd/json.h>
#include <lightningd/jsonrpc.h>
#include <lightningd/lightningd.h>
#include <wallet/wallet.h>
static void json_add_datastore(struct json_stream *response,
const char *key, const u8 *data)
{
json_add_string(response, "key", key);
json_add_hex(response, "hex", data, tal_bytelen(data));
}
static struct command_result *json_datastore(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
const char *key;
u8 *data;
if (!param(cmd, buffer, params,
p_req("key", param_string, &key),
p_req("hex", param_bin_from_hex, &data),
NULL))
return command_param_failed();
if (!wallet_datastore_add(cmd->ld->wallet, key, data))
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Key already exists");
response = json_stream_success(cmd);
json_add_datastore(response, key, data);
return command_success(cmd, response);
}
static struct command_result *json_listdatastore(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
const char *key;
const u8 *data;
if (!param(cmd, buffer, params,
p_opt("key", param_string, &key),
NULL))
return command_param_failed();
response = json_stream_success(cmd);
json_array_start(response, "datastore");
if (key) {
data = wallet_datastore_fetch(cmd, cmd->ld->wallet, key);
if (data) {
json_object_start(response, NULL);
json_add_datastore(response, key, data);
json_object_end(response);
}
} else {
struct db_stmt *stmt;
for (stmt = wallet_datastore_first(cmd, cmd->ld->wallet,
&key, &data);
stmt;
stmt = wallet_datastore_next(cmd, cmd->ld->wallet,
stmt, &key, &data)) {
json_object_start(response, NULL);
json_add_datastore(response, key, data);
json_object_end(response);
}
}
json_array_end(response);
return command_success(cmd, response);
}
static struct command_result *json_deldatastore(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
const char *key;
u8 *data;
if (!param(cmd, buffer, params,
p_req("key", param_string, &key),
NULL))
return command_param_failed();
data = wallet_datastore_remove(cmd, cmd->ld->wallet, key);
if (!data)
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
"Key does not exist");
response = json_stream_success(cmd);
json_add_datastore(response, key, data);
return command_success(cmd, response);
}
static const struct json_command datastore_command = {
"datastore",
"utility",
json_datastore,
"Add a {key} and {hex} data to the data store",
};
AUTODATA(json_command, &datastore_command);
static const struct json_command deldatastore_command = {
"deldatastore",
"utility",
json_deldatastore,
"Remove a {key} from the data store",
};
AUTODATA(json_command, &deldatastore_command);
static const struct json_command listdatastore_command = {
"listdatastore",
"utility",
json_listdatastore,
"List the datastore, optionally only {key}",
};
AUTODATA(json_command, &listdatastore_command);