Skip to content
Snippets Groups Projects
Commit 62115c0b authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: Proof of concept for Bloxberg integration in Python client.

parent 47357561
No related branches found
No related tags found
2 merge requests!22Release 0.3,!3Bloxberg proof of concept
Pipeline #7570 passed with warnings
This commit is part of merge request !3. Comments created here will be created in the context of that merge request.
"""Integration with the Bloxberg proof-of-existence blockchain.
"""
print("Warning: The Bloxberg module is still experimental and under active development.")
......@@ -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")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment