Browse Source

Merge remote-tracking branch 'upstream/master'

master
Adam Peter Burns 1 year ago
parent
commit
d389327b2e
15 changed files with 3861 additions and 4297 deletions
  1. +38
    -0
      bindings/java/decode/zenroom/Zenroom.java
  2. +41
    -0
      bindings/java/testZenroom.java
  3. +37
    -43
      bindings/javascript/README.md
  4. +0
    -0
      bindings/javascript/dist/.empty
  5. +1
    -1
      bindings/javascript/dist/index.d.ts
  6. +482
    -0
      bindings/javascript/dist/wrapper.js
  7. +1
    -1
      bindings/javascript/package.json
  8. +91
    -92
      bindings/javascript/src/wrapper.js
  9. +1
    -1
      bindings/javascript/test/test.js
  10. +3019
    -4157
      bindings/javascript/yarn.lock
  11. +58
    -0
      build/build-java.sh
  12. +8
    -0
      build/config.mk
  13. +1
    -1
      build/linux.mk
  14. +1
    -1
      src/Makefile
  15. +82
    -0
      test/zencode_given/run.sh

+ 38
- 0
bindings/java/decode/zenroom/Zenroom.java View File

@@ -0,0 +1,38 @@
/*
* DECODE App – A mobile app to control your personal data
*
* Copyright (C) 2019 – DRIBIA Data Research S.L.
*
* DECODE App is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DECODE App is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* email: info@dribia.com
*/

/**
* This class contains the JNI bindings for the native Zenroom library.
* It's package and method names are tied to it, be careful if you need changing them
*/

package decode.zenroom;



public class Zenroom {

public native String zenroom(String script, String conf, String key, String data);

public String execute(String script, String conf, String key, String data) {
return zenroom(script, conf, key, data);
}
}

+ 41
- 0
bindings/java/testZenroom.java View File

@@ -0,0 +1,41 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import decode.zenroom.Zenroom;

public class testZenroom {

static {
try {
System.loadLibrary("zenroom");
// System.out.printf("Loaded zenroom native library\n");
} catch(Throwable exc) {
System.out.printf("Could not load zenroom native library: %s\n", exc.getMessage());
}
}

public static String conf = "debug=1";
public static String keys = "";
public static String data = "";

public static void main(String[] args) {
String script = "";
script = new String (readAllBytesJava7(args[0]));
String result = (new Zenroom()).execute(script, conf, keys, data);
System.out.printf("testZenroom result: %s", result);
}

private static String readAllBytesJava7(String filePath) {
String content = "";

try {
content = new String (Files.readAllBytes(Paths.get(filePath)));
} catch (IOException e) {
e.printStackTrace();
}

return content;
}

}

+ 37
- 43
bindings/javascript/README.md View File

@@ -1,4 +1,5 @@
# Use Zenroom in JavaScript

<br>
<p align="center">
<a href="https://dev.zenroom.org/">
@@ -8,16 +9,10 @@
<br><br>
<table><tr><td>
<h2 align="center">
Zenroomjs 🧰
<br>Zenroomjs provides a javascript wrapper of <a href="https://zenroom.org">Zenroom</a>, a secure and small virtual machine for crypto language processing.
Zenroom js bindings 🧰
<br>Zenroom js bindings provides a javascript wrapper of <a href="https://zenroom.org">Zenroom</a>, a secure and small virtual machine for crypto language processing.
</h2>
<p align="center">
<a href="https://travis-ci.com/DECODEproject/zenroomjs">
<img src="https://travis-ci.com/DECODEproject/zenroomjs.svg?branch=master" alt="Build Status">
</a>
<a href="https://codecov.io/gh/DECODEproject/zenroomjs">
<img src="https://codecov.io/gh/DECODEproject/zenroomjs/branch/master/graph/badge.svg" />
</a>
<a href="https://badge.fury.io/js/zenroom">
<img alt="npm" src="https://img.shields.io/npm/v/zenroom.svg">
</a>
@@ -28,23 +23,24 @@ Zenroomjs 🧰

<br><br>


</td></tr></table>

## 💾 Install

```bash
yarn add zenroom
// or if you use npm
npm install zenroom
```

* * *

## 🎮 Usage
## 🎮 Usage

To start using the zenroom module just

```js
const zenroom = require('zenroom').default
const zenroom = require('zenroom')
```

