diff --git a/idhub/management/commands/insert_dids_to_dlt_register.py b/idhub/management/commands/insert_dids_to_dlt_register.py new file mode 100644 index 0000000..b2c3d95 --- /dev/null +++ b/idhub/management/commands/insert_dids_to_dlt_register.py @@ -0,0 +1,49 @@ +import requests + +from django.conf import settings +from django.core.management.base import BaseCommand +from django.contrib.auth import get_user_model +from idhub.models import Schemas, DID + + +User = get_user_model() + + +class Command(BaseCommand): + help = "Insert in dlt register pairs of did:schema_id" + + def handle(self, *args, **kwargs): + self.save_in_verificable_register() + + def save_in_verificable_register(self): + if not settings.API_TOKEN: + return + + for did in DID.objects.filter(user__isnull=True): + hashes = self.get_verificable_register(did) + for schema in Schemas.objects.all(): + schema_id = schema.get_id() + if not schema_id in settings.RESTRICTED_ISSUANCE_CREDENTIAL_TYPES: + continue + + if schema_id in hashes: + continue + + data = { + "api_token": settings.API_TOKEN, + "did": did.did, + "hash": schema_id + } + requests.post( + settings.VERIFIABLE_REGISTER_URL + '/registerPair', + data=data + ) + + def get_verificable_register(self, did): + response = requests.get( + settings.VERIFIABLE_REGISTER_URL + '/readPairs', + params={'did': did.did} + ) + response.raise_for_status() + + return response.json()['hashes'] diff --git a/idhub/models.py b/idhub/models.py index e68c905..8a5a163 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -3,6 +3,7 @@ import ujson import pytz import hashlib import datetime +import requests from collections import OrderedDict from django.db import models from django.conf import settings @@ -493,6 +494,42 @@ class DID(models.Model): def get_organization(self): return Organization.objects.get(main=True) + def save_in_verificable_register(self): + if not settings.API_TOKEN: + return + + if self.user: + return + + hashes = self.get_verificable_register() + for schema in Schemas.objects.all(): + schema_id = schema.get_id() + if not schema_id in settings.RESTRICTED_ISSUANCE_CREDENTIAL_TYPES: + continue + + schema_hash = schema.get_hash_id() + if schema_hash in hashes: + continue + + data = { + "api_token": settings.API_TOKEN, + "did": self.did, + "hash": schema_hash + } + requests.post( + settings.VERIFIABLE_REGISTER_URL + '/registerPair', + data=data + ) + + def get_verificable_register(self): + response = requests.get( + settings.VERIFIABLE_REGISTER_URL + '/readPairs', + params={'did': self.did} + ) + response.raise_for_status() + + return response.json()['hashes'] + class Schemas(models.Model): type = models.CharField(max_length=250) file_schema = models.CharField(_('Schema'), max_length=250) @@ -592,6 +629,13 @@ class Schemas(models.Model): def get_data(self): return json.loads(self.data) + def get_id(self): + return self.get_data().get("$id") + + def get_hash_id(self): + id = self.get_data().get("$id") + return hashlib.sha3_256(id.encode("utf-8")).hexdigest() + class VerificableCredential(models.Model): """ diff --git a/trustchain_idhub/settings.py b/trustchain_idhub/settings.py index 473fa24..6b1c2bc 100644 --- a/trustchain_idhub/settings.py +++ b/trustchain_idhub/settings.py @@ -241,4 +241,6 @@ ENABLE_EMAIL = config('ENABLE_EMAIL', default=True, cast=bool) CREATE_TEST_USERS = config('CREATE_TEST_USERS', default=False, cast=bool) ENABLE_2FACTOR_AUTH = config('ENABLE_2FACTOR_AUTH', default=True, cast=bool) COMMIT = config('COMMIT', default='') - +API_TOKEN = config("API_TOKEN", '') +VERIFIABLE_REGISTER_URL = config("VERIFIABLE_REGISTER_URL") +RESTRICTED_ISSUANCE_CREDENTIAL_TYPES = config('RESTRICTED_ISSUANCE_CREDENTIAL_TYPES', default=[], cast=literal_eval)