What if your salary was paid in Libra?

Though Libra has a long way to go before running in production, the Libra testnet was launched on the day of the official announcement of the Libra project
This testnet is experimental, but is supposed to have the same properties as the mainnet, making application migration from testnet to mainnet straightforward.
So it makes sense to look into Libra with this testnet, assuming that the regulatory challenges will settle one day and Libra will launch its mainnet.

As the nitty-gritty regulatory work has just started to begin for Libra to go into production one day, we can see how ambitious this project really is. It is quite possible that it actually will never be launched at all.

So, we moved our use case a little bit into the future, but thanks to the Libra testnet, we can try it right now! (…with worthless Libra testnet coins of course…)

Working for the Wintermute DAO

As we are far into the future, we can be creative about the payment processes then. So our scenario is this:

Henry and his hacker buddies are working for the Wintermute DAO. They are not employed (employment these days is only possible for goverment jobs), but working based on a contract they signed with the Wintermute DAO, an AI representing a decentrally organized corporation and concluding contracts with all kind of external parties. 

We are not really sure why and how, but Excel actually survived the atomic wars, the zombie apocalypse and Ragnarök and is used by the Wintermute DAO to store the obligations to the contractors:

The DAOs payment processor reads these datasets line by line, extracts the amount and Libra address and transfers the amount. Optionally (see below) the signature is verified to make sure the address actually belongs to the contractor.

Starting the Payroll…

  1. Start the java-libra-shell
  2. Create account with mnemonic with command a cwm ‘chaos crash develop error fix laptop logic machine network patch setup void’
  3. Generated account 0 is Wintermute’s sender account. For each hacker account, add the index to the mnemonic, eg. to create Henry’s account, use a cwm ‘chaos crash develop error fix laptop logic machine network patch setup void’ 1
  4. Note the account balances and sequence numbers with command q b INDEX, eg. q b 1 for Henry’s account
  5. Run the java-payment-processor
  6. Note how the balances have now changed, account 0 (the sender account) has 700 Libra less, accounts 1-5 balances have increased with 100, 120, …. and the sequence numbers changed accordingly

Implementing the Payment Process in Java

Using Libra from Java is really simple: we have implemented the application using jlibra and the extension jlibra-spring-boot-starter.

As you can see from the repo, there is not much to do: 

  1. Create a pom.xml or build.gradle for your application
  2. Specify the application.properties for your testnet
  3. Inject the utility classes: JLibra, Peer2PeerTransaction, QueryAccountBalance, etc.
  4. Use the pre-configured classes in your application

We implemented all steps necessary for the payment processor in the Main class, the crucial functionality (coin transfer) is implemented in Main.java

@Autowired
private PeerToPeerTransfer peerToPeerTransfer;

@Autowired
private AccountStateQuery accountStateQuery;

@Autowired
private JLibra jLibra;

private long transferLibraToReceiver(String receiverAddress, BigDecimal amount) {
long seqNo = fetchLatestSequenceNumber(KeyUtils.toByteArrayLibraAddress(SENDER_PUBLIC_KEY.getEncoded()));
PeerToPeerTransfer.PeerToPeerTransferReceipt receipt =
peerToPeerTransfer.transferFunds(receiverAddress, amount.longValue() * 1_000_000, ExtKeyUtils.publicKeyFromBytes(SENDER_PUBLIC_KEY.getEncoded()), ExtKeyUtils.privateKeyFromBytes(SENDER_PRIVATE_KEY.getEncoded()), jLibra.getGasUnitPrice(), jLibra.getMaxGasAmount());
System.out.println(" (seqNo " + seqNo + ") " + receipt.getStatus());
return seqNo;
}

That’s all. In our sample application, this works against the testnet with the preconfigured accounts (corresponding to the mnemonic stated in the README).

Real World Use Cases before the year 2525

But how can we use this before the arrival of AI-Hackers and decentralized autonomous organizations? And what if we want to get our salary in EUR and just want to do international payments with Libra?

What did we do here? We connected Libra to an existing Java application. This Java application can do whatever Java applications do our days, most likely some enterprisey stuff. 

So how about connecting the payment processor of your corporate ERP system? Easy. How about integrating in one of those huge EAI products? Peasy. Workflows, BPNM Engines, Data Science frameworks? Yes, just like that. If it’s accessible from Java (or one language of the other adapters like JavaScript, Python, Go, Rust) it can use Libra. 

Bonus: Authorizing Transactions

In the old banking world, just giving your account information to the employer was enough. The banks checked all the regulatory stuff and made sure that the right person got the right money – or reverse the transaction if it was not. In the Libra world, that’s not that easy. 

There is a simple way of making sure that the address given is actually belonging to the person authorized to get the payment: signatures. By signing a pre-defined message like
I am working for Wintermute DAO and own Libra address 0x123 which should retrieve my earnings. Signed, Peter on 12/12/2103. 
It is easy to link the address to the signature, making sure that the address is correct as nobody could have signed the message without access to the private key of the Libra address. 

This way, the payments are arguably more safe than the old way of transfering money, given that “the system” makes sure that the address is checked for AML and is KYCed. This will presumably be true for the “wallet-level” applications in Libra.

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