Connecting to Core lightning 🎯 | lndart | cln_grpc | Me and Core Lightning in sky⚡

Kavan Desai
6 min readJul 25, 2022

--

Summer of Bitcoin Week 3!!! Here comes one of the most amazing part of the summer of bitcoin journey. I connected to core lightning with gRPC for the first time and the feeling was just awesome💜.

Story of Me and Core Lightning in sky⚡

The start of week was really hazy but the week ending was super clear with the target and with some handful of working code🙂.

Yeahhh, this week I connected to Core Lightning with dart code✈️️. On of the major milestone of my project was accomplished this week.

And Once upon a time … lol 😂 not like this

I finished my last weeks task and I had made some rough gRPC structure of the project so I initiated some static code for connection in gRPC and I knew that I need to have some certificates included and work on it so I add my certificates in assets folder(very bad stuff). I started to thing about the implementation, how can I include certificates. I read some stackoverflow answers [https://stackoverflow.com/questions/59761842/self-signed-cert-for-grpc-on-flutter] my mentor Vincenzo Palazzo suggested me to look at GO implementation to connect to core lightning [this](it really helped👍🏻). I analysed and had drawn some inference, one of the key inference was that I need to build some function which decodes the TLS certificates and I need to provide it as server credentials in-order to connect. This was the authentification part of gRPC. And I tried to surf how to create this type of function in dart. I found an interesting stackoverflow answer [https://stackoverflow.com/a/68380390] and I used it to make the “LoadTLSChannelCredentials” function of cln_grpc. I set my port to 8001 reserved gRPC port and had a dry run over the code. After some debugging, 💥Boommm💥, I got some lines of response from core lightning for a getinfo method call.

The first thing I did after making the successful connection to Core Lightning was I excitedly inform my mentor Vincenzo Palazzo about it😎. It was like me and lightning flying in sky🌠.

The tasks for this week were not defined as we were developing a totally new experimental library for dart. But we had a stack of tasks 📚to be completed and stack of planning about the project.

Technical👨🏻‍💻:

The below code is the first code spinet with all the shit coding patterns in a raw format🤐.

A channel provides a connection to gRPC server, here we have lightning network as gRPC server. The default behaviour of gRPC can be changed by specifying channel options, such as turning on or off message compression. And channel options play a major role here. We need to connect to a protected network i.e. lightning and thus we need some authentication. Here as a part of authentication we use TLS certificate provided by lightning itself (one can find TLS certificate in their .lightning dir after running node with “— grpc-port=8001”). Now we need a stub which has all the information about the services which the server provides. The stub is initialised as a NodeClient because the protobuf has a class called NodeClient which needs to be invoked here. Stub gets all the information from the server using proto buffer. The protobuf generation commands is mentioned in here.

So now the process is very simple and easy. We have a stub which has all the services and we only need to call the service with the correct request.

Here we go🚀

Photo by Andrew Neel on Unsplash

Next task🎯: Converting a simple dart code to a generic interface

To make a library we need to handle inputs and outputs. In dart there is a very cool feature of declaring a type as “dynamic”. But we should not use it unless needed because if we put every where dynamic we need to always check which type of input user is using in order to get the valid input. Therefore, specifying the type is really necessary. To make a simple code generic in nature we need to indulge with object oriented programming(here we use cs fundamentals😆). We need to implement a class which has some constructor and some functions. In cln_grpc case we need to initialise the gRPC client in constructor. Rather than having a function of connection we choose to initialised client in constructor because we want that users should have a seamless interaction without any knowledge of server and client.

The second part is to create some service specific generic functions which user can call and get that service directly with some couple of lines of code. So in order to create a method we need some specific request as input from user which is bad but again we cannot override any of this protobuf things for sure.

Challenge: — Total of more than 30 services are implemented by server. And total of more than 60 services core lightning provides. Actually, in future we cannot implement all these services as a functions. These problem is a challenge for us.

⚡Me and Core lightning in sky⚡

link

Finished implementing a generic code again with a raw structure. Vincenzo Palazzo taught me how to well format some code and make it readable(Thanks). At the end of 3rd week I was up with some handful of working code. I was really very happy about it and I once even felt that I have aced my project😉.

Actually this project was totally experimental. Vincenzo Palazzo has some idea💡 to implement some clients for core lightning and I was just crazy about gRPC and Dart and Flutter💯. Accidentally the ideas and skills matched and I got selected for Summer of bitcoin. In the 3rd week I achieved one of the biggest milestone for this super experimental project👑. And the end of 3rd week was carzyyyyy🚀.

Research : What are the effects of changing the block interval from the default 600 seconds (10 minutes)?

This was the question assigned to me for the Bitcoin seminars. Question is really interesting because I also had one question about the question which I was going to question someone!😂. So I had a question why 10 minutes why not less that 10 minutes? So I will again give some short insight to a long answer. The average block interval is 10 minutes. And If we make it shorter we need block propagation to be faster. Please check the docs here if interested.

Here are some of the interesting resources📚,

--

--