项目作者: darosior

项目描述 :
C-lightning plugins and RPC library
高级语言: C++
项目地址: git://github.com/darosior/lightningcpp.git
创建时间: 2019-04-07T14:44:17Z
项目社区:https://github.com/darosior/lightningcpp

开源协议:BSD 3-Clause Clear License

下载


lightning-cpp

A C++11 wrapper to build plugins for C-lightning and access its JSON-RPC interface

Installation

This library makes an extensive use of libjson-rpc-cpp. Recommended way is to build it from source (here are Debian instructions but you can find more here) :

  1. apt install libcurl4 libmicrohttpd-dev libjsoncpp-dev libargtable2-dev cmake libhiredis-dev catch libcurl4-openssl-dev
  2. git clone git://github.com/cinemast/libjson-rpc-cpp.git
  3. mkdir -p libjson-rpc-cpp/build
  4. cd libjson-rpc-cpp/build
  5. cmake -DUNIX_DOMAIN_SOCKET_CLIENT=YES .. && make -j8
  6. make install # as root
  7. ldconfig # as root

Then you can install lightningcpp :

  1. git clone git@github.com:darosior/lightningcpp.git
  2. cd lightningcpp
  3. make
  4. su -c "make install"

Usage

RPC wrapper

  1. #include <clightningrpc.h>
  2. #include <iostream>
  3. #include <string>
  4. int main (int argc, char *argv[])
  5. {
  6. CLightningWrapper lightning = CLightningWrapper("/home/darosior/.lightning/lightning-rpc");
  7. std::cout << lightning.getInfo() << std::endl;
  8. return 0;
  9. }

Plugin

You can write a plugin by whether inheriting the RpcMethod class :

  1. #include <clightningplugin.h>
  2. class Helloworld: public RpcMethod {
  3. public:
  4. Helloworld(): RpcMethod() {
  5. name = "hello";
  6. description = "launch me so that I can greet the world";
  7. }
  8. Json::Value main(Json::Value ms) {
  9. return Json::Value("Hello world !");
  10. }
  11. };
  12. int main(int argc, char *argv[]) {
  13. Plugin testPlugin;
  14. Helloworld helloworld;
  15. testPlugin.addMethod(helloworld);
  16. testPlugin.start();
  17. return 0;
  18. }

Or you can instanciate an RpcMethod object and set the main function :

  1. #include <clightningplugin.h>
  2. #include <string>
  3. int main(int argc, char *argv[]) {
  4. Plugin testPlugin;
  5. RpcMethod byeworld("bye", "[name]", "Launch me so I can say bye to someone", "I am a long description");
  6. byeworld.setMain([&](Json::Value ms) {
  7. std::string bye = "Bye bye ";
  8. if (!params.empty())
  9. return Json::Value(bye + params.asString());
  10. return Json::Value(bye + "world !");
  11. });
  12. testPlugin.addMethod(byeworld);
  13. testPlugin.start();
  14. return 0;
  15. }

Options

You can add startup options, for example with the byebye plugin from above :

  1. #include <clightningplugin.h>
  2. #include <string>
  3. int main(int argc, char *argv[]) {
  4. Plugin testPlugin;
  5. testPlugin.addOption("byename", "world", "who to say bye to");
  6. RpcMethod byeworld("bye", "[name]", "Launch me so I can say bye to someone", "A LONG DESC");
  7. byeworld.setMain([&](Json::Value ms) {
  8. std::string bye = "Bye bye ";
  9. if (params.size() == 1)
  10. return Json::Value(bye + params[0].asString() + " !");
  11. return Json::Value(bye + testPlugin.getOptionValue("byename").asString() + " !");
  12. });
  13. testPlugin.addMethod(byeworld);
  14. testPlugin.start();
  15. return 0;
  16. }

You can use addOption() by whether passing it a Json::Value, or the option
name, default value, description, and type (which defaults to string and is
not passed in the above example).

Notifications

You can subscribe to lightningd‘s notifications :

  1. #include <clightningplugin.h>
  2. #include <fstream>
  3. class Helloworld: public RpcMethod {
  4. public:
  5. Helloworld(): RpcMethod() {
  6. name = "hello";
  7. description = "launch me so that I can greet the world";
  8. }
  9. Json::Value main(Json::Value ms) {
  10. return Json::Value("Hello world !");
  11. }
  12. };
  13. void uselessLogger(Json::Value ms) {
  14. std::ofstream logFile;
  15. logFile.open("log_file.log", std::ios::app);
  16. std::string what = params["warning"]["log"].asString();
  17. logFile << what << std::endl;
  18. logFile.close();
  19. }
  20. int main(int argc, char *argv[]) {
  21. Plugin testPlugin;
  22. Helloworld helloworld;
  23. testPlugin.addMethod(helloworld);
  24. testPlugin.subscribe("warning", &uselessLogger);
  25. testPlugin.start();
  26. return 0;
  27. }

Hooks

You can subscribe to lightningd‘s
hooks
(but be sure
of what you are doing) :

  1. #include <clightningplugin.h>
  2. #include <fstream>
  3. class Helloworld: public RpcMethod {
  4. public:
  5. Helloworld(): RpcMethod() {
  6. name = "hello";
  7. description = "launch me so that I can greet the world";
  8. }
  9. Json::Value main(Json::Value ms) {
  10. return Json::Value("Hello world !");
  11. }
  12. };
  13. Json::Value dbDump(Json::Value ms) {
  14. std::ofstream logFile;
  15. logFile.open("db.log", std::ios::app);
  16. std::string operation = params["writes"][0].asString();
  17. logFile << operation << std::endl;
  18. logFile.close();
  19. return Json::Value(true);
  20. }
  21. int main(int argc, char *argv[]) {
  22. Plugin testPlugin;
  23. Helloworld helloworld;
  24. testPlugin.addMethod(helloworld);
  25. testPlugin.hookSubscribe("db_write", &dbDump);
  26. testPlugin.start();
  27. return 0;
  28. }

Design

Brief

Highly inspired by Christian Decker’s pylightning.

Rpc wrapper

The RPC wrapper is just a class (CLightningRpc) with methods wrapping lightningd‘s API.

Plugin interface

There are 2 classes for the Plugin management : Plugin and RpcMethod. Every RPC method added to lightningd must be an instance of
RpcMethod, which is composed of a method’s attributes (namely the name, usage, description and long description) and a pointer to the
function to be executed when the method is called through lightningd.

This pointer defaults to the main(Json::Value&) function of the same class.
Any method’s main function must take a Json::Value object as parameter and return a Json::Value.
You can assign it after instanciation with the setMain() method (as per examples above).

More about C-lightning plugins

You can find more about C-lightning plugins :

Plugins in other languages :

LICENCE

BSD 3-clause clear