add endpoint for check the proofs

This commit is contained in:
Cayo Puigdefabregas 2023-10-03 14:37:49 +02:00
parent 9ca92f4c56
commit bc3d3abcd7
5 changed files with 67 additions and 11 deletions

View File

@ -11,7 +11,7 @@ from flask.json import jsonify
from flask.views import View from flask.views import View
from ereuse_devicehub import __version__ 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 from ereuse_devicehub.resources.device.models import Device
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -170,7 +170,7 @@ class DidView(View):
data = { data = {
'document': {}, 'document': {},
'dpp': self.id_dpp, 'dpp': self.id_dpp,
'algorithm': "sha3_256", 'algorithm': ALGORITHM,
'components': components, 'components': components,
'manufacturer DPP': '', 'manufacturer DPP': '',
} }
@ -198,7 +198,7 @@ class DidView(View):
rr = { rr = {
'dpp': d.key, 'dpp': d.key,
'document': d.snapshot.json_hw, 'document': d.snapshot.json_hw,
'algorithm': "sha3_256", 'algorithm': ALGORITHM,
'manufacturer DPP': '', 'manufacturer DPP': '',
} }
dpps.append(rr) dpps.append(rr)

View File

@ -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.resources.user.models import User
from ereuse_devicehub.teal.db import CASCADE_OWN from ereuse_devicehub.teal.db import CASCADE_OWN
ALGORITHM = "sha3_256"
PROOF_ENUM = { PROOF_ENUM = {
'Register': 'Register', 'Register': 'Register',
'IssueDPP': 'IssueDPP', 'IssueDPP': 'IssueDPP',
@ -20,7 +24,11 @@ PROOF_ENUM = {
'EWaste': 'EWaste', '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): class Proof(Thing):

View File

@ -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/<int:proof_id>', view_func=ProofView.as_view('proof'))

View File

@ -509,11 +509,11 @@ class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice):
def register_proof(self): def register_proof(self):
"""This method is used for register a proof of erasure en dlt.""" """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 deviceCHID = self.device.chid
docHash = self.snapshot.phid_dpp docHash = self.snapshot.phid_dpp
docHashAlgorithm = 'sha3_256' docHashAlgorithm = ALGORITHM
proof_type = PROOF_ENUM['Erase'] proof_type = PROOF_ENUM['Erase']
dh_instance = app.config.get('ID_FEDERATED', 'dh1') dh_instance = app.config.get('ID_FEDERATED', 'dh1')
@ -544,8 +544,11 @@ class EraseBasic(JoinedWithOneDeviceMixin, ActionWithOneDevice):
"type": PROOF_ENUM['Erase'], "type": PROOF_ENUM['Erase'],
"device": self.device, "device": self.device,
"action": self.snapshot, "action": self.snapshot,
"documentId": self.snapshot.id,
"timestamp": timestamp, "timestamp": timestamp,
"issuer_id": g.user.id, "issuer_id": g.user.id,
"documentSignature": self.snapshot.phid_dpp,
"normalizeDoc": self.snapshot.json_hw,
} }
proof = Proof(**d) proof = Proof(**d)
db.session.add(proof) db.session.add(proof)
@ -1744,13 +1747,17 @@ class EWaste(ActionWithMultipleDevices):
api = API(api_dlt, token_dlt, "ethereum") 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 from ereuse_devicehub.resources.enums import StatusCode
for device in self.devices: for device in self.devices:
deviceCHID = device.chid deviceCHID = device.chid
docHash = self.generateDocSig() docHash = self.generateDocSig()
docHashAlgorithm = 'sha3_256' docHashAlgorithm = ALGORITHM
proof_type = PROOF_ENUM['EWaste'] proof_type = PROOF_ENUM['EWaste']
result = api.generate_proof( result = api.generate_proof(
deviceCHID, deviceCHID,
@ -1770,8 +1777,10 @@ class EWaste(ActionWithMultipleDevices):
"type": PROOF_ENUM['EWaste'], "type": PROOF_ENUM['EWaste'],
"device": device, "device": device,
"action": self, "action": self,
"documentId": self.id,
"timestamp": timestamp, "timestamp": timestamp,
"issuer_id": g.user.id, "issuer_id": g.user.id,
"documentSignature": docHash,
"normalizeDoc": self.doc, "normalizeDoc": self.doc,
} }
proof = Proof(**d) proof = Proof(**d)

View File

@ -983,13 +983,17 @@ class Device(Thing):
snapshot = [x for x in self.actions if x.t == 'Snapshot'] snapshot = [x for x in self.actions if x.t == 'Snapshot']
if not snapshot: if not snapshot:
return return
snapshot = snapshot[0]
d = { d = {
"type": PROOF_ENUM['Register'], "type": PROOF_ENUM['Register'],
"device": self, "device": self,
"action": snapshot[0], "action": snapshot,
"timestamp": timestamp, "timestamp": timestamp,
"issuer_id": g.user.id, "issuer_id": g.user.id,
"documentId": snapshot.id,
"documentSignature": snapshot.phid_dpp,
"normalizeDoc": snapshot.json_hw,
} }
proof = Proof(**d) proof = Proof(**d)
db.session.add(proof) db.session.add(proof)