or with ES6 syntax:
@@ -53,7 +49,7 @@ or with ES6 syntax:
import zenroom from 'zenroom'
```

the zenroomjs module is architectured as a [Revealing Module Pattern](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript) this also means that once you set some options it will remain till the object lifetime, unless you reset or overwrite them.
the zenroom javascript module is architectured as a [Revealing Module Pattern](https://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript) this also means that once you set some options it will remain till the object lifetime, unless you reset or overwrite them.

Another used paradigm is method chaining this means that you can chain the different methods, let's see some hello worldish example:

@@ -73,7 +69,6 @@ To initialize the options there are two ways, the one with the chaining that we
```js
// method chaining
zenroom.script('print("hello world")')
.verbosity(1)
.success(() => { console.log('everything goes smooth') })
.error(() => { console.error('something very bad happened') })
.zenroom_exec()
@@ -84,7 +79,6 @@ zenroom.script('print("hello world")')
// using the init() method
options = {
script: 'print("hello world")',
verbosity: 1,
success: () => { console.log('everything goes smooth') },
error: () => { console.error('something very bad happened') }
}
@@ -100,7 +94,7 @@ All the available options and method are covered in the next API section

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->

#### script
### script

Set the zenroom script to run

@@ -108,11 +102,11 @@ The syntax of the zenroom scripts are extensively available at
<https://zenroom.dyne.org/api/tutorials/Syntax.html>
You may want also to look at some example in a live executable environment here <https://zenroom.dyne.org/demo/>

##### Parameters
#### Parameters

- `script` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the lua script to be set

##### Examples
#### Examples

Example usage of `script()`

@@ -129,17 +123,17 @@ zenroom.script(script).zenroom_exec().reset()

Returns **zenroom** the zenroom module

#### keys
### keys

Set the keys JSON for you zenroom execution

the keys will be available in script as the `KEYS` variable

##### Parameters
#### Parameters

- `keys` **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the keys to be set as an object

##### Examples
#### Examples

Example usage of `keys()`

@@ -161,17 +155,17 @@ zenroom.script(script).keys(keys).zenroom_exec().reset()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### data
### data

Set the data for your zenroom execution

The data will be available in script as the `DATA` variable

##### Parameters
#### Parameters

- `data` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)**

##### Examples
#### Examples

Example usage of `data()`

@@ -193,17 +187,17 @@ zenroom.script(script).data(data).zenroom_exec()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### conf
### conf

Set the conf before your zenroom execution

