add insert dids to dlt register

This commit is contained in:
Cayo Puigdefabregas 2024-06-17 11:07:51 +02:00
parent 4a0f234e6c
commit ec6e63e3ea
3 changed files with 96 additions and 1 deletions

View file

@ -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']

View file

@ -3,6 +3,7 @@ import ujson
import pytz import pytz
import hashlib import hashlib
import datetime import datetime
import requests
from collections import OrderedDict from collections import OrderedDict
from django.db import models from django.db import models
from django.conf import settings from django.conf import settings
@ -493,6 +494,42 @@ class DID(models.Model):
def get_organization(self): def get_organization(self):
return Organization.objects.get(main=True) 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): class Schemas(models.Model):
type = models.CharField(max_length=250) type = models.CharField(max_length=250)
file_schema = models.CharField(_('Schema'), max_length=250) file_schema = models.CharField(_('Schema'), max_length=250)
@ -592,6 +629,13 @@ class Schemas(models.Model):
def get_data(self): def get_data(self):
return json.loads(self.data) 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): class VerificableCredential(models.Model):
""" """

View file

@ -241,4 +241,6 @@ ENABLE_EMAIL = config('ENABLE_EMAIL', default=True, cast=bool)
CREATE_TEST_USERS = config('CREATE_TEST_USERS', default=False, cast=bool) CREATE_TEST_USERS = config('CREATE_TEST_USERS', default=False, cast=bool)
ENABLE_2FACTOR_AUTH = config('ENABLE_2FACTOR_AUTH', default=True, cast=bool) ENABLE_2FACTOR_AUTH = config('ENABLE_2FACTOR_AUTH', default=True, cast=bool)
COMMIT = config('COMMIT', default='') 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)