From bc3d3abcd734f7d46d1693c22f8169b9ac3dffcd Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 3 Oct 2023 14:37:49 +0200 Subject: [PATCH] add endpoint for check the proofs --- ereuse_devicehub/modules/did/views.py | 6 ++-- ereuse_devicehub/modules/dpp/models.py | 10 +++++- ereuse_devicehub/modules/dpp/views.py | 39 +++++++++++++++++++-- ereuse_devicehub/resources/action/models.py | 17 ++++++--- ereuse_devicehub/resources/device/models.py | 6 +++- 5 files changed, 67 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/modules/did/views.py b/ereuse_devicehub/modules/did/views.py index 6ebf55a7..1999eb1b 100644 --- a/ereuse_devicehub/modules/did/views.py +++ b/ereuse_devicehub/modules/did/views.py @@ -11,7 +11,7 @@ from flask.json import jsonify from flask.views import View from ereuse_devicehub import __version__ -from ereuse_devicehub.modules.dpp.models import Dpp +from ereuse_devicehub.modules.dpp.models import Dpp, ALGORITHM from ereuse_devicehub.resources.device.models import Device logger = logging.getLogger(__name__) @@ -170,7 +170,7 @@ class DidView(View): data = { 'document': {}, 'dpp': self.id_dpp, - 'algorithm': "sha3_256", + 'algorithm': ALGORITHM, 'components': components, 'manufacturer DPP': '', } @@ -198,7 +198,7 @@ class DidView(View): rr = { 'dpp': d.key, 'document': d.snapshot.json_hw, - 'algorithm': "sha3_256", + 'algorithm': ALGORITHM, 'manufacturer DPP': '', } dpps.append(rr) diff --git a/ereuse_devicehub/modules/dpp/models.py b/ereuse_devicehub/modules/dpp/models.py index 2d2fc303..0486e54a 100644 --- a/ereuse_devicehub/modules/dpp/models.py +++ b/ereuse_devicehub/modules/dpp/models.py @@ -12,6 +12,10 @@ from ereuse_devicehub.resources.models import STR_SM_SIZE, Thing from ereuse_devicehub.resources.user.models import User from ereuse_devicehub.teal.db import CASCADE_OWN + +ALGORITHM = "sha3_256" + + PROOF_ENUM = { 'Register': 'Register', 'IssueDPP': 'IssueDPP', @@ -20,7 +24,11 @@ PROOF_ENUM = { 'EWaste': 'EWaste', } -_sorted_proofs = {'order_by': lambda: Proof.created, 'collection_class': SortedSet} + +_sorted_proofs = { + 'order_by': lambda: Proof.created, + 'collection_class': SortedSet +} class Proof(Thing): diff --git a/ereuse_devicehub/modules/dpp/views.py b/ereuse_devicehub/modules/dpp/views.py index e39e8ba3..014ce772 100644 --- a/ereuse_devicehub/modules/dpp/views.py +++ b/ereuse_devicehub/modules/dpp/views.py @@ -1,3 +1,38 @@ -from flask import Blueprint +import json -dpp = Blueprint('dpp', __name__, template_folder='templates') +from flask import Blueprint +from flask.views import View +from .models import Proof, Dpp, ALGORITHM + +dpp = Blueprint('dpp', __name__, url_prefix='/', template_folder='templates') + + +class ProofView(View): + methods = ['GET'] + + def dispatch_request(selfi, proof_id): + proof = Proof.query.filter_by(timestamp=proof_id).first() + + if not proof: + proof = Dpp.query.filter_by(timestamp=proof_id).one() + document = proof.snapshot.json_hw + else: + document = proof.normalizeDoc + + data = { + "algorithm": ALGORITHM, + "document": document + } + + d = { + '@context': ['https://ereuse.org/proof0.json'], + 'data': data, + } + + return json.dumps(d) + + +########## +# Routes # +########## +dpp.add_url_rule('/proofs/', view_func=ProofView.as_view('proof')) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 35daaa34..f7610475 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -509,11 +509,11 @@ class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice): def register_proof(self): """This method is used for register a proof of erasure en dlt.""" - from ereuse_devicehub.modules.dpp.models import PROOF_ENUM + from ereuse_devicehub.modules.dpp.models import PROOF_ENUM, ALGORITHM deviceCHID = self.device.chid docHash = self.snapshot.phid_dpp - docHashAlgorithm = 'sha3_256' + docHashAlgorithm = ALGORITHM proof_type = PROOF_ENUM['Erase'] dh_instance = app.config.get('ID_FEDERATED', 'dh1') @@ -544,8 +544,11 @@ class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice): "type": PROOF_ENUM['Erase'], "device": self.device, "action": self.snapshot, + "documentId": self.snapshot.id, "timestamp": timestamp, "issuer_id": g.user.id, + "documentSignature": self.snapshot.phid_dpp, + "normalizeDoc": self.snapshot.json_hw, } proof = Proof(**d) db.session.add(proof) @@ -1744,13 +1747,17 @@ class EWaste(ActionWithMultipleDevices): api = API(api_dlt, token_dlt, "ethereum") - from ereuse_devicehub.modules.dpp.models import PROOF_ENUM, Proof + from ereuse_devicehub.modules.dpp.models import ( + PROOF_ENUM, + Proof, + ALGORITHM + ) from ereuse_devicehub.resources.enums import StatusCode for device in self.devices: deviceCHID = device.chid docHash = self.generateDocSig() - docHashAlgorithm = 'sha3_256' + docHashAlgorithm = ALGORITHM proof_type = PROOF_ENUM['EWaste'] result = api.generate_proof( deviceCHID, @@ -1770,8 +1777,10 @@ class EWaste(ActionWithMultipleDevices): "type": PROOF_ENUM['EWaste'], "device": device, "action": self, + "documentId": self.id, "timestamp": timestamp, "issuer_id": g.user.id, + "documentSignature": docHash, "normalizeDoc": self.doc, } proof = Proof(**d) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 4aac75e3..4601b1db 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -983,13 +983,17 @@ class Device(Thing): snapshot = [x for x in self.actions if x.t == 'Snapshot'] if not snapshot: return + snapshot = snapshot[0] d = { "type": PROOF_ENUM['Register'], "device": self, - "action": snapshot[0], + "action": snapshot, "timestamp": timestamp, "issuer_id": g.user.id, + "documentId": snapshot.id, + "documentSignature": snapshot.phid_dpp, + "normalizeDoc": snapshot.json_hw, } proof = Proof(**d) db.session.add(proof)