all the available configuration are available [here](https://github.com/DECODEproject/Zenroom/blob/master/src/zen_config.c#L100-L106)

##### Parameters
#### Parameters

- `conf` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** the string of configuration to be set

##### Examples
#### Examples

Example usage of `conf()`

@@ -221,7 +215,7 @@ zenroom.script(script).conf(conf).zenroom_exec()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### print_err
### print_err

Set the print_err callback to customize
the behaviour of the print_err calls made to stderr
@@ -229,7 +223,7 @@ by default it prints to the console.error

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

##### Examples
#### Examples

Example usage of `print_err()`

@@ -248,7 +242,7 @@ zenroom.print_err(print_err_fn).script(script).zenroom_exec()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### print
### print

Set the print callback to customize
the behaviour of the print calls made to stdout
@@ -256,7 +250,7 @@ by default it prints to the console.log

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

##### Examples
#### Examples

Example usage of `print()`

@@ -275,13 +269,13 @@ zenroom.print(printFunction).script(script).zenroom_exec()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### success
### success

Set the success callback that is executed after a successful execution of zenroom

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

##### Examples
#### Examples

Example usage of `success()`

@@ -300,13 +294,13 @@ zenroom.script(script).success(()=>{

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### error
### error

Set the error callback that is executed after an unsuccessful execution of zenroom

Type: [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)

##### Examples
#### Examples

Example usage of `error()`

@@ -325,7 +319,7 @@ zenroom.script(script).error(()=>{

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### zenroom_exec
### zenroom_exec

Execute the zenroom vm (using the previously setted options)

@@ -333,7 +327,7 @@ It is usually the last method of the chain, but like the other methods returns
the zenroom module itself, so can be used for other calls if you need to make more
executions in a row

##### Examples
#### Examples

Example usage of `zenroom_exec()`

@@ -350,7 +344,7 @@ zenroom.script(script).zenroom_exec()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### zencode_exec
### zencode_exec

Execute zencode contracts (using the previously setted options)

@@ -358,7 +352,7 @@ It is usually the last method of the chain, but like the other methods returns
the zenroom module itself, so can be used for other calls if you need to make more
executions in a row

##### Examples
#### Examples

Example usage of `zencode_exec()`

@@ -375,7 +369,7 @@ zenroom.script(script).zencode_exec()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### init
### init

This method allows the configuration of your call by passing one
configuration option object. You can use the chain methods after this anyway.
@@ -394,11 +388,11 @@ The following options are available:
<li><strong>error</strong></li>
</ul>

##### Parameters
#### Parameters

- `options`

##### Examples
#### Examples

Example usage of `init()`

@@ -446,7 +440,7 @@ zenroom.init(encrypt_secret_to_many).zenroom_exec()

Returns **[object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** the zenroom module

#### reset
### reset

Reset the setted options already provided and cleans up the zenroom module

@@ -454,7 +448,7 @@ It is usually the last method of the chain, but like the other methods returns
the zenroom module itself, so can be used for other calls if you need to make more
executions in a row

##### Examples
#### Examples

Example usage of `reset()`



+ 0
- 0
bindings/javascript/dist/.empty View File


bindings/javascript/index.d.ts → bindings/javascript/dist/index.d.ts View File

@@ -3,7 +3,7 @@ declare module 'zenroom' {
export function success():any;
export function error():any;
export function print(text: string): any;
export function printErr(text:string): any;
export function print_err(text:string): any;
export function keys(keys: any): any
export function data(data: any): any
export function conf(conf: string): any;

+ 482
- 0
bindings/javascript/dist/wrapper.js View File

@@ -0,0 +1,482 @@
"use strict";

require("core-js/modules/es.symbol");

require("core-js/modules/es.symbol.description");

require("core-js/modules/es.symbol.iterator");

require("core-js/modules/es.array.iterator");

require("core-js/modules/es.object.assign");

require("core-js/modules/es.object.to-string");

require("core-js/modules/es.string.iterator");

require("core-js/modules/web.dom-collections.iterator");

require("core-js/stable");

var _zenroom = _interopRequireDefault(require("../dist/lib/zenroom.js"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

var C = (0, _zenroom.default)();
/* istanbul ignore next */

var zenroomExec = function zenroomExec(script) {
var conf = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var keys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var data = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
C.then(function (Module) {
Module.ccall('zenroom_exec', 'number', ['string', 'string', 'string', 'string'], [script, conf, keys, data]);
});
};
/* istanbul ignore next */


var zencodeExec = function zencodeExec(script) {
var conf = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
var keys = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
var data = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
C.then(function (Module) {
Module.ccall('zencode_exec', 'number', ['string', 'string', 'string', 'string'], [script, conf, keys, data]);
});
};

var stringify = function stringify(field) {
if (!field) {
return null;
}

try {
return JSON.stringify(JSON.parse(field));
} catch (e) {
if (_typeof(field) === 'object') {
return JSON.stringify(field);
}

if (typeof field === 'string') {
return field;
}
}
};

var zenroom = function () {
var self = {};
self.options = {};

var __debug = function __debug() {
return self;
};
/**
* Set the zenroom script to run
*
* The syntax of the zenroom scripts are extensively available at
* https://zenroom.dyne.org/api/tutorials/Syntax.html
* You may want also to look at some example in a live executable environment here https://zenroom.dyne.org/demo/
*
* @example <caption>Example usage of `script()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = 'print("hello")'
* zenroom.script(script).zenroom_exec().reset()
*
* @param {string} script the lua script to be set
* @returns {zenroom} the zenroom module
*/


var script = function script(_script) {
self.script = _script;
return this;
};
/**
* Set the keys JSON for you zenroom execution
*
* the keys will be available in script as the `KEYS` variable
*
* @example <caption>Example usage of `keys()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = `
* keys = JSON.decode(KEYS)
* print(keys)
* `
*
* const keys = {a: 1, b: 2}
* zenroom.script(script).keys(keys).zenroom_exec().reset()
*
* @param {object} keys the keys to be set as an object
* @returns {object} the zenroom module
*/


var keys = function keys(_keys) {
self.keys = stringify(_keys);
return this;
};
/**
* Set the data for your zenroom execution
*
* The data will be available in script as the `DATA` variable
*
* @example <caption>Example usage of `data()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = `
* data = JSON.decode(DATA)
* print(data)
* `
*
* const data = {a: 1, b: 2}
* zenroom.script(script).data(data).zenroom_exec()
*
* @param {string} data
* @returns {object} the zenroom module
*/


var data = function data(_data) {
self.data = stringify(_data);
return this;
};
/**
* Set the conf before your zenroom execution
*
* all the available configuration are available [here](https://github.com/DECODEproject/Zenroom/blob/master/src/zen_config.c#L100-L106)
*
* @example <caption>Example usage of `conf()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = 'print("hello")'
* const conf = 'debug=1,memwipe=0'
* zenroom.script(script).conf(conf).zenroom_exec()
*
* @param {string} conf the string of configuration to be set
* @returns {object} the zenroom module
*/


var conf = function conf(_conf) {
self.conf = _conf;
return this;
};
/**
* Set the print_err callback to customize
* the behaviour of the print_err calls made to stderr
* by default it prints to the console.error
*
* @example <caption>Example usage of `print_err()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const savedLines = []
* const print_err_fn = (text) => { savedLines.push(text) }
* const script = 'print("hello")'
* zenroom.print_err(print_err_fn).script(script).zenroom_exec()
*
* @callback print_err
* @returns {object} the zenroom module
*/


var print_err = function print_err(e) {
self.print_err = e;
C.then(function (Module) {
Module.print_err = function (text) {
return self.print_err(text);
};
});
return this;
};
/**
* Set the print callback to customize
* the behaviour of the print calls made to stdout
* by default it prints to the console.log
*
* @example <caption>Example usage of `print()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const savedLines = []
* const printFunction = (text) => { savedLines.push(text) }
* const script = 'print("hello")'
* zenroom.print(printFunction).script(script).zenroom_exec()
*
* @callback print
* @returns {object} the zenroom module
*/


var print = function print(printFunction) {
self.print = printFunction;
C.then(function (Module) {
Module.print = function (text) {
return self.print(text);
};
});
return this;
};
/**
* Set the success callback that is executed after a successful execution of zenroom
*
* @example <caption>Example usage of `success()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = 'print("hello")'
* zenroom.script(script).success(()=>{
* pleaseRunSomeOtherMethodAfter()
* }).zenroom_exec()
*
* @callback success
* @returns {object} the zenroom module
*/


var success = function success(successCallback) {
self.success = successCallback;
C.then(function (Module) {
Module.exec_ok = successCallback;
});
return this;
};
/**
* Set the error callback that is executed after an unsuccessful execution of zenroom
*
* @example <caption>Example usage of `error()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = 'print("hello")';
* zenroom.script(script).error(()=>{
* pleaseRunSomeOtherMethodAfterError()
* }).zenroom_exec()
*
* @callback error
* @returns {object} the zenroom module
*/


var error = function error(errorCallback) {
self.error = errorCallback;
C.then(function (Module) {
Module.exec_error = errorCallback;
});
return this;
};
/**
* Execute the zenroom vm (using the previously setted options)
*
* It is usually the last method of the chain, but like the other methods returns
* the zenroom module itself, so can be used for other calls if you need to make more
* executions in a row
*
* @example <caption>Example usage of `zenroom_exec()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = 'print("hello")';
* zenroom.script(script).zenroom_exec()
*
* @returns {object} the zenroom module
*/


var zenroom_exec = function zenroom_exec() {
zenroomExec(self.script, self.conf, self.keys, self.data);
return this;
};
/**
* Execute zencode contracts (using the previously setted options)
*
* It is usually the last method of the chain, but like the other methods returns
* the zenroom module itself, so can be used for other calls if you need to make more
* executions in a row
*
* @example <caption>Example usage of `zencode_exec()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const zencode = 'print("hello")';
* zenroom.script(script).zencode_exec()
*
* @returns {object} the zenroom module
*/


var zencode_exec = function zencode_exec() {
zencodeExec(self.script, self.conf, self.keys, self.data);
return this;
};
/**
* This method allows the configuration of your call by passing one
* configuration option object. You can use the chain methods after this anyway.
*
* If some attribute is already set, those will be overwritten by the new options
*
* The following options are available:
* <ul>
* <li><strong>script</strong></li>
* <li><strong>keys</strong></li>
* <li><strong>conf</strong></li>
* <li><strong>data</strong></li>
* <li><strong>print</strong></li>
* <li><strong>success</strong></li>
* <li><strong>error</strong></li>
* </ul>
*
* @example <caption>Example usage of `init()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const encrypt_secret_to_many = {
* script: `keyring = ECDH.new()
* secret = str(DATA)
* keys = JSON.decode(KEYS)
* keyring:private( base64(keys.keyring.secret) )
* res = {}
* for name,pubkey in pairs(keys.recipients) do
* pub = base64(pubkey)
* enc = ECDH.encrypt(keyring,pub,secret,keyring:public())
* res[name] = str( MSG.pack( map(enc,base64) ) ):base64()
* end
* print(JSON.encode(res))`,
*
* keys: {
* keyring : {
* public : "BHMjcDM/aljpi8pNxFQ436R6F3J+kaB/Xk1kAVFPmkoLVyeFltDZPgiIYRquh+m2IfvPioBfet7YCd5vVXYoRTk=",
* secret : "ChW5qi5y//ISDIHKx5Fvxl+XY8IyDGVBHUfELp3PqJQ="
* },
* recipients : {
* paulus : "BBUw6Nr3A30cN65maERvAk1cEv2Ji6Vs80kSlpodOC0SCtM8ucaS7e+s158uVMSr3BsvIXVspBeafiL8Qb3kcgc=",
* mayo : "BHqBoQ2WJ3/FGVNTXzdIc+K/HzNx05bWzEhn8m58FvSsaqWVdH52jI6fQWdkdjnbqVKCJGmbjA/OCJ+IKHbiySI=",
* mark : "BFgkjrRMvN+wkJ6qA4UvMaNlYBvl37C9cNYGkqOE4w43AUzkEzcyIIdE6BrgOEUEVefhOOnO6SCBQMgXHXJUUPY=",
* francesca : "BCo102mVybieKMyhex8tnVtFM5+Wo1oP02k8JVwKF9OLIjw7w0LmofItbuAcfWl9rcoe++XLI3sySZnqljIfeyU=",
* jim : "BEs1jeqL0nVwFi7OmG4YdtlWuKADyOvZR4XHpLAEswg8ONPXQHvwJ8+PkHkphoORfSjk2045bMdYkwboU4FdG2Y=",
* jaromil : "BBZYJtHvFg0vGCxPROAWrThcGZ+vFZJj86k+uncjvbm4DysIg7cWS3J6GrcJKCY55Uf40m2KfBwfaT+T7TTO1e8="
* }
* },
*
* data: 'This is a secret message.'
* }
*
*
* zenroom.init(encrypt_secret_to_many).zenroom_exec()
*
* @returns {object} the zenroom module
*/


var init = function init(options) {
/* istanbul ignore next */
self.options = Object.assign(self.options, options) || {};
script(self.options.script || '');
keys(self.options.keys || null);
conf(self.options.conf || null);
data(self.options.data || null);
print(self.options.print || function (text) {
return console.log(text);
});
print_err(self.options.print_err || function (text) {
return console.error(text);
});
success(self.options.success || new Function()); // eslint-disable-line no-new-func

error(self.options.error || new Function()); // eslint-disable-line no-new-func

return this;
};

var __setup = function __setup() {
print(self.print || function (text) {
return console.log(text);
});
print_err(self.print_err || function (text) {
return console.error(text);
});
success(self.success || function () {});
error(self.error || function () {});
};
/**
* Reset the setted options already provided and cleans up the zenroom module
*
* It is usually the last method of the chain, but like the other methods returns
* the zenroom module itself, so can be used for other calls if you need to make more
* executions in a row
*
* @example <caption>Example usage of `reset()`</caption>
* // returns zenroom
* import zenroom from 'zenroom'
* // or without ES6 syntax
* // const zenroom = require('zenroom')
*
* const script = 'print("hello")';
* zenroom.script(script)
* .zenroom_exec() // This runs the script
* .reset()
* .zenroom_exec() // This does not run the script anymore
*
* @returns {object} the zenroom module
*/


var reset = function reset() {
self = {};
self.options = {};

__setup();

return this;
};

__setup();

return {
script: script,
keys: keys,
conf: conf,
data: data,
print: print,
print_err: print_err,
success: success,
zenroom_exec: zenroom_exec,
zencode_exec: zencode_exec,
error: error,
init: init,
reset: reset,
__debug: __debug
};
}();

module.exports = zenroom;

+ 1
- 1
bindings/javascript/package.json View File

@@ -1,6 +1,6 @@
{
"name": "zenroom",
"version": "1.0.6",
"version": "1.1.0",
"description": "wrapper of Zenroom, a secure and small virtual machine for crypto language processing",
"main": "index.js",
"repository": "https://github.com/DECODEproject/Zenroom.git",


+ 91
- 92
bindings/javascript/src/wrapper.js View File

@@ -1,56 +1,55 @@
import 'core-js/stable'
import 'regenerator-runtime/runtime'
import 'core-js/stable';
import Zenroom from '../dist/lib/zenroom.js';

import Zenroom from '../dist/lib/zenroom.js'
const C = Zenroom()
const C = Zenroom();

/* istanbul ignore next */
const zenroomExec = (script, conf = null, keys = null, data = null) => {
C.then(Module => {
C.then((Module) => {
Module.ccall(
'zenroom_exec',
'number',
['string', 'string', 'string', 'string'],
[script, conf, keys, data]
)
})
}
[script, conf, keys, data],
);
});
};

/* istanbul ignore next */
const zencodeExec = (script, conf = null, keys = null, data = null) => {
C.then(Module => {
C.then((Module) => {
Module.ccall(
'zencode_exec',
'number',
['string', 'string', 'string', 'string'],
[script, conf, keys, data]
)
})
}
[script, conf, keys, data],
);
});
};

const stringify = (field) => {
if (!field) {
return null
return null;
}

try {
return JSON.stringify(JSON.parse(field))
return JSON.stringify(JSON.parse(field));
} catch (e) {
if (typeof field === 'object') {
return JSON.stringify(field)
} else if (typeof field === 'string') {
return field
return JSON.stringify(field);
} if (typeof field === 'string') {
return field;
}
}
}
};

const zenroom = (function () {
let self = {}
self.options = {}
let self = {};
self.options = {};

const __debug = function () {
return self
}
return self;
};

/**
* Set the zenroom script to run
@@ -72,9 +71,9 @@ const zenroom = (function () {
* @returns {zenroom} the zenroom module
*/
const script = function (script) {
self.script = script
return this
}
self.script = script;
return this;
};

/**
* Set the keys JSON for you zenroom execution
@@ -99,9 +98,9 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const keys = function (keys) {
self.keys = stringify(keys)
return this
}
self.keys = stringify(keys);
return this;
};

/**
* Set the data for your zenroom execution
@@ -126,9 +125,9 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const data = function (data) {
self.data = stringify(data)
return this
}
self.data = stringify(data);
return this;
};

/**
* Set the conf before your zenroom execution
@@ -149,9 +148,9 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const conf = function (conf) {
self.conf = conf
return this
}
self.conf = conf;
return this;
};

/**
* Set the print_err callback to customize
@@ -173,13 +172,13 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const print_err = function (e) {
self.print_err = e
C.then(Module => {
Module.print_err = text => self.print_err(text)
})
self.print_err = e;
C.then((Module) => {
Module.print_err = (text) => self.print_err(text);
});

return this
}
return this;
};

/**
* Set the print callback to customize
@@ -201,13 +200,13 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const print = function (printFunction) {
self.print = printFunction
C.then(Module => {
Module.print = text => self.print(text)
})
self.print = printFunction;
C.then((Module) => {
Module.print = (text) => self.print(text);
});

return this
}
return this;
};

/**
* Set the success callback that is executed after a successful execution of zenroom
@@ -227,12 +226,12 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const success = function (successCallback) {
self.success = successCallback
C.then(Module => {
Module.exec_ok = successCallback
})
return this
}
self.success = successCallback;
C.then((Module) => {
Module.exec_ok = successCallback;
});
return this;
};

/**
* Set the error callback that is executed after an unsuccessful execution of zenroom
@@ -252,12 +251,12 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const error = function (errorCallback) {
self.error = errorCallback
C.then(Module => {
Module.exec_error = errorCallback
})
return this
}
self.error = errorCallback;
C.then((Module) => {
Module.exec_error = errorCallback;
});
return this;
};

/**
* Execute the zenroom vm (using the previously setted options)
@@ -278,9 +277,9 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const zenroom_exec = function () {
zenroomExec(self.script, self.conf, self.keys, self.data)
return this
}
zenroomExec(self.script, self.conf, self.keys, self.data);
return this;
};

/**
* Execute zencode contracts (using the previously setted options)
@@ -301,9 +300,9 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const zencode_exec = function () {
zencodeExec(self.script, self.conf, self.keys, self.data)
return this
}
zencodeExec(self.script, self.conf, self.keys, self.data);
return this;
};

/**
* This method allows the configuration of your call by passing one
@@ -366,26 +365,26 @@ const zenroom = (function () {
*/
const init = function (options) {
/* istanbul ignore next */
self.options = Object.assign(self.options, options) || {}
self.options = Object.assign(self.options, options) || {};

script(self.options.script || '')
keys(self.options.keys || null)
conf(self.options.conf || null)
data(self.options.data || null)
print(self.options.print || (text => console.log(text)))
print_err(self.options.print_err || (text => console.error(text)))
success(self.options.success || new Function()) // eslint-disable-line no-new-func
error(self.options.error || new Function()) // eslint-disable-line no-new-func
script(self.options.script || '');
keys(self.options.keys || null);
conf(self.options.conf || null);
data(self.options.data || null);
print(self.options.print || ((text) => console.log(text)));
print_err(self.options.print_err || ((text) => console.error(text)));
success(self.options.success || new Function()); // eslint-disable-line no-new-func
error(self.options.error || new Function()); // eslint-disable-line no-new-func

return this
}
return this;
};

const __setup = function () {
print(self.print || (text => console.log(text)))
print_err(self.print_err || (text => console.error(text)))
success(self.success || (() => {}))
error(self.error || (() => {}))
}
print(self.print || ((text) => console.log(text)));
print_err(self.print_err || ((text) => console.error(text)));
success(self.success || (() => {}));
error(self.error || (() => {}));
};

/**
* Reset the setted options already provided and cleans up the zenroom module
@@ -409,13 +408,13 @@ const zenroom = (function () {
* @returns {object} the zenroom module
*/
const reset = function () {
self = {}
self.options = {}
__setup()
return this
}
self = {};
self.options = {};
__setup();
return this;
};

__setup()
__setup();

return {
script,
@@ -430,8 +429,8 @@ const zenroom = (function () {
error,
init,
reset,
__debug
}
})()
__debug,
};
}());

