dyne / slangroom-chain

Execute chain of zencode smart contracts
https://dyne.org/slangroom-chain/
GNU Affero General Public License v3.0
0 stars 0 forks source link

Add utility chainParse #7

Open matteo-cristino opened 3 weeks ago

matteo-cristino commented 3 weeks ago

If reading slangroom-chain from file we end can end up with a string like:

{
    "steps": [
        {
            "id": "first",
            "zencode": "Given nothing\nWhen I write the string 'hello' in 'say hi'\nThen print 'say_hi'",
            "onAfter": (result, zencode) => {
                const r = JSON.parse(result);
                r['say_hi'] = 'hello from onAfter';
                result = JSON.stringify(r);
                return;
            }
        },
        {
            "id": "second",
            "dataFromStep": "first",
            "zencode": "Given I have a 'string' named 'say_hi'\nThen print the data"
        }
    ]
}

and using JSON.parse() on it, it will obviously brake with error:

SyntaxError: Unexpected token '(', ..."onAfter": (result, z"... is not valid JSON

It could be usefull to have an utility called like chainParse that parse the JSON using an eval on the functions, but checking that the functions take only in input the required input and nothing more. To be thougth carefully since eval can expose to security risks.

puria commented 2 weeks ago

I suggest that we can use yaml for chains, so you can have newlines and contracts more readable when inside the chain. Plus you can have the onAfter not breaking inside a yaml escaped string field