GoVertical presents

Blockchain Startup Ideation Workshop

Hosted by TiE Seattle & Madrona Venture Labs

How to create a decentralized application running on Hyperledger Fabric Network

Demo App: Trusted News Network (TNN for short)

There are couple ways to go about creating decentralized application that runs on the Hyperledger Fabric blockchain network. Either creating application natively using the Golang chaincode or using the highly recommended Hyperledger Composer framework.

Before proceeding, be sure to check out How to setup Hyperledger Fabric and Composer to prepare your environment.

Hyperledger Composer essentially speeds up app development time significantly, by exposing a RESTful API that interacts with the Fabric blockchain, however it does abstract out most of the interesting stuff that is awesome about the underlying Fabric blockchain technology, for instance, the installation and instantiation of channels and joining of peers to channels, building and deploying the chaincode and how applications interacts with the network natively by querying and updating the shared ledger.

This post uses the Hyperledger Composer framework to help accelerate the development of the TNN demo application.

Here is a simple description of the TNN demo application. It represents a network of users (Participants) that either produces or consumes video news clips (Assets). Rewards and reputation points are transacted between producers and consumers of published clips.

  • Producers earns rewards/points every time the clips they have produced and published into the network are consumed (emitting a watched Transaction).
  • Consumers also earns reputation points every time they consume a published clip.

Now, lets dive (and delve) into the steps in creating the TNN decentralized application. Sample source code is provided in the resource section (clone or fork as you wish). I pretty much followed the the developer tutorial for creating the Hyperledger Composer solution.

  1. Install Hyperledger Composer
  2. Install the VSCode editor, this is a lightweight open source IDE. Note, once installed, I recommend installing the Hyperledger Composer Extension, it is particularly necessary for syntax highlighting
  • Clone sample project. This project essentially is a sample business network definition, it contains the data model and business logic. Open up a terminal window and fire off this following clone command

    ~$ git clone https://github.com/ejiro/news-composer-network.git
  • Open the project using the VSCode editor. The structure of the project with directory and files that are included should be similar to this.
  • Open the package.json file from within the IDE. Note the name, version number and description of the project. Also, modify the prepublish script by changing the business network archive to news-network.
  • Open and inspect the content of the file, models/sample.cto, this is the schema that defines the participants, the assets and transaction in the network. This file is written with the Composer modeling language that defines the relationships between defined assets and participants and the Transaction mechanism in which participants interacts with assets.

  • Open and inspect the content of the file, lib/sample.js, this is the business logic written in native javascript using primarily using the Hyperledger Composer API for interacting between the client application and the blockchain network. The defined transaction processor function, watchVideoClip, act as an interface for submitting transaction that may update the ledger in the blockchain.

  • Open the access control file, permissions.acl. This file list out the different permissions between participants and assets. This is where the rules and privacy configurations are defined.

That is it in terms of code base. Though you may choose to wire up a unit test, that is outside the scope of this post.

  • Generate and Deploy a Business Network Archive (BNA) file. This is the deployable executable file within the Composer runtime. If you are not familiar with VSCode IDE, it has a built-in terminal window, you may choose to use it to issue commands, it does simplify navigating between terminal windows

    # Generates a news-network.bna file within the /dist directory
    ~$ npm install
    # or
    ~$ mkdir dist
    ~$ composer archive create --sourceType dir --sourceName . -a ./dist/news-network.bna


    # Deploys the BNA file into running Fabric blockchain network.
    ~$ composer network deploy -a dist/news-network.bna -p hlfv1 -i PeerAdmin -s randomString

    # Run the following command to verify by noting the success response
    ~$ composer network ping -n news-network -p hlfv1 -i admin -s adminpw
  • Generate RESTful API

    # if already in the root of the project, issue the command
    ~$ composer-rest-server

Follow and answer the onscreen input request. Here is how mine looked after entering all requested info. By default, the connection profile name is hlfv1, and the network identifier is news-network, the Fabric username and password is admin and adminpw respectively.

If all goes well, you should see the Web Server URL and the REST API. The API uses the LoopBack API explorer.

Web server listening at: http://localhost:3000
Browse your REST API at http://localhost:3000/explorer

Browse around and explore the REST API. From the onset, you should notice the different endpoints, especially that of the NewsClip and the User endpoints. You can test out a simple query within the explorer.

  • Create a Web Application utilizing the generated REST API. At this point, it is up to you what front-facing application (mobile, web) you prefer to create utilizing the REST API. I decided to go with a simple node.js (could have gone with python flask) web application that is entirely separated from the composer solution. You can clone the simple newsclip node application, and fire it up by running the following commands.

    # clone the front facing newsclips node application
    ~$ git clone https://github.com/ejiro/newsclips-node-app.git

    # fire up the app
    ~$ cd newsclips-node-app
    ~$ npm install~$ node ./bin/www

    # Browse and explore application at: http://localhost:8080/

Congratulations! Check the resource section for additional information and links to get you started.

Resources

Questions? Send us a note!

Ⓒ 2017 TiE Seattle and Madrona Venture Labs. Contact us