Skip to content

Commit

Permalink
feat: for legacy support, continue using raw routing keys
Browse files Browse the repository at this point in the history
In connection protocol.

Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Oct 16, 2023
1 parent e5695b1 commit 756b513
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
29 changes: 27 additions & 2 deletions aries_cloudagent/connections/models/diddoc/diddoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from typing import List, Sequence, Union

from ....did.did_key import DIDKey

from .publickey import PublicKey, PublicKeyType
from .service import Service
from .util import canon_did, canon_ref, ok_did, resource
Expand Down Expand Up @@ -116,13 +118,36 @@ def set(self, item: Union[Service, PublicKey]) -> "DIDDoc":
"Cannot add item {} to DIDDoc on DID {}".format(item, self.did)
)

def serialize(self) -> dict:
@staticmethod
def _normalize_routing_keys(service: dict) -> dict:
"""Normalize routing keys in service.
Args:
service: service dict
Returns: service dict with routing keys normalized
"""
routing_keys = service.get("routingKeys")
if routing_keys:
routing_keys = [
DIDKey.from_did(key).public_key_b58
if key.startswith("did:key:")
else key
for key in routing_keys
]
service["routingKeys"] = routing_keys
return service

def serialize(self, normalize_routing_keys: bool = False) -> dict:
"""Dump current object to a JSON-compatible dictionary.
Returns:
dict representation of current DIDDoc
"""
service = [service.to_dict() for service in self.service.values()]
if normalize_routing_keys:
service = [self._normalize_routing_keys(s) for s in service]

return {
"@context": DIDDoc.CONTEXT,
Expand All @@ -136,7 +161,7 @@ def serialize(self) -> dict:
for pubkey in self.pubkey.values()
if pubkey.authn
],
"service": [service.to_dict() for service in self.service.values()],
"service": service,
}

def to_json(self) -> str:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
from typing import Sequence
from urllib.parse import parse_qs, urljoin, urlparse

from marshmallow import EXCLUDE, ValidationError, fields, validates_schema
from marshmallow import EXCLUDE, ValidationError, fields, pre_load, validates_schema

from .....did.did_key import DIDKey
from .....messaging.agent_message import AgentMessage, AgentMessageSchema
from .....messaging.valid import (
GENERIC_DID_EXAMPLE,
Expand Down Expand Up @@ -58,6 +59,16 @@ def __init__(
self.recipient_keys = list(recipient_keys) if recipient_keys else None
self.endpoint = endpoint
self.routing_keys = list(routing_keys) if routing_keys else None
self.routing_keys = (
[
DIDKey.from_did(key).public_key_b58
if key.startswith("did:key:")
else key
for key in self.routing_keys
]
if self.routing_keys
else None
)
self.image_url = image_url

def to_url(self, base_url: str = None) -> str:
Expand Down Expand Up @@ -157,6 +168,19 @@ class Meta:
},
)

@pre_load
def transform_routing_keys(self, data, **kwargs):
"""Transform routingKeys from did:key refs, if necessary."""
routing_keys = data.get("routingKeys")
if routing_keys:
data["routingKeys"] = [
DIDKey.from_did(key).public_key_b58
if key.startswith("did:key:")
else key
for key in routing_keys
]
return data

@validates_schema
def validate_fields(self, data, **kwargs):
"""Validate schema fields.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class DIDDocWrapper(fields.Field):
"""Field that loads and serializes DIDDoc."""

def _serialize(self, value, attr, obj, **kwargs):
def _serialize(self, value: DIDDoc, attr, obj, **kwargs):
"""Serialize the DIDDoc.
Args:
Expand All @@ -20,7 +20,7 @@ def _serialize(self, value, attr, obj, **kwargs):
The serialized DIDDoc
"""
return value.serialize()
return value.serialize(normalize_routing_keys=True)

def _deserialize(self, value, attr=None, data=None, **kwargs):
"""Deserialize a value into a DIDDoc.
Expand Down

0 comments on commit 756b513

Please sign in to comment.