Reader small image

You're reading from  Hyperledger Cookbook

Product typeBook
Published inApr 2019
Reading LevelBeginner
PublisherPackt
ISBN-139781789534887
Edition1st Edition
Languages
Concepts
Right arrow
Authors (3):
Xun (Brian) Wu
Xun (Brian) Wu
author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

Chuanfeng Zhang
Chuanfeng Zhang
author image
Chuanfeng Zhang

Chuanfeng Zhang is enthusiastic and passionate about technologies and trading and data analysis, with 20 years' experience in both the technology and finance sectors. He has worked at top investment banks and technology firms including Goldman Sachs, Credit Suisse, and IBM. He has led, designed, and successfully developed many enterprise-scale systems with diverse architectures for algorithmic trading, order management, risk management, business intelligence, and more.
Read more about Chuanfeng Zhang

Zhibin (Andrew) Zhang
Zhibin (Andrew) Zhang
author image
Zhibin (Andrew) Zhang

Andrew Zhang is an IBM Watson Cloud Advocate. He has many years of experience in cloud platforms, big data analytics, and machine learning. He works with start-ups and enterprise clients in the government, education, healthcare, and life science industries. His current interests are in embedding AI and blockchain open source technologies for consumer and enterprise applications. In his spare time, Andrew enjoys reading, traveling, and spending time with his family and friends.
Read more about Zhibin (Andrew) Zhang

View More author details
Right arrow

Modeling a Business Network Using Hyperledger Composer

Hyperledger Composer is a set of collaboration tools for business owners and developers that make it easy to write chaincode for Hyperledger Fabric and decentralized applications (DApps). With Composer, you can quickly build POC and deploy chaincode to the blockchain in a short amount of time. Hyperledger Composer consists of the following toolsets:

  • A modeling language called CTO: A domain modeling language that defines a business model, concept, and function for a business network definition
  • Playground: Rapid configuration, deployment, and testing of a business network
  • command-line interface (CLI) tools: The client command-line tool is used to integrate business network with Hyperledger Fabric

In this chapter, we will explore the Composer business network and development components, including implementing models, transaction...

The Hyperledger Composer business network and development components

You can use Hyperledger Composer to quickly build a business network. Here, you can define assets, participants, transactions, access-control rules, and optional events and queries in the business network. A model (.cto) file contains all of the preceding definitions in the business network. Asset is an associate with a real-world object, and participants have their own unique identity. The transactions will interact with assets. Participants can do this via transactions. The structure of a business network also includes an access control (permissions.acl), which specifies the access-control rules, a script (logic.js) file that implements transaction logic, and a package.json file that contains project metadata:

...

Setting up the Hyperledger Composer prerequisites environment

A development environment consists of all Composer tools and networks that are required in order to start building Hyperledger Composer. This is not a hard task. The Hyperledger Composer can run on various operation systems including Unix (Ubuntu), macOS, or Windows. In this section, we will cover Ubuntu.

Getting ready

Before we set up a development environment, make sure you have installed the following prerequisites:

  • Operating systems: Ubuntu Linux (64-bit) 14.04/16.04 LTS, or macOS 10.12
  • Node: version >=8.9 and <9
  • npm: >=v5.x
  • Git: >=2.9.x
  • Python: 2.7.x
  • Docker Engine: Version 17.03 or higher
  • Docker Compose: Version 1.8 or higher
To run Hyperledger...

Setting up the development environment

Composer-CLI is the most important tool for Composer deployment; it contains all the essential command-line operations. Other very useful tools include Composer REST server, generator Hyperledger Composer, Yeoman, and Playground. Composer CLI provides many useful tools for developers; we will be using it in this recipe.

Composer CLI can be used to perform multiple administrative, operational, and development tasks. Here is a summary of the CLI commands:

Command

Description

Examples

composer archive <subcommand>

Composer archive command.

Composer archive list.

composer card <subcommand>

Command for managing business network cards.

Composer card list.

composer generator <subcommand>

Composer generator command to convert a business network definition into code.

Composer generator docs.

composer...

Configuring a Composer business network

