{{announcement.body}}
{{announcement.title}}

Simplified Blockchain Part 2: Ethereum Example With Java Client

DZone 's Guide to

Simplified Blockchain Part 2: Ethereum Example With Java Client

Simple example explaining how powerful blockchain is.

· Security Zone ·
Free Resource

In the previous article, we got acquainted with the theoretical foundations of the blockchain. In this article, we implement the functionality of the passport office.

Before You Start

In order to generate and deploy smart contract, you need to install next tools:

  • Solc — Solidity compiler: Solc download link
  • Ganache-cli  Testing Ethereum network, to install execute: npm install -g ganache-cli 
  • Web3j — Tool to generate java client. to install execute next command in windows power shell:
PowerShell


Blockchain Integration Process

  1. Write smart contract.
  2. Deploy implemented contract in network (we use ganache cli - test network).
  3. Get address of deployed contract.
  4. Generate java client for this smart contract.
  5. Use generated client.

Implementation - Solidity Language

Solidity is Ethereum's smart contract language. Its syntax is quite poor because of blockchain limitation and its youth. 

Passport Storage Contract

The key function of the electronic passport office is to issue passports, update them, and allow passport owners to read them. The following interfaces are required:

Plain Text


Explanation of Implementation

Key interfaces are:

Plain Text


They provide features to create passports (only with admin rights) and to read passports (owner rights). In Ethereum, every method caller has an account. Using the context value: message.sender, we identify the user account.

Contract internal fields:

Plain Text


The first constant is a hardcoded value of admin account address. Only from that account, can a new passport be added and all previous passport history records be viewed.

The second line is a dictionary or key (passport owner address) value (passport JSON details) storage. I've decided to make value a string and not a struct to simplify the example. 

The last variable is an array of history records that keeps a record of all changes ever done to passports. 

Protection From Internal Corrupted Employees

Plain Text


By having a history records array, we make our application invulnerable, even to internal attacks. Even internal employees with admin access won't be able to adjust any data without being seen.  

This history records track any activity and can't be deleted once transaction is approved by (half + 1) nodes. It's not possible to replace it or change it. This protection is the milestone that makes blockchain different from any other architecture. In any other systems (e.g. banking systems), transaction approval is based on one main trusted server. 

Deploying Process. Remix IDE.

The easiest way to deploy and debug you contract is to use the Remix IDE.

Before execution start ganache cli test network:    ganache-cli -l 9000000 -d > log.txt 

The first param, -l, solves a potential problem with the gas limit (it's another big story). -d, on the other hand, generates the same accounts all the time. The inequality, " > ", saves all output in a text file.

Remix IDE

On the right side, in file explore, you can create a new contract and after compile it on the Solidity compile tab (currently selected on image). After it opens, click the "Deploy" tab, select Web3 Provider, and use recommended localhost:8545. If you can't connect to the network, double check that your ganace-cli application is running. Select the account that you hardcoded as admin and deploy:

Deploying and running transaction

After deployment, on the left side, you can open contract interfaces and use them. When a contract is deployed, you need to save its address (on the left side there is copy button). In the Java client, this value has to be pasted in CONTRACT_ADDRESS

Generating Java Client API:

  1. Save PassportService contract into the PassportService.sol file.
  2. Generate bin and abi files: solc PassportService.sol --bin --abi --optimize -o . 
  3. Generate java client file: web3j solidity generate -b PassportService.bin  -a PassportService.abi -o 
    .  -p package_name

Call Contract From Java Client API:

Check the generated PassportService class in the folder; you don't need to change it, only call it.

Passport Service class

Now, you can create its instance and make a call. But before that, let's take a look into the accounts generated by ganache-cli (check log file when ganache-cli is started).

PowerShell


At the top, we can see account addresses, at bottom their private keys. Accounts are used as passport address (like id). Private keys are used to call contract under its account. In the example, I've used the first account as admin and second one as John's account.

Java Client Implementation:

Java


Code logically split in 4 parts

  • Web3j initialization.
  • Accessing to passport contract as admin and creating new passport.
  • Accessing to passport contract as user John and reading passport.
  • Accessing to passport contract as admin and printing all history records.

The result of execution:

PowerShell


Conclusion

Both articles together explain briefly what blockchain is. From theoretical and practical perspectives. I believe that this technology is new and original. Over time, the hype will go away and it will occupy its niche. I hope my article has clarified and shown the practical aspects of the blockchain.

Topics:
blockchain, ethereum, java, security, solidity, tutorial

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}