在 Hyperledger Fabric V1.0 的本地开发环境中使用 REST API 支持
Use REST API support in local development environment for Hyperledger Fabric V1.0
我已经设置了一个 HyperLedger Fabric V1.0 网络 与 4 个组织 每个都有1 对等 按照步骤 Building Your First Network.
Now I have
- org1.example.com - with peer: peer0.org1.example.com and msp: Org1MSP
- org2.example.com - with peer: peer0.org2.example.com and msp: Org2MSP
- org3.example.com - with peer: peer0.org3.example.com and msp: Org3MSP
- org4.example.com - with peer: peer0.org4.example.com and msp: Org4MSP
现在我可以将链代码安装到对等点并在通道上实例化链代码。我还可以使用 here 中提到的命令调用和查询链码,例如
Invoke:
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
-C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'
Query:
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
我之前使用的是 IBM Bluemix 提供的 Hyperledger Fabric V0.6 服务,我的 java 应用程序通过 Rest API.
如何在此本地网络[=42=中使用Rest API ] setup using docker image?, 然后我的 java applications 可以与我的链代码交互。
由于我不太熟悉这个 本地网络 设置,请告诉我如何让它工作。
Note:
I am using Windows 7 machine and network is setup by running
the commands in docker quick start terminal
提前致谢..
Hyperledger Fabric v.1.0.0 中没有 REST API,但是有 Java SDK 可用于与节点交互。您可以使用以下 Maven 依赖项设置您的 java 项目:
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>1.0.0</version>
</dependency>
现在您可以使用 SDK APIs 来 invoke/query 您的链代码:
获取 HF 客户端实例
final HFClient client = HFClient.createNewInstance();
为客户设置加密材料
// Set default crypto suite for HF client
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
client.setUserContext(new User() {
public String getName() {
return "testUser";
}
public Set<String> getRoles() {
return null;
}
public String getAccount() {
return null;
}
public String getAffiliation() {
return null;
}
public Enrollment getEnrollment() {
return new Enrollment() {
public PrivateKey getKey() {
// Load your private key
}
public String getCert() {
// Read client certificate
}
};
}
public String getMspId() {
return "Org1MSP";
}
});
现在频道配置:
final Channel channel = client.newChannel("mychannel");
channel.addOrderer(client.newOrderer("orderer0", "grpc://localhost:7050"));
channel.addPeer(client.newPeer("peer0", "grpc://localhost:7051"));
channel.initialize();
创建交易提案:
final TransactionProposalRequest proposalRequest = client.newTransactionProposalRequest();
final ChaincodeID chaincodeID = ChaincodeID.newBuilder()
.setName("myCC")
.setVersion("1.0")
.setPath("github.com/yourpackage/chaincode/")
.build();
proposalRequest.setChaincodeID(chaincodeID);
proposalRequest.setFcn("fcn");
proposalRequest.setProposalWaitTime(TimeUnit.SECONDS.toMillis(10));
proposalRequest.setArgs(new String[]{"arg1", "arg2"});
发送提案
final Collection<ProposalResponse> responses = channel.sendTransactionProposal(proposalRequest);
CompletableFuture<BlockEvent.TransactionEvent> txFuture = channel.sendTransaction(responses, client.getUserContext());
BlockEvent.TransactionEvent event = txFuture.get();
System.out.println(event.toString());
我已经设置了一个 HyperLedger Fabric V1.0 网络 与 4 个组织 每个都有1 对等 按照步骤 Building Your First Network.
Now I have
- org1.example.com - with peer: peer0.org1.example.com and msp: Org1MSP
- org2.example.com - with peer: peer0.org2.example.com and msp: Org2MSP
- org3.example.com - with peer: peer0.org3.example.com and msp: Org3MSP
- org4.example.com - with peer: peer0.org4.example.com and msp: Org4MSP
现在我可以将链代码安装到对等点并在通道上实例化链代码。我还可以使用 here 中提到的命令调用和查询链码,例如
Invoke:
peer chaincode invoke -o orderer.example.com:7050 --tls $CORE_PEER_TLS_ENABLED --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C $CHANNEL_NAME -n mycc -c '{"Args":["invoke","a","b","10"]}'Query:
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
我之前使用的是 IBM Bluemix 提供的 Hyperledger Fabric V0.6 服务,我的 java 应用程序通过 Rest API.
如何在此本地网络[=42=中使用Rest API ] setup using docker image?, 然后我的 java applications 可以与我的链代码交互。
由于我不太熟悉这个 本地网络 设置,请告诉我如何让它工作。
Note:
I am using Windows 7 machine and network is setup by running the commands in docker quick start terminal
提前致谢..
Hyperledger Fabric v.1.0.0 中没有 REST API,但是有 Java SDK 可用于与节点交互。您可以使用以下 Maven 依赖项设置您的 java 项目:
<dependency>
<groupId>org.hyperledger.fabric-sdk-java</groupId>
<artifactId>fabric-sdk-java</artifactId>
<version>1.0.0</version>
</dependency>
现在您可以使用 SDK APIs 来 invoke/query 您的链代码:
获取 HF 客户端实例
final HFClient client = HFClient.createNewInstance();
为客户设置加密材料
// Set default crypto suite for HF client
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
client.setUserContext(new User() {
public String getName() {
return "testUser";
}
public Set<String> getRoles() {
return null;
}
public String getAccount() {
return null;
}
public String getAffiliation() {
return null;
}
public Enrollment getEnrollment() {
return new Enrollment() {
public PrivateKey getKey() {
// Load your private key
}
public String getCert() {
// Read client certificate
}
};
}
public String getMspId() {
return "Org1MSP";
}
});
现在频道配置:
final Channel channel = client.newChannel("mychannel");
channel.addOrderer(client.newOrderer("orderer0", "grpc://localhost:7050"));
channel.addPeer(client.newPeer("peer0", "grpc://localhost:7051"));
channel.initialize();
创建交易提案:
final TransactionProposalRequest proposalRequest = client.newTransactionProposalRequest();
final ChaincodeID chaincodeID = ChaincodeID.newBuilder()
.setName("myCC")
.setVersion("1.0")
.setPath("github.com/yourpackage/chaincode/")
.build();
proposalRequest.setChaincodeID(chaincodeID);
proposalRequest.setFcn("fcn");
proposalRequest.setProposalWaitTime(TimeUnit.SECONDS.toMillis(10));
proposalRequest.setArgs(new String[]{"arg1", "arg2"});
发送提案
final Collection<ProposalResponse> responses = channel.sendTransactionProposal(proposalRequest);
CompletableFuture<BlockEvent.TransactionEvent> txFuture = channel.sendTransaction(responses, client.getUserContext());
BlockEvent.TransactionEvent event = txFuture.get();
System.out.println(event.toString());