Luniverse User Guide

루니버스 사용을 환영합니다!
이 페이지에서는 루니버스 서비스 사용 가이드를 제공합니다.
루니버스를 사용하여 다양한 디앱(DApp)을 제작해보세요.

Welcome to Luniverse!
This page provides a guide to using the Luniverse service.
Try to create various DApps by utilizing Luniverse.

Guides

Developing DApp on Hyperledger Fabric

Preparing for DApp development

A connection profile and MSP information(certificate and private key) on a Fabric network are required in order to create a Fabric client (DApp). Luniverse automatically creates a connection profile and MSP information for users to easily download them on Luniverse console.

Download Fabric network connection file and user MSP information

Hyperledger Fabric for Commercial chains

Choose the Fabric Network tab created independently on Luniverse console. You can download a connection profile by clicking a [Download] of Network Profile as shown below. Please refer to "How to manage Certificate of Fabric users" for more details about a user's MSP information.


Hyperledger Fabric SDK Download

To develop a 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);

Updated 8 months ago

Developing DApp on Hyperledger Fabric


Suggested Edits are limited on API Reference Pages

You can only suggest edits to Markdown body content, but not to the API spec.