简体   繁体   中英

Use REST API support in local development environment for Hyperledger Fabric V1.0

I have setup a HyperLedger Fabric V1.0 network with 4 organisations each having 1 peer by following the steps Building Your First Network .

Now I have

  1. org1.example.com - with peer : peer0.org1.example.com and msp : Org1MSP
  2. org2.example.com - with peer : peer0.org2.example.com and msp : Org2MSP
  3. org3.example.com - with peer : peer0.org3.example.com and msp : Org3MSP
  4. org4.example.com - with peer : peer0.org4.example.com and msp : Org4MSP

And now I can install the chaincode to peers and instantiate the chaincode on the channel. I can also able to invoke and query chain code by using the commands mentioned here like

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"]}'

I was previously using Hyperledger Fabric V0.6 service provided by IBM Bluemix and my java applications were invoking the chain code through the Rest API .

How can I use the Rest API here in this local network setup using docker image ?, then my java applications can interact with my chaincode.
Since I am not so familiar with this local network setup, please suggest me how can I make it work.

Note:
I am using Windows 7 machin e and network is setup by running the commands in docker quick start terminal

Thanks in advance..

There is no REST API in Hyperledger Fabric v.1.0.0, however there is Java SDK which could be used to interact with peers. You can setup your java project with following maven dependencies:

<dependency>
  <groupId>org.hyperledger.fabric-sdk-java</groupId>
  <artifactId>fabric-sdk-java</artifactId>
  <version>1.0.0</version>
</dependency>

Now you can use SDK APIs to invoke/query your chaincodes:

Get instance of HF client

    final HFClient client = HFClient.createNewInstance();

Setup crypto materials for client

    // 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";
        }
    });

Now channel configuration:

    final Channel channel = client.newChannel("mychannel");

    channel.addOrderer(client.newOrderer("orderer0", "grpc://localhost:7050"));
    channel.addPeer(client.newPeer("peer0", "grpc://localhost:7051"));

    channel.initialize();

Create transaction proposal:

    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"});

Send proposal

    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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM