Skip to content

Commit

Permalink
feat: allow missing conf and extra in zencode chainz (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
puria authored Feb 26, 2024
1 parent af3a67f commit a5d7dd1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
29 changes: 26 additions & 3 deletions pkg/zencode/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
import { Plugin } from '@slangroom/core';
import { Plugin } from '@slangroom/core';
import type { JsonableObject } from '@slangroom/shared';
import { zencodeExec } from '@slangroom/shared';

const p = new Plugin();

const exec = async (script: string, data: JsonableObject, keys: JsonableObject, extra: JsonableObject, conf: string = '') => {
if (!script) throw new Error('script is required');
if (!data) throw new Error('data is required');
if (!keys) throw new Error('keys is required');

return await zencodeExec(script, { data, keys, extra, conf });
}

/**
* @internal
*/
export const zencodeExecPlugin = p.new(['script', 'data', 'keys' ], '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 = {} as JsonableObject;
const conf = "";

const zout = await exec(script, data, keys, extra, conf);

return ctx.pass(zout.result)
});

/**
* @internal
*/
export const zencodeExecPlugin = p.new(['script', 'data', 'keys', 'extra', 'conf'], 'execute zencode', async (ctx) => {
export const zencodeExecFullPlugin = 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 });
const zout = await exec(script, data, keys, extra, conf);

return ctx.pass(zout.result)
});
Expand Down
30 changes: 30 additions & 0 deletions pkg/zencode/test/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,36 @@ import { zencode } from '@slangroom/zencode';
import type { JsonableObject } from '@slangroom/shared';


test('run zencode without conf and extra', async (t) => {
const scriptCreate = `
Rule unknown ignore
Given I send keys 'neo_keys' and send script 'neo_script' and send data 'neo_data' 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_data: {},
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="
});

});

test('run zencode', async (t) => {
const scriptCreate = `
Rule unknown ignore
Expand Down

0 comments on commit a5d7dd1

Please sign in to comment.