From ef8825568fbe953623ef89d1442301be015b9bbe Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Mon, 20 Feb 2023 19:16:43 +0100 Subject: [PATCH] add proof --- ereuse_devicehub/resources/action/models.py | 41 ++++++++++++++++++- .../resources/action/views/snapshot.py | 7 +++- ereuse_devicehub/resources/device/models.py | 7 ++++ ereuse_devicehub/resources/did/models.py | 1 + 4 files changed, 54 insertions(+), 2 deletions(-) diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 85caab24..d7140459 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -487,6 +487,46 @@ class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice): return self.snapshot.device.phid() return '' + def register_proof(self): + """This method is used for register a proof of erasure en dlt""" + + if 'trublo' not in app.blueprints.keys() or not self.snapshot: + return + + if not session.get('token_dlt'): + return + + token_dlt = session.get('token_dlt') + api_dlt = app.config.get('API_DLT') + if not token_dlt or not api_dlt: + return + + api = API(api_dlt, token_dlt, "ethereum") + + from ereuse_devicehub.resources.did.models import PROOF_ENUM, Proof + + deviceCHID = self.device.chid + docSig = hashlib.sha3_256(self.snapshot.json_wb.encode('utf-8')).hexdigest() + docID = "{}".format(self.snapshot.uuid or '') + issuerID = "dh1:{user}".format(user=g.user.id) + proof_type = PROOF_ENUM['Erase'] + result = api.generate_proof(deviceCHID, docID, docSig, issuerID, proof_type) + from ereuse_devicehub.resources.enums import StatusCode + + if result['Status'] == StatusCode.Success: + timestamp = ( + result.get('Data', {}).get('data', {}).get('timestamp', time.time()) + ) + d = { + "type": PROOF_ENUM['Register'], + "device": self.device, + "snapshot": self.snapshot, + "timestamp": timestamp, + "issuer_id": g.user.id, + } + proof = Proof(**d) + db.session.add(proof) + def __str__(self) -> str: return '{} on {}.'.format(self.severity, self.date_str) @@ -780,7 +820,6 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): docID = "{}".format(self.uuid or '') issuerID = "dh1:{user}".format(user=g.user.id) result = api.issue_passport(dpp, docID, docSig, issuerID) - # import pdb;pdb.set_trace() if result['Status'] is not StatusCode.Success: return timestamp = result['Data'].get('timestamp', time.time()) diff --git a/ereuse_devicehub/resources/action/views/snapshot.py b/ereuse_devicehub/resources/action/views/snapshot.py index a9a689bc..e95323de 100644 --- a/ereuse_devicehub/resources/action/views/snapshot.py +++ b/ereuse_devicehub/resources/action/views/snapshot.py @@ -12,7 +12,7 @@ from flask import g from sqlalchemy.util import OrderedSet from ereuse_devicehub.db import db -from ereuse_devicehub.resources.action.models import Snapshot +from ereuse_devicehub.resources.action.models import EraseBasic, Snapshot from ereuse_devicehub.resources.device.models import Computer from ereuse_devicehub.resources.device.sync import Sync from ereuse_devicehub.resources.enums import Severity, SnapshotSoftware @@ -126,6 +126,11 @@ class SnapshotMixin: snapshot.device.register_dlt() snapshot.register_passport_dlt() + for ac in snapshot.actions: + if not isinstance(ac, EraseBasic): + continue + ac.register_proof() + return snapshot def is_server_erase(self, snapshot): diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index e0931f8a..450fd6b8 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -915,6 +915,13 @@ class Device(Thing): proof = Proof(**d) db.session.add(proof) + if not hasattr(self, 'components'): + return + + for c in self.components: + if isinstance(c, DataStorage): + c.register_dlt() + def unreliable(self): self.user_trusts = False i = 0 diff --git a/ereuse_devicehub/resources/did/models.py b/ereuse_devicehub/resources/did/models.py index d1b15377..c7209dd3 100644 --- a/ereuse_devicehub/resources/did/models.py +++ b/ereuse_devicehub/resources/did/models.py @@ -15,6 +15,7 @@ PROOF_ENUM = { 'Register': 'Register', 'IssueDPP': 'IssueDPP', 'proof_of_recycling': 'proof_of_recycling', + 'Erase': 'Erase', } _sorted_proofs = {'order_by': lambda: Proof.created, 'collection_class': SortedSet}