In the previous section, we set up a development environment. In this section, we will create the FarmaTrace business network definition including the .cto, .script, .query, and .acl files, and then package them as a .bna file using Composer tools.

The yo hyperledger-composer command has three options: Angular, business network, and CLI. These are defined as follows:

  • hyperledger-composer:businessnetwork is used to generate a skeleton business network with an asset, participant and transaction defined, as well as a Mocha unit test.
  • hyperledger-composer:angular is used to generate the Angular application by connecting to a running business network.
  • hyperledger-composer:cli is used to create a standard npm module with the usual attributes of name, author, description and help to create the sample structure.
...

Implementing models, transaction logic, access control, and query definitions

We will use Yeoman to generate a businessnetwork project template. Here, we need to modify the provided template files.

Getting ready

In Playground, select an empty-business-network template to start our model, logic, and ACL implementation:

We first rename the mode file under the models folder to org.packt.farmatrace.cto. Then we add a new script file by clicking on add a file, selecting Script File (.js), and adding a new Query File (.qry) by clicking add a file. You should then select Query File (.qry):

Remove the default generated content in these files. In earlier FarmaTrace use case analyses, we identified that we need to create the following...

Deploying, testing, and exporting business network archives using the Composer command-line interface

We implemented all the models, transactions, and query functions in the previous recipe. It is now time for us to deploy the application to the Fabric network.

Getting ready

There are multiple ways to do this. In the Playground, there is an option to export a file as a .bna file. We can then upload the .bna file to Ubuntu server to begin deployment. We can also update all related files in the Ubuntu server's farmatrace-network folder, including logic.js, permissions.acl, org.packt.farmatrace.cto, and queries.qry, and then manually generate a .bna file.

...

Interacting with Composer through the RESTful API

Once you deploy the business network archive to the blockchain network, how do you integrate it with your client application? One solution would be to use the Hyperledger Composer REST server.

The Hyperledger Composer REST server can be used to generate a Swagger REST endpoint API from a deployed Hypeledger Fabric business network. The REST server utilizes the LoopBack and converts the business network model into an open API definition. An authenticated client calls the REST server via these endpoint APIs to interact with a blockchain. Each invoked transaction must be signed by a certificate. The REST server starts this identity and signs all transactions with this. At runtime, the REST server implements Create, Read, Update, and Delete (CRUD) operations to manipulate the state of assets and participants and allow transactions...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hyperledger Cookbook
Published in: Apr 2019Publisher: PacktISBN-13: 9781789534887
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Xun (Brian) Wu

Xun (Brian) Wu is a senior blockchain architect and consultant. With over 20 years of hands-on experience across various technologies, including Blockchain, big data, cloud, AI, systems, and infrastructure, Brian has worked on more than 50 projects in his career. He has authored nine books, which have been published by O'Reilly, Packt, and Apress, focusing on popular fields within the Blockchain industry. The titles of his books include: Learn Ethereum (First Edition), Learn Ethereum (Second Edition), Blockchain for Teens, Hands-On Smart Contract Development with Hyperledger Fabric V2, Hyperledger Cookbook, Blockchain Quick Start Guide, Security Tokens and Stablecoins Quick Start Guide, Blockchain by Example, and Seven NoSQL Databases in a Week.
Read more about Xun (Brian) Wu

author image
Chuanfeng Zhang

Chuanfeng Zhang is enthusiastic and passionate about technologies and trading and data analysis, with 20 years' experience in both the technology and finance sectors. He has worked at top investment banks and technology firms including Goldman Sachs, Credit Suisse, and IBM. He has led, designed, and successfully developed many enterprise-scale systems with diverse architectures for algorithmic trading, order management, risk management, business intelligence, and more.
Read more about Chuanfeng Zhang

author image
Zhibin (Andrew) Zhang

Andrew Zhang is an IBM Watson Cloud Advocate. He has many years of experience in cloud platforms, big data analytics, and machine learning. He works with start-ups and enterprise clients in the government, education, healthcare, and life science industries. His current interests are in embedding AI and blockchain open source technologies for consumer and enterprise applications. In his spare time, Andrew enjoys reading, traveling, and spending time with his family and friends.
Read more about Zhibin (Andrew) Zhang