From 58480f82350b56b9278bca5bde74712546df3a3c Mon Sep 17 00:00:00 2001 From: Alberto Lerda Date: Mon, 22 Jan 2024 10:40:07 +0100 Subject: [PATCH] feat: plugin to run zencode --- pkg/core/src/visitor.ts | 2 +- pkg/zencode/.npmignore | 1 + pkg/zencode/package.json | 30 ++++++++++++++++++++++++++++++ pkg/zencode/src/index.ts | 1 + pkg/zencode/src/plugin.ts | 24 ++++++++++++++++++++++++ pkg/zencode/test/e2e.ts | 37 +++++++++++++++++++++++++++++++++++++ pkg/zencode/tsconfig.json | 1 + pnpm-lock.yaml | 9 +++++++++ 8 files changed, 104 insertions(+), 1 deletion(-) create mode 120000 pkg/zencode/.npmignore create mode 100644 pkg/zencode/package.json create mode 100644 pkg/zencode/src/index.ts create mode 100644 pkg/zencode/src/plugin.ts create mode 100644 pkg/zencode/test/e2e.ts create mode 120000 pkg/zencode/tsconfig.json diff --git a/pkg/core/src/visitor.ts b/pkg/core/src/visitor.ts index 20f2375f..bb4d7518 100644 --- a/pkg/core/src/visitor.ts +++ b/pkg/core/src/visitor.ts @@ -146,7 +146,7 @@ const getDatakeys = (params: ZenParams, rhs: string): undefined | Jsonable => */ const fetchDatakeys = (params: ZenParams, rhs: string): Jsonable => { const val = getDatakeys(params, rhs); - if (!val) throw new Error('cannot be undefined'); + if (val === undefined) throw new Error('cannot be undefined'); return val; }; diff --git a/pkg/zencode/.npmignore b/pkg/zencode/.npmignore new file mode 120000 index 00000000..b4359f69 --- /dev/null +++ b/pkg/zencode/.npmignore @@ -0,0 +1 @@ +../../.npmignore \ No newline at end of file diff --git a/pkg/zencode/package.json b/pkg/zencode/package.json new file mode 100644 index 00000000..47029073 --- /dev/null +++ b/pkg/zencode/package.json @@ -0,0 +1,30 @@ +{ + "name": "@slangroom/zencode", + "version": "1.12.0", + "dependencies": { + "@slangroom/core": "workspace:*", + "@slangroom/shared": "workspace:*" + }, + "repository": "https://github.com/dyne/slangroom", + "license": "AGPL-3.0-only", + "type": "module", + "main": "./build/esm/src/index.js", + "types": "./build/esm/src/index.d.ts", + "exports": { + ".": { + "import": { + "types": "./build/esm/src/index.d.ts", + "default": "./build/esm/src/index.js" + } + }, + "./*": { + "import": { + "types": "./build/esm/src/*.d.ts", + "default": "./build/esm/src/*.js" + } + } + }, + "publishConfig": { + "access": "public" + } +} diff --git a/pkg/zencode/src/index.ts b/pkg/zencode/src/index.ts new file mode 100644 index 00000000..7cb509e7 --- /dev/null +++ b/pkg/zencode/src/index.ts @@ -0,0 +1 @@ +export * from '@slangroom/zencode/plugin'; diff --git a/pkg/zencode/src/plugin.ts b/pkg/zencode/src/plugin.ts new file mode 100644 index 00000000..d6d5c065 --- /dev/null +++ b/pkg/zencode/src/plugin.ts @@ -0,0 +1,24 @@ +import { Plugin } from '@slangroom/core'; +import type { JsonableObject } from '@slangroom/shared'; +import { zencodeExec } from '@slangroom/shared'; + +const p = new Plugin(); + +/** + * @internal + */ +export const zencodeExecPlugin = p.new(['script', 'data', 'keys', 'extra', 'conf'], 'execute zencode', async (ctx) => { + const script = ctx.fetch('script') as string; + const data = (ctx.get('data') || {}) as JsonableObject; + const keys = (ctx.get('keys') || {}) as JsonableObject; + const extra = (ctx.get('extra') || {}) as JsonableObject; + const conf = (ctx.get('conf') || "") as string; + + const zout = await zencodeExec(script, { data, keys, extra, conf }); + + return ctx.pass(zout.result) +}); + + + +export const zencode = p; diff --git a/pkg/zencode/test/e2e.ts b/pkg/zencode/test/e2e.ts new file mode 100644 index 00000000..9847c018 --- /dev/null +++ b/pkg/zencode/test/e2e.ts @@ -0,0 +1,37 @@ +import test from 'ava'; +import { Slangroom } from '@slangroom/core'; +import { zencode } from '@slangroom/zencode'; +import type { JsonableObject } from '@slangroom/shared'; + + +test('run zencode', async (t) => { + const scriptCreate = ` +Rule unknown ignore +Given I send keys 'neo_keys' and send script 'neo_script' and send data 'neo_data' and send conf 'neo_conf' and send extra 'neo_extra' and execute zencode and output into 'ecdh_public_key' +Given I have a 'string dictionary' named 'ecdh_public_key' +Then print data +`; + const slangroom = new Slangroom(zencode); + const res = await slangroom.execute(scriptCreate, { + keys: { + neo_keys: { + keyring: { + ecdh: "FJ5Esc1koLSH+9pKSdI65tcyH2HowzXMe0UdsqktmZU=", + } + }, + neo_conf: "", + neo_data: {}, + neo_extra: {}, + neo_script: ` + Scenario 'ecdh': Create the public key + Given I have the 'keyring' + When I create the ecdh public key + Then print the 'ecdh public key' + ` + }, + }); + t.deepEqual((res.result['ecdh_public_key'] as JsonableObject), { + ecdh_public_key: "BLOYXryyAI7rPuyNbI0/1CfLFd7H/NbX+osqyQHjPR9BPK1lYSPOixZQWvFK+rkkJ+aJbYp6kii2Y3+fZ5vl2MA=" + }); + +}); diff --git a/pkg/zencode/tsconfig.json b/pkg/zencode/tsconfig.json new file mode 120000 index 00000000..fd0e4743 --- /dev/null +++ b/pkg/zencode/tsconfig.json @@ -0,0 +1 @@ +../../tsconfig.json \ No newline at end of file diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index a1b46436..1da4a087 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -224,6 +224,15 @@ importers: specifier: ^5.1.3 version: 5.1.3 + pkg/zencode: + dependencies: + '@slangroom/core': + specifier: workspace:* + version: link:../core + '@slangroom/shared': + specifier: workspace:* + version: link:../shared + packages: /@aashutoshrathi/word-wrap@1.2.6: