How to make your first transaction in the Libra payment system

Julkaistu aiheella Teknologia
Libra-main.jpg
Sauli Ketola profile blue cropped
Kirjoittaja
Sauli Ketola
Software Architect

Sauli Ketola is a Software Architect at Nitor with a background in developing software for finance and retail. Enjoys hacking his hobby projects and cheering for his favourite hockey team Vaasan Sport.

January 27, 2020 · 5 min lukuaika

Libra is a cryptocurrency and a new global payment system started by Facebook. They made the test environment public last June and are launching the actual network during 2020.

Bitcoin has now existed for over a decade, and almost everyone has owned bitcoin at some point – or at least knows someone who has. Still, bitcoin never became the digital money it was meant to be. Most probably, your local grocery store doesn’t accept it, nor do you use it when you need to transfer money to your friends or family. The reasons holding most of the cryptocurrencies back include their inability to scale and the volatility of their value.

Facebook is now on a mission to create a new crypto-currency for the masses, with features that target the problems with the previous ones:

  • Instead of creating the currency by mining and leaving the value to be determined by the market, Libra will have the value of the currency backed by a reserve of assets like bank deposits and government securities. This means that the value of Libra should only fluctuate based on the value of these backing assets.

  • Only trusted parties, the members of the Libra Association, are allowed to run servers that validate the transactions. Therefore, the proof-of-work mechanism is not required. This increases both the speed and throughput of processing transactions (and does not require the crazy amounts of electricity consumed by the big cryptocurrencies). The validating nodes still have to agree to confirm a transaction, and the network can tolerate up to ⅓ of the validators to be compromised.

The test environment for Libra has been available since June, so developers and companies can already start getting prepared for making and accepting payments in Libra.The rest of this article explains the basic concepts of Libra and has code examples for creating transactions in the testnet to show you how easy transferring money can be with Libra.

Sending money to your friend shouldn’t be harder than getting them an Uber ride home. Libra has the potential to bridge the gap between traditional financial networks and new digital currency technology while reducing the costs for everyone — especially consumers.

- Peter Hazlehurst, Head of Payments and Risk, Uber Technologies, Inc.

Accounts

The only thing you need for setting up a new account for sending and receiving transactions is a public-private key pair you can create by using the Ed25519 signature scheme. This key-pair is used to sign the transactions digitally, and the account identifier (address) is derived from the public key by hashing. You can create a new account in the Libra system by sending money to it or by making an account creation transaction.

After creating the key-pair, you can create the account address by making a SHA3 hash of the public key.

Transactions

To make a transaction, you need to specify the following information:

  • Transaction sequence number

  • Gas amount. Gas is a small payment for making the transaction that will be deducted from the sender account.

  • Sender account address

  • Expiration time

  • The public key of the sending account

  • Transaction script with arguments

  • Signature

You also need to serialize the transaction data by using a serialization mechanism that is unique to Libra. The serialized data is then signed, and the transaction is ready to be sent to the Libra API.

The amount of money to be sent nor the receiver of the payment is not specified in the transaction attributes. This information is included in the transaction script and arguments, which are written in a Libra-specific programming language called Move.

‘Move’ transaction script

The transaction script makes it possible for you to create much more complex transactions than simple money transfers from one account to another. These type of programmable transactions are also called “smart contracts”, and you can also specify for example several receivers for the money and define conditions that need to be met for the transaction to go through.

For this, Libra has its own programming language called Move. A Libra -specific programming language was designed to efficiently and safely create logic around transactions.

To get started, you don’t need to learn this new language, as there are ready-made scripts for most common use cases in the Libra code repository.

Libra API

Libra API uses gRPC, which is a web-service framework created by Google. The API is defined in service definition files, which are used to generate the client code for different platforms. The testnet API is running at http://ac.testnet.libra.org:8000 and can be accessed without registration.

For testing purposes, Libra also provides a service for adding money (minting) to test accounts.

Behind the API, the transaction will go through different validations after which it will be sent to other validator nodes in the network. The validators will vote on the transaction to be executed, and the changes will then be written to the ledger. You can find a more detailed description from the Libra documentation: https://developers.libra.org/docs/life-of-a-transaction

Making the API call

JLibra is a Java client library I started implementing as an open source project after the testnet was made public. JLibra is used for creating transactions and querying information about the accounts. There are also several other client libraries available for different programming languages. By using a client library, you don’t need to implement the serialization, signing and the gRPC call yourself. If you are interested in the details of those, you can find them in the source code.

  1. The first step is to generate key-pairs for the accounts that are involved in the transaction. GenerateKeysExample.java

  2. Next, you need to create some money for testing purposes. This is done using the Libra faucet API and is obviously only available in the test environment. MintExample.java

  3. After creating money in the accounts, you can check the balance by querying the API. From the response, you can also find the account sequence number for the transaction. GetAccountStateExample.java

Now you can transfer the money minted in step 2 between accounts.TransferExample.java

Hopefully, you got an idea of how simple and fast transferring money can be with Libra. For more information, see the Libra website and the developer documentation at:

https://libra.org
https://developers.libra.org/docs/welcome-to-libra

You’re also welcome to join the development of the JLibra client library for Java: https://github.com/ketola/jlibra

Sauli Ketola profile blue cropped
Kirjoittaja
Sauli Ketola
Software Architect

Sauli Ketola is a Software Architect at Nitor with a background in developing software for finance and retail. Enjoys hacking his hobby projects and cheering for his favourite hockey team Vaasan Sport.