Skip to content

Commit

Permalink
feat: realign service with DID Core spec
Browse files Browse the repository at this point in the history
This PR realigns the service endpoint definitions to what is outlined in
the DID Core spec. Specifically, permitting the service endpoint to be a
string, map, or set of strings and/or maps. DIDs, DID URLs, general
URLs, and now any string is now permitted. This is to support specifying
arbitrary URIs. The rules for parsing URIs would be overly complicated
to fully validate; slightly greater flexibility is preferrable to
maintaining large regexes.

Signed-off-by: Daniel Bluhm <dbluhm@pm.me>
  • Loading branch information
dbluhm committed Sep 22, 2023
1 parent b7e6063 commit 1c69aed
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
23 changes: 13 additions & 10 deletions pydid/service.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
"""DID Doc Service."""

from typing import List, Optional, Union

from pydantic import Extra, AnyUrl
from typing import Any, List, Mapping, Optional, Union
from typing_extensions import Literal

from pydantic import AnyUrl, Extra, StrictStr

from .did import DID
from .did_url import DIDUrl
from .resource import Resource


class ServiceEndpoint(Resource):
"""List of Service Endpoints"""

uri: Union[DIDUrl, AnyUrl]
EndpointStrings = Union[DID, DIDUrl, AnyUrl, StrictStr]


class Service(Resource):
"""Representation of DID Document Services."""

id: DIDUrl
type: str
service_endpoint: Union[DIDUrl, AnyUrl, Literal[""], List[ServiceEndpoint]]
service_endpoint: Union[
EndpointStrings,
List[Union[EndpointStrings, Mapping[str, Any]]],
Mapping[str, Any],
]


class DIDCommV1Service(Service):
Expand All @@ -34,6 +36,7 @@ class Config:
type: Literal[
"IndyAgent", "did-communication", "DIDCommMessaging"
] = "did-communication"
service_endpoint: EndpointStrings
recipient_keys: List[DIDUrl]
routing_keys: List[DIDUrl] = []
accept: Optional[List[str]] = None
Expand All @@ -43,10 +46,10 @@ class Config:
DIDCommService = DIDCommV1Service


class DIDCommV2ServiceEndpoint(ServiceEndpoint):
class DIDCommV2ServiceEndpoint(Resource):
"""DID Communication Service Endpoint."""

uri: Union[DIDUrl, AnyUrl]
uri: EndpointStrings
accept: Optional[List[str]] = None
routing_keys: List[DIDUrl] = []

Expand Down
44 changes: 39 additions & 5 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@
"routingKeys": ["did:example:somemediator#somekey1"],
"accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"],
},
{
"id": "did:example:123#didcomm-1",
"type": "did-communication",
"serviceEndpoint": "didcomm:transport/queue",
"recipientKeys": ["did:example:123#keys-1"],
"routingKeys": [],
"accept": ["didcomm/aip2;env=rfc587"],
},
{
"id": "did:example:123456789abcdefghi#didcomm-1",
"type": "DIDCommMessaging",
Expand Down Expand Up @@ -64,11 +72,6 @@
"type": "LinkedDomains",
"serviceEndpoint": True,
},
{
"id": "did:example:123456789abcdefghi#didcomm-1",
"type": "DIDCommMessaging",
"serviceEndpoint": ["http://example.com"],
},
]


Expand Down Expand Up @@ -127,6 +130,15 @@ def test_serialization(service_raw):
"priority": 0,
"accept": ["didcomm/v2", "didcomm/aip2;env=rfc587"],
},
{
"id": "did:example:123#didcomm-1",
"type": "did-communication",
"serviceEndpoint": "didcomm:transport/queue",
"recipientKeys": ["did:example:123#keys-1"],
"routingKeys": [],
"priority": 0,
"accept": ["didcomm/aip2;env=rfc587"],
},
{
"id": "did:example:123456789abcdefghi#didcomm-1",
"type": "DIDCommMessaging",
Expand All @@ -138,6 +150,28 @@ def test_serialization(service_raw):
}
],
},
{
"id": "did:example:123456789abcdefghi#didcomm-1",
"type": "DIDCommMessaging",
"serviceEndpoint": [
{
"uri": "didcomm:transport/queue",
"accept": ["didcomm/v2"],
"routingKeys": [],
}
],
},
{
"id": "did:example:123456789abcdefghi#didcomm-1",
"type": "DIDCommMessaging",
"serviceEndpoint": [
{
"uri": "did:example:mediator",
"accept": ["didcomm/v2"],
"routingKeys": [],
}
],
},
]

DIDCOMM_INVALID_SERVICES = [
Expand Down

0 comments on commit 1c69aed

Please sign in to comment.