module.exports = zenroom
module.exports = zenroom;

+ 1
- 1
bindings/javascript/test/test.js View File

@@ -1,6 +1,6 @@
import { expect } from 'chai'
import sinon from 'sinon'
import zenroom from '../src/wrapper'
import zenroom from '../dist/wrapper'
const capcon = require('capture-console')
var assert = require('assert')



+ 3019
- 4157
bindings/javascript/yarn.lock
File diff suppressed because it is too large
View File


+ 58
- 0
build/build-java.sh View File

@@ -0,0 +1,58 @@
#!/usr/bin/env bash

if [ -z "${JAVA_HOME}" ]; then
echo "JAVA_HOME environment variable is not set. Setting JAVA_HOME to unlinked path from `which javac`"
export JAVA_HOME=${JAVA_HOME:=`dirname $(dirname $(readlink -f $(which javac)))`}
fi

echo "JAVA_HOME = ${JAVA_HOME}"

LIB_SRC_PATH=${LIB_SRC_PATH:=${PWD}/src}
LIB_DST_PATH=${LIB_DST_PATH:=${PWD}/build/target/java/jniLibs}
LOCAL_ARCH=$(uname -m)

build () {

echo "${0}: Building Java ${2} JNI lib ..."
make clean
make java-$1
mkdir -p ${LIB_DST_PATH}/${3}
cp -v ${LIB_SRC_PATH}/zenroom.so ${LIB_DST_PATH}/${3}/libzenroom.so
}


