Code Monkey home page Code Monkey logo

lndart.cln_grpc's Introduction

lndart.cln_grpc

๐ŸŽฏ Dart GRPC client for core lightning ๐ŸŽฏ

GitHub Workflow Status

Table of Content

  • Introduction
  • How to Use
  • How Contribute
  • License

Introduction

A dart library which facilitates dart GRPC client for core lightning

Why cln_grpc?

cln_grpc library is a GRPC wrapper with a generic interface which facilitates easy interaction with core lightning for dart and flutter. It can be used to direclty access core lightning node and to perform some operations on it such as calling rpc method supported by the GRPC interface.

How to Use

How to connect to core lightning using cln_grpc

To connect to a grpc server we need port of server, host and server credentials

Here in this library the only required parameter is path to certificates for authentification, and also provides some optional parameters such as

  • host("localhost" by default): Where the server is running, this need to match what is declared inside the tls certificate
  • port(8001 by default): where the grpc server is running, specified as grpc-port in the core lightning option
  • authority("localhost" by default)
  • channelcredentials: Options controlling TLS security settings on a ClientChannel.

To initiate the client and make a connection with the server we declare client like this:

var client = GRPCClient(rootPath: "<path_to_certificates>");

How to call a method:

There are several ways to call a method

  1. call getinfo with a parse function
  var response = await client.getinfo(onDecode: (Map<String, dynamc> json) => json);
  print('Response from server\n$response');
  1. generic call <T, R> with a parse function

Here we need to serialize the request method in order to encode it from json to map

class ListTransactionProxy extends Serializable {
  ListtransactionsRequest proxy;

  ListTransactionProxy(this.proxy);

  factory ListTransactionProxy.build() =>
      ListTransactionProxy(ListtransactionsRequest());

  @override
  Map<String, dynamic> toJSON() => proxy.toProto3Json() as Map<String, dynamic>;

  @override
  T as<T>() => proxy as T;
}

after serializing the request we call the generic "call" method and provide some requires parameters as below

  var transactionList =
      await client.call<ListTransactionProxy, ListtransactionsResponse>(
          method: "listtransactions", params: ListTransactionProxy.build());
  print("Transactions of node");
  print(transactionList.transactions);
  1. Direct acces to stub incase a method is not found implemented(skip the library architecture)
  var forwardsList=await client.stub.listForwards(ListforwardsRequest());
  print("forwards of node");
  print(forwardsList.forwards);

Close client connection:

Shutting down the grpc client connection is really easy calling the close method like this:

  client.close();

How Contribute

Build system

You can use the make file to make sure that your code can pass the sanity code check of the CI:

The make file contains the following target:

  • make: formatting, analyze and compile the code;
  • make fmt: formatting and analyze the code;
  • make check: run the unit test (if any)

Read out Hacking guide to find and learn on how we manage the change request (Pull request) workflow.

Generation Code

In order to update the code client from the new proto file generated by core lightning, we need to copy the new file in the proton we need to generate some dart files from the protos, and use the following commands to update the dart code:

Requirements:

  • Upgrade protobuf version equal or above release 3.15.
  • Add protoc-gen-dart in your path.
  • Or else use this command protoc --dart_out=grpc:./lib/src/ --plugin=protoc-gen-dart=$HOME/.pub-cache/bin/protoc-gen-dart ./protos/primitives.proto ./protos/node.proto with plugin flag to generate the .pb.dart files.

Here are the steps to generate .pb.dart files:

  • Be on the root of the project.
  • Run this command make gen to generate the .pb.dart files for both /primitives.proto and /node.proto.
  • Rename ./lib/src/protos to ./lib/src/generated.

License

Copyright 2022 Vincenzo Palazzo <[email protected]>. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of Google Inc. nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

lndart.cln_grpc's People

Contributors

kavan-desai avatar vincenzopalazzo avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

lndart.cln_grpc's Issues

try to generalize the GRPC call in a way that allow the user to forget that are using the grpc client

This is very fancy and experimental stuff, however, we can use the good feature of the dart that is dynamic to make a generic call

This is the diff that I did on my local implementation

diff --git a/example/grpc_client_example.dart b/example/grpc_client_example.dart
index 7a8ea98..3611e59 100644
--- a/example/grpc_client_example.dart
+++ b/example/grpc_client_example.dart
@@ -4,5 +4,10 @@ Future<void> main(List<String> args) async {
   var client = GRPCClient(rootPath: args[0], certClientPath: "/");
   var response = await client.getinfo();
   print('Response from server\n$response');
+
+  /// It is possible also use the generic call like
+
+  response = await client.call<GetinfoRequest, GetinfoResponse>(method: "getinfo", payload: GetinfoRequest());
+  print("Hello from node ${response.alias}");
   client.close();
 }
diff --git a/lib/src/cln_grpc_base.dart b/lib/src/cln_grpc_base.dart
index dc4ef70..11d2684 100644
--- a/lib/src/cln_grpc_base.dart
+++ b/lib/src/cln_grpc_base.dart
@@ -73,8 +73,12 @@ class GRPCClient {
     print("Client Shutdown");
   }
 
