Developing Hyperledger Fabric DApp

Preparing for DApp development

Download Fabric network connection file and user MSP information

  • Click the [Download SDK Configuration] button to download a connection profile about Free Trial network.
  • Click the [Download MSP Info] button to download MSP information (certificates and private keys). MSP files containing cert.pem(certificate) and key.pem(private key) are zipped.

Hyperledger Fabric SDK Download

To develop Hyperledger Fabric DApp, download SDK for the desired language from the following official github and install.

Writing Service Code for DApp

Write DApp service code by using files and SDK you downloaded previously. For detailed information about SDK, please refer to API documents for each SDK.

📘

In here, we explain to you the necessary process for developing with java with sample code. This development process is just reference and you don’t necessarily have to follow this order.

  1. Create a channel object by reading the Hyperledger Configuration file downloaded from Luniverse console. If the configuration file is saved under path “/Users/glory/connection-profile-hyperledger.yaml” the code looks like the following:
org.hyperledger.fabric.sdk.NetworkConfig networkConfig = NetworkConfig.fromYamlFile(new File(“/Users/glory/connection-profile-hyperledger.yaml”));
org.hyperledger.fabric.sdk.HFClient client = HFClient.createNewInstance();
org.hyperledger.fabric.sdk.Channel channel = client.loadChannelFromConfig(“mychannel”, networkConfig);
  1. Create Fabric User object and HFClient object by using the MSP information file downloaded from Luniverse console. This process will be written into the following code:
java.io.String certificate = new java.io.String(IOUtils.toByteArray(new java.io.FileInputStream(new java.io.File("./cert.pem"))), "UTF-8");
java.io.File privatekeyfile = new java.io.File("./key.pem");

EnrolleMentImpl enrollment = null;
try {
    java.security.PrivateKey privateKey = getPrivateKeyFromBytes( org.apache.commons.io.IOUtils.toByteArray(new java.io.FileInputStream(privatekeyfile)));
    enrollment = new EnrolleMentImpl(privateKey, certificate);
} catch(Exception e){
    e.printStackTrace();
}

org.hyperledger.fabric.sdk.FabricUser user = neworg.hyperledger.fabric.sdk.FabricUser();
user.setMspId(clientOrg.getMspId());
user.setName(userName);
user.setOrganization(clientOrg.getName());
user.setEnrollment(enrollment);

org.hyperledger.fabric.sdk.HFClient client client = org.hyperledger.fabric.sdk.HFClient.createNewInstance();
client.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
client.setUserContext(user);

static final class EnrolleMentImpl implements Enrollment, Serializable {
        private static final long serialVersionUID = -2784835212445309006L;
        private final PrivateKey privateKey;
        private final String certificate;

        public EnrolleMentImpl(PrivateKey privateKey, String certificate) {
                this.certificate = certificate;
                this.privateKey = privateKey;
        }

        @Override
        public PrivateKey getKey() {
                return privateKey;
        }

        @Override
        public String getCert() {
                return certificate;
        }
}
  1. Using HFClient object, send desired transaction proposal to peer. If you assume the name of chaincode that you want to execute as “mycc”, version as “1.0”, function you want to invoke as “invoke”, arguments as “a, 10”, the following code will be created:
org.hyperledger.fabric.sdk.ChaincodeID chaincodeID = org.hyperledger.fabric.sdk.ChaincodeID.newBuilder().setName(“mycc”).setVersion(“1.0”).build();

org.hyperledger.fabric.sdk.TransactionProposalRequest transactionProposalRequest = client.newTransactionProposalRequest();
transactionProposalRequest.setChaincodeID(chaincodeID);
transactionProposalRequest.setChaincodeLanguage(TransactionRequest.Type.JAVA);
transactionProposalRequest.setFcn(“invoke”);
transactionProposalRequest.setArgs(“a, 10”);

Collection<org.hyperledger.fabric.sdk.ProposalResponse> proposalResponse = channel.sendTransactionProposal(proposalRequest, channel.getPeers());
  1. Check the result of proposal sent to peer. According to the result of the proposal, if successfully done, transaction is sent to Orderer, and if failed, DApp service could be failed or retry proposal.
    The way it works according to the result can be implemented according to the requirements of the DApp service. Currently, the assurance policy of Luniverse Hyperledger Fabric BaaS Open Beta is that if at least one peer out of two succeeds, it is supposed to execute transaction, but it is possible to have the DApp to send transaction only if all two peers have a successful proposal. Transaction is sent like the following sample code:
for (org.hyperledger.fabric.sdk.ProposalResponse response :proposalResponse) {
    if (response.getStatus() == ProposalResponse.Status.SUCCESS) {
    channel.sendTransaction(response);
    }
}
  1. Even if all peers get success response of proposal and send transaction, according to specification of Hyperledger Fabric, real transaction may not be applied in the ledger. Thus, Hyperledger Fabric advises you to receive event of success or failure or transaction request and process it.
    Java SDK also supports this and you can write transaction sending code like the following method:
channel.sendTransaction(response).thenApply(transactionEvent -> {
    System.out.println("tx success - id : " + transactionEvent.getTransactionID());
    // Success
}).exceptionally(e -> {
    // Failed
}).get(5000, TimeUnit.SECONDS);