当前位置:网站首页 > Java教程 > 正文

fabric sdk java教程



/ * @Description: * @author: caib * @Date: 2021/07/15/15:51 */ public class Chaincode { private static final String Crypto_Config_Path = "D:\ideaProjects\fabric-sdk-demo\src\main\resources\crypto-config\"; private static final String CA_Pemfile = Crypto_Config_Path+"peerOrganizations\org1.example.com\ca\ca.org1.example.com-cert.pem"; private String adminKeyFile = Crypto_Config_Path+"peerOrganizations\" + "org1.example.com\users\\msp\keystore\383afd7e044f36903e4149d28398cc38eece0fc36b19de0dc_sk"; private String adminCertFile = Crypto_Config_Path+"peerOrganizations\org1.example.com\users\\" + "msp\signcerts\"; private HFClient client; private Channel channel; HFClient InitClient() throws Exception{ this.client = HFClient.createNewInstance(); client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite()); client.setUserContext(new LocalUser("admin", "Org1MSP", adminKeyFile, adminCertFile)); return client; } void CreateChannelIns(String channelName) throws Exception{ this.channel = client.newChannel(channelName); Properties proper; proper = loadTLSFile("peerOrganizations\org1.example.com\peers\peer0.org1.example.com\msp\tlscacerts\tlsca.org1.example.com-cert.pem", "peer0.org1.example.com"); Peer peer = client.newPeer("peer0.org1.example.com","grpcs://192.168.10.128:7051", proper); channel.addPeer(peer); proper = loadTLSFile("ordererOrganizations\example.com\orderers\orderer.example.com\msp\tlscacerts\tlsca.example.com-cert.pem", "orderer.example.com"); Orderer orderer = client.newOrderer("orderer1.example.com","grpcs://192.168.10.128:7050", proper); channel.addOrderer(orderer); channel.initialize(); } / * 为Fabric网络中节点配置TLS根证书 * * @param rootTLSCert 根证书路径 * @param hostName 节点域名 * @return * @throws IOException */ private static Properties loadTLSFile(String rootTLSCert, String hostName) throws IOException { Properties properties = new Properties(); properties.put("pemBytes", Files.readAllBytes(Paths.get( Crypto_Config_Path + rootTLSCert))); properties.setProperty("sslProvider", "openSSL"); properties.setProperty("negotiationType", "TLS"); properties.setProperty("trustServerCertificate", "true"); properties.setProperty("hostnameOverride", hostName); return properties; } void Query() throws Exception{ QueryByChaincodeRequest req = this.client.newQueryProposalRequest(); ChaincodeID cid = ChaincodeID.newBuilder().setName("mylocks").build(); req.setChaincodeID(cid); req.setFcn("queryAllUsers"); req.setArgs("a"); ProposalResponse[] rsp = this.channel.queryByChaincode(req).toArray(new ProposalResponse[0]); System.out.format("rsp message => %s ",rsp[0].getProposalResponse().getResponse().getPayload().toStringUtf8()); } void Query(String chainCodeName, String funcName , String... args) throws Exception{ QueryByChaincodeRequest req = this.client.newQueryProposalRequest(); ChaincodeID cid = ChaincodeID.newBuilder().setName(chainCodeName).build(); req.setChaincodeID(cid); req.setFcn(funcName); req.setArgs(args); // req.setChaincodeEndorsementPolicy(); ProposalResponse[] rsp = this.channel.queryByChaincode(req).toArray(new ProposalResponse[0]); System.out.format("rsp message => %s ",rsp[0].getProposalResponse().getResponse().getPayload().toStringUtf8()); } void Invode () throws Exception { QueryByChaincodeRequest req = this.client.newQueryProposalRequest(); ChaincodeID cid = ChaincodeID.newBuilder().setName("mylocks").build(); //提交链码交易 TransactionProposalRequest req2 = client.newTransactionProposalRequest(); req2.setChaincodeID(cid); req2.setFcn("inc"); req2.setArgs("10"); Collection<ProposalResponse> rsp2 = channel.sendTransactionProposal(req2); BlockEvent.TransactionEvent event = channel.sendTransaction(rsp2).get(); System.out.format("txid: %s ", event.getTransactionID()); System.out.format("valid: %b ", event.isValid()); } }

版权声明


相关文章:

  • java办公软件教程2025-01-13 22:58:01
  • 新手安装java教程2025-01-13 22:58:01
  • java桌面界面教程2025-01-13 22:58:01
  • java蓝牙传送教程2025-01-13 22:58:01
  • 黑马java教程pdf2025-01-13 22:58:01
  • java菜鸟教程好吗2025-01-13 22:58:01
  • java冒泡使用教程2025-01-13 22:58:01
  • java视频教程 传智播客2025-01-13 22:58:01
  • java开发系统教程2025-01-13 22:58:01
  • editplus java使用教程2025-01-13 22:58:01