build "${LOCAL_ARCH}" "${LOCAL_ARCH}-linux-java" "${LOCAL_ARCH}"
# build "arm" "arm-linux-androideabi" "armeabi-v7a"
# build "aarch64" "aarch64-linux-android" "arm64-v8a"

# BEGIN test
ZEN_JAVA_LIB_PATH=${LIB_DST_PATH}/${LOCAL_ARCH}
ZEN_JAVA_CLASSPATH="bindings/java"
ZENCODE_SCRIPT="bindings/java/alice_keygen.zen"
ZENCODE_TEST_CLASS="testZenroom"

echo "${0}: Compiling test ${ZENCODE_TEST_CLASS}.java ..."
# build test java source (hack)
cd bindings/java/decode/zenroom/
javac Zenroom.java
cd -

cd bindings/java
javac ${ZENCODE_TEST_CLASS}.java
cd -
# create test data
echo "${0}: Creating test zencode ${ZENCODE_SCRIPT}..."
cat << EOF | tee ${ZENCODE_SCRIPT}
rule check version 1.0.0
Scenario 'simple': Create the keypair
Given that I am known as 'Alice'
When I create the keypair
Then print my data
EOF
# run test
Z="java -classpath ${ZEN_JAVA_CLASSPATH} -Djava.library.path=${ZEN_JAVA_LIB_PATH} ${ZENCODE_TEST_CLASS} ${ZENCODE_SCRIPT}"
echo "${0}: Invoking java ${ZENCODE_TEST_CLASS}: ${Z} "
${Z}
# END test

