Skip to content

Latest commit

 

History

History
131 lines (71 loc) · 5.12 KB

zenroom-javascript1.md

File metadata and controls

131 lines (71 loc) · 5.12 KB

Make ❤️ with Zenroom and Javascript

Make ❤️ with Zenroom and Javascript

How to use Zenroom within the JS ecosystem

This article is part of a series of blog posts about interacting with Zenroom inside the Javascript messy world. This is the first entry and at the end of the article you should be able to run a fancy hello world within nodejs.

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.

https://www.zenroom.org The zenroom guru

Zenroom is a dyne.org project since 2017 and is 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 just released the 1.0.0 version of Zenroom 🎉 Hopefully first of many to come TRL9 releases.

📑 Some RTFM and resources

So first things first, let’s start by where to look for good information about Zenroom (docs that are continuously under enhancement and update).

🌐 How a VM could live in a browser?

So basically Zenroom is a virtual machine that is mostly written in C and high-level languages 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, WebAssembly and asm.js

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

So let’s start by our first hello world example in node.js I’m familiar with yarn so I’ll use that but if you prefer you can use npm

$ mkdir zenroom-nodejs-test$ cd zenroom-nodejs-test $ yarn init $ yarn add zenroom

The previous commands create a folder and a js project and will add zenroom javascript wrapper as a dependency. The wrapper is a very simple utility around the pure emscripten build.

Next create a index.js with the following content

const zenroom = require('zenroom').default
zenroom.script('print("Hello World!")').zenroom.exec()

So the first line will import the zenroom module the second one executes a very simple Hello World! zenroom script.

NB. The documentation of the JS wrapper API is available here .

Now we need to run the file we just created by simply run

$ node index.js

🎉🎉🎉

You’ll find the asciimation here: https://asciinema.org/a/274518

Let’s complicate it a bit! Let’s run Zencode!

Now that we saw that everything is let’s procede with some sofistication, let’s run a Natural Language Smart Contract called Zencode.

So create a new file called keygen.js and put the following code:

const zenroom = require('zenroom')

const keygen_contract = `rule check version 1.0.0
Scenario 'simple': Create the keypair`
Given that I am known as 'Puria'
When I create the keypair
Then print my data`

zenroom.script(keygen_contract).zencode_exec()

Et voila the result is something like (I prettified for the purpose of readability)

{
  "zenroom": {
    "curve": "goldilocks",
    "encoding": "url64",
    "version": "1.0.0+a7fab75",
    "scenario": "simple"
  },
  "Puria": {
    "keypair": {
      "public_key": "u64:BBqhjaIXr6vPMVhQKSU1vau5lUJDXwGBul0OwZYarNnUhbG2W6bMY-uo2dH-W4ymjx-vU_3agTQm2N1F25xq8o74DutvNW3ZX8GHROa5zIi7TIDoXy-_5sSyKBeVnGZ9IrFkoo9R2cbtREjOE6hgZ-Q",
      "private_key": "u64:IT-cZZQf1-yzXF6GSrvQGScRHGbZeh8_LGFMIGCKrxKZtbk3RJbWXLlBlOfJ3oAWgaaYa5mc9iM"
    }
  }
}

NB. The documentation of zencode is available here

In the next article we will see how to run the same examples within a browser.