Make 💏 with Zencode and Javascript AKA easy crypto and human-readable smart contracts in JS

Make 💏 with Zencode and Javascript AKA easy crypto and human-readable smart contracts in JS

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

Zenroom 📝 Documentation
Zenroom is a portable and independent virtual machine for secure cryptographic operations. Zencode is the name of the language executed by Zenroom: it is simple to understand and can process large data structures while operating cryptographic transformations on them.
Issues · dyne/Zenroom
Embedded no-code VM executing human-like language to manipulate data and process cryptographic operations. - Issues · dyne/Zenroom
Apiroom
Zenroom’s web playground
zenroom
wrapper of Zenroom, a secure and small virtual machine for crypto language processing. Latest version: 4.7.1, last published: 5 days ago. Start using zenroom in your project by running `npm i zenroom`. There are 14 other projects in the npm registry using zenroom.

🌐 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

GitHub - dyne/blog-code-samples: Code samples linked from https://medium.com/think-do-tank
Code samples linked from https://medium.com/think-do-tank - GitHub - dyne/blog-code-samples: Code samples linked from https://medium.com/think-do-tank

Tune in to the discussion 💬

(These services are bridged: join your favorite and reach them all)

🗨️ Matrix
🗨️ Telegram
🗨️ Discord

Support Dyne 🫱🏿‍🫲🏾

🪙 Bitcoins: bc1qz9wz2f9swcefra2tfrhk4fx49evqsv03m9nx4l
Ko-Fi
🍴 Github.com
🧁 LiberaPay
🍥 Patreon.com

Follow Dyne.org 🗞️

Social Media everywhere!

🐘 Mastodon
🎬 Peertube
🐭 Lemmy
📸 Instagram
🐦 Xitter
👔 Linkedin
🪞 Facebook
✍️ Medium

Dyne.org

Dyne.org

Dyne.org is a digital community & free software foundry. We share tools, practices & narratives that empower artists, creatives & citizens in the digital age. - Website
Haparandadam 7-A1 1013AK Amsterdam
Puria Nafisi Azizi

Puria Nafisi Azizi

Resident hacker @dyne.org 🌋 Think & Do Tank - Website