In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what is the Java application development method of Fabric block chain". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the Java application development method of Fabric block chain".
Basic principles
In the Fabric block chain, the application accesses the chain code through the node's RPC protocol interface:
Similar to the chain code communication protocol encapsulated by Shim API, Fabric Java SDK provides the encapsulation of the node RPC protocol interface. The entry class is HFClient, and the chain code query and transaction operations are encapsulated in the Channel class:
Because Fabric is a licensed block chain, applications also need to hold certificates and private keys to represent their identity. HFClient instances rely on the implementation object of the User interface to access a specific identity certificate and private key, so before accessing the chain code, we need to define a simple User interface implementation class.
Implement the User interface
The identity of the HFClient instance accessing the Fabric network is represented by the User interface implementation object, so we need to define a simple User interface implementation class LocalUser:
It is easy to understand that the identity of a user is identified by its certificate, and the transaction also needs the private key corresponding to the certificate, so the core logic of LocalUser is to use the specified certificate and private key PEM file to meet the requirements of the User interface.
First, complete the skeleton of class LocalUser according to the requirements of User API:
Import org.hyperledger.fabric.sdk.User;import org.hyperledger.fabric.sdk.security.CryptoPrimitives;public class LocalUser implements User {/ / implement 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 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 an implementation class X509Enrollment suitable for X509 certificate is preset, so we can load the user's private key and signing 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) / / convert PEM text to private key object return new X509Enrollment (privateKey,new String (certPem)); / / create and return X509Enrollment object} access chain code
With the User interface implementation class, you can simply create a HFClient instance, get the channel object, 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 instance 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 HFClient instance HFClient client = HFClient.createNewInstance (); client.setCryptoSuite (CryptoSuite.Factory.getCryptoSuite ()); client.setUserContext (user); / / create tunnel instance Channel channel = client.newChannel ("ch2"); 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 (); / / submit chain code transaction TransactionProposalRequest req2 = client.newTransactionProposalRequest (); req2.setChaincodeID (cid); req2.setFcn ("inc"); req2.setArgs ("10"); Collection 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 ()) }} Thank you for your reading, the above is the content of "what is the Java application development method of Fabric block chain". After the study of this article, I believe you have a deeper understanding of what the Java application development method of Fabric block chain is, and the specific use needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.