-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from decentralized-identity/tdw-init
Initial did log creation
- Loading branch information
Showing
15 changed files
with
215 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Union, List, Dict, Any | ||
from pydantic import BaseModel, Field | ||
from .did_document import DidDocument | ||
from .di_proof import DataIntegrityProof | ||
|
||
|
||
class BaseModel(BaseModel): | ||
def model_dump(self, **kwargs) -> Dict[str, Any]: | ||
return super().model_dump(by_alias=True, exclude_none=True, **kwargs) | ||
|
||
|
||
class Witness(BaseModel): | ||
id: str = Field(None) | ||
weight: int = Field(None) | ||
|
||
|
||
class WitnessParam(BaseModel): | ||
threshold: int = Field(None) | ||
selfWeight: int = Field(None) | ||
witnesses: List[Witness] = Field(None) | ||
|
||
|
||
class LogParameters(BaseModel): | ||
prerotation: bool = Field(None) | ||
portable: bool = Field(None) | ||
updateKeys: List[str] = Field(None) | ||
nextKeyHashes: List[str] = Field(None) | ||
witness: WitnessParam = Field(None) | ||
deactivated: bool = Field(None) | ||
ttl: bool = Field(None) | ||
method: str = Field(None) | ||
scid: str = Field(None) | ||
|
||
|
||
class InitialLogEntry(BaseModel): | ||
versionId: str = Field() | ||
versionTime: str = Field() | ||
parameters: LogParameters = Field() | ||
state: dict = Field() | ||
proof: Union[DataIntegrityProof, List[DataIntegrityProof]] = Field(None) | ||
|
||
|
||
class LogEntry(BaseModel): | ||
versionId: str = Field() | ||
versionTime: str = Field() | ||
parameters: LogParameters = Field() | ||
state: DidDocument = Field() | ||
proof: Union[DataIntegrityProof, List[DataIntegrityProof]] = Field(None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,4 @@ | ||
from .askar import AskarStorage, AskarVerifier | ||
from .trust_did_web import TrustDidWeb | ||
|
||
__all__ = [ | ||
"AskarVerifier", | ||
"AskarStorage", | ||
"TrustDidWeb" | ||
] | ||
__all__ = ["AskarVerifier", "AskarStorage", "TrustDidWeb"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,103 +1,50 @@ | ||
from config import settings | ||
from datetime import datetime, timezone | ||
from datetime import datetime | ||
from app.models.did_log import LogParameters, InitialLogEntry | ||
import canonicaljson | ||
import json | ||
from multiformats import multihash, multibase | ||
|
||
|
||
class TrustDidWeb: | ||
def __init__(self): | ||
self.did_string_base = r'did:tdw:{SCID}:'+settings.DOMAIN | ||
|
||
def _define_parameters(self, update_key=None, next_key=None, ttl=100): | ||
self.method_version = "did:tdw:0.4" | ||
self.did_string_base = r"did:tdw:{SCID}:" + settings.DOMAIN | ||
|
||
def _init_parameters(self, update_key, next_key=None, ttl=100): | ||
# https://identity.foundation/trustdidweb/#generate-scid | ||
parameters = { | ||
"method": 'did:tdw:0.3', | ||
"scid": r"{SCID}", | ||
"updateKeys": [update_key], | ||
"portable": False, | ||
"prerotation": False, | ||
"nextKeyHashes": [], | ||
# "witness": {}, | ||
"deactivated": False, | ||
"ttl": ttl, | ||
} | ||
parameters = LogParameters( | ||
method=self.method_version, scid=r"{SCID}", updateKeys=[update_key] | ||
) | ||
return parameters | ||
|
||
def _generate_entry_hash(self, log_entry): | ||
# https://identity.foundation/trustdidweb/#generate-entry-hash | ||
jcs = canonicaljson.encode_canonical_json(log_entry) | ||
multihashed = multihash.digest(jcs, 'sha2-256') | ||
encoded = multibase.encode(multihashed, 'base58btc')[1:] | ||
return encoded | ||
|
||
|
||
def _init_state(self, did_doc): | ||
return json.loads(json.dumps(did_doc).replace("did:web:", r"did:tdw:{SCID}:")) | ||
|
||
def _generate_scid(self, log_entry): | ||
# https://identity.foundation/trustdidweb/#generate-scid | ||
jcs = canonicaljson.encode_canonical_json(log_entry) | ||
multihashed = multihash.digest(jcs, 'sha2-256') | ||
encoded = multibase.encode(multihashed, 'base58btc')[1:] | ||
multihashed = multihash.digest(jcs, "sha2-256") | ||
encoded = multibase.encode(multihashed, "base58btc")[1:] | ||
return encoded | ||
|
||
def _add_placeholder_scid(self, item): | ||
if isinstance(item, str): | ||
return item.replace('did:web:', r'did:tdw:{SCID}:') | ||
elif isinstance(item, list): | ||
item['id'].replace('did:web:', r'did:tdw:{SCID}:') | ||
return item | ||
else: | ||
pass | ||
|
||
def _web_to_tdw(self, did_doc): | ||
did_doc['id'] = self._add_placeholder_scid(did_doc['id']) | ||
for idx, item in enumerate(did_doc['verificationMethod']): | ||
did_doc['verificationMethod'][idx] = self._add_placeholder_scid(did_doc['verificationMethod'][idx]) | ||
|
||
def _init_parameters(self, update_key): | ||
return { | ||
"method": 'did:tdw:0.3', | ||
"scid": r"{SCID}", | ||
"updateKeys": [update_key], | ||
"portable": False, | ||
"prerotation": False, | ||
"nextKeyHashes": [], | ||
"deactivated": False, | ||
} | ||
|
||
def _init_did_doc(self): | ||
return { | ||
"@context": [], | ||
"id": r"{SCID}", | ||
} | ||
|
||
def provision_log_entry(self, did_doc, update_key): | ||
did_doc = json.loads(json.dumps(did_doc).replace('did:web:', r'did:tdw:{SCID}:')) | ||
preliminary_did_log_entry = [ | ||
r'{SCID}', | ||
str(datetime.now(timezone.utc).isoformat("T", "seconds")), | ||
self._init_parameters(update_key=update_key), | ||
{ | ||
"value": did_doc | ||
} | ||
] | ||
scid = self._generate_scid(preliminary_did_log_entry) | ||
log_entry = json.loads(json.dumps(preliminary_did_log_entry).replace('{SCID}', scid)) | ||
entry_hash = self._generate_entry_hash(log_entry) | ||
log_entry[0] = f'1-{entry_hash}' | ||
return log_entry | ||
|
||
def create(self, did_doc): | ||
|
||
def _generate_entry_hash(self, log_entry): | ||
# https://identity.foundation/trustdidweb/#generate-entry-hash | ||
jcs = canonicaljson.encode_canonical_json(log_entry) | ||
multihashed = multihash.digest(jcs, "sha2-256") | ||
encoded = multibase.encode(multihashed, "base58btc")[1:] | ||
return encoded | ||
|
||
def create(self, did_doc, update_key): | ||
# https://identity.foundation/trustdidweb/#create-register | ||
did_string = did_doc['id'].replace('did:web:', r'did:tdw:{SCID}:') | ||
authorized_keys = [] | ||
initial_did_doc = self._web_to_tdw(did_doc) | ||
parameters = self._define_parameters(update_key=did_doc['verificaitonMethod'][0]['publicKeyMultibase']) | ||
did_log_entry = [ | ||
r'{SCID}', | ||
str(datetime.now().isoformat('T', 'seconds')), | ||
parameters, | ||
{"value": initial_did_doc} | ||
] | ||
scid = self._generate_scid(did_log_entry) | ||
did_log_entry = json.loads(json.dumps(did_log_entry).replace('{SCID}', scid)) | ||
log_entry_hash = self._generate_entry_hash(did_log_entry) | ||
did_log_entry[0] = f'1-{log_entry_hash}' | ||
log_entry = InitialLogEntry( | ||
versionId=r"{SCID}", | ||
versionTime=str(datetime.now().isoformat("T", "seconds")), | ||
parameters=self._init_parameters(update_key=update_key), | ||
state=self._init_state(did_doc), | ||
).model_dump() | ||
scid = self._generate_scid(log_entry) | ||
log_entry = json.loads(json.dumps(log_entry).replace("{SCID}", scid)) | ||
log_entry_hash = self._generate_entry_hash(log_entry) | ||
log_entry["versionId"] = f"1-{log_entry_hash}" | ||
return log_entry |
Oops, something went wrong.