Convector with NestJS 🤯
By: Mariano Alvarez
Tags: JavaScript, Nestjs, Convector, Blockchain, Typescript

These days I’ve been playing around with Convector, which is a javascript framework to develop smart contracts and blockchains. Honestly Kudos to the WoldSibu for bringing such a great tool to the javascript world.

I won’t dive too much into how convector works but I highly encourage you to check it out here 😉.

Convector has a set of tutorials in their website which give you a walkthrough of how to use the framework. I noticed that one of those is to create a custom back-end to expose the APIs using NodeJS, specifically with Express.

I was wondering if I could use a different NodeJS Framework instead of Express?? is it possible?? 🤔 The answer is YES! It’s just Javascript. Soooo We’re going to use NestJS in this tutorial.

But what is NestJS? It’s defined in their website as “A progressive Node.js framework for building efficient, reliable and scalable server-side applications.” One of the cool things is that it’s very similar to Angular, follows the same patters, and has similar features like providers, pipes, modules.

📝 Before we start

Install Node 8 (version recommended by the documentation)

Install docker

Read the smart contract tutorial in convector website before continue.

⏰ Too much talk, Chap chap!

We’re going to use the example provided in the back-end tutorial as our base project and then migrate the Express app to NestJS. Fork the repo here.

🛠 Set-Up

Please execute the following order, otherwise, you might run into some weird errors:

Install Hurley which will help us to set up a testing network

npm i -g @worldsibu/hurley Run rm -rf ./packages/server to delete Express App Install the NestJS CLI by running npm install -g @nestjs/cli Move to the package folder run nest new server . This is going to scaffold a NestJS project for you. Access to the server folder cd server Install dotenv for handle environmental variables

npm i dotenv --save-dev Go back to the root of the project and install the smart contract packages that are going to be consumed by NestJS

npx lerna add participant-cc --scope server --no-bootstrap

npx lerna add person-cc --scope server --no-bootstrap To avoid typing conflicts add the skipLibCheck flag in the root and server tsconfig files. (There is a problem between Jest used by NestJS and Mocha used by Convector)

root/tsconfig.json

9. And lastly npm install

If npx don’t work, update the NPM version or just install lerna globally by running npm install -g lerna and run the same command with no npx

👷🏻‍♂️ What are we going to build?

We’re going to re-use the two smart contracts that were created in the smart-contract tutorial and expose them through endpoints using NestJS.

Here is a brief explanation of what it is done in the smart contract tutorial:

There are three data structures in the blockchain: Network Participants, Attribute, and Person. Each data structure is defined as:

Network Participants: These are the companies issuing attributes in the network. They are responsible for what they state for users and are identifiable in the network.

These are the companies issuing attributes in the network. They are responsible for what they state for users and are identifiable in the network. Attribute: It’s the statement that a Network Participant certifies for a Person .

It’s the statement that a certifies for a . Person: These are the people representation in the network. They have attributes assigned and can simply query them as needed to certify in front of a third party.

3. Business rules

Create a person should be the responsibility of just one organization — the government.

should be the responsibility of just one organization — the government. Each organization ( network participant ) should be able to issue or certify attributes.

) should be able to issue or certify attributes. Each organization ( network participant ) should be able to check/query attributes from a person.

) should be able to check/query attributes from a person. A single ID should be enough to query all the information related to one person and his attributes.

Only the organization that certifies an attribute can edit it.

The flow should look like this:

The government can “enroll” a new person.

The government can add some attributes to that person by default.

Then, other organizations can start to issue attributes (profession, insurance status, work experience).

Every change in the attributes issued will be evident. Changes on attributes can only be performed by the organization that issued them in the first place.

Definition and business rules were taken from the tutorial

Let take a look to the following example to understand better:

Assume that a network participant is the government, a person is you, and an attribute your born certificate. The government is in charge of registering your birthday and issue a born certificate that you’ll store. But for some reason, your name has a typo (I’m sorry for you 😂) in the certificate and only the county court (another network participant that is not the government) can help you. The county court can fix your name but not create another person.

👨🏻‍💻 Code Code

We already bootstrap the NestJS project, the next step is to create a module, a controller, and a service for each smart-contract.

Let’s start with the participant logic. To generate the module run:

nest generate module participant

This command is going to create the participant module and register it in the app module automatically.

Every command run using the Nest CLI is going to automatically register the feature (provider, module, pipe, etc) to the scope module if it exists otherwise it will be added to the app module.

To create the controller:

nest generate controller participant

Let’s do very quick the same for person but this time we’re going to create a service for this module:

nest generate module person nest generate controller person nest generate service person

and create two files in src folder: env.ts and convector.ts :

src/server/env.ts

packages/server/src/convector.ts

Your src folder should look like this

NestJS source folder

Now, what we need to do is start migrating the logic. All the code related to the communication with convector and any other heavy calculation to our services and the controllers are going to be in charge of exposing the endpoints and pass down the data to the services.

Copy and paste to the corresponding file

Participant Controller

Person Service

Person Controller

⚙️ Run Convector and test

Let’s run the same commands used in the back-end tutorial:

Make sure that Docker is running. Go to the root and run:

3. Test the endpoints

4. Run a few transactions

Result:

Conclusion

Convector is a great tool to create smart contracts using javascript, it’s one more option in the web developer plate. I find it awesome that it doesn’t stick to a NodeJS framework. On the other hand, NestJS is a very robust framework inspired in Angular, letting the Front-end devs move easily to the back-end side.

Find the full example here