How to use Rest API support in local development environment

0 votes

I have a HyperLedger Fabric V1.0 network. It has 4 organizations and each organization has 1 peer.

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

To invoke and query, i am using the following code:

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

Now i want to use REST API in local network using docker image. Then i want to interact with chaincode using java
application.
How can i achieve this?

Jul 11, 2018 in Blockchain by digger
• 26,740 points

retagged Nov 22, 2018 by Omkar 889 views

1 answer to this question.

0 votes

Rest API is not available for Hyperledger Fabric v.1.0.0. But you can use Java SDK to interact with peers. 
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>

Next, you have to get instance of HF client like this: 
final HFClient client = HFClient.createNewInstance();

Next you have to setup crypto materials for client like this:

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

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

Now, create a 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"});

Lastly, send the 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());

answered Jul 11, 2018 by slayer
• 29,370 points

Related Questions In Blockchain

+1 vote
1 answer

How to setup testnet environment in Ethereum??

You can connect to a node using ...READ MORE

answered Apr 18, 2018 in Blockchain by Shashank
• 10,400 points
870 views
0 votes
1 answer
0 votes
1 answer

How i can use nodejs to watch transactions in bitcoin network?

you can use  const Socket = require('blockchain.info/Socket'); const mySocket ...READ MORE

answered Jul 9, 2018 in Blockchain by digger
• 26,740 points
1,281 views
0 votes
1 answer

Network issue while testing Chaincode.

You can do the following things: You can ...READ MORE

answered Jul 3, 2018 in Blockchain by Omkar
• 69,220 points
583 views
0 votes
1 answer
0 votes
1 answer

Hyperledger Composer rest server NPM versions

The Hyperledger Composer pre-requisites can be installed ...READ MORE

answered Jul 13, 2018 in Blockchain by Christine
• 15,790 points
830 views
0 votes
1 answer

How to Track Payment API in Coinbase

Use the callback_url option as described in the API. ...READ MORE

answered Sep 5, 2018 in Blockchain by slayer
• 29,370 points
812 views
0 votes
1 answer

How to use Coinbase send_money API?

<?php require_once(dirname(__FILE__) . '/../lib/Coinbase.php'); // Create an application at ...READ MORE

answered Sep 12, 2018 in Blockchain by slayer
• 29,370 points
1,871 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP