Make šŸ’ with Zencode and Javascript AKA easy crypto and human-readable smart contracts in JS

Puria Nafisi Azizi
think-do-tank
Published in
4 min readJan 21, 2021

This article is part of a series of blog posts about interacting with Zenroom VM inside the Javascript/Typescript messy world. This is the first entry and at the end of the article you should be able to implement your own encryption library with Elliptic-curve Diffieā€“Hellman.

šŸ§˜šŸ¼ā€ā™€ļø Zenroom

If you landed on this page accidentally maybe you heard of Javascript but not about Zenroom. This later as says on the official website is

a tiny and portable virtual machine that integrates in any application to authenticate and restrict access to data and execute human-readable smart contracts.

Zenroom is a dyne.org project since 2017 and was part of the DECODE project about data-ownership and technological sovereignty.

If you are landed here on purpose, as the time of writing this, we are just close to release the LTS version of Zenroom šŸŽ‰

šŸ“‘ Some RTFM and resources

Before diving into code snippets to copy ā€” paste in your terminal, let me point you to some webpages where you can Ctrl+F šŸ” random keywords

šŸŒ How a VM could live in a browser?

So basically Zenroom is a virtual machine that is mostly written in C and has no access to I/O and no access to networking, this is the reason that makes it so portable.

In the past years we got a huge effort from nice projects to transpile native code to Javascript, we are talking about projects like emscripten and WebAssembly.

This is exactly what we used to create a WASM (WebAssembly) build by using the Emscripten toolkit, that behaves in a good manner with the JS world.

šŸ’» Letā€™s get our hands dirty

Letā€™s start by our first hello world example in node.js Iā€™m more familiar with yarn so Iā€™ll use that but if you prefer you can use npm

$ mkdir hello-world-zencode
$ cd !$
$ yarn init
$ yarn add zenroom

The previous commands create a folder and a javascript project and will add zenroom npm package as a dependency. This library is a very simple wrapper around the pure zenroom wasm build.

Now create a index.js with the following content

run with

$ node index.js

šŸ„³ we just run our hello world in node.js

Let's go through lines; In first line we import the zencode_exec function from the zenroom package. Two major functions are exposed:

  • zencode_exec to execute Zencode (natural language smart contracts). To learn more about zencode syntax look here
  • zenroom_exec to execute our special flavor of Lua enhanced with Zenroomā€™s special effects

Before you šŸ¤¬ on me for the underscore casing, this was a though decision but is on purpose to keep the naming consistent across all of our bindings.

zencode_exec is an asynchronous function, means return a Promise (more on promises here) and accepts two parameters:

  • the smart contract, mandatory, in form of string
  • an optional object literal that can contain {data, keys, conf} in brief data and keys is how you pass data to your smart contract, and conf is a configuration string that changes the Zenroom VM behaviour. All of them should be passed in form of stringā€¦ this means that even if you need to pass a JSON you need to JSON.stringify it before, as we did on line 5 of the previous snippet

zencode_exec resolves the promise with an object that contains two attributes:

  • result this is the output of the execution of the smart contract in form of string
  • logs the logs of the virtual machineā€¦ if there are some errors ā€” warning they are printed here

In the previous snippet we just passed the result by using Object destructuring on line 8

šŸ” Letā€™s complicate it a bit! Letā€™s encrypt!

Now that we saw how the basics works, letā€™s proceed with some sophistication: let's encrypt a message with a password/secret with ECDH (Elliptic-curve Diffieā€“Hellman) on the elliptic curve SECP256K1 sounds complicated, isn't it?

Et voila šŸ¤Æ as easy as the hello the worldā€¦ if you run it you'll get something like

But being able to encrypt without having a decrypt function is useless like leaving your mark with this machine

So let's tidy up a bit and create our own encryption/decryption library with some javascript fu

There you go encryption ā€” decryption with password ā€” secret over Elliptic-curve Diffieā€“Hellman on curve SECP256K1 in 30 super easy lines of code.

Now hold tight until next week for the part 2ā€¦ in the meantime clap this post and spread it all over the socials.

One last thing, you'll find the working code project on Github

--

--