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.
- 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
Add the relevant dependency to your project:
Java 8:
Java 8:
compile ('org.web3j:quorum:4.8.4')
See instructions as per the Quorum project page
To send synchronous requests:
To send asynchronous requests:
To use an RxJava Observable:
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:
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:
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.
web3j supports sending raw private transactions through a connection to Quorum Transaction Managers. Code examples
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
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
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 a single QuorumTransactionManager method signAndSend
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
Sample code for sending raw private transactions via smart contract, QuorumTransactionManager and Enclave