add action Recycled with dlt proof

This commit is contained in:
Cayo Puigdefabregas 2024-02-09 20:45:17 +01:00
parent 9fb2b1c94a
commit 74fe50b6fb
6 changed files with 90 additions and 0 deletions

View file

@ -43,6 +43,7 @@ from ereuse_devicehub.parser.parser import ParseSnapshotLsHw
from ereuse_devicehub.parser.schemas import Snapshot_lite
from ereuse_devicehub.resources.action.models import Snapshot, Trade
from ereuse_devicehub.resources.action.schemas import EWaste as EWasteSchema
from ereuse_devicehub.resources.action.schemas import Recycled as RecycledSchema
from ereuse_devicehub.resources.action.schemas import Snapshot as SnapshotSchema
from ereuse_devicehub.resources.action.views.snapshot import (
SnapshotMixin,
@ -866,6 +867,11 @@ class ActionFormMixin(FlaskForm):
doc = "{}".format(ewaste)
self.instance.register_proof(doc)
if self.instance.type == 'Recycled':
recycled = RecycledSchema().dump(self.instance)
doc = "{}".format(recycled)
self.instance.register_proof(doc)
db.session.commit()
self.devices.data = devices

View file

@ -22,6 +22,7 @@ PROOF_ENUM = {
'proof_of_recycling': 'proof_of_recycling',
'Erase': 'Erase',
'EWaste': 'EWaste',
'Recycled': 'Device_recycled',
}

View file

@ -221,6 +221,11 @@ class EWasteDef(ActionDef):
SCHEMA = schemas.EWaste
class RecycledDef(ActionDef):
VIEW = None
SCHEMA = schemas.Recycled
class RecyclingDef(ActionDef):
VIEW = None
SCHEMA = schemas.Recycling

View file

@ -1720,6 +1720,74 @@ class Ready(ActionWithMultipleDevices):
"""
class Recycled(ActionWithMultipleDevices):
def register_proof(self, doc):
"""This method is used for register a proof of erasure en dlt."""
if 'dpp' not in app.blueprints.keys():
return
if not session.get('token_dlt'):
return
if not doc:
return
self.doc = doc
token_dlt = session.get('token_dlt')
api_dlt = app.config.get('API_DLT')
dh_instance = app.config.get('ID_FEDERATED', 'dh1')
if not token_dlt or not api_dlt:
return
api = API(api_dlt, token_dlt, "ethereum")
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 = ALGORITHM
proof_type = PROOF_ENUM['Recycled']
result = api.generate_proof(
deviceCHID,
docHashAlgorithm,
docHash,
proof_type,
dh_instance,
)
if result['Status'] == StatusCode.Success.value:
timestamp = result.get('Data', {}).get('data', {}).get('timestamp')
if not timestamp:
return
d = {
"type": PROOF_ENUM['Recycled'],
"device": device,
"action": self,
"documentId": self.id,
"timestamp": timestamp,
"issuer_id": g.user.id,
"documentSignature": docHash,
"normalizeDoc": self.doc,
}
proof = Proof(**d)
db.session.add(proof)
def generateDocSig(self):
if not self.doc:
return
return hashlib.sha3_256(self.doc.encode('utf-8')).hexdigest()
class EWaste(ActionWithMultipleDevices):
"""The device is declared as e-waste, this device is not allow use more.

View file

@ -527,6 +527,10 @@ class EWaste(ActionWithMultipleDevicesCheckingOwner):
__doc__ = m.EWaste.__doc__
class Recycled(ActionWithMultipleDevicesCheckingOwner):
__doc__ = m.Recycled.__doc__
class ActionStatus(Action):
rol_user = NestedOn(s_user.User, dump_only=True, exclude=('token',))
devices = NestedOn(

View file

@ -232,6 +232,12 @@
E-Waste
</a>
</li>
<li>
<a href="javascript:newAction('Recycled')" class="dropdown-item">
<i class="bi bi-recycle"></i>
Recycled
</a>
</li>
</ul>
</div>