From 62115c0b639f0b4eb17393c0f4fd99792910af4b Mon Sep 17 00:00:00 2001 From: Daniel Hornung <d.hornung@indiscale.com> Date: Wed, 12 May 2021 11:29:12 +0200 Subject: [PATCH] ENH: Proof of concept for Bloxberg integration in Python client. --- src/caosadvancedtools/bloxberg/__init__.py | 4 + src/caosadvancedtools/bloxberg/bloxberg.py | 94 +++++++++++++++------- 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/src/caosadvancedtools/bloxberg/__init__.py b/src/caosadvancedtools/bloxberg/__init__.py index e69de29b..5ca50276 100644 --- a/src/caosadvancedtools/bloxberg/__init__.py +++ b/src/caosadvancedtools/bloxberg/__init__.py @@ -0,0 +1,4 @@ +"""Integration with the Bloxberg proof-of-existence blockchain. +""" + +print("Warning: The Bloxberg module is still experimental and under active development.") diff --git a/src/caosadvancedtools/bloxberg/bloxberg.py b/src/caosadvancedtools/bloxberg/bloxberg.py index 492b0fe8..42af1e11 100644 --- a/src/caosadvancedtools/bloxberg/bloxberg.py +++ b/src/caosadvancedtools/bloxberg/bloxberg.py @@ -29,6 +29,24 @@ from ..models.parser import parse_model_from_string from . import swagger_client +__model_yaml = """ +BloxbergCertificate: + obligatory_properties: + pepper: + datatype: TEXT + hash: + datatype: TEXT + proofValue: + datatype: TEXT + certificateJSON: + datatype: TEXT + recommended_properties: + certified: + datatype: REFERENCE +""" +__model = parse_model_from_string(__model_yaml) + + class Bloxberg: """A Bloxberg instance can be used to obtain or verify certificates.""" @@ -69,7 +87,7 @@ A BloxbergCertificate Record with all the necessary Properties. pepper = str(secrets.randbits(1024)) entity.retrieve() hasher = hashlib.sha256() - hasher.update(pepper) + hasher.update(pepper.encode(encoding="utf8")) hasher.update(str(entity).encode(encoding="utf8")) entity_hash = "0x" + hasher.hexdigest() print(entity_hash) @@ -79,16 +97,23 @@ A BloxbergCertificate Record with all the necessary Properties. enable_ipfs=False) # Submit hash & obtain response result = self._api.create_bloxberg_certificate_create_bloxberg_certificate_post(body=body) - cert = result[0] - json_s = json.dumps(cert.to_dict) - - from IPython import embed; embed() + attribute_map = result[0].attribute_map + cert = result[0].to_dict() + for old, new in attribute_map.items(): + if old == new: + continue + cert[new] = cert.pop(old) + json_s = json.dumps(cert) # Generate result Record - data_model = - cert_rec = db.Record().add_parent + cert_rec = db.Record().add_parent("BloxbergCertificate") # Extract information and put into result + cert_rec.add_property(property="certified", value=entity) + cert_rec.add_property(property="pepper", value=pepper) + cert_rec.add_property(property="hash", value=entity_hash) + cert_rec.add_property(property="proofvalue", value=cert["proof"]["proofValue"]) + cert_rec.add_property(property="certificateJSON", value=json_s) # Return result - pass + return cert_rec def verify(self, certificate): """Attempt to verify the certificate. @@ -124,34 +149,49 @@ Write the JSON to this file. return content -def ensure_data_model(): +def ensure_data_model(force=False): """Make sure that the data model fits our needs. Most importantly, this means that a suitable RecordType "BoxbergCertificate" must exist. """ - model_yaml = """ - BloxbergCertificate: - obligatory_properties: - pepper: - datatype: TEXT - hash: - datatype: TEXT - proofValue: - datatype: TEXT - certificateJSON: - datatype: TEXT - """ - model = parse_model_from_string(model_yaml) - model.sync_data_model() + __model.sync_data_model(noquestion=force) + +def certify_entity(entity, json_filename=None): + """Certify the given entity and store the result in the CaosDB. -def certify_entity(entity): - """Certify the given entity.""" +Parameters +---------- +entity : caosdb.Entity + The Entity to be certified. + +json_filename : str + If given, store the JSON here. +""" if isinstance(entity, int): entity = db.Entity(id=entity) blx = Bloxberg() print("Obtaining certificate...") certificate = blx.certify(entity) - from IPython import embed; embed() - + print("Certificate was successfully obtained.") + certificate.insert() + print("Certificate was stored in CaosDB.") + + if json_filename: + with open(json_filename, "w") as json_file: + json_file.write(certificate.get_property("certificateJSON").value) + + +def demo_run(): + """Run the core functions for demonstration purposes.""" + print("Making sure that the remote data model is up to date.") + ensure_data_model() + print("Data model is up to date.") + import caosdb as db + CertRT = db.RecordType(name="BloxbergCertificate").retrieve() + print("Certifying the `BloxbergCertificate` RecordType...") + json_filename = "/tmp/cert.json" + certify_entity(CertRT, json_filename=json_filename) + print("Certificate json file can be found here: {}".format(json_filename)) + print("You can verify the certificate here: https://certify.bloxberg.org/verify") -- GitLab