Hyperledger Fabric Typescript Boilerplate.

jo vinkenroye
wearetheledger
Published in
4 min readJan 25, 2018

The Hyperledger Fabric SDK for Node.js provides a powerful API to interact with a Hyperledger Fabric blockchain. The SDK is designed to be used in the Node.js JavaScript runtime. Unfortunately, we couldn’t find a good Express boilerplate that interacts Restfully between a frontend and the Hyperledger Fabric (HLF) Peers using the client. As we are getting more and more requests for Hyperledger projects, this template is an attempt at creating a reusable project starter to set up new Hyperledger Fabric applications in no-time.

We started by looking at Express frameworks that support typescript. Express frameworks allow for easy integration with the myriad third-party plugins available. This is how we stumbled upon Nestjs, a Node.js framework that provides an MVC application architecture and Typescript out of the box.

Application Architecture

A Nest application is divided into modules. Similar to Angular modules, they bundle a collection of controllers, services, filters and configuration information. You can think of it as containers for the different parts of your application. Every Nest application has at least one module- the root module. The root module is the place where Nest starts to arrange the application tree. In fact, the root module can be the only module in your application, especially when the app is small. For large applications, however, it doesn’t make sense. In most cases, you’ll have several modules, each with a closely related set of capabilities.

We decided to split up the application architecture into several modules according to their dependencies and responsibilities:

Application modules

App Module and Routing

The application serves as an API between a restful frontend(not included) and the HLF peers. The app module contains a set of controllers and components (nest’s equivalent for services) which take care of incoming HTTP requests. (Notice that the app.module imports the other modules)

app.module.ts

Because this is a boilerplate app, we will only add two controllers:

  • The Ping Controller points to the root path and simply indicates whether the server is running
  • The Assets Controller contains a ‘GetAll’ and a ‘Create’ method which will be used to simulate the handling of a query and an invoke to the HLF Client.

The Asset ‘Data Transfer Object’ or DTO simply contains a ‘name’ and ‘description’ property and can be found in the asset.model.ts file:

asset.model.ts

You may notice the ApiModelProperty decorators above the properties. These were added because we are using the OpenAPI (Swagger) implementation for Nest and the decorators make the properties accessible to the SwaggerModule. More information on using OpenAPI with Nest can be found here.

Chain Module

In the asset.service.ts, you can see we are using a ‘requesthelper’. The requestHelper is part of the Chain Module and abstracts:

  • Validation of incoming requests using yup schemas
  • HLF Chain query requests
  • HLF Chain invoke requests

Query requests are handled differently from Invoke requests. Because of concurrency issues, invoke requests are first pushed onto a FIFO queue so they can be handled consecutively. We use an AWS SQS to accomplish this.

Queue Module

The Queue Module takes care of the concurrency issues that are caused when multiple users try to execute the same transaction at the same time. An AWS SQS FIFO queue is placed between the business logic and the Hyperledger Fabric SDK. We start listening to this queue after the HLF Client has been successfully initialized.

app.module.ts

Invoke requests are pushed onto this queue in the invokeRequest method in the requestHelper.

requesthelper.ts

The queueListener then handles the incoming invoke requests consecutively:

queuelistener.service.ts

EventModule

After the HLF SDK handles our requests, we use Pusher to trigger a notification in the frontend. We are looking at doing this with web-sockets in the future. This is the reason why the pusher service is currently called the WebSocketService.

Authentication (WIP)

We are using Auth0 to authenticate our users in the frontend. The request headers will contain the necessary information that allows the authentication middleware to verify incoming requests. This is still a work in progress as we are looking to couple/extend identity management into HLF.

Conclusion

The Nest framework and it’s modular architecture together with serverside Typescript has allowed us to develop an efficient boilerplate solution that enables us to abstract away the current HLF concurrency issues.

(Feel free to use and/or contribute to the future development of this project.)

We now have a reusable template that helps us to iterate faster during upcoming projects and allows us to focus more on core Hyperledger Blockchain development tasks, like writing chain-code, managing identities, implementing security and setting up clients, orderers and peers .

The source code for the boilerplate can be found here.

Get in touch

Interested in starting your own blockchain project, but don’t know how? Do you need help starting your token sale or having one audited? Get in touch https://theledger.be

--

--