echo "Java JNI libs built under ${LIB_DST_PATH}"

+ 8
- 0
build/config.mk View File

@@ -136,6 +136,14 @@ endif
# -DCMAKE_ANDROID_NDK=${sysroot}
#milagro_cmake_flags += -DCMAKE_ANDROID_STANDALONE_TOOLCHAIN=${ndk} -DCMAKE_SYSTEM_VERSION=26

ifneq (,$(findstring java,$(MAKECMDGOALS)))
jdk = ${JAVA_HOME}
ldflags += -shared
cflags += -fPIC ${cflags_protection} -DLIBRARY -D'ARCH=\"LINUX\"' -DARCH_LINUX
cflags += -DLUA_USE_DLOPEN -I${jdk}/include -I${jdk}/include/linux
system := Java
endif

ifneq (,$(findstring android,$(MAKECMDGOALS)))
ndk = ${NDK_HOME}
toolchain = ${ndk}/toolchains/llvm/prebuilt/linux-x86_64


+ 1
- 1
build/linux.mk View File

@@ -29,7 +29,7 @@ linux: apply-patches milagro lua53 embed-lua
make -C src linux
@cp -v src/zenroom build/zenroom

android-arm android-x86 android-aarch64: apply-patches milagro lua53 embed-lua
android-arm android-x86 android-aarch64 java-x86_64: apply-patches milagro lua53 embed-lua
CC=${gcc} CFLAGS="${cflags}" LDFLAGS="${ldflags}" LDADD="${ldadd}" \
LD="${ld}" RANLIB="${ranlib}" AR="${ar}" \
make -C src $@


