Introduction to JRPCLightning 0.2.2

Vincenzo Palazzo
3 min readOct 29, 2021

A new version of JRPClightning is out, with this release there are several improvements for JVM clients that want to integrate c-lightning with RPC call in it.

In particular, this library was originally developed for learning purposes with design patterns and Java, for this reason, there are some use cases where some patterns used are considered “antipattern” for the modern developing model, like the Singleton pattern.

One problem that Singleton caused is the ability to create different instances of the client linked to different lightning nodes. For this reason, version 0.2.2 of the library has now deprecated the Singleton that will be removed in version 0.3.0, and implementing a lite client where there is only a generic method to call all the types of commands in c-lightning.

Walkthrough the changes!

A big exciting change in the library is the methodology to instantiate a new RPC interface, in particular, it is possible to use the following classes:

  • LiteCLightningRPC: It is a lite client version to talk with the c-lightning RPC interface. In particular, you have two methods to make calls, that are described in the java-doc.
  • CLightningRPC: It is a more complex client than the previous one, and it offers a complete wrapping around c-lightning, where you can work only with Java Object without worry the c-lightning JSON response format. More on java-doc

Code Example with LiteCLightningRPC

A very cool property to have in a library is the possibility to give the user the power to use all the minimal amount of work to achieve the result that he wants, and this in JRPCLightning is achieved with the LiteCLightningRPC that offers a low-level API to make an RPC request to the method, but at the same time a very customizable method.

In fact, LiteCLightningRPC offers two methods that are:

  • call: That takes a String that is the method name, and a Java class where wrapping the result, and it returns a Java Object with the type specified and filled with the JSON payload received by the user.
  • rawCall​: More commonly in a library that wants to integrate, c-lightning RPC calls in it and has already some ​JSON library like GSON or Jackson with some interface to wrapping the library. The method rawCall returns the complete payload received from the c-lightning UNIX socket, so you can use the library together with your architecture.

An example of usage can be the following one:

public class TestLiteCLightningRPC {
private LiteCLightningRPC liteRpc = new LiteCLightningRPC();

@Test
public void testGetInfoCall() {
CLightningGetInfo getInfo = liteRpc.call("getinfo", CLightningGetInfo.class);
TestCase.assertFalse(getInfo.getId().isEmpty());
}
}

You can access this example on the source code at the following link

How the class CLightningRPC looks without singleton?

This is a brief recall of how the library API was, in fact, to make an RPC call you could use the following code:

CLightningGetInfo infoNode = CLightningRPC.getInstance().getInfo();
String id = infoNode.getId();
String color = infoNode.getColor();
System.out.println("id=" + id);
System.out.println("color=" + color);

Now the getInstance() method is deprecated and it will be removed in the next major release 0.3.0. However, this is available already the new API, where you can instantiate the client with the following code

CLightningRPC client = new CLightningRPC();
CLightningGetInfo infoNode = client.getInfo();
String id = infoNode.getId();
String color = infoNode.getColor();
System.out.println("id=" + id);
System.out.println("color=" + color);

In this case, the constructor will load a file of properties if exist with the name clightning-rpc.properties that need to contain the following content:

RPC_DIR=/path/rpc/file/lightning-rpc

Or you can pass the path as a parameter constructor, as the following example

String path = "/home/.lightning/lightning-rpc";
CLightningRPC client = new CLightningRPC(path);
CLightningGetInfo infoNode = client.getInfo();
String id = infoNode.getId();
String color = infoNode.getColor();
System.out.println("id=" + id);
System.out.println("color=" + color);

Conclusion

This version of the library brings in several improvements, and fixes some performance issues, and one of the important things is that now it is possible to bring the library inside another library or tool.

Look more on the Github Release https://github.com/clightning4j/JRPClightning/releases/tag/v0.2.2

Supports

In addition, there is also a bolt 12

lno1pg357enxv4ezqar0ypehqmmwwdhhygz22fgyxnrfva58gmnfdenjqam0wf43ug8zgz9ynura9aqg8frngsfca7y7wct7vwgeyqkf925dfx6hfftq4mcypm3wjg9cfkk37v92kjxpxae590az2xw9nnfe5ar8afs74rvr5e2lrej50cjlw8ldzjtj7s50qj3hfytjyg2pywkl2c7e7643zflgfues

Or, simply connect to my ln node to grow my network :) https://bruce.lnmetrics.info

You can find me on Github @vincenzopalazzo

--

--

Vincenzo Palazzo

I'm an Open Source software developer, and in my free time, I'm a master's student. My main focus is to build some tools useful for other developers.