From 6cf289f5b125e3d0618dbe87b831bc94de415a6d Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 14 Nov 2023 09:48:36 +0100 Subject: [PATCH] new way for to get a new did --- idhub/admin/views.py | 3 +-- idhub/models.py | 12 +++++++++++- idhub/user/views.py | 3 +-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/idhub/admin/views.py b/idhub/admin/views.py index 0492f5d..786b0be 100644 --- a/idhub/admin/views.py +++ b/idhub/admin/views.py @@ -20,7 +20,6 @@ from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.http import HttpResponse from django.contrib import messages -from utils.apiregiter import iota from utils import credtools from idhub_auth.models import User from idhub.mixins import AdminView @@ -511,7 +510,7 @@ class DidRegisterView(Credentials, CreateView): def form_valid(self, form): form.instance.user = self.request.user - form.instance.did = iota.issue_did() + form.instance.set_did() form.save() messages.success(self.request, _('DID created successfully')) return super().form_valid(form) diff --git a/idhub/models.py b/idhub/models.py index bb9b4ae..1c0c8e8 100644 --- a/idhub/models.py +++ b/idhub/models.py @@ -2,6 +2,7 @@ import json import requests from django.db import models from django.utils.translation import gettext_lazy as _ +from idhub_ssikit import generate_did_controller_key from idhub_auth.models import User @@ -14,7 +15,6 @@ from idhub_auth.models import User class DID(models.Model): created_at = models.DateTimeField(auto_now=True) - did = models.CharField(max_length=250, unique=True) label = models.CharField(max_length=50) # In JWK format. Must be stored as-is and passed whole to library functions. # Example key material: @@ -33,6 +33,16 @@ class DID(models.Model): return True return False + @property + def did(self): + return self.get_key().get("d") + + def set_did(self): + self.key_material = idhub_ssikit.generate_did_controller_key() + + def get_key(self): + return json.loads(self.key_material) + class Schemas(models.Model): file_schema = models.CharField(max_length=250) diff --git a/idhub/user/views.py b/idhub/user/views.py index f45f7e6..02c0cfe 100644 --- a/idhub/user/views.py +++ b/idhub/user/views.py @@ -12,7 +12,6 @@ from django.shortcuts import get_object_or_404, redirect from django.urls import reverse_lazy from django.http import HttpResponse from django.contrib import messages -from utils.apiregiter import iota from idhub.user.forms import ProfileForm, RequestCredentialForm, CredentialPresentationForm from idhub.mixins import UserView from idhub.models import DID, VerificableCredential @@ -183,7 +182,7 @@ class DidRegisterView(MyWallet, CreateView): def form_valid(self, form): form.instance.user = self.request.user - form.instance.did = iota.issue_did() + form.instance.set_did() form.save() messages.success(self.request, _('DID created successfully')) return super().form_valid(form)