-// This is Vincent dream!
-// Future<T> call<T>({required String method, payload = HashMap<String,dynamic>}) async {
-//   return ;
-// }
+  /// generic call method that is able to call any type of GRPC method, and allow
+  /// us to make the interface equal to the dart.clightning package
+  Future<T> call<R, T>({required String method, required R payload}) async {
+    switch (method) {
+      case "getinfo": return await stub.getinfo(payload as GetinfoRequest) as T;
+    }
+   throw Exception("method $method not found, report a issue on Github or try to use client.stub to use the raw grpc client");
+  }
 }

and this is the call implementation

  /// generic call method that is able to call any type of GRPC method, and allow
  /// us to make the interface equal to the dart.clightning package
  Future<T> call<R, T>({required String method, required R payload}) async {
    switch (method) {
      case "getinfo": return await stub.getinfo(payload as GetinfoRequest) as T;
    }
   throw Exception("method $method not found, report a issue on Github or try to use client.stub to use the raw grpc client");
  }

What do you think to use this pattern?

give the user to be able to change the stub, in the case that he want use a older version of core lightning

older version of core lighting required an older version of the proton file (usually) and we need to allow the user to change the GRPC implementation and use the own implementation

Also, I'm assuming that https://blog.blockstream.com/en-greenlight-by-blockstream-lightning-made-easy/ by blockstre will use the grpc and this can allow a possible user of green light to use these API

We need to think a little bit on it

rpc: the grpc proto file where the API are generated need to be last tagged version

The actual grpc API are generated in the current version of master, and the listtransaction want an additional filed, on my node I receive the following error

Another exception was thrown: Exception: gRPC Error (code: 2, codeName: UNKNOWN, message: Error calling method ListTransactions: RpcError { code: None, message: "Malformed response from lightningd: missing field `amount_msat`" },
details: [], rawResponse: null, trailers: {date: Sun, 26 Jun 2022 17:00:06 GMT})

We need to generate the GRPC file from the last release https://github.com/ElementsProject/lightning/releases

P.S: You need to check out the git tag!

provide a GRPC client implementation

We need to have a client class implementation, with some interface like

class GRPCClient {
  String? rootPath;
  String? certClientPath;
  ChannelOptions opts;
  late ClientOption client;

  GRPCClient({this.rootPath, this.certClientPath, this.opts = ChannelCredentials.insecure()}) {
    //TODO init client here with the not null option
  }

 Future<GetInfoResponse> getinfo() { }
 
  // This is Vincent dream!
 Future<T> call<T>({required String method, payload: HashMap<String, dynamic> = {}}) { }
}

ci: release ignore file (??)

Generate file cause following error

NullSafetyCompliance.compliant
Package validation found the following errors:
* line 10, column 1 of lib/src/generated/node.pb.dart: This package does not have fixnum in the `dependencies` section of `pubspec.yaml`.
     โ•ท
  10 โ”‚ import 'package:fixnum/fixnum.dart' as $fixnum;
     โ”‚ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     โ•ต
* line 10, column 1 of lib/src/generated/primitives.pb.dart: This package does not have fixnum in the `dependencies` section of `pubspec.yaml`.

ci failure

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
grpc_test_1   | โ”‚ #0   LogManager.debug (package:cln_common/src/logger/logger_manager.dart:16:13)
grpc_test_1   | โ”‚ dart-lightning/lndart.clnapp#1   new AppListFunds.fromJSON (package:clnapp/model/app_model/list_funds.dart:18:28)
grpc_test_1   | โ”œโ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„โ”„
grpc_test_1   | โ”‚ ๐Ÿ› Full listfunds json received: {}
grpc_test_1   | โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
grpc_test_1   | 
00:15 +4 -1: /root/test/grpc_client_appapi_test.dart: clnapp gRPC_client tests API List Funds [E]                                                                                                      
grpc_test_1   |   type 'Null' is not a subtype of type 'List<dynamic>' in type cast
grpc_test_1   |   package:clnapp/model/app_model/list_funds.dart 23:29  new AppListFunds.fromJSON
grpc_test_1   |   package:clnapp/api/cln/cln_client.dart 78:52          CLNApi.listFunds.<fn>
grpc_test_1   |   package:cln_grpc/src/cln_grpc_base.dart 101:22        GRPCClient.listFunds
grpc_test_1   |   
grpc_test_1   | 
grpc_test_1   | To run this test again: /usr/local/flutter/bin/cache/dart-sdk/bin/dart test /root/test/grpc_client_appapi_test.dart -p vm --plain-name 'clnapp gRPC_client tests API List Funds'

docs: wrong to json method

class ListTransactionProxy extends Serializable {
  ListtransactionsRequest proxy;

  ListTransactionProxy(this.proxy);

  factory ListTransactionProxy.build() =>
      ListTransactionProxy(ListtransactionsRequest());

  @override
  Map<String, dynamic> toJSON() => proxy.writeToJsonMap();

  @override
  T as<T>() => proxy as T;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.