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

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2022-06-10 13:09:48 +02:00
parent ecb828f1d6
commit ebabd3de7d

View File

@ -20,14 +20,14 @@ from planetmint_driver import Planetmint
def test_zenroom_signing(): def test_zenroom_signing():
# bdb_root_url = 'http://localhost:9984/' # bdb_root_url = 'http://localhost:9984/'
# bdb = Planetmint(bdb_root_url) # bdb = Planetmint(bdb_root_url)
# generate the keypairs/wallets for biolabs and the hospital # generate the keypairs/wallets for biolabs and the hospital
# the pacemaker will only e represented by its public key address # the pacemaker will only e represented by its public key address
# derived from the attached RFID tag's EPC code # derived from the attached RFID tag's EPC code
from planetmint_driver.crypto import generate_keypair, CryptoKeypair from planetmint_driver.crypto import generate_keypair
biolabs, hospital = generate_keypair(), generate_keypair() biolabs, hospital = generate_keypair(), generate_keypair()
# biolabs = CryptoKeypair(private_key='2KF5Qx4ksFWQ7j7DgTj1jYhQ6eoP38WoyFVMjTR5hDgK', public_key='2KF5Qx4ksFWQ7j7DgTj1jYhQ6eoP38WoyFVMjTR5hDgK')
print(biolabs.private_key) print(biolabs.private_key)
print(biolabs.public_key) print(biolabs.public_key)
print(hospital.private_key) print(hospital.private_key)
@ -38,30 +38,54 @@ def test_zenroom_signing():
# hospital = CryptoKeypair(private_key='ASHwLY9zG43rNkCZgRFBV6K9j9oHM1joxYMxHRiNyPja', public_key='A7fpfDpaGkJubquXbj3cssMhx5GQ1599Sxc7MxR9SWa8') # hospital = CryptoKeypair(private_key='ASHwLY9zG43rNkCZgRFBV6K9j9oHM1joxYMxHRiNyPja', public_key='A7fpfDpaGkJubquXbj3cssMhx5GQ1599Sxc7MxR9SWa8')
# create a digital asset for biolabs # create a digital asset for biolabs
# for readability we turn the original EPC code into capital hex chars # for readability we turn the original EPC code into capital hex chars
rfid_token = { # rfid_token = {
'data': { # 'data': {
'token_for': { # 'token_for': {
'UCODE_DNA': { # 'UCODE_DNA': {
'EPC_serial_number': 'E2003787C9AE8209161AF72F', # 'EPC_serial_number': 'E2003787C9AE8209161AF72F',
'amount_issued': 100, # 'amount_issued': 100,
'pegged_to' : 'SFR', # 'pegged_to' : 'SFR',
#'pub_key' : elements.public_key, # #'pub_key' : elements.public_key,
# }
# },
# #'description': 'Biolab\'s blockchain settlement system for pacemakers.',
# },
# }
HOUSE_ASSETS = {
"data": {
"houses": [
{
"name": "Harry",
"team": "Gryffindor",
},
{
"name": "Draco",
"team": "Slytherin",
} }
}, ],
#'description': 'Biolab\'s blockchain settlement system for pacemakers.', }
}, }
}
version = '2.0' version = '2.0'
script = """Scenario 'TakeoutCTL': "To provision, the pacemaker id#527663 the first time and store the output as keypair.keys"
Given that I am known as 'identifier' CONDITION_SCRIPT = """Rule input encoding base58
When I create my new keypair Rule output encoding base58
Then print all data Scenario 'ecdh': create the signature of an object
""" Given I have the 'keys'
script2 = """Scenario 'TakeoutCTL': "For settlement, the pacemaker id#527663 with keypair.keys activated locck 'did:r3c:MBs2h46THPD3ezJ7Giisq5MJbuWo7mpz8GF9NbW1BspjoICAgIGtleXJpbmcgPSBFQ0RILm5ldygpCiAgICBrZXlyaW5nOmtleWdlbigpCiAgICAKICAgIC0tIGV4cG9ydCB0aGUga2V5cGFpciB0byBqc29uCiAgICBleHBvcnQgPSBKU09OLmVuY29kZSgKICAgICAgIHsKICAgICAgICAgIHB1YmxpYyAgPSBrZXlyaW5nOiBwdWJsaWMoKTpiYXNlNjQoKSwKICAgICAgICAgIHByaXZhdGUgPSBrZXlyaW5nOnByaXZhdGUoKTpiYXNlNjQoKQogICAgICAgfQogICAgKQogICAgcHJpbnQoZXhwb3J0KQoWBE5vbmUWBE5vbmUWBE5vbmUCAQA='" Given that I have a 'string dictionary' named 'houses' inside 'asset'
Given that I am known as 'identifier' When I create the signature of 'houses'
When my signature validated When I rename the 'signature' to 'data.signature'
Then verify transaction and settle Then print the 'data.signature'"""
"""
FULFILL_SCRIPT = \
"""Rule input encoding base58
Rule output encoding base58
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'
Then print the string 'ok'"""
SK_TO_PK = \ SK_TO_PK = \
"""Rule input encoding base58 """Rule input encoding base58
Rule output encoding base58 Rule output encoding base58
@ -94,23 +118,19 @@ def test_zenroom_signing():
keys={'keys': bob}).output)) keys={'keys': bob}).output))
# CRYPTO-CONDITIONS: instantiate an Ed25519|Zenroom crypto-condition for hospital
#ed25519 = Ed25519Sha256(public_key=base58.b58decode(hospital.public_key)) zenroomscpt = ZenroomSha256(script=FULFILL_SCRIPT, data=ZENROOM_DATA, keys=zen_public_keys)
zenroomscpt = ZenroomSha256(script=script2, data=ZENROOM_DATA, keys=zen_public_keys)
# print(F'ed25519 is: {ed25519.public_key}')
print(F'zenroom is: {zenroomscpt.script}') print(F'zenroom is: {zenroomscpt.script}')
# CRYPTO-CONDITIONS: generate the condition uri # CRYPTO-CONDITIONS: generate the condition uri
# condition_uri = ed25519.condition.serialize_uri() condition_uri_zen = zenroomscpt.condition.serialize_uri()
condition_uri_zen = zenroomscpt.condition.serialize_uri() print(F'\nzenroom condition URI: {condition_uri_zen}')
#print(F'condition_uri is: {condition_uri}') #print(F'condition_uri is: {condition_uri}')
# # print(F'condition_uri_zen is: {condition_uri_zen}') # # print(F'condition_uri_zen is: {condition_uri_zen}')
# ZEN-CRYPTO-CONDITION: generate the condition did # ZEN-CRYPTO-CONDITION: generate the condition did
zen_condition_did = 'did:bdb:MIIBMxaCARoKICAgIC0tIGdlbmVyYXRlIGEgc2ltcGxlIGtleXJpbmcKICAgIGtleXJpbmcgPSBFQ0RILm5ldygpCiAgICBrZXlyaW5nOmtleWdlbigpCiAgICAKICAgIC0tIGV4cG9ydCB0aGUga2V5cGFpciB0byBqc29uCiAgICBleHBvcnQgPSBKU09OLmVuY29kZSgKICAgICAgIHsKICAgICAgICAgIHB1YmxpYyAgPSBrZXlyaW5nOiBwdWJsaWMoKTpiYXNlNjQoKSwKICAgICAgICAgIHByaXZhdGUgPSBrZXlyaW5nOnByaXZhdGUoKTpiYXNlNjQoKQogICAgICAgfQogICAgKQogICAgcHJpbnQoZXhwb3J0KQoWBE5vbmUWBE5vbmUWBE5vbmUCAQA=' #zen_condition_did = 'did:bdb:MIIBMxaCARoKICAgIC0tIGdlbmVyYXRlIGEgc2ltcGxlIGtleXJpbmcKICAgIGtleXJpbmcgPSBFQ0RILm5ldygpCiAgICBrZXlyaW5nOmtleWdlbigpCiAgICAKICAgIC0tIGV4cG9ydCB0aGUga2V5cGFpciB0byBqc29uCiAgICBleHBvcnQgPSBKU09OLmVuY29kZSgKICAgICAgIHsKICAgICAgICAgIHB1YmxpYyAgPSBrZXlyaW5nOiBwdWJsaWMoKTpiYXNlNjQoKSwKICAgICAgICAgIHByaXZhdGUgPSBrZXlyaW5nOnByaXZhdGUoKTpiYXNlNjQoKQogICAgICAgfQogICAgKQogICAgcHJpbnQoZXhwb3J0KQoWBE5vbmUWBE5vbmUWBE5vbmUCAQA='
# CRYPTO-CONDITIONS: construct an unsigned fulfillment dictionary # CRYPTO-CONDITIONS: construct an unsigned fulfillment dictionary
"""unsigned_fulfillment_dict = {
'type': ed25519.TYPE_NAME,
'public_key': base58.b58encode(ed25519.public_key).decode(),
}"""
unsigned_fulfillment_dict_zen = { unsigned_fulfillment_dict_zen = {
'type': zenroomscpt.TYPE_NAME, 'type': zenroomscpt.TYPE_NAME,
'public_key': base58.b58encode(hospital.public_key).decode(), 'public_key': base58.b58encode(hospital.public_key).decode(),
@ -118,9 +138,7 @@ def test_zenroom_signing():
output = { output = {
'amount': '10', 'amount': '10',
'condition': { 'condition': {
#'details': unsigned_fulfillment_dict,
'details': unsigned_fulfillment_dict_zen, 'details': unsigned_fulfillment_dict_zen,
#'uri': condition_uri,
'uri': condition_uri_zen, 'uri': condition_uri_zen,
#'did': zen_condition_did, #'did': zen_condition_did,
#'script': script, #'script': script,
@ -138,21 +156,39 @@ def test_zenroom_signing():
} }
token_creation_tx = { token_creation_tx = {
'operation': 'CREATE', 'operation': 'CREATE',
'asset': rfid_token, 'asset': HOUSE_ASSETS,#rfid_token,
'metadata': None, 'metadata': None,
'outputs': (output,), 'outputs': (output,),
'inputs': (input_,), 'inputs': (input_,),
'version': version, 'version': version,
'id': None, 'id': None,
} }
# JSON: serialize the transaction-without-id to a json formatted string # JSON: serialize the transaction-without-id to a json formatted string
message = json.dumps( message = json.dumps(
token_creation_tx, token_creation_tx,
sort_keys=True, sort_keys=True,
skipkeys=False,
separators=(',', ':'), separators=(',', ':'),
ensure_ascii=False, ensure_ascii=False,
) )
message = sha3.sha3_256(message.encode())
# 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 = zenroomscpt.sign(message, CONDITION_SCRIPT, alice)
assert(zenroomscpt.validate(message=message))
### WORkS until here
# CRYPTO-CONDITIONS: sign the serialized transaction-without-id # CRYPTO-CONDITIONS: sign the serialized transaction-without-id
#ed25519.sign(message.digest(), base58.b58decode(biolabs.private_key)) #ed25519.sign(message.digest(), base58.b58decode(biolabs.private_key))
## zenroomscpt.sign(message.digest(), base58.b58decode(biolabs.private_key)) ## zenroomscpt.sign(message.digest(), base58.b58decode(biolabs.private_key))
@ -161,7 +197,7 @@ def test_zenroom_signing():
# CRYPTO-CONDITIONS: generate the fulfillment uri # CRYPTO-CONDITIONS: generate the fulfillment uri
# fulfillment_uri = ed25519.serialize_uri() # fulfillment_uri = ed25519.serialize_uri()
fulfillment_uri_zen = zenroomscpt.serialize_uri() fulfillment_uri_zen = zenroomscpt.serialize_uri()
print(f'\nfulfillment_uri_zen is: {fulfillment_uri_zen}\n\n') print(f'\nfulfillment_uri_zen is: {fulfillment_uri_zen}')
fulfillment_fromuri_zen = zenroomscpt.from_uri(fulfillment_uri_zen) fulfillment_fromuri_zen = zenroomscpt.from_uri(fulfillment_uri_zen)
# print(F'fulfillment_uri is: {fulfillment_uri}') # print(F'fulfillment_uri is: {fulfillment_uri}')
@ -182,19 +218,26 @@ def test_zenroom_signing():
#token_creation_tx['inputs'][0]['fulfillment'] = fulfillment_uri ## there is the problem with fulfillment uri #token_creation_tx['inputs'][0]['fulfillment'] = fulfillment_uri ## there is the problem with fulfillment uri
#print(F'token_creation_tx is: {token_creation_tx}') #print(F'token_creation_tx is: {token_creation_tx}')
# JSON: serialize the id-less transaction to a json formatted string # JSON: serialize the id-less transaction to a json formatted string
tx = token_creation_tx
tx['id'] = None
json_str_tx = json.dumps( json_str_tx = json.dumps(
token_creation_tx, tx,
sort_keys=True, sort_keys=True,
separators=(',', ':'), skipkeys=False,
ensure_ascii=False, separators=(',', ':')
) )
# SHA3: hash the serialized id-less transaction to generate the id # SHA3: hash the serialized id-less transaction to generate the id
shared_creation_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest() shared_creation_txid = sha3.sha3_256(json_str_tx.encode()).hexdigest()
# add the id # add the id
token_creation_tx['id'] = shared_creation_txid token_creation_tx['id'] = shared_creation_txid
print(F'The TX to be consensed: {token_creation_tx}') #print(F'The TX to be consensed: {token_creation_tx}')
# send CREATE tx into the bdb network # send CREATE tx into the bdb network
##tx = signed_create_tx.to_dict()
##tx['id'] = None
##payload = json.dumps(tx, skipkeys=False, sort_keys=True,
## separators=(',', ':'))
##assert sha3.sha3_256(payload.encode()).hexdigest() == signed_create_tx.id
#returned_creation_tx = bdb.transactions.send_commit(token_creation_tx) #returned_creation_tx = bdb.transactions.send_commit(token_creation_tx)
#tx = request.get_json(force=True) #tx = request.get_json(force=True)
@ -210,6 +253,7 @@ def test_zenroom_signing():
except SchemaValidationError as e: except SchemaValidationError as e:
assert() assert()
except ValidationError as e: except ValidationError as e:
print(e)
assert() assert()
#pool = current_app.config['bigchain_pool'] #pool = current_app.config['bigchain_pool']
#with pool() as planet: #with pool() as planet:
@ -227,39 +271,26 @@ def test_zenroom_signing():
assert() assert()
#assert status_code == 202 #assert status_code == 202
#returned_creation_tx = bdb.transactions.send_async(token_creation_tx) #returned_creation_tx = bdb.transactions.send_async(token_creation_tx)
#print(f"created TX : {returned_creation_tx}" ) #print(f"created TX : {returned_creation_tx}" )
# result, errors = zenroom.zencode_exec(script) # result, errors = zenroom.zencode_exec(script)
# result, errors = zenroom.zencode_exec(script) # result, errors = zenroom.zencode_exec(script)
#print(result) #print(result)
''' # '''
Settlement on the Magic Mote chain is a prerequisite to sttlement on the respective settlement chain, # Settlement on the Magic Mote chain is a prerequisite to sttlement on the respective settlement chain,
Liquid, Ethereum, Bitcoin, Hyperledger Fabric or Coreda R3. # Liquid, Ethereum, Bitcoin, Hyperledger Fabric or Coreda R3.
Therefore, policies can become part of the transaction fulfillment logic. Quite exciting. # Therefore, policies can become part of the transaction fulfillment logic. Quite exciting.
This way the Oracle Servce ith built right inro the transaction itself. # This way the Oracle Servce ith built right inro the transaction itself.
More precisely, it is part of the fulfillment. # More precisely, it is part of the fulfillment.
Therefore, interweaving the blockchain transactions with external systems becomes trivial. # Therefore, interweaving the blockchain transactions with external systems becomes trivial.
Add to this the capability to multipart each and every transaction thanks to code mobility # Add to this the capability to multipart each and every transaction thanks to code mobility
and thanks to transferring code with the state of the VM itself, the power of the system # and thanks to transferring code with the state of the VM itself, the power of the system
becomes comprehensible. # becomes comprehensible.
Then consider that each and evry transaction is enabled to carry around its very own # Then consider that each and evry transaction is enabled to carry around its very own
interface to visualize transaction and chainstate on DLT enabled machines via the # interface to visualize transaction and chainstate on DLT enabled machines via the
Magic Mote UI. # Magic Mote UI.
Smart dust , indeed. # Smart dust , indeed.
''' # '''