• Stars
    star
    111
  • Rank 314,510 (Top 7 %)
  • Language
    Java
  • License
    Other
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

web3j integration layer for JP Morgan's Quorum

web3j-quorum: Java integration library for Quorum

https://travis-ci.org/web3j/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 17:

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

Gradle

Java 17:

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

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 Quorum Transaction Managers. Code examples

Connection to Tessera via HTTP

Credentials credentials = ...
//connect to quorum node via http or ipc as described above
Quorum quorum = ...

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

Connection to Tessera via IPC

Credentials credentials = ...
//connect to quorum node via http or ipc as described above
Quorum quorum = ...

//build http client that supports ipc connection
UnixDomainSocketFactory socketFactory = new UnixDomainSocketFactory(new File("TESSERA_IPC_PATH"));
     OkHttpClient client = new OkHttpClient.Builder()
             .socketFactory(socketFactory)
             .build();

EnclaveService enclaveService = new EnclaveService("http://localhost", TESSERA_THIRD_PARTY_PORT, client);
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

Connection to Constellation via IPC

Credentials credentials = ...
//connect to quorum node via http or ipc as described above
Quorum quorum = ...

//build http client that supports ipc connection
UnixDomainSocketFactory socketFactory = new UnixDomainSocketFactory(new File("CONSTELLATION_IPC_PATH"));
     OkHttpClient client = new OkHttpClient.Builder()
             .socketFactory(socketFactory)
             .build();

EnclaveService enclaveService = new EnclaveService("http://localhost", CONSTELLATION_THIRD_PARTY_PORT, client);
Enclave enclave = new Constellation(enclaveService, quorum);

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

Using the QuorumTransactionManager with Smart Contract Wrappers

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

Using the QuorumTransactionManager alone

Using a single QuorumTransactionManager method signAndSend

RawTransaction rawTransaction = ...
EthSendTransaction ethSendTransaction = qrtxm.signAndSend(rawTransaction);

Using multiple exposed QuorumTransactionManager methods (storeRawRequest, sign, sendRaw)

//send raw bytecode to QuorumTranasctionManager
SendResponse storeRawResponse = qrtxm.storeRawRequest(HEX_ENCODED_SMARTCONTRACT_BYTECODE, TM_FROM_KEY, Arrays.asList(TM_TO_KEY_ARRAY));
String tesseraTxHash = Numeric.toHexString(Base64.getDecoder().decode(storeRawResponse.getKey()));

//create raw transaction with hash returned from QuorumTransactionManager
RawTransaction rawTransaction = ...

//sign raw transaction
String signedTx = qrtxm.sign(rawTransaction);

//send signed raw transaction to quorum node
EthSendTransaction ethSendTransaction = qrtxm.sendRaw(signedTx, Arrays.asList(TM_TO_KEY_ARRAY));

Retrieving a private transaction payload with Enclave receive method

String payload = enclave.receiveRequest(tesseraTxHash, TM_TO_KEY);

Full sample code

Sample code for sending raw private transactions via smart contract, QuorumTransactionManager and Enclave

Using web3j RawTransactionManager

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

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

More Repositories

1

web3j

Lightweight Java and Android library for integration with Ethereum clients
Java
4,890
star
2

sample-project-gradle

Sample web3j project using Gradle
Java
269
star
3

web3j-spring-boot-starter

Spring Boot Starter for web3j
Java
227
star
4

web3j-maven-plugin

web3j Maven plugin
Java
112
star
5

libp2p

libp2p implementation in Kotlin for JVM platforms
Kotlin
78
star
6

svm

Solidity Version Manager. Like nvm or jabba, but for Solidity. Cross platform.
Kotlin
49
star
7

examples

Example code supporting articles
Java
48
star
8

web3j-gradle-plugin

web3j Gradle plugin
Java
38
star
9

sample-project-maven

Sample web3j Maven project
Java
34
star
10

web3j-openapi

OpenAPI compliant service generator for Solidity Smart contracts
Solidity
33
star
11

Intellij-solidity-debugger

Kotlin
27
star
12

solidity-gradle-plugin

Gradle plugin providing tasks to compile Solidity contracts.
Solidity
21
star
13

web3j-corda

Lightweight JVM library for integrating with Corda
Kotlin
14
star
14

web3j-sokt

Kotlin-based wrapper for multiple installations of the solidity compiler (solc)
Kotlin
12
star
15

web3j-eth2

Web3j integrations for Ethereum 2.0 (Eth2) network
Kotlin
10
star
16

web3j-evmexample

Minimal example for getting started with web3j-evm
Java
9
star
17

web3j.github.io

web3j web site
JavaScript
6
star
18

web3j-aion

web3j integration with the Aion network
Kotlin
5
star
19

web3j-unitexample

Minimal example for getting started with web3j-unit
Java
4
star
20

web3j-openapi-gradle-plugin

Kotlin
4
star
21

web3j-workshop-2019

JAX London 2019 Web3j Workshop
Java
4
star
22

ion

Java bindings for the Ion Interoperability Framework
Solidity
4
star
23

web3j-deployer-demo

Demo usage of web3j-deployer
Java
3
star
24

web3j-deployer

Library to define, configure and run Ethereum contract deployments and transactions
Kotlin
3
star
25

web3j-deployer-plugin

Gradle plugin for web3j-deployer
Kotlin
2
star
26

fabric

Kotlin
2
star
27

solidity-darwin-binaries

Static binaries of the Solidity compiler for macOS
2
star
28

web3j-unit-docker-compose-example

Example to spin up VMWare Concord using docker-compose to test contracts in web3j-unit
Java
2
star
29

homebrew-web3j

web3j homebrew tap
Ruby
2
star
30

aion-samples

web3j Aion integration samples
Java
1
star
31

epirus-gradle-plugin

Gradle Plugin for the Epirus Platform.
Kotlin
1
star
32

.github

Share workflow templates, secrets, and self-hosted runners.
1
star
33

quorum-spring-boot-starter

A Spring Boot starter for working with Quorum
1
star