/
- @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"<span style="color: rgba(0, 0, 0, 1)">;private String adminCertFile = Crypto_Config_Path+“peerOrganizations\org1.example.com\users\\” +
"msp\signcerts\"<span style="color: rgba(0, 0, 0, 1)">;private HFClient client; private Channel channel;
HFClient InitClient() throws Exception{
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.client =<span style="color: rgba(0, 0, 0, 1)"> HFClient.createNewInstance(); client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite()); client.setUserContext(</span><span style="color: rgba(0, 0, 255, 1)">new</span> LocalUser("admin", "Org1MSP"<span style="color: rgba(0, 0, 0, 1)">, adminKeyFile, adminCertFile)); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> client;}
void CreateChannelIns(String channelName) throws Exception{
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.channel =<span style="color: rgba(0, 0, 0, 1)"> client.newChannel(channelName); Properties proper; proper </span>= loadTLSFile("peerOrganizations\org1.example.com\peers\peer0.org1.example.com\msp\tlscacerts\tlsca.org1.example.com-cert.pem", "peer0.org1.example.com"<span style="color: rgba(0, 0, 0, 1)">); Peer peer </span>= client.newPeer("peer0.org1.example.com","grpcs://192.168.10.128:7051"<span style="color: rgba(0, 0, 0, 1)">, proper); channel.addPeer(peer); proper </span>= loadTLSFile("ordererOrganizations\example.com\orderers\orderer.example.com\msp\tlscacerts\tlsca.example.com-cert.pem", "orderer.example.com"<span style="color: rgba(0, 0, 0, 1)">); Orderer orderer </span>= client.newOrderer("orderer1.example.com","grpcs://192.168.10.128:7050"<span style="color: rgba(0, 0, 0, 1)">, 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 </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.client.newQueryProposalRequest(); ChaincodeID cid </span>= ChaincodeID.newBuilder().setName("mylocks"<span style="color: rgba(0, 0, 0, 1)">).build(); req.setChaincodeID(cid); req.setFcn(</span>"queryAllUsers"<span style="color: rgba(0, 0, 0, 1)">); req.setArgs(</span>"a"<span style="color: rgba(0, 0, 0, 1)">); ProposalResponse[] rsp </span>= <span style="color: rgba(0, 0, 255, 1)">this</span>.channel.queryByChaincode(req).toArray(<span style="color: rgba(0, 0, 255, 1)">new</span> ProposalResponse[0<span style="color: rgba(0, 0, 0, 1)">]); System.out.format(</span>"rsp message => %s”,rsp[0].getProposalResponse().getResponse().getPayload().toStringUtf8()); }
void Query(String chainCodeName, String funcName , String… args) throws Exception{
QueryByChaincodeRequest req </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.client.newQueryProposalRequest(); ChaincodeID cid </span>=<span style="color: rgba(0, 0, 0, 1)"> ChaincodeID.newBuilder().setName(chainCodeName).build(); req.setChaincodeID(cid); req.setFcn(funcName); req.setArgs(args);// req.setChaincodeEndorsementPolicy();
ProposalResponse[] rsp = <span style="color: rgba(0, 0, 255, 1)">this</span>.channel.queryByChaincode(req).toArray(<span style="color: rgba(0, 0, 255, 1)">new</span> ProposalResponse[0<span style="color: rgba(0, 0, 0, 1)">]); System.out.format(</span>"rsp message => %s”,rsp[0].getProposalResponse().getResponse().getPayload().toStringUtf8()); }
void Invode () throws Exception {
QueryByChaincodeRequest req </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.client.newQueryProposalRequest(); ChaincodeID cid </span>= ChaincodeID.newBuilder().setName("mylocks"<span style="color: rgba(0, 0, 0, 1)">).build(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">提交链码交易</span> TransactionProposalRequest req2 =<span style="color: rgba(0, 0, 0, 1)"> client.newTransactionProposalRequest(); req2.setChaincodeID(cid); req2.setFcn(</span>"inc"<span style="color: rgba(0, 0, 0, 1)">); req2.setArgs(</span>"10"<span style="color: rgba(0, 0, 0, 1)">); Collection</span><ProposalResponse> rsp2 =<span style="color: rgba(0, 0, 0, 1)"> channel.sendTransactionProposal(req2); BlockEvent.TransactionEvent event </span>=<span style="color: rgba(0, 0, 0, 1)"> channel.sendTransaction(rsp2).get(); System.out.format(</span>"txid: %s“, event.getTransactionID());
System.out.format(</span>"valid: %b”, event.isValid()); }
}
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/h6javajc/10942.html