def test_broken_zencode(): with pytest.raises(ZenroomException) as e: contract = """Scenario coconut: 'coconut' When I""" conf = "color:0\ndebug:1" result = zenroom.zencode_exec(contract) assert result.stderr assert "{}" == result.stdout
def create_keypair(): contract = """ rule check version 1.0.0 Scenario 'ecdh': create a keypair Given nothing When I create the keypair Then print 'keypair' """ try: result = zenroom.zencode_exec(contract) except Exception as e: print(f'Exception in zenroom call: {e}') # breakpoint() keypair = json.loads(result.output)['keypair'] print(f'challenge: {keypair}') return keypair
def verify_signed_vc(): data = '{"my-vc": {"@context":["https://www.w3.org/2018/credentials/v1","http://genecoop.waag.org/credentials/v1#",{"gc_cred":"http://genecoop.waag.org/credentials/v1#","gc_docs":"https://genecoop.waag.org/docs/","gc_ids":"https://genecoop.waag.org/ids/","gc_schema":"http://genecoop.waag.org/schema/v1#"}],"credentialSubject":{"gc_cred:consents_to":[{"id":"gc_schema:exp_000","name":"Array SNP request + Analysis and interpretation"},{"id":"gc_schema:exp_001","name":"Copy Number Variation"},{"id":"gc_schema:exp_003","name":"Population study. DNA data available for secondary use"}],"gc_cred:prohibits":[{"id":"gc_schema:exp_002","name":"SNP Variant detection (Biomarker or Pathogenic)"}],"id":"https://genecoop.waag.org/ids/superduper"},"given_to":{"id":"https://genecoop.waag.org/ids/researcherA"},"id":"gc_docs:3e424387d2","issuanceDate":"2021-06-07T14:09:17.162Z","issuer":{"id":"gc_ids:GeneCoop","name":"GeneCoop Consent Service"},"name":"Consent for Eye Melanoma research","proof":{"jws":"eyJhbGciOiJFUzI1NksiLCJiNjQiOnRydWUsImNyaXQiOiJiNjQifQ..LeL430a4rG39VUECHePk1RjejevSGZtn97iqeoT3glmSTX2qZr0X1O07h-Gg7X1DgYW-pR17idDVTGsXsaVG1Q","proofPurpose":"authenticate","type":"Zenroom","verificationMethod":"https://genecoop.waag.org/ids/superduper"},"termsOfUse":[{"gc_cred:obligation":[{"gc_cred:action":[{"id":"gc_cred:FetchLatestConsent"}],"gc_cred:assignee":{"id":"gc_cred:AllVerifiers"},"gc_cred:assigner":{"id":"gc_ids:GeneCoop"},"gc_cred:target":{"id":"gc_docs:3e424387d2"}}],"type":"gc_cred:IssuerPolicy"}],"type":["VerifiableCredential","VerifiableConsent"]}}' keys = '{"Issuer": {"public_key": "BCPyi2O0McdRkkfB6+DbXfz7Wiv7UmX9+rYiQCUBov6ZVu/kE0PZMThgAowU/E0F8xsRnAE/QeBGR4/yEtnlj/8=" } }' contract = """ Rule check version 1.0.0 Scenario 'w3c' : verify w3c vc signature Scenario 'ecdh' : verify Given I have a 'public key' from 'Issuer' Given I have a 'verifiable credential' named 'my-vc' When I verify the verifiable credential named 'my-vc' Then print the string 'verification passed' """ try: # result = zenroom.zencode_exec(contract, keys=json.dumps(keys), data=json.dumps(data)) # breakpoint() result = zenroom.zencode_exec(contract, keys=keys, data=data, conf='debug=0') except Exception as e: print(f'Exception in zenroom call: {e}') # print(f'Exception in zenroom call: {e}') return False # result = zenroom.zencode_exec(contract, data=json.dumps(data)) print(f'result: {result}') if not result.output == '': res_json = json.loads(result.output) if type(res_json['output']) == list: parsed_res = res_json['output'][0] else: parsed_res = res_json['output'] if parsed_res == 'verification_passed': print(f'Verification passed') return True print(f'Verification NOT passed') return False
def verify_signature(): contract = """ rule check version 1.0.0 Scenario 'ecdh': verify the signature of a request Given I have a 'public key' from 'Signer' and I have a 'string' named 'message' and I have a 'signature' named 'message.signature' When I verify the 'message' has a signature in 'message.signature' by 'Signer' Then print 'verification passed' as 'string' """ data = '{"message": "9yDO2VAkbs2Dqb8NcAU/deaL9TBzM7K+/jHdGAz/Wqpw/+evNsVlBLsoShFe/k4ZbaPk/K6b8r0ZhkyNTUI6wQ==", "message.signature": {"r":"lvzeTbEUpgcT3lfjqvp7XcWKNX0rLxlPGMGDkYWs52w=","s":"7uAqNP6shbT50k4k56TepoiO/oges6L3koFqg0LQUmI="} }' keys = '{"Signer": {"public_key": "BGRO8HAnOMeESESnhekVyrsr0q9tHNtI3X7eYaNoVfr9wzThRuL6E9WVltW1jT6z1IVHXSdophZJkBEuYjfa4qg=" } }' try: # result = zenroom.zencode_exec(contract, keys=json.dumps(keys), data=json.dumps(data)) # breakpoint() result = zenroom.zencode_exec(contract, keys=keys, data=data, conf='debug=0') except Exception as e: print(f'Exception in zenroom call: {e}') return False if not result.output == '': res_json = json.loads(result.output) res_json = json.loads(result.output) if type(res_json['output']) == list: parsed_res = res_json['output'][0] else: parsed_res = res_json['output'] if parsed_res == 'verification_passed': print(f'Verification passed') return True print(f'Verification NOT passed') return False
def create_challenge(): """ Call zenroom to create a random challenge """ contract = """ rule check version 1.0.0 Given nothing When I create the random object of '512' bits and I rename the 'random_object' to 'challenge' Then print 'challenge' """ try: result = zenroom.zencode_exec(contract) except Exception as e: print(f'Exception in zenroom call: {e}') challenge = json.loads(result.output)['challenge'] print(f'challenge: {challenge}') return challenge
def sign_challenge(public_key, private_key, challenge): """ Sign a challenge with the Signer's credentials """ contract = """ rule check version 1.0.0 Scenario 'ecdh': create the signature of a challenge Given I have a 'string' named 'challenge' and I am 'Signer' and I have my 'keypair' When I create the signature of 'challenge' and I rename the 'signature' to 'challenge.signature' Then print the 'challenge.signature' """ data = { "challenge": challenge } keys = { "Signer": { "keypair": { "private_key": private_key, "public_key": public_key } } } try: result = zenroom.zencode_exec( contract, keys=json.dumps(keys), data=json.dumps(data)) except Exception as e: print(f'Exception in zenroom call: {e}') signature = json.loads(result.output)['challenge.signature'] print(f'signature: {signature}') return signature
def verify_challenge(public_key, challenge, signature): """ Call zenroom to verify the signature of a challenge """ contract = """ rule check version 1.0.0 Scenario 'ecdh': verify the signature of a request Given I have a 'public key' from 'Signer' and I have a 'string' named 'challenge' and I have a 'signature' named 'signature' When I verify the 'challenge' has a signature in 'signature' by 'Signer' Then print 'verification passed' as 'string' """ data = { "challenge": challenge, "signature": signature } keys = { "Signer": { "public_key": public_key } } try: result = zenroom.zencode_exec( contract, keys=json.dumps(keys), data=json.dumps(data)) except Exception as e: print(f'Exception in zenroom call: {e}') verification = json.loads(result.output)['output'] print(f'verification: {verification}') return verification