dyne / Zenroom

Embedded no-code VM executing human-like language to manipulate data and process cryptographic operations.
https://dev.zenroom.org
GNU Affero General Public License v3.0
195 stars 62 forks source link

Keep updated documentation on PyPi #775

Closed andrea-dintino closed 10 months ago

andrea-dintino commented 10 months ago

This 2:

https://pypi.org/project/zenroom/

https://dev.zenroom.org/#/pages/python

are badly outdated

puria commented 10 months ago

They are in sync with https://github.com/dyne/Zenroom/blob/master/docs/pages/python.md just checked are 4mnths old... seems maintained no? Anyway you can contribute correction directly there, the edits are automatically published on pipy and dev.zenroom.org

andrea-dintino commented 10 months ago

Also please use this as example for Python:

from zenroom import zenroom

keys = '{ "participant": { "keyring": { "ethereum": "6b4f32fc48ff19f0c184f1b7c593fbe26633421798191931c210a3a9bb46ae22" } } }'

data = '{ "myString": "I love the Beatles, all but 3", "participant ethereum address": "0x2B8070975AF995Ef7eb949AE28ee7706B9039504" }'

contract = """Scenario ethereum: sign ethereum message

# Here we are loading the private key and the message to be signed
Given I am 'participant'
Given I have my 'keyring'
Given I have a 'string' named 'myString'
Given I have a 'ethereum address' named 'participant ethereum address'

# Here we are creating the signature according to EIP712
When I create the ethereum signature of 'myString'
When I rename the 'ethereum signature' to 'myString.ethereum-signature'

# Here we copy the signature, which we'll print in a different format
When I copy 'myString.ethereum-signature' to 'myString.ethereum-signature.rsv'

# Here we print the signature in the regular 65 bytes long 'signaure hash' format
When I create ethereum address from ethereum signature 'myString.ethereum-signature' of 'myString'
When I copy 'ethereum address' to 'newEthereumAddress'

If I verify 'newEthereumAddress' is equal to 'participant ethereum address'
Then print string 'all good, the recovered ethereum address matches the original one'
Endif

Then print the 'myString.ethereum-signature'
Then print the 'newEthereumAddress'

# Here we print the copy of the signature in the [r,s,v], simply printing it as 'hex'
Then print the 'myString.ethereum-signature.rsv' as 'hex'
"""

result = zenroom.zencode_exec(contract, "", keys, data)
print(result.output)
pedro-nonfree commented 10 months ago

Example usage of zencode_exec(script, keys=None, data=None, conf=None)

src https://pypi.org/project/zenroom/

Remember that it changed the invocation order

-zencode_exec(script, keys=None, data=None, conf=None)
+zencode_exec(script, conf=None, keys=None, data=None)

Also, that as you said in the chat, the second example would go out

We have been removing support for Lua direct execution, for security reasons

I suggest small modifications on the proposed example from the previous reply.

  1. using the built-in json library, it is easier to copypaste the keys and data to and from apiroom.net (which improves Developer eXperience)
  2. also exposing conf when it is void, so it is easier to read and understand usage

In fact, it might be better to accept "python objects" as arguments, because looks like a "better integration with python"

from zenroom import zenroom
import json

conf = {}

keys = {
    "participant": {
        "keyring": {
            "ethereum": "6b4f32fc48ff19f0c184f1b7c593fbe26633421798191931c210a3a9bb46ae22"
        }
    }
}

data = {
    "myString": "I love the Beatles, all but 3",
    "participant ethereum address": "0x2B8070975AF995Ef7eb949AE28ee7706B9039504"
}

contract = """Scenario ethereum: sign ethereum message

# Here we are loading the private key and the message to be signed
Given I am 'participant'
Given I have my 'keyring'
Given I have a 'string' named 'myString'
Given I have a 'ethereum address' named 'participant ethereum address'

# Here we are creating the signature according to EIP712
When I create the ethereum signature of 'myString'
When I rename the 'ethereum signature' to 'myString.ethereum-signature'

# Here we copy the signature, which we'll print in a different format
When I copy 'myString.ethereum-signature' to 'myString.ethereum-signature.rsv'

# Here we print the signature in the regular 65 bytes long 'signaure hash' format
When I create ethereum address from ethereum signature 'myString.ethereum-signature' of 'myString'
When I copy 'ethereum address' to 'newEthereumAddress'

If I verify 'newEthereumAddress' is equal to 'participant ethereum address'
Then print string 'all good, the recovered ethereum address matches the original one'
Endif

Then print the 'myString.ethereum-signature'
Then print the 'newEthereumAddress'

# Here we print the copy of the signature in the [r,s,v], simply printing it as 'hex'
Then print the 'myString.ethereum-signature.rsv' as 'hex'
"""

result = zenroom.zencode_exec(contract, json.dumps(conf), json.dumps(keys), json.dumps(data))
print(result.output)
puria commented 10 months ago
from zenroom import zenroom
import json

conf = {}

Please pay attention that the conf is a string in this https://dev.zenroom.org/#/pages/how-to-embed?id=configuration-directives format "debug=3,logfmt=json"

pedro-nonfree commented 10 months ago

@puria I blindly supposed that the conf would be similar to keys and data

https://dev.zenroom.org/#/pages/zenroom-config

You can pass Zenroom several attributes, wrapped in quotes and separated by a comma

then it doesnot make sense to use conf as python-object-json

using it this way, it works because later it says:

conf = ""
# (...)
result = zenroom.zencode_exec(contract, conf, json.dumps(keys), json.dumps(data))

In https://www.npmjs.com/package/zenroom , they also recommend doing the json thing; another way would be to explain it somehow the same to have more coherence

puria commented 10 months ago

Yes, both on cli, js, and python the calls are binded to the same primitives that is zencode_exec and that actually accepts just strings.

So the data and keys are escaped json (means JSON.stringify in js world and json.dumps in python). But the conf has that one special syntax: key=value,key2=value that's why I pointed out in the provious comment, if you put a dict in there will not gonna work ;p

puria commented 10 months ago

Ah I understand...! You thought I wrote that code, but it's actually a quote. Unfortunately the M$ github interface does not help so much! There is a small gray line on the left ;p

puria commented 10 months ago

Fixed by #776