Skip to content

Web3 Besu

Purpose

A helper layer over the Hyperledger Besu Blockchain API: web3j-besu

Methods

Binding name: p6.web3besu


Value: ZERO_GAS_PROVIDER

This is a static value that can be used in various web3j calls that require a ContractGasProvider.

This value provider specifies zero gas price with a maximum gas of three million.


Method: Besu build(final String url)

Build an instance of a Besu object (extension of Web3j) using the supplied node URL to build an HttpService connection.

Note

You can use the core Web3j Besu instance build methods using UnixIpcService or WindowsIpcService for a more efficient ‘same host’ connection.


Method: Base64String findPrivacyGroup(final Besu node, final Base64String targetGroup, final Base64String... memberKeys) throws P6Exception

Find the privacy group using the supplied Besu instance, member keys and target group id.


Method: BesuPrivateTransactionManager buildPrivateTransactionManager(final Besu node, final Credentials credentials, final Base64String privateFrom, final Base64String privateToGroup)

Create a private transaction manager using the Besu instance, Credentials and From/To Ids.

Note

The transaction manager created uses the ZERO_GAS_PROVIDER.


Example

/**
 * Hyperledger Besu example requires besu-quickstart.git (run-privacy.sh)
 * running locally plus a demo-app-smart-contract-x.x.jar deployed in P6_DATA/lib
 * See: https://github.com/amalto/solidity-jar-builder for build instructions.
 * 
 * The Besu privacy quickstart starts up three nodes with their associated Orion instances, 
 * which will henceforth be called Alice, Bob and Charlie for ease of use
 * 
 * @import _BesuImports
 */

def ALICE_ENCLAVE_KEY = Base64String.wrap('A1aVtMxLCUHmBVHXoZzzBgPbW/wj5axDpW9X8l91SGo=')
def BOB_ENCLAVE_KEY = Base64String.wrap('Ko2bVqD+nNlNYL5EE7y3IdOnviftjiizpjRt+HTuFBs=')

def nodeAlice = p6.web3besu.build("http://localhost:20000")
log.info 'Alice node: ' + nodeAlice.web3ClientVersion().send().getWeb3ClientVersion()

def nodeBob = p6.web3besu.build("http://localhost:20002")
log.info 'Bob node: ' + nodeBob.web3ClientVersion().send().getWeb3ClientVersion()

def credentialsAlice = Credentials.create("8f2a55949038a9610f50fb23b5883af3b4ecb3c3bb792cbcefbd1542c692be63")

// Create a new 'Demo' privacy group as Alice
def privacyGroup = nodeAlice.privCreatePrivacyGroup( Arrays.asList(ALICE_ENCLAVE_KEY, BOB_ENCLAVE_KEY), 'DemoGroup', 'Platform 6 Demonstration Group').send()
log.info 'Privacy group created by Alice: ' + privacyGroup.getPrivacyGroupId()

// Find the privacy group that was just built by Alice from Bob's node
def privacyGroupIdFromBobNode = p6.web3besu.findPrivacyGroup(nodeBob, privacyGroup.getPrivacyGroupId(), ALICE_ENCLAVE_KEY, BOB_ENCLAVE_KEY)
log.info 'Found privacy group as Bob: ' + privacyGroupIdFromBobNode

// Create two private transaction manager instances for Alice and Bob in order to interact with smart contracts
def txmAlice =  p6.web3besu.buildPrivateTransactionManager(nodeAlice, credentialsAlice, ALICE_ENCLAVE_KEY, privacyGroup.getPrivacyGroupId())

def credentialsBob = Credentials.create("c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3")
def txmBob = p6.web3besu.buildPrivateTransactionManager(nodeBob, credentialsBob, BOB_ENCLAVE_KEY, privacyGroupIdFromBobNode)

// Deploy the contract from Alice's node giving initial value of 42!
def ssContractAlice = SimpleStorage.deploy(nodeAlice, txmAlice, p6.web3besu.ZERO_GAS_PROVIDER, BigInteger.valueOf(42)).send()

log.info 'Contract deployed, address:' + ssContractAlice.getContractAddress()

// Get an instance of the contact from Bob's node
def ssContractBob = SimpleStorage.load(ssContractAlice.getContractAddress(), nodeBob, txmBob, p6.web3besu.ZERO_GAS_PROVIDER)

log.info 'Contract loaded by Bob, address:' + ssContractBob.getContractAddress()

ssContractBob.get().send()

log.info 'Bob called contract.get()'

Besu Imports

import org.web3j.tx.response.PollingPrivateTransactionReceiptProcessor
import org.web3j.tx.gas.BesuPrivacyGasProvider
import org.web3j.crypto.Wallet
import org.web3j.protocol.core.methods.response.Web3ClientVersion
import org.web3j.utils.Base64String
import org.web3j.crypto.Credentials

import io.platform6.demo.sc.SimpleStorage

log.debug '---Starting---'