From 574ab36da40f5d91fb31092b7bd9d61c4bb21369 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 20 Jan 2023 18:58:25 +0100 Subject: [PATCH] add registar issuer passport --- .../8334535d56fa_add_digital_passport_dpp.py | 14 +--- ereuse_devicehub/resources/action/models.py | 77 ++++++++++++++++++- 2 files changed, 80 insertions(+), 11 deletions(-) diff --git a/ereuse_devicehub/migrations/versions/8334535d56fa_add_digital_passport_dpp.py b/ereuse_devicehub/migrations/versions/8334535d56fa_add_digital_passport_dpp.py index 9004724e..8d7d6cb6 100644 --- a/ereuse_devicehub/migrations/versions/8334535d56fa_add_digital_passport_dpp.py +++ b/ereuse_devicehub/migrations/versions/8334535d56fa_add_digital_passport_dpp.py @@ -1,7 +1,7 @@ """add digital passport dpp Revision ID: 8334535d56fa -Revises: 4b7f77f121bf +Revises: 93daff872771 Create Date: 2023-01-19 12:01:54.102326 """ @@ -12,7 +12,7 @@ from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. revision = '8334535d56fa' -down_revision = '4b7f77f121bf' +down_revision = '93daff872771' branch_labels = None depends_on = None @@ -59,18 +59,13 @@ def upgrade(): ), sa.ForeignKeyConstraint( ['issuer_id'], - [f'common.user.id'], + ['common.user.id'], ), sa.PrimaryKeyConstraint('id'), schema=f'{get_inv()}', ) # op.create_index(op.f('ix_proof_created'), 'proof', ['created'], unique=False, schema=f'{get_inv()}') # op.create_index(op.f('ix_proof_timestamp'), 'proof', ['timestamp'], unique=False, schema=f'{get_inv()}') - op.add_column( - 'device', - sa.Column('chid_dpp', citext.CIText(), nullable=True), - schema=f'{get_inv()}', - ) op.add_column( 'snapshot', sa.Column('phid_dpp', citext.CIText(), nullable=True), @@ -120,7 +115,7 @@ def upgrade(): ), sa.ForeignKeyConstraint( ['issuer_id'], - [f'common.user.id'], + ['common.user.id'], ), sa.Column('key', sa.Unicode(), nullable=False), sa.PrimaryKeyConstraint('id'), @@ -133,7 +128,6 @@ def downgrade(): op.drop_table('proof', schema=f'{get_inv()}') # op.drop_index(op.f('ix_proof_created'), table_name='proof', schema=f'{get_inv()}') # op.drop_index(op.f('ix_proof_timestamp'), table_name='proof', schema=f'{get_inv()}') - op.drop_column('device', 'chid_dpp', schema=f'{get_inv()}') op.drop_column('snapshot', 'phid_dpp', schema=f'{get_inv()}') op.drop_column('snapshot', 'json_wb', schema=f'{get_inv()}') op.drop_column('snapshot', 'json_hw', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index 31b91c7e..541c4309 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -11,11 +11,13 @@ Within the above general classes are subclasses in A order. """ import copy +import hashlib import json from collections import Iterable from contextlib import suppress from datetime import datetime, timedelta, timezone from decimal import ROUND_HALF_EVEN, ROUND_UP, Decimal +from operator import itemgetter from typing import Optional, Set, Union from uuid import uuid4 @@ -24,8 +26,9 @@ import teal.db from boltons import urlutils from citext import CIText from dateutil.tz import tzutc +from ereuseapi.methods import API from flask import current_app as app -from flask import g +from flask import g, session from sortedcontainers import SortedSet from sqlalchemy import JSON, BigInteger, Boolean, CheckConstraint, Column from sqlalchemy import Enum as DBEnum @@ -751,6 +754,78 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice): snapshots.append(s) return snapshots and 'update' or 'new_device' + def register_passport_dlt(self): + if 'trublo' not in app.blueprints.keys() or not self.hid: + return + + if not session.get('token_dlt'): + return + + token_dlt = session.get('token_dlt').split(".")[1] + api_dlt = app.config.get('API_DLT') + if not token_dlt or not api_dlt: + return + + api = API(api_dlt, token_dlt, "ethereum") + dpp = "{chid}:{phid}".format(chid=self.device.chid, phid=self.phid) + docSig = hashlib.sha3_256(self.json_wb.encode('utf-8')).hexdigest() + docID = "{}".format(self.uuid or '') + issuerID = "dh1:{user}".format(user=g.user.id) + api.issue_passport(dpp, docID, docSig, issuerID) + + # def register_dlt(self): + # from ereuse_devicehub.resources.did.models import PROOF_ENUM, Dpp, Proof + + # # Register device + # response = requests.post( + # "http://dlt.ereuse.org:3005/registerDevice", + # data={"DeviceCHID": self.device.chid}, + # ) + + # resp = json.loads(response.text) + # if 'Success' in resp['status']: + # timestamp = resp['data'].get('timestamp', time.time()) + # d = { + # "type": PROOF_ENUM['Register'], + # "device_id": self.device.id, + # "snapshot": self, + # "timestamp": timestamp, + # "issuer_id": g.user.id, + # } + # proof = Proof(**d) + # db.session.add(proof) + + # # Register a new Passport + # dpp = "{chid}:{phid}".format(chid=self.device.chid, phid=self.phid) + # if Dpp.query.filter_by(key=dpp).all(): + # return + + # issuerID = "dh1:{user}".format(user=g.user.id) + # documentId = hashlib.sha3_256(self.json_wb.encode('utf-8')).hexdigest() + # data = { + # "DeviceDPP": dpp, + # "IssuerID": issuerID, + # "DocumentID": documentId, + # "DocumentSignature": "", + # } + # response = requests.post("http://dlt.ereuse.org:3005/issuePassport", data=data) + # if not response.status_code == 201: + # return + # resp = json.loads(response.text) + # if 'Success' not in resp['status']: + # return + # timestamp = resp['data'].get('timestamp', time.time()) + # d_issue = { + # "device_id": self.device.id, + # "snapshot": self, + # "timestamp": timestamp, + # "issuer_id": g.user.id, + # "documentId": documentId, + # "key": dpp, + # } + # dpp_issue = Dpp(**d_issue) + # db.session.add(dpp_issue) + def __str__(self) -> str: return '{}. {} version {}.'.format(self.severity, self.software, self.version)