jlibra – Connecting Libra to Java

Having learned from other Blockchain implementations, Libra starts with a nice package, a sound framework and concept and several developer goodies which other implementations had to invent first.

Right from the start Libra keeps mass adoption and developer attraction in mind and therefore is designed to be language agnostic. Even though the node is implemented in Rust, a gRPC interface is co-deployed, so other languages and platforms can easily use Libra through gRPC. 

Using Libra 101

Libra did a nice job in creating a usable package for developers. Though Windows is currently not supported, with Mac and Linux installing a Libra node and client is straigthforward and a Testnet is publicly available, so developers can immediately begin to explore the Move language and the patterns of the Libra Blockchain.

With the Rust node a client is included and a gRPC interface is deployed, so accessing the node is possible for a variety of languages and platforms.

Entering the Java Universe

There already exist several libraries for accessing the services directly from the languages without having to deal with gRPC itself. As Libra defined their interfaces with Protobuf, Java is well suited for using the interface, as Protobuf supports a quite sophisticated type system, which can be mapped to respective Java types. In contrast to other languages like JavaScript and Python, Java is statically typed and therefore the Java accessor classes must be generated from the Protobuf definitions in a precompile step.

Schematic Overview of Libra Integration into Java Environment

In Java, this is most often done by some build system, for jlibra a Maven step is executed before the Java class compile step, which generates the Java classes from the Protobuf definitions. An advantage of this precompile step is type-safety: as soon as the Protobuf changes, the library doesn’t compile any more, instead of failing at runtime.

Using Libra from Java Applications

As jlibra still is low-level and meant as a thin library layer above the Libra API, using the library requires some boilerplate code in Java applications which want to access Libra.

To keep this boilerplate to a minimum, two projects have been created on top of jlibra to ease development with Libra from Java: jlibra-spring-boot-starter and java-libra-client.

Structure of jlibra projects

Using jlibra in custom Java applications

With the dependency 

<dependency>
<groupId>dev.jlibra</groupId>
<artifactId>jlibra-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>

The JLibra project can be injected into any Spring-based project like this:

@Autowired
private JLibra jlibra;

This JLibra object is preconfigured and “ready-to-go” by Spring Boot externalized configuration, eg. you can provide an application.properties file with the configuration:

jlibra.service-url=ac.testnet.libra.org
jlibra.service-port=80
jlibra.faucet-url=faucet.testnet.libra.org
jlibra.faucet-port=80
jlibra.gas-unit-price=0
jlibra.max-gas-amount=10000

Currently, the spring starter project creates a gRPC channel, but you are free to provide your own implementation or inject a channel from the existing environment.

Predefined Actions

There are predefined actions in the action package of jlibra-spring-boot-starter:

  • AccountStateQuery
  • PeerToPeerTransfer

These actions can just be @Autowired and are preconfigured as well, so you don’t have to deal with jlibra directly.
This is WIP and other actions will be added soon.

@Autowired
private PeerToPeerTransfer peerToPeerTransfer;

public void transfer(...) {
peerToPeerTransfer.transfer(...);
}

How the starter project can be used to speed up developement for Libra in Java is demonstrated by the java-libra-client project.

Accessing Libra from a Java based Shell

The showcase provides the same functionality the Rust CLI provides, but is implemented in Java using Spring Shell. Though being usable as a standalone client, the main focus of this project is to show the integration patterns for Libra from Java applications.

You can try the initial release here: https://github.com/ice09/java-libra-client/releases/tag/v0.0-alpha-1

Start it with java -jar java-libra-client-0.0-alpha-1.jar

Sample recording of java-libra-client with account creation, balance query and coin transfer