From 6f2e5ea73f9aae9364f0ba848953af79eb4a7032 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 6 Jan 2023 16:13:41 -0500 Subject: [PATCH 1/7] fix: error class improperly exported Signed-off-by: Daniel Bluhm --- pydid/__init__.py | 1 + pydid/doc/__init__.py | 1 + 2 files changed, 2 insertions(+) diff --git a/pydid/__init__.py b/pydid/__init__.py index b999927..55a3434 100644 --- a/pydid/__init__.py +++ b/pydid/__init__.py @@ -11,6 +11,7 @@ BaseDIDDocument, BasicDIDDocument, DIDDocument, + DIDDocumentError, NonconformantDocument, ) from .resource import Resource diff --git a/pydid/doc/__init__.py b/pydid/doc/__init__.py index 2d89b42..32a5826 100644 --- a/pydid/doc/__init__.py +++ b/pydid/doc/__init__.py @@ -6,6 +6,7 @@ DIDDocumentRoot, BasicDIDDocument, DIDDocument, + DIDDocumentError, ) from .builder import ( From f846391bed6865040ec3ca7c30407f30dd53e925 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 6 Jan 2023 16:14:07 -0500 Subject: [PATCH 2/7] fix: type hint on deserialize_document Signed-off-by: Daniel Bluhm --- pydid/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pydid/__init__.py b/pydid/__init__.py index 55a3434..16cf2e2 100644 --- a/pydid/__init__.py +++ b/pydid/__init__.py @@ -52,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: From 33bd2b1b4df717d27c12ad7d9c73806b87a89ed2 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 6 Jan 2023 16:14:37 -0500 Subject: [PATCH 3/7] feat!: publicKey to verificationMethod as correction BREAKING CHANGE: publicKey is no longer accepted by default. To achieve the same behavior, the document must have the correction created with this commit applied first. This can be done by calling it directly with the dictionary value of the document or by using `deserialize_document` with the correction method specified in the corrections list. See docs on pydid.doc.corrections.public_key_is_verification_method for examples. Signed-off-by: Daniel Bluhm --- pydid/doc/corrections.py | 26 ++++++++++++++++++++++++++ pydid/doc/doc.py | 12 ------------ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/pydid/doc/corrections.py b/pydid/doc/corrections.py index a8cc30c..b8a9dcd 100644 --- a/pydid/doc/corrections.py +++ b/pydid/doc/corrections.py @@ -46,3 +46,29 @@ 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"} + >>> doc_raw = pydid.corrections.public_key_is_verification_method(doc_raw) + + Or: + >>> import pydid + >>> doc_raw = {"@context": "http://example.com", "id": "did:example:123"} + >>> doc = pydid.deserialize_document( + ... doc, corrections=[pydid.corrections.public_key_is_verification_method] + ... ) + """ + + if "publicKey" in value: + value["verificationMethod"] = value["publicKey"] + return value diff --git a/pydid/doc/doc.py b/pydid/doc/doc.py index d442f82..6b16d33 100644 --- a/pydid/doc/doc.py +++ b/pydid/doc/doc.py @@ -58,18 +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.""" From 6e672a8b72a325cb36f656f8f4cd81e39ae5f710 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 6 Jan 2023 16:39:17 -0500 Subject: [PATCH 4/7] docs: update examples in corrections Signed-off-by: Daniel Bluhm --- pydid/doc/corrections.py | 18 ++++++++++++++---- pyproject.toml | 2 +- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/pydid/doc/corrections.py b/pydid/doc/corrections.py index b8a9dcd..fc744d4 100644 --- a/pydid/doc/corrections.py +++ b/pydid/doc/corrections.py @@ -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: @@ -58,15 +58,25 @@ def public_key_is_verification_method(value: dict) -> dict: with the pydid.deserialize_document method: >>> import pydid - >>> doc_raw = {"@context": "http://example.com", "id": "did:example:123"} + >>> 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 Or: >>> import pydid - >>> doc_raw = {"@context": "http://example.com", "id": "did:example:123"} + >>> doc_raw = { + ... "@context": "http://example.com", + ... "id": "did:example:123", + ... "publicKey": [{"id": "did:example:123#key-1"}], + ... } >>> doc = pydid.deserialize_document( - ... doc, corrections=[pydid.corrections.public_key_is_verification_method] + ... doc_raw, corrections=[pydid.corrections.public_key_is_verification_method] ... ) + >>> assert doc.verification_method """ if "publicKey" in value: diff --git a/pyproject.toml b/pyproject.toml index d423424..6bad172 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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] From f07e0228e26067dfe83c1c383d2830d0cc18bb62 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Fri, 6 Jan 2023 16:42:26 -0500 Subject: [PATCH 5/7] fix: formatting and unused imports Signed-off-by: Daniel Bluhm --- commitlint.config.js => .commitlint.config.js | 0 .pre-commit-config.yaml | 6 +++--- pydid/doc/doc.py | 3 ++- 3 files changed, 5 insertions(+), 4 deletions(-) rename commitlint.config.js => .commitlint.config.js (100%) diff --git a/commitlint.config.js b/.commitlint.config.js similarity index 100% rename from commitlint.config.js rename to .commitlint.config.js diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 23f8398..1a8c164 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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://gitlab.com/pycqa/flake8 + rev: 3.9.0 hooks: - id: flake8 - additional_dependencies: ['toml'] stages: [commit] diff --git a/pydid/doc/doc.py b/pydid/doc/doc.py index 6b16d33..dac97d2 100644 --- a/pydid/doc/doc.py +++ b/pydid/doc/doc.py @@ -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 @@ -58,6 +58,7 @@ def _listify(cls, value): return value return [value] + class BaseDIDDocument(DIDDocumentRoot, IndexedResource, ABC): """Abstract BaseDIDDocument class.""" From f9056a3de2d2035aee8e828582603e83ed8539c9 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 24 Aug 2023 14:23:26 -0400 Subject: [PATCH 6/7] fix: flake8 pre-commit Signed-off-by: Daniel Bluhm --- .pre-commit-config.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 1a8c164..d770c33 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -12,7 +12,7 @@ repos: hooks: - id: black stages: [commit] - - repo: https://gitlab.com/pycqa/flake8 + - repo: https://github.com/pycqa/flake8 rev: 3.9.0 hooks: - id: flake8 From 5fc5dbe2cfbd6af23a2d0c94b5360752f8bf1664 Mon Sep 17 00:00:00 2001 From: Daniel Bluhm Date: Thu, 24 Aug 2023 14:26:09 -0400 Subject: [PATCH 7/7] fix: public key correction removes public key Signed-off-by: Daniel Bluhm --- pydid/doc/corrections.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pydid/doc/corrections.py b/pydid/doc/corrections.py index fc744d4..a543cdb 100644 --- a/pydid/doc/corrections.py +++ b/pydid/doc/corrections.py @@ -65,6 +65,7 @@ def public_key_is_verification_method(value: dict) -> dict: ... } >>> 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 @@ -80,5 +81,5 @@ def public_key_is_verification_method(value: dict) -> dict: """ if "publicKey" in value: - value["verificationMethod"] = value["publicKey"] + value["verificationMethod"] = value.pop("publicKey") return value