+ 1
- 1
src/Makefile View File

@@ -99,7 +99,7 @@ ios-lib: CFLAGS += -D LIBRARY
ios-lib: ${SOURCES}
libtool -static -o zenroom-ios-${TARGET}.a ${SOURCES} $(shell find ../lib -name \*.a) ${LDADD}

android-arm android-x86 android-aarch64: ${SOURCES} zenroom_jni.o
android-arm android-x86 android-aarch64 java-x86_64 java-x86 java-arm java-aarch64: ${SOURCES} zenroom_jni.o
${CC} ${CFLAGS} ${SOURCES} zenroom_jni.o -o zenroom.so ${LDFLAGS} ${LDADD}

arm: ${SOURCES}


+ 82
- 0
test/zencode_given/run.sh View File

@@ -0,0 +1,82 @@
#!/usr/bin/env bash

# https://pad.dyne.org/code/#/2/code/edit/NTsTFsGUExxvnycVzM32AJvZ/

####################
# common script init
if ! test -r ../utils.sh; then
echo "run executable from its own directory: $0"; exit 1; fi
. ../utils.sh
Z="`detect_zenroom_path` `detect_zenroom_conf`"
####################

tmp=`mktemp`

cat <<EOF | tee nothing.zen | $Z -z
rule check version 1.0.0
Given nothing
When I create the random object of '256' bits
Then print the 'random object'
EOF

