add registar issuer passport

This commit is contained in:
Cayo Puigdefabregas 2023-01-20 18:58:25 +01:00
parent fbe8600cc1
commit 574ab36da4
2 changed files with 80 additions and 11 deletions

View file

@ -1,7 +1,7 @@
"""add digital passport dpp """add digital passport dpp
Revision ID: 8334535d56fa Revision ID: 8334535d56fa
Revises: 4b7f77f121bf Revises: 93daff872771
Create Date: 2023-01-19 12:01:54.102326 Create Date: 2023-01-19 12:01:54.102326
""" """
@ -12,7 +12,7 @@ from sqlalchemy.dialects import postgresql
# revision identifiers, used by Alembic. # revision identifiers, used by Alembic.
revision = '8334535d56fa' revision = '8334535d56fa'
down_revision = '4b7f77f121bf' down_revision = '93daff872771'
branch_labels = None branch_labels = None
depends_on = None depends_on = None
@ -59,18 +59,13 @@ def upgrade():
), ),
sa.ForeignKeyConstraint( sa.ForeignKeyConstraint(
['issuer_id'], ['issuer_id'],
[f'common.user.id'], ['common.user.id'],
), ),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
schema=f'{get_inv()}', 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_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.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( op.add_column(
'snapshot', 'snapshot',
sa.Column('phid_dpp', citext.CIText(), nullable=True), sa.Column('phid_dpp', citext.CIText(), nullable=True),
@ -120,7 +115,7 @@ def upgrade():
), ),
sa.ForeignKeyConstraint( sa.ForeignKeyConstraint(
['issuer_id'], ['issuer_id'],
[f'common.user.id'], ['common.user.id'],
), ),
sa.Column('key', sa.Unicode(), nullable=False), sa.Column('key', sa.Unicode(), nullable=False),
sa.PrimaryKeyConstraint('id'), sa.PrimaryKeyConstraint('id'),
@ -133,7 +128,6 @@ def downgrade():
op.drop_table('proof', schema=f'{get_inv()}') 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_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_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', 'phid_dpp', schema=f'{get_inv()}')
op.drop_column('snapshot', 'json_wb', schema=f'{get_inv()}') op.drop_column('snapshot', 'json_wb', schema=f'{get_inv()}')
op.drop_column('snapshot', 'json_hw', schema=f'{get_inv()}') op.drop_column('snapshot', 'json_hw', schema=f'{get_inv()}')

View file

@ -11,11 +11,13 @@ Within the above general classes are subclasses in A order.
""" """
import copy import copy
import hashlib
import json import json
from collections import Iterable from collections import Iterable
from contextlib import suppress from contextlib import suppress
from datetime import datetime, timedelta, timezone from datetime import datetime, timedelta, timezone
from decimal import ROUND_HALF_EVEN, ROUND_UP, Decimal from decimal import ROUND_HALF_EVEN, ROUND_UP, Decimal
from operator import itemgetter
from typing import Optional, Set, Union from typing import Optional, Set, Union
from uuid import uuid4 from uuid import uuid4
@ -24,8 +26,9 @@ import teal.db
from boltons import urlutils from boltons import urlutils
from citext import CIText from citext import CIText
from dateutil.tz import tzutc from dateutil.tz import tzutc
from ereuseapi.methods import API
from flask import current_app as app from flask import current_app as app
from flask import g from flask import g, session
from sortedcontainers import SortedSet from sortedcontainers import SortedSet
from sqlalchemy import JSON, BigInteger, Boolean, CheckConstraint, Column from sqlalchemy import JSON, BigInteger, Boolean, CheckConstraint, Column
from sqlalchemy import Enum as DBEnum from sqlalchemy import Enum as DBEnum
@ -751,6 +754,78 @@ class Snapshot(JoinedWithOneDeviceMixin, ActionWithOneDevice):
snapshots.append(s) snapshots.append(s)
return snapshots and 'update' or 'new_device' 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: def __str__(self) -> str:
return '{}. {} version {}.'.format(self.severity, self.software, self.version) return '{}. {} version {}.'.format(self.severity, self.software, self.version)