In the super-book Fabric block chain, the application can use the official Fabric-sdk-java development kit to access the chain code through the RPC protocol interface of the node. The java development kit encapsulates the GRPC code chain access protocol of the Fabric block chain, which helps focus developers on business logic. This articl E will help you quickly start developing Java applications for Fabric block chains by writing a simple Java application that can access Fabric chain codes.
If you want to quickly grasp the chain code of Fabric block chain and its application development, it is recommended to visit the online interactive course of Hui Zhi Net.
Basic principles
In the Fabric block chain, the application accesses the chain code through the RPC protocol interface of the node:
Similar to the encapsulation of chain code communication protocol by Shim API, Fabric Java SDK provides RPC protocol for nodes.
The encapsulation of the interface, whose entry class is HFClient, encapsulates the query and transaction operation of chain code in Channel class:
Since Fabric is a licensed block chain, applications also need to hold certificates and private keys to represent their identity. The HFClient instance relies on the implementation object of User interface.
To access a specific identity certificate and private key, we need to first define a simple User interface implementation class before accessing the chain code.
Implementing User Interface
The identity of the HFClient instance accessing the Fabric network is represented by an object using the User interface, so we need to define a simple one.
User interface implements class LocalUser:
It's easy to understand that a user's identity is identified by his certificate, and the transaction also requires the private key corresponding to the certificate, so LocalUser
The core logic is to use the specified certificate and private key PEM files to meet the User interface requirements.
First, the skeleton of the class LocalUser is completed according to the requirements of User interface:
import org.hyperledger.fabric.sdk.User; import org.hyperledger.fabric.sdk.security.CryptoPrimitives; public class LocalUser implements User{ //Implementing User Interface private String name; private String mspId; private Enrollment enrollment; LocalUser(String name,String mspId){ this.name = name; this.mspId = mspId; } private Enrollment loadFromPemFile(String keyFile,String certFile){ /*See the note below.*/ } @Override public String getName(){ return name; } @Override public String getMspId() { return mspId} @Override public Enrollment getEnrollment() { return enrollment; } @Override public String getAccount() { return null; } @Override public String getAffiliation() { return null; } }
In Fabric Java SDK, the Enrollment interface is used to provide access to the user's private key and certificate, and a suitable one is preset.
The implementation class of X509 certificate is X509 Enrollment, so we can load the user's private key and signature certificate from the PEM file in the local MSP directory:
private Enrollment loadFromPemFile(String keyFile,String certFile) throws Exception{ byte[] keyPem = Files.readAllBytes(Paths.get(keyFile)); //Load private key PEM text byte[] certPem = Files.readAllBytes(Paths.get(certFile)); //Load certificate PEM text CryptoPrimitives suite = new CryptoPrimitives(); //Load cryptography Suite PrivateKey privateKey = suite.bytesToPrivateKey(keyPem); //Converting PEM text to private key objects return new X509Enrollment(privateKey,new String(certPem)); //Create and return the X509Enrollment object }
Access chain code
With the User interface implementation class, the next step is to create an instance of HFClient and get the channel object.
You can query the chain code or submit the chain code transaction.
The complete implementation code is as follows:
package com.hubwiz.demo; import org.hyperledger.fabric.sdk.User; import org.hyperledger.fabric.sdk.HFClient; import org.hyperledger.fabric.sdk.Channel; import org.hyperledger.fabric.sdk.Peer; import org.hyperledger.fabric.sdk.Orderer; import org.hyperledger.fabric.sdk.security.CryptoSuite; import org.hyperledger.fabric.sdk.ChaincodeID; import org.hyperledger.fabric.sdk.QueryByChaincodeRequest; import org.hyperledger.fabric.sdk.ProposalResponse; import org.hyperledger.fabric.sdk.TransactionProposalRequest; import org.hyperledger.fabric.sdk.BlockEvent.TransactionEvent; import java.util.Collection; import java.util.concurrent.CompletableFuture; public class App{ public static void main(String[] args) throws Exception{ System.out.println("counter app"); //Create User instances String keyFile = "../solo-network/msp/keystore/user-key.pem"; String certFile = "../solo-network/msp/signcerts/user-cert.pem"; LocalUser user = new LocalUser("admin","SampleOrg",keyFile,certFile); //Create an instance of HFClient HFClient client = HFClient.createNewInstance(); client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite()); client.setUserContext(user); //Create channel instances Channel channel = client.newChannel("ch1"); Peer peer = client.newPeer("peer1`","grpc://127.0.0.1:7051"); channel.addPeer(peer); Orderer orderer = client.newOrderer("orderer1","grpc://127.0.0.1:7050"); channel.addOrderer(orderer); channel.initialize(); //Query chain code QueryByChaincodeRequest req = client.newQueryProposalRequest(); ChaincodeID cid = ChaincodeID.newBuilder().setName("counter-cc").build(); req.setChaincodeID(cid); req.setFcn("value"); ProposalResponse[] rsp = channel.queryByChaincode(req).toArray(new ProposalResponse[0]); System.out.format("rsp message => %s\n",rsp[0].getProposalResponse().getResponse().getPayload().toStringUtf8()); //Submitting Chain Code Transactions TransactionProposalRequest req2 = client.newTransactionProposalRequest(); req2.setChaincodeID(cid); req2.setFcn("inc"); req2.setArgs("10"); Collection<ProposalResponse> rsp2 = channel.sendTransactionProposal(req2); TransactionEvent event = channel.sendTransaction(rsp2).get(); System.out.format("txid: %s\n", event.getTransactionID()); System.out.format("valid: %b\n", event.isValid()); } }
Links to the original text: Fabric-sdk-java Chain Code Access Quick Start-Huizhi Network