NEM Blockchain onboarding combined with ReasonML crash course
This article explains basic usage of NEM2-SDK with ReasonML, together with NEM2-CLI.
NEM is a relatively new blockchain technology, that handles multisignature transactions with ease, and has a wonderful dev-tooling.
First of all, let’s go trough the technologies that we will use:
- NEM Blockchain is a platform, that aims to ease the blockchain integration into existing / new apps, by providing a very very nice 💆♀️ developer experience out of the box. It enables private/public chains, smart asset trading and multi-signature transactions.
We will start by installing the NEM Catapult stack first. We are mainly interested in those two parts:
- Catapult server nodes (blockchain node); This layer handles the P2P blockchain magic 🧙♂️🔮
- Catapult Rest nodes (API layer); This layer provides an easy way for us (developers) to integrate with NEM 💻
1. Installing Catapult
Start off by cloning the catapult-service-bootstrap repository, and follow up by composing all the docker services using the commands below.
For those who are not familiar with docker or git, we’re copying a bunch of code from a remote repository, and then starting the provided services using docker ‘virtual machines’.
Next step is to verify, that our Rest and Blockchain nodes are running. You can do that by querying the REST API provided by Catapult Rest, as shown below.
You have successfully installed NEM Blockchain and REST nodes on your machine! 😎
2. Setting up ReasonML / NEM environment
To develop our blockchain integration with Catapult REST, we will use the NEM SDK, with ReasonML.
Start by creating the appropriate folder structure:
Commands below are executed outside of the ‘git cloned’ directory we created in step 1
We will need an environment to compile our reason scripts. Use a node based docker image, with bs-platform (ReasonML tooling) installed. Click here to learn what role does bs-platform a.k.a. bucklescript compiler play here.
Next step is to build our docker image, so we can use our reason environment.
We will use run-in-docker.sh to provide an interactive shell with ReasonML installed.
Congrats, you’re now ready to communicate with NEM Blockchain, trough scripts written in ReasonML — all that you need to compile and run ReasonML scripts, happens in the docker image we’ve just built.
3. Reason project setup
So far so good, we’ve established a solid base. NEM blockchain and REST nodes are running, and we have a docker-image with a tiny helper script to run ReasonML code.
Next step is to scaffold/generate a reason project.
To confirm that our project has been generated as expected, let’s compile and execute our generated demo script.
Feel free to use a code editor of your choice, I usually opt-in for vscode. Our docker image, takes / mounts files from the same file system that we can access from our ‘real computer’. To learn more click here.
Let’s get this bread
Our setup works as expected, we can move on to the fun part (allegedly 🤔).
First exercise, will create a new account on NEM Blockchain, using the sdk, and the cli.
In our docker environment, install nem2-sdk.
Generating a new account — SDK
We want to create a NEM account / wallet, which is a basic identity block for the blockchain — enabling us to use all the cool NEM features such as smart asset trading, or multisignature transactions. We will end up with a private key, public key and an address.
Let’s verify that the file compiles and executes first, then we’ll walk trough it’s contents.
Looks familiar huh? What does it do?
- Import the SDK,
- Extract the typescript enum’s value, for MIJIN_TEST
- If the test network is specified properly, use SDK’s Account class, to create a new account
You can now execute the output script, using node.
Generating a new account — CLI
So far we’ve been able to create a new NEM Account, using the provided SDK, with custom ReasonML bindings. Now let’s try achieving the same result using the NEM CLI.
Our docker image already contains nem2-cli, so let’s use it.
In case your catapult services are not running, you can use the following commands to get them up again.
Now we will use the cli, to generate a new account, and save it to the CLI’s profile list. More or less the same, as our code did prieviously. Make sure to run the following commands, inside run-in-docker.sh — because you need access to nem2-cli that’s installed there.
Command below, will ask you for certain information, we’ll use the same network type as in our Reason examples, while NEM2 Node URL, will change (shown below) — this is due to the fact, that the REST Node, runs in a separate container, than our run-in-docker.sh, so essentially this comes down to docker networking, which you can learn more about here.
Using private keys generated with Reason
If you want to, you can populate the CLI’s profile list, with accounts generated programatically (using our Reason code).
It’s fairly straight forward:
Congrats, you’ve successfully created two (or more?) NEM accounts, which you can now use to explore the rest of the NEM Blockchain. There’s a vast documentation available, you can find it at this link.
In the follow up article, we will explore the NEM2 Faucet, Mosaics and Multi-signature transactions.
This will allow us to trade our custom Bread 🥖 (Mosaic / smart asset), for xem (virtual currency) with other accounts on the blockchain.