README.md 4.48 KB
Newer Older
Alex Puig's avatar
Alex Puig committed
1 2 3 4
# A Javascript Library for Zenroom

zenroom-lib is a javascript library to interact with the [Zenroom Virtual Machine](https://zenroom.org/)

Chuck LeDuc Díaz's avatar
Chuck LeDuc Díaz committed
5 6 7
|Package|Branch|Pipeline|Coverage|
|:-:|:-:|:-:|:-:|
[![npm version](https://badge.fury.io/js/%40caelum-tech%2Fzenroom-lib.svg)](https://badge.fury.io/js/%40caelum-tech%2Fzenroom-lib)|[`master`](https://gitlab.com/caelum-tech/Lorena/zenroom-lib/tree/master)|[![pipeline status](https://gitlab.com/caelum-tech/Lorena/zenroom-lib/badges/master/pipeline.svg)](https://gitlab.com/caelum-tech/Lorena/zenroom-lib/commits/master)|[![coverage report](https://gitlab.com/caelum-tech/Lorena/zenroom-lib/badges/master/coverage.svg)](https://gitlab.com/caelum-tech/Lorena/zenroom-lib/commits/master)|
Chuck LeDuc Díaz's avatar
Chuck LeDuc Díaz committed
8

9
Zenroom 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.
Alex Puig's avatar
Alex Puig committed
10 11 12

## Installation
```shell
Chuck LeDuc Díaz's avatar
Chuck LeDuc Díaz committed
13 14 15 16 17 18 19
npm install @caelum-tech/zenroom-lib
```

### For React
This library uses [Zenroom](https://www.npmjs.com/package/zenroom) 1.0.0 [which has a problem](https://www.dyne.org/using-zenroom-with-javascript-react-part3/) with [create-react-app](https://create-react-app.dev/).  To fix this issue we've added [a script](https://gitlab.com/caelum-tech/lorena/zenroom-lib/blob/master/bin/zenroom_modules.sh) which patches the installed package:
```shell
./node_modules/@caelum-tech/zenroom-lib/bin/zenroom_modules.sh
Alex Puig's avatar
Alex Puig committed
20 21 22 23 24 25
```

## Usage

Initialize
```javascript
Chuck LeDuc Díaz's avatar
Chuck LeDuc Díaz committed
26
const Zen = require('@caelum-tech/zenroom-lib')
Alex Puig's avatar
Alex Puig committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
let z = new Zen()
```

## Keypairs

Create keypairs
```javascript
// Create a new keypair for Alice & Bob
let alice_keypair = await z.newKeyPair('Alice')
let bob_keypair = await z.newKeyPair('Bob')
```

Being the result:
```javascript
{
  zenroom: {
    curve: 'goldilocks',
    encoding: 'url64',
    version: '1.0.0+a7fab75',
    scenario: 'simple'
  },
  Alice: {
    keypair: {
      public_key: 'u64:BH4GCburF7yL1KhITA676nxKIgEB2SQZ9BmeehuoWgPObMpb9ZTTigBgfhbrwLTxf0tWtRK6kM6D0DVItqdMWGRDsII75qXcLOunQTTiGcpAH3_iTlqjzXUDeDzcudyFhZByFohsi1wCqeAXPsKsjeQ',
      private_key: 'u64:IKwYf6BRXMQBveMizlkx0k1ru3qg3wApZBAfZ2sUL6nUGKG9tvU6hB9s4cmN-Gi2rXDjeIm-quk'
    }
  }
}
```

Retrieve only the public Key
```javascript
// Create a new keypair for Alice & Bob
let alice_public = await z.publicKey('Alice', alice_keypair)
let bob_public = await z.publicKey('Bob', bob_keypair)
```

Being the result:
```javascript
{
  zenroom: {
    curve: 'goldilocks',
    encoding: 'url64',
    version: '1.0.0+a7fab75',
    scenario: 'simple'
  },
  Alice: {
    public_key: 'u64:BJeFhvqKzJERiHrZaMHlPR6ms59086qcwtafngq2nJvyDUatcdH7NSkVGvf5iKnWpsC546lTjhLIxWDf1-wfUdRy3dXa6Y6OzQvmMtqrRh33t5pXvuCDylRGiA4DqWKV6ocymggIvhdtMaXLOvNDoy4'
  }
}

```

## Signatures
Sign a message
```javascript
// Create a new keypair for Alice & Bob
const signature = await z.signMessage('Alice', alice_keypair, message)
```
Being the result:
```javascript
{
  zenroom: {
    curve: 'goldilocks',
    encoding: 'url64',
    version: '1.0.0+a7fab75',
    scenario: 'simple'
  },
  Alice: {
    draft: 'u64:SGVsbG9fV29ybGQ',
    signature: {
      s: 'u64:H71LonTCQOhhvuYCx9dMXNLDe0g-qngR28njL0tAgn8mdX2YYu2tAn9jyeaUJmBpk9iJijr7cvE',
      r: 'u64:Pv4lnBlJgPaFxEGXHntwIaUem__tjFpWQMOG9yelvb2VB5xvj2PXMTg-SsHExfL6BSPaHwFSbdo'
    }
  }
}
```

Check the signature
```javascript
// Create a new keypair for Alice & Bob
const check = await z.checkSignature('Alice', alice_public, signature, 'Bob')
```
Being the result:
```javascript
{
  zenroom: {
    curve: 'goldilocks',
    encoding: 'url64',
    version: '1.0.0+a7fab75',
    scenario: 'simple'
  },
  signature: 'correct'
}

```

## Encryption
encrypts a message
```javascript
// Create a new keypair for Alice & Bob
const msg_encrypted = await z.encryptMessage('Alice', alice_keypair, 'Bob', bob_public, 'HelloWorld')
```
Being the result:
```javascript
{
  secret_message: {
    iv: 'u64:Da57UyzCWz0gbxCeLpPPLA',
    header: 'u64:VGhpc19pc190aGVfaGVhZGVy',
    text: 'u64:4vazxwae5d4Pi9E',
    checksum: 'u64:pRGDjsiYg_9dQS1rWk-gVg'
  },
  zenroom: {
    curve: 'goldilocks',
    encoding: 'url64',
    version: '1.0.0+a7fab75',
    scenario: 'simple'
  }
}

```

Decrypts a message
```javascript
// Create a new keypair for Alice & Bob
let msg = await z.decryptMessage('Alice', alice_public, 'Bob', bob_keypair, msg_encrypted)
```
Being the result:
```javascript
{
  message: 'Hello_World',
  zenroom: {
    curve: 'goldilocks',
    encoding: 'url64',
    version: '1.0.0+a7fab75',
    scenario: 'simple'
  },
  header: 'This_is_the_header'
}
```