set +e
echo '{}' > $tmp
cat <<EOF | tee nothing.zen | $Z -z -a $tmp 2>/dev/null
rule check version 1.0.0
Given nothing
When I create the random object of '256' bits
Then print the 'random object'
EOF
if ! test $? == 1; then
echo "ERROR in Given nothing"
exit 1; fi
set -e

cat <<EOF | tee nothing.zen | $Z -z
rule check version 1.0.0
Given nothing
When I create the random object of '256' bits
Then print the 'random object'
EOF

echo '{ "anykey": "anyvalue" }' > $tmp
cat <<EOF | tee have.zen | $Z -z -a $tmp
rule check version 1.0.0
rule input encoding string
rule output encoding string
Given I have a 'anykey'
Then print the 'anykey'
EOF
echo '{ "anykey": "616e7976616c7565" }' > $tmp
cat <<EOF | tee have.zen | $Z -z -a $tmp
rule check version 1.0.0
Given I have a 'anykey' as 'hex'
Then print the 'anykey' as 'string'
EOF

cat <<EOF > $tmp
{
"Andrea":{
"keypair":{
"private_key":"IIiTD89L6_sbIvaUc5jAVR88ySigaBXppS5GLUjm7Dv2OLKbNIVdiZ48jpLGskKVDPpukKe4R0A",
"public_key":"BFKQTA1ZiebF0is_LtMcVgu4QXC-HOjMpCwDPLuvuXGVAgORIn5NUm7Ey7UDljeNrTCZvhEqxCPjSvWLtIuSYXeZcHWENp7oO37nv7hL2Qj1vMwwlpeRhnSZnjhnKYjq5aTQV1T-eH3e0UcJASzvnb8"
},
"robba":"1000",
"Arr":[
"peppe2",
"peppe3",
"peppe4"
]
}
}
EOF
cat <<EOF | tee have_valid.zen | $Z -z -a $tmp
rule check version 1.0.0
scenario 'simple'
Given I am 'Andrea'
and I have my 'keypair'
and debug
Then print the 'keypair'
EOF

rm -f $tmp

Loading…
Cancel
Save