Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix!: publicKey to verificationMethod correction, minor fixes #58

Merged
merged 7 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ repos:
hooks:
- id: commitlint
stages: [commit-msg]
args: ["--config", ".commitlint.config.js"]
additional_dependencies: ['@commitlint/config-conventional']
- repo: https://github.com/psf/black
rev: 23.7.0
hooks:
- id: black
stages: [commit]
- repo: https://gitlab.com/retnikt/flake9
rev: 3.8.3.post1
- repo: https://github.com/pycqa/flake8
rev: 3.9.0
hooks:
- id: flake8
additional_dependencies: ['toml']
stages: [commit]
3 changes: 2 additions & 1 deletion pydid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
BaseDIDDocument,
BasicDIDDocument,
DIDDocument,
DIDDocumentError,
NonconformantDocument,
)
from .resource import Resource
Expand Down Expand Up @@ -51,7 +52,7 @@ def deserialize_document(
corrections: Optional[List[Callable]] = None,
*,
strict: bool = False,
cls: Type[BaseDIDDocument] = None,
cls: Optional[Type[BaseDIDDocument]] = None,
) -> BaseDIDDocument:
"""Deserialize a document from a dictionary."""
if corrections:
Expand Down
1 change: 1 addition & 0 deletions pydid/doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
DIDDocumentRoot,
BasicDIDDocument,
DIDDocument,
DIDDocumentError,
)

from .builder import (
Expand Down
39 changes: 38 additions & 1 deletion pydid/doc/corrections.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def insert_missing_ids(value: dict) -> dict:
>>> import pydid
>>> doc_raw = {"@context": "http://example.com", "id": "did:example:123"}
>>> doc = pydid.deserialize_document(
... doc, corrections=[pydid.corrections.insert_missing_ids]
... doc_raw, corrections=[pydid.corrections.insert_missing_ids]
... )
"""
if "id" not in value:
Expand All @@ -46,3 +46,40 @@ def _walk(value: Union[str, list, dict]):
_walk(nested)

return value


def public_key_is_verification_method(value: dict) -> dict:
"""Transform publicKey to verificationMethod.

This is helpful if dealing with DID resolvers that have not updated to the
latest DID Spec.

This correction can be applied directly to a raw document dictionary or
with the pydid.deserialize_document method:

>>> import pydid
>>> doc_raw = {
... "@context": "http://example.com",
... "id": "did:example:123",
... "publicKey": [{"id": "did:example:123#key-1"}],
... }
>>> doc_raw = pydid.corrections.public_key_is_verification_method(doc_raw)
>>> assert "verificationMethod" in doc_raw
>>> assert "publicKey" not in doc_raw

Or:
>>> import pydid
>>> doc_raw = {
... "@context": "http://example.com",
... "id": "did:example:123",
... "publicKey": [{"id": "did:example:123#key-1"}],
... }
>>> doc = pydid.deserialize_document(
... doc_raw, corrections=[pydid.corrections.public_key_is_verification_method]
... )
>>> assert doc.verification_method
"""

if "publicKey" in value:
value["verificationMethod"] = value.pop("publicKey")
return value
13 changes: 1 addition & 12 deletions pydid/doc/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from abc import ABC
from typing import Any, List, Optional, Union

from pydantic import Field, root_validator, validator
from pydantic import Field, validator
from typing_extensions import Annotated

from ..did import DID, InvalidDIDError
Expand Down Expand Up @@ -58,17 +58,6 @@ def _listify(cls, value):
return value
return [value]

@root_validator(pre=True, allow_reuse=True)
@classmethod
def _allow_publickey_instead_of_verification_method(cls, values: dict):
"""Accept publickKey, transforming to verificationMethod.

This validator handles a common DID Document mutation.
"""
if "publicKey" in values:
values["verificationMethod"] = values["publicKey"]
return values


class BaseDIDDocument(DIDDocumentRoot, IndexedResource, ABC):
"""Abstract BaseDIDDocument class."""
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pytest-asyncio = "^0.21.0"
coverage = {extras = ["toml"], version = "^7.2.0"}

[tool.pytest.ini_options]
addopts = "-p no:warnings --cov pydid"
addopts = "-p no:warnings --cov pydid --doctest-modules"
markers = "int: integration tests"

[tool.coverage.report]
Expand Down