/web3j-quorum

web3j integration layer for JP Morgan's Quorum

Primary LanguageJavaOtherNOASSERTION

web3j-quorum: Java integration library for Quorum

https://travis-ci.org/web3j/quorum.svg?branch=master

web3j-quorum is an extension to web3j providing support for JP Morgan's Quorum API.

web3j is a lightweight, reactive, type safe Java library for integrating with clients (nodes) on distributed ledger or blockchain networks.

For further information on web3j, please refer to the main project page and the documentation at Read the Docs.

Features

  • Support for Quorum's private transactions through private transaction manager
  • Ability to send signed private transactions
  • Works out the box with web3j's smart contract wrappers

Getting started

Add the relevant dependency to your project:

Maven

Java 8:

<dependency>
  <groupId>org.web3j</groupId>
  <artifactId>quorum</artifactId>
  <version>4.0.6</version>
</dependency>

Gradle

Java 8:

compile ('org.web3j:quorum:4.0.6')

Run Quorum

See instructions as per the Quorum project page

Start sending requests

To send synchronous requests:

Quorum quorum = Quorum.build(new HttpService("http://localhost:22001"));
Web3ClientVersion web3ClientVersion = quorum.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();

To send asynchronous requests:

Quorum quorum = Quorum.build(new HttpService("http://localhost:22001"));
Web3ClientVersion web3ClientVersion = quorum.web3ClientVersion().sendAsync().get();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();

To use an RxJava Observable:

Quorum quorum = Quorum.build(new HttpService("http://localhost:22001"));
quorum.web3ClientVersion().observable().subscribe(x -> {
    String clientVersion = x.getWeb3ClientVersion();
    ...
});

IPC

web3j also supports fast inter-process communication (IPC) via file sockets to clients running on the same host as web3j. To connect simply use UnixIpcService or WindowsIpcService instead of HttpService when you create your service:

// OS X/Linux/Unix:
Quorum quorum = Quorum.build(new UnixIpcService("/path/to/socketfile"));
...

// Windows
Quorum quorum = Quorum.build(new WindowsIpcService("/path/to/namedpipefile"));
...

Smart Contract Wrappers

Smart contract wrappers generated using web3j 2.0+ work out the box with with web3j-quorum.

The only difference is that you'll need to use the Quorum ClientTransactionManager:

QuorumTransactionManager transactionManager = new QuorumTransactionManager(
        web3j, "0x<from-address>", Arrays.asList("<privateFor-public-key>", ...);
YourSmartContract contract = YourSmartContract.deploy(
    <web3j>, <transactionManager>, GAS_PRICE, GAS_LIMIT,
    <param1>, ..., <paramN>).send();

These wrappers are similar to the web3j smart contract wrappers with the exception that the transactions are signed by the Quorum nodes rather then by web3j. They also support the privateFor field on transactions.

See the web3j documentation for a detailed overview of smart contracts and web3j.

Sending Raw Private Transactions

web3j supports sending raw private transactions through a connection to Tessera (Quorum transaction manager). Code examples

Credentials credentials = ...

EnclaveService enclaveService = new EnclaveService("http://TESSERA_THIRD_PARTY_URL", TESSERA_THIRD_PARTY_PORT, httpClient);
Enclave enclave = new Tessera(enclaveService, quorum);

QuorumTransactionManager qrtxm = new QuorumTransactionManager(
    quorum, credentials, TM_FROM_KEY, Arrays.asList(TM_TO_KEY_ARRAY),
    enclave,
    30,     // Retry times
    2000);  // Sleep

YourSmartContract.deploy(client,
    qrtxm,
    GAS_PRICE, GAS_LIMIT,
    <param1>, ..., <paramN>).send();

// Raw txn
RawTransactionManager qrtxm = new RawTransactionManager(
      quorum,
      credentials,
      30,     // Retry times
      2000);  // Sleep

YourSmartContract.deploy(quorum,
      qrtxm,
      GAS_PRICE, GAS_LIMIT,
      <param1>, ..., <paramN>).send();