paint-brush
Hyperledger Fabric + Convector + Convector CLI (JavaScript only)by@waltermontes
721 reads
721 reads

Hyperledger Fabric + Convector + Convector CLI (JavaScript only)

by Walter MontesNovember 26th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Are you new to blockchain? You may have probably already heard about the amazing work that the <em>Linux Foundation</em> has done with <em>Hyperledger Fabric</em>. It’s a modular framework for building permissioned blockchain networks.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Hyperledger Fabric + Convector + Convector CLI (JavaScript only)
Walter Montes HackerNoon profile picture

Are you new to blockchain? You may have probably already heard about the amazing work that the Linux Foundation has done with Hyperledger Fabric. It’s a modular framework for building permissioned blockchain networks.

Here I want to showcase how to create your own smart contract system in an easier way that you would expect. And with the only JavaScript!

I will use as a baseline the design from the Fabric-Sample repo FabCar, I won’t change its design, only migrate the code to Convector Framework.

As the title mentions, I’ll be using the new Convector CLI with Hyperledger Fabric as the underlying blockchain technology.




npm i -g @worldsibu/convector-cliconv new fabcar -c carcd fabcarnpm i

What you get by running that command is a whole JavaScript structure for a Fabric smart contract (chaincode).

We will need to migrate the current structure from the fabcar.go to a car.model.ts TypeScript class. The first difference is that Convector helps you to structure your mind around Model/Controller pattern. So instead of having everything in a single file, Convector-CLI generates two files for you.

One is a simple model with a few Decorators to make it easier for validations, as well a controller class in which you define your smart contract rules.

The rest are supporting files. For example*.config.json are for installation in the blockchain purposes. It already comes in with all the details the network will need.

Convector recommends using Lerna for a multipackage project as well TypeScript (required for now).

Let’s take a look at the original Go structure:






type Car struct {Make string `json:"make"`Model string `json:"model"`Colour string `json:"colour"`Owner string `json:"owner"`}

As you can see it’s a fairly simple structure. Open your Convector-CLI generated project’s folder and go to ./packages/car-cc/src/car.model.ts .

After migrating the car structure from Golang, my car.model.ts file looks like this:

As you can see it is still fairly simple yet it has a few @ Decorators. This will help you avoid validation mistakes since it takes away simple verifications out of your code, for example instead of doing if(!item.owner) throw new Error('owner cannot be null'); , you only write @Required() on top of the property.

Now, we need to migrate the chaincode’s logic. I won’t copy the whole original code here, it may be better for you to look for it right in Fabric’s repo. As a overview, they include the following functions for the smart contract:

if function == "queryCar" { return s.queryCar(APIstub, args) } else if function == "initLedger" { return s.initLedger(APIstub) } else if function == "createCar" { return s.createCar(APIstub, args) } else if function == "queryAllCars" { return s.queryAllCars(APIstub) } else if function == "changeCarOwner" { return s.changeCarOwner(APIstub, args) }

They would look like this on Convector:

Cleaner TypeScript/Convector way of doing a controller with the typical functions.

See how simple the code becomes? That’s the main goal of Convector. In less than 20 minutes you can have your smart contract developed.

Obviously the code may look great, but let’s test it out! Convector-CLI creates a basic set of scripts to consume Convector Tools (in this case Development Environment manager — that installs you a blockchain network, and Chaincode Manager which helps you installing and upgrading chaincodes).

Back to the console run:


# Wake yourself up a dev blockchain$ npm run env:restart









...Register admin for org1Register admin for org2Register user1 for org1Register user1 for org2Register user2 for org1Register user2 for org2Register user3 for org1Register user3 for org2


# Now install the chaincode!$ npm run cc:start -- car 1

...

Org1MSP




Org2MSPInstantiated successfullyInitializing controllers now...Initialization successfully

That’s all! Now you have a running network (see details of what gets provisioned for you here Development Environment) and your own chaincode installed.

The next step is to call it from your backend! Another great thing about Convector is that you can use the native Fabric tools, so from your programming language invoke your chaincode with the corresponding Fabric SDK. If you are using NodeJS the framework will provide you helpers to connect your backend server with your blockchain. More info about it here.