Final zenroom (#147)

* zenroom fixes

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* expl. defined the aiohttp package

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* increased version number and fixed a zenroom runtime bug

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* added fialing zenroom tx signing test

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* extended test to pass zenrooom validation, but to fail planetmint validation.

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* added manual tx crafting

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* added zenroom fulfillment verification

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* the last mile before integration

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* zenroom unit tests are passing

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* simplified zenroom unit tests

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* removed obsolte lines from the zenroom tests

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* fixed acceptance tests

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* adjusted zenroom integraiton tests

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* fixed linting errors

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* simplified zenroom unit test

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* increased version number

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* using cryptoconditions without print message

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* increased cc usage to 0.9.9 readded daemon proceses

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* increased version to 0.9.6

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>

* fixed deployment issue for 0.9.6

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel
2022-06-17 11:04:12 +02:00
committed by GitHub
parent e87f4c08e4
commit 5bd3784dbf
16 changed files with 1128 additions and 779 deletions

View File

@@ -5,36 +5,40 @@
import pytest
GENERATE_KEYPAIR = \
"""Rule input encoding base58
Rule output encoding base58
Scenario 'ecdh': Create the keypair
Given that I am known as 'Pippo'
When I create the ecdh key
When I create the testnet key
Then print data"""
# secret key to public key
SK_TO_PK = \
"""Rule input encoding base58
Rule output encoding base58
Scenario 'ecdh': Create the keypair
Given that I am known as '{}'
Given I have the 'keys'
When I create the ecdh public key
When I create the testnet address
Then print my 'ecdh public key'
Then print my 'testnet address'"""
CONDITION_SCRIPT = """
Scenario 'ecdh': create the signature of an object
Given I have the 'keyring'
Given that I have a 'string dictionary' named 'houses' inside 'asset'
When I create the signature of 'houses'
Then print the 'signature'"""
FULFILL_SCRIPT = \
"""Rule input encoding base58
Rule output encoding base58
Scenario 'ecdh': Bob verifies the signature from Alice
"""Scenario 'ecdh': Bob verifies the signature from Alice
Given I have a 'ecdh public key' from 'Alice'
Given that I have a 'string dictionary' named 'houses' inside 'asset'
Given I have a 'signature' named 'data.signature' inside 'result'
When I verify the 'houses' has a signature in 'data.signature' by 'Alice'
Given I have a 'signature' named 'signature' inside 'result'
When I verify the 'houses' has a signature in 'signature' by 'Alice'
Then print the string 'ok'"""
SK_TO_PK = \
"""Scenario 'ecdh': Create the keypair
Given that I am known as '{}'
Given I have the 'keyring'
When I create the ecdh public key
When I create the bitcoin address
Then print my 'ecdh public key'
Then print my 'bitcoin address'"""
GENERATE_KEYPAIR = \
"""Scenario 'ecdh': Create the keypair
Given that I am known as 'Pippo'
When I create the ecdh key
When I create the bitcoin key
Then print data"""
ZENROOM_DATA = {
'also': 'more data'
}
HOUSE_ASSETS = {
"data": {
@@ -51,19 +55,11 @@ HOUSE_ASSETS = {
}
}
ZENROOM_DATA = {
'also': 'more data'
metadata = {
'units': 300,
'type': 'KG'
}
CONDITION_SCRIPT = """Rule input encoding base58
Rule output encoding base58
Scenario 'ecdh': create the signature of an object
Given I have the 'keys'
Given that I have a 'string dictionary' named 'houses' inside 'asset'
When I create the signature of 'houses'
When I rename the 'signature' to 'data.signature'
Then print the 'data.signature'"""
@pytest.fixture
def gen_key_zencode():
return GENERATE_KEYPAIR
@@ -86,4 +82,4 @@ def zenroom_house_assets():
@pytest.fixture
def zenroom_data():
return ZENROOM_DATA
return ZENROOM_DATA

View File

@@ -1,68 +1,73 @@
# GOAL:
# In this script I tried to implement the ECDSA signature using zenroom
# However, the scripts are customizable and so with the same procedure
# we can implement more complex smart contracts
# PUBLIC IDENTITY
# The public identity of the users in this script (Bob and Alice)
# is the pair (ECDH public key, Testnet address)
import os
import json
import base58
from hashlib import sha3_256
from cryptoconditions.types.ed25519 import Ed25519Sha256
from cryptoconditions.types.zenroom import ZenroomSha256
from zenroom import zencode_exec
from planetmint_driver import Planetmint
from planetmint_driver.crypto import generate_keypair
import hashlib
from cryptoconditions import ZenroomSha256
from json.decoder import JSONDecodeError
def test_zenroom(gen_key_zencode, secret_key_to_private_key_zencode, fulfill_script_zencode,
condition_script_zencode, zenroom_data, zenroom_house_assets):
alice = json.loads(ZenroomSha256.run_zenroom(gen_key_zencode).output)['keys']
bob = json.loads(ZenroomSha256.run_zenroom(gen_key_zencode).output)['keys']
zen_public_keys = json.loads(ZenroomSha256.run_zenroom(secret_key_to_private_key_zencode.format('Alice'),
keys={'keys': alice}).output)
zen_public_keys.update(json.loads(ZenroomSha256.run_zenroom(secret_key_to_private_key_zencode.format('Bob'),
keys={'keys': bob}).output))
# CRYPTO-CONDITIONS: instantiate an Ed25519 crypto-condition for buyer
zenSha = ZenroomSha256(script=fulfill_script_zencode, keys=zen_public_keys, data=zenroom_data)
def test_zenroom_signing(gen_key_zencode, secret_key_to_private_key_zencode,
fulfill_script_zencode, zenroom_data, zenroom_house_assets,
condition_script_zencode):
biolabs = generate_keypair()
version = '2.0'
alice = json.loads(zencode_exec(gen_key_zencode).output)['keyring']
bob = json.loads(zencode_exec(gen_key_zencode).output)['keyring']
zen_public_keys = json.loads(zencode_exec(secret_key_to_private_key_zencode.format('Alice'),
keys=json.dumps({'keyring': alice})).output)
zen_public_keys.update(json.loads(zencode_exec(secret_key_to_private_key_zencode.format('Bob'),
keys=json.dumps({'keyring': bob})).output))
zenroomscpt = ZenroomSha256(script=fulfill_script_zencode, data=zenroom_data, keys=zen_public_keys)
print(F'zenroom is: {zenroomscpt.script}')
# CRYPTO-CONDITIONS: generate the condition uri
condition_uri = zenSha.condition.serialize_uri()
condition_uri_zen = zenroomscpt.condition.serialize_uri()
print(F'\nzenroom condition URI: {condition_uri_zen}')
# CRYPTO-CONDITIONS: construct an unsigned fulfillment dictionary
unsigned_fulfillment_dict = {
'type': zenSha.TYPE_NAME,
'script': fulfill_script_zencode,
'keys': zen_public_keys,
unsigned_fulfillment_dict_zen = {
'type': zenroomscpt.TYPE_NAME,
'public_key': base58.b58encode(biolabs.public_key).decode(),
}
output = {
'amount': '1000',
'amount': '10',
'condition': {
'details': unsigned_fulfillment_dict,
'uri': condition_uri,
'details': unsigned_fulfillment_dict_zen,
'uri': condition_uri_zen,
},
'data': zenroom_data,
'script': fulfill_script_zencode,
'conf': '',
'public_keys': (zen_public_keys['Alice']['ecdh_public_key'], ),
'public_keys': [biolabs.public_key,],
}
input_ = {
'fulfillment': None,
'fulfills': None,
'owners_before': (zen_public_keys['Alice']['ecdh_public_key'], ),
'owners_before': [biolabs.public_key,]
}
metadata = {
"result": {
"output": ["ok"]
}
}
token_creation_tx = {
'operation': 'CREATE',
'asset': zenroom_house_assets,
'metadata': None,
'outputs': (output,),
'inputs': (input_,),
'version': '2.0',
'metadata': metadata,
'outputs': [output,],
'inputs': [input_,],
'version': version,
'id': None,
}
@@ -74,12 +79,36 @@ condition_script_zencode, zenroom_data, zenroom_house_assets):
ensure_ascii=False,
)
try:
assert(not zenSha.validate(message=message))
except JSONDecodeError:
pass
except ValueError:
pass
# major workflow:
# we store the fulfill script in the transaction/message (zenroom-sha)
# the condition script is used to fulfill the transaction and create the signature
#
# the server should ick the fulfill script and recreate the zenroom-sha and verify the signature
message = zenSha.sign(message, condition_script_zencode, alice)
assert(zenSha.validate(message=message))
message = zenroomscpt.sign(message, condition_script_zencode, alice)
assert(zenroomscpt.validate(message=message))
message = json.loads(message)
fulfillment_uri_zen = zenroomscpt.serialize_uri()
message['inputs'][0]['fulfillment'] = fulfillment_uri_zen
tx = message
tx['id'] = None
json_str_tx = json.dumps(
tx,
sort_keys=True,
skipkeys=False,
separators=(',', ':')
)
# SHA3: hash the serialized id-less transaction to generate the id
shared_creation_txid = sha3_256(json_str_tx.encode()).hexdigest()
message['id'] = shared_creation_txid
# `https://example.com:9984`
plntmnt = Planetmint(os.environ.get('PLANETMINT_ENDPOINT'))
sent_transfer_tx = plntmnt.transactions.send_commit(message)
print( f"\n\nstatus and result : + {sent_transfer_tx}")