MCPcopy Create free account
hub / github.com/ElementsProject/lightning / plugin_rpcmethod_add

Function plugin_rpcmethod_add

lightningd/plugin.c:1348–1410  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1346}
1347
1348static const char *plugin_rpcmethod_add(struct plugin *plugin,
1349 const char *buffer,
1350 const jsmntok_t *meth)
1351{
1352 const jsmntok_t *nametok, *usagetok, *deprtok;
1353 struct json_command *cmd;
1354 const char *usage, *err;
1355
1356 nametok = json_get_member(buffer, meth, "name");
1357 usagetok = json_get_member(buffer, meth, "usage");
1358 deprtok = json_get_member(buffer, meth, "deprecated");
1359
1360 if (!nametok || nametok->type != JSMN_STRING) {
1361 return tal_fmt(plugin,
1362 "rpcmethod does not have a string \"name\": %.*s",
1363 meth->end - meth->start, buffer + meth->start);
1364 }
1365
1366 if (usagetok && usagetok->type != JSMN_STRING) {
1367 return tal_fmt(plugin,
1368 "\"usage\" is not a string: %.*s",
1369 meth->end - meth->start, buffer + meth->start);
1370 }
1371
1372 cmd = notleak(tal(plugin, struct json_command));
1373 cmd->name = json_strdup(cmd, buffer, nametok);
1374 if (usagetok)
1375 usage = json_strdup(tmpctx, buffer, usagetok);
1376 else
1377 return tal_fmt(plugin,
1378 "\"usage\" not provided by plugin");
1379
1380 usage = json_escape_unescape_len(tmpctx, take(usage), strlen(usage));
1381 if (!usage)
1382 return tal_fmt(plugin,
1383 "\"usage\" contains invalid escape sequences");
1384
1385 err = json_parse_deprecated(cmd, buffer, deprtok, &cmd->depr_start, &cmd->depr_end);
1386 if (err)
1387 return tal_steal(plugin, err);
1388
1389 cmd->dev_only = false;
1390 cmd->dispatch = plugin_rpcmethod_dispatch;
1391 cmd->check = plugin_rpcmethod_check;
1392 if (!jsonrpc_command_add(plugin->plugins->ld->jsonrpc, cmd, take(usage))) {
1393 struct plugin *p =
1394 find_plugin_for_command(plugin->plugins->ld, cmd->name);
1395 if (p)
1396 return tal_fmt(
1397 plugin,
1398 "Could not register method \"%s\", a method with "
1399 "that name is already registered by plugin %s",
1400 cmd->name, p->cmd);
1401 else
1402 return tal_fmt(plugin,
1403 "Could not register method \"%s\", a "
1404 "builtin method with "
1405 "that name is already registered",

Callers 1

plugin_rpcmethods_addFunction · 0.85

Calls 6

json_get_memberFunction · 0.85
json_escape_unescape_lenFunction · 0.85
json_parse_deprecatedFunction · 0.85
jsonrpc_command_addFunction · 0.85
find_plugin_for_commandFunction · 0.70
json_strdupFunction · 0.50

Tested by

no test coverage detected