2021-08-25 12:20:12 +09:30
|
|
|
#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,
|
2021-08-25 12:21:04 +09:30
|
|
|
const char *key, const u8 *data,
|
|
|
|
|
u64 generation)
|
2021-08-25 12:20:12 +09:30
|
|
|
{
|
2021-08-25 12:20:13 +09:30
|
|
|
const char *str;
|
2021-08-25 12:20:12 +09:30
|
|
|
json_add_string(response, "key", key);
|
2021-08-25 12:21:04 +09:30
|
|
|
json_add_u64(response, "generation", generation);
|
2021-08-25 12:20:12 +09:30
|
|
|
json_add_hex(response, "hex", data, tal_bytelen(data));
|
2021-08-25 12:20:13 +09:30
|
|
|
str = utf8_str(response, data, tal_bytelen(data));
|
|
|
|
|
if (str)
|
|
|
|
|
json_add_string(response, "string", str);
|
2021-08-25 12:20:12 +09:30
|
|
|
}
|
|
|
|
|
|
2021-08-25 12:20:37 +09:30
|
|
|
enum ds_mode {
|
|
|
|
|
DS_MUST_EXIST = 1,
|
|
|
|
|
DS_MUST_NOT_EXIST = 2,
|
|
|
|
|
DS_APPEND = 4
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static struct command_result *param_mode(struct command *cmd,
|
|
|
|
|
const char *name,
|
|
|
|
|
const char *buffer,
|
|
|
|
|
const jsmntok_t *tok,
|
|
|
|
|
enum ds_mode **mode)
|
|
|
|
|
{
|
|
|
|
|
*mode = tal(cmd, enum ds_mode);
|
|
|
|
|
if (json_tok_streq(buffer, tok, "must-create"))
|
|
|
|
|
**mode = DS_MUST_NOT_EXIST;
|
|
|
|
|
else if (json_tok_streq(buffer, tok, "must-replace"))
|
|
|
|
|
**mode = DS_MUST_EXIST;
|
|
|
|
|
else if (json_tok_streq(buffer, tok, "create-or-replace"))
|
|
|
|
|
**mode = 0;
|
|
|
|
|
else if (json_tok_streq(buffer, tok, "must-append"))
|
|
|
|
|
**mode = DS_MUST_EXIST | DS_APPEND;
|
|
|
|
|
else if (json_tok_streq(buffer, tok, "create-or-append"))
|
|
|
|
|
**mode = DS_APPEND;
|
|
|
|
|
else
|
|
|
|
|
return command_fail_badparam(cmd, name, buffer, tok,
|
|
|
|
|
"should be 'must-create',"
|
|
|
|
|
" 'must-replace',"
|
|
|
|
|
" 'create-or-replace',"
|
|
|
|
|
" 'must-append',"
|
|
|
|
|
" or 'create-or-append'");
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 12:20:12 +09:30
|
|
|
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;
|
2021-08-25 12:20:13 +09:30
|
|
|
const char *key, *strdata;
|
2021-08-25 12:20:37 +09:30
|
|
|
u8 *data, *prevdata;
|
|
|
|
|
enum ds_mode *mode;
|
2021-08-25 12:21:04 +09:30
|
|
|
u64 *generation, actual_gen;
|
2021-08-25 12:20:12 +09:30
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
|
p_req("key", param_string, &key),
|
2021-08-25 12:20:13 +09:30
|
|
|
p_opt("string", param_string, &strdata),
|
|
|
|
|
p_opt("hex", param_bin_from_hex, &data),
|
2021-08-25 12:20:37 +09:30
|
|
|
p_opt_def("mode", param_mode, &mode, DS_MUST_NOT_EXIST),
|
2021-08-25 12:21:04 +09:30
|
|
|
p_opt("generation", param_u64, &generation),
|
2021-08-25 12:20:12 +09:30
|
|
|
NULL))
|
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
2021-08-25 12:20:13 +09:30
|
|
|
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");
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 12:21:04 +09:30
|
|
|
if (generation && !(*mode & DS_MUST_EXIST))
|
2021-08-25 12:20:12 +09:30
|
|
|
return command_fail(cmd, JSONRPC2_INVALID_PARAMS,
|
2021-08-25 12:21:04 +09:30
|
|
|
"generation only valid with must-replace"
|
|
|
|
|
" or must-append");
|
|
|
|
|
|
|
|
|
|
prevdata = wallet_datastore_fetch(cmd, cmd->ld->wallet, key,
|
|
|
|
|
&actual_gen);
|
|
|
|
|
if ((*mode & DS_MUST_NOT_EXIST) && prevdata)
|
|
|
|
|
return command_fail(cmd, DATASTORE_UPDATE_ALREADY_EXISTS,
|
2021-08-25 12:20:12 +09:30
|
|
|
"Key already exists");
|
|
|
|
|
|
2021-08-25 12:20:37 +09:30
|
|
|
if ((*mode & DS_MUST_EXIST) && !prevdata)
|
2021-08-25 12:21:04 +09:30
|
|
|
return command_fail(cmd, DATASTORE_UPDATE_DOES_NOT_EXIST,
|
2021-08-25 12:20:37 +09:30
|
|
|
"Key does not exist");
|
|
|
|
|
|
2021-08-25 12:21:04 +09:30
|
|
|
if (generation && actual_gen != *generation)
|
|
|
|
|
return command_fail(cmd, DATASTORE_UPDATE_WRONG_GENERATION,
|
|
|
|
|
"generation is different");
|
|
|
|
|
|
2021-08-25 12:20:37 +09:30
|
|
|
if ((*mode & DS_APPEND) && prevdata) {
|
|
|
|
|
size_t prevlen = tal_bytelen(prevdata);
|
|
|
|
|
tal_resize(&prevdata, prevlen + tal_bytelen(data));
|
|
|
|
|
memcpy(prevdata + prevlen, data, tal_bytelen(data));
|
|
|
|
|
data = prevdata;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-25 12:21:04 +09:30
|
|
|
if (prevdata) {
|
2021-08-25 12:20:37 +09:30
|
|
|
wallet_datastore_update(cmd->ld->wallet, key, data);
|
2021-08-25 12:21:04 +09:30
|
|
|
actual_gen++;
|
|
|
|
|
} else {
|
2021-08-25 12:20:37 +09:30
|
|
|
wallet_datastore_create(cmd->ld->wallet, key, data);
|
2021-08-25 12:21:04 +09:30
|
|
|
actual_gen = 0;
|
|
|
|
|
}
|
2021-08-25 12:20:37 +09:30
|
|
|
|
2021-08-25 12:20:12 +09:30
|
|
|
response = json_stream_success(cmd);
|
2021-08-25 12:21:04 +09:30
|
|
|
json_add_datastore(response, key, data, actual_gen);
|
2021-08-25 12:20:12 +09:30
|
|
|
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;
|
2021-08-25 12:21:04 +09:30
|
|
|
u64 generation;
|
2021-08-25 12:20:12 +09:30
|
|
|
|
|
|
|
|
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) {
|
2021-08-25 12:21:04 +09:30
|
|
|
data = wallet_datastore_fetch(cmd, cmd->ld->wallet, key,
|
|
|
|
|
&generation);
|
2021-08-25 12:20:12 +09:30
|
|
|
if (data) {
|
|
|
|
|
json_object_start(response, NULL);
|
2021-08-25 12:21:04 +09:30
|
|
|
json_add_datastore(response, key, data, generation);
|
2021-08-25 12:20:12 +09:30
|
|
|
json_object_end(response);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
struct db_stmt *stmt;
|
|
|
|
|
|
|
|
|
|
for (stmt = wallet_datastore_first(cmd, cmd->ld->wallet,
|
2021-08-25 12:21:04 +09:30
|
|
|
&key, &data, &generation);
|
2021-08-25 12:20:12 +09:30
|
|
|
stmt;
|
|
|
|
|
stmt = wallet_datastore_next(cmd, cmd->ld->wallet,
|
2021-08-25 12:21:04 +09:30
|
|
|
stmt, &key, &data,
|
|
|
|
|
&generation)) {
|
2021-08-25 12:20:12 +09:30
|
|
|
json_object_start(response, NULL);
|
2021-08-25 12:21:04 +09:30
|
|
|
json_add_datastore(response, key, data, generation);
|
2021-08-25 12:20:12 +09:30
|
|
|
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;
|
2021-08-25 12:21:04 +09:30
|
|
|
u64 *generation;
|
|
|
|
|
u64 actual_gen;
|
2021-08-25 12:20:12 +09:30
|
|
|
|
|
|
|
|
if (!param(cmd, buffer, params,
|
|
|
|
|
p_req("key", param_string, &key),
|
2021-08-25 12:21:04 +09:30
|
|
|
p_opt("generation", param_u64, &generation),
|
2021-08-25 12:20:12 +09:30
|
|
|
NULL))
|
|
|
|
|
return command_param_failed();
|
|
|
|
|
|
2021-08-25 12:21:04 +09:30
|
|
|
if (generation) {
|
|
|
|
|
data = wallet_datastore_fetch(cmd, cmd->ld->wallet, key,
|
|
|
|
|
&actual_gen);
|
|
|
|
|
if (data && actual_gen != *generation)
|
|
|
|
|
return command_fail(cmd, DATASTORE_DEL_WRONG_GENERATION,
|
|
|
|
|
"generation is different");
|
|
|
|
|
}
|
|
|
|
|
data = wallet_datastore_remove(cmd, cmd->ld->wallet, key, &actual_gen);
|
2021-08-25 12:20:12 +09:30
|
|
|
if (!data)
|
2021-08-25 12:21:04 +09:30
|
|
|
return command_fail(cmd, DATASTORE_DEL_DOES_NOT_EXIST,
|
2021-08-25 12:20:12 +09:30
|
|
|
"Key does not exist");
|
|
|
|
|
|
|
|
|
|
response = json_stream_success(cmd);
|
2021-08-25 12:21:04 +09:30
|
|
|
json_add_datastore(response, key, data, actual_gen);
|
2021-08-25 12:20:12 +09:30
|
|
|
return command_success(cmd, response);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const struct json_command datastore_command = {
|
|
|
|
|
"datastore",
|
|
|
|
|
"utility",
|
|
|
|
|
json_datastore,
|
2021-08-25 12:20:13 +09:30
|
|
|
"Add a {key} and {hex}/{string} data to the data store",
|
2021-08-25 12:20:12 +09:30
|
|
|
};
|
|
|
|
|
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);
|