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

feat: 00B v1.1 support #1962

Merged
merged 7 commits into from
Oct 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions aries_cloudagent/connections/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""

import logging
from typing import List, Sequence, Tuple
from typing import Optional, List, Sequence, Tuple, Text

from pydid import (
BaseDIDDocument as ResolvedDocument,
Expand Down Expand Up @@ -227,7 +227,9 @@ async def remove_keys_for_did(self, did: str):
storage: BaseStorage = session.inject(BaseStorage)
await storage.delete_all_records(self.RECORD_TYPE_DID_KEY, {"did": did})

async def resolve_invitation(self, did: str):
async def resolve_invitation(
self, did: str, service_accept: Optional[Sequence[Text]] = None
):
"""
Resolve invitation with the DID Resolver.

Expand All @@ -241,7 +243,7 @@ async def resolve_invitation(self, did: str):

resolver = self._profile.inject(DIDResolver)
try:
doc_dict: dict = await resolver.resolve(self._profile, did)
doc_dict: dict = await resolver.resolve(self._profile, did, service_accept)
doc: ResolvedDocument = pydid.deserialize_document(doc_dict, strict=True)
except ResolverError as error:
raise BaseConnectionManagerError(
Expand Down
41 changes: 24 additions & 17 deletions aries_cloudagent/core/protocol_registry.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"""Handle registration and publication of supported protocols."""

import logging
import re

from string import Template
from typing import Mapping, Sequence

from ..config.injection_context import InjectionContext
from ..utils.classloader import ClassLoader

from .error import ProtocolMinorVersionNotSupported
from .error import ProtocolMinorVersionNotSupported, ProtocolDefinitionValidationError

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -91,28 +91,34 @@ def create_msg_types_for_minor_version(self, typesets, version_definition):
curr_minor_version = version_definition["current_minor_version"]
min_minor_version = version_definition["minimum_minor_version"]
major_version = version_definition["major_version"]
if curr_minor_version >= min_minor_version and curr_minor_version >= 1:
if curr_minor_version >= min_minor_version:
for version_index in range(min_minor_version, curr_minor_version + 1):
to_check = f"{str(major_version)}.{str(version_index)}"
updated_typeset.update(
self._get_updated_tyoeset_dict(typesets, to_check, updated_typeset)
self._get_updated_typeset_dict(typesets, to_check, updated_typeset)
)
else:
raise ProtocolDefinitionValidationError(
"min_minor_version is greater than curr_minor_version for the"
f" following typeset: {str(typesets)}"
)
return (updated_typeset,)

def _get_updated_tyoeset_dict(self, typesets, to_check, updated_typeset) -> dict:
def _get_updated_typeset_dict(self, typesets, to_check, updated_typeset) -> dict:
for typeset in typesets:
for msg_type_string, module_path in typeset.items():
updated_msg_type_string = Template(msg_type_string).substitute(
version=to_check
updated_msg_type_string = re.sub(
r"(\d+\.)?(\*|\d+)", to_check, msg_type_string
)
updated_typeset[updated_msg_type_string] = module_path
return updated_typeset

def _template_message_type_check(self, typeset) -> bool:
for msg_type_string, _ in typeset.items():
if "$version" in msg_type_string:
return True
return False
def _message_type_check_for_minor_verssion(self, version_definition) -> bool:
if not version_definition:
return False
curr_minor_version = version_definition["current_minor_version"]
min_minor_version = version_definition["minimum_minor_version"]
return bool(curr_minor_version >= 1 and curr_minor_version >= min_minor_version)

def _create_and_register_updated_typesets(self, typesets, version_definition):
updated_typesets = self.create_msg_types_for_minor_version(
Expand Down Expand Up @@ -153,17 +159,18 @@ def register_message_types(self, *typesets, version_definition=None):
"""

# Maintain support for versionless protocol modules
template_msg_type_version = True
updated_typesets = None
for typeset in typesets:
if not self._template_message_type_check(typeset):
minor_versions_supported = self._message_type_check_for_minor_verssion(
version_definition
)
if not minor_versions_supported:
for typeset in typesets:
self._typemap.update(typeset)
template_msg_type_version = False

# Track versioned modules for version routing
if version_definition:
# create updated typesets for minor versions and register them
if template_msg_type_version:
if minor_versions_supported:
updated_typesets = self._create_and_register_updated_typesets(
typesets, version_definition
)
Expand Down
126 changes: 114 additions & 12 deletions aries_cloudagent/core/tests/test_protocol_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,19 @@ def test_message_type_query(self):
assert matches == ()

def test_create_msg_types_for_minor_version(self):
MSG_PATH = "aries_cloudagent.protocols.introduction.v0_1.messages"
test_typesets = (
{
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/$version/fake-forward-invitation": "aries_cloudagent.protocols.introduction.v0_1.messages.forward_invitation.ForwardInvitation",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/$version/fake-invitation": "aries_cloudagent.protocols.introduction.v0_1.messages.invitation.Invitation",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/$version/fake-invitation-request": "aries_cloudagent.protocols.introduction.v0_1.messages.invitation_request.InvitationRequest",
"https://didcom.org/introduction-service/$version/fake-forward-invitation": "aries_cloudagent.protocols.introduction.v0_1.messages.forward_invitation.ForwardInvitation",
"https://didcom.org/introduction-service/$version/fake-invitation": "aries_cloudagent.protocols.introduction.v0_1.messages.invitation.Invitation",
"https://didcom.org/introduction-service/$version/fake-invitation-request": "aries_cloudagent.protocols.introduction.v0_1.messages.invitation_request.InvitationRequest",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/1.0/fake-forward-invitation": f"{MSG_PATH}.forward_invitation.ForwardInvitation",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/1.0/fake-invitation": f"{MSG_PATH}.invitation.Invitation",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/1.0/fake-invitation-request": f"{MSG_PATH}.invitation_request.InvitationRequest",
"https://didcom.org/introduction-service/1.0/fake-forward-invitation": f"{MSG_PATH}.forward_invitation.ForwardInvitation",
"https://didcom.org/introduction-service/1.0/fake-invitation": f"{MSG_PATH}.invitation.Invitation",
"https://didcom.org/introduction-service/1.0/fake-invitation-request": f"{MSG_PATH}.invitation_request.InvitationRequest",
},
)
test_version_def = {
"current_minor_version": 1,
"current_minor_version": 0,
"major_version": 1,
"minimum_minor_version": 0,
"path": "v0_1",
Expand All @@ -78,23 +79,124 @@ def test_create_msg_types_for_minor_version(self):
in updated_typeset
)
assert (
"https://didcom.org/introduction-service/1.1/fake-forward-invitation"
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/1.0/fake-forward-invitation"
in updated_typeset
)

def test_introduction_create_msg_types_for_minor_version(self):
MSG_PATH = "aries_cloudagent.protocols.introduction.v0_1.messages"
test_typesets = (
{
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/0.1/invitation-request": f"{MSG_PATH}.invitation_request.InvitationRequest",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/0.1/invitation": f"{MSG_PATH}.invitation.Invitation",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/0.1/forward-invitation": f"{MSG_PATH}.invitation_messages.forward_invitation.ForwardInvitation",
"https://didcom.org/introduction-service/0.1/invitation-request": f"{MSG_PATH}.invitation_request.InvitationRequest",
"https://didcom.org/introduction-service/0.1/invitation": f"{MSG_PATH}.invitation.Invitation",
"https://didcom.org/introduction-service/0.1/forward-invitation": f"{MSG_PATH}.forward_invitation.ForwardInvitation",
},
)
test_version_def = {
"current_minor_version": 1,
"major_version": 0,
"minimum_minor_version": 1,
"path": "v0_1",
}
updated_typesets = self.registry.create_msg_types_for_minor_version(
test_typesets, test_version_def
)
updated_typeset = updated_typesets[0]
assert (
"https://didcom.org/introduction-service/1.1/fake-invitation"
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/0.1/invitation-request"
in updated_typeset
)
assert (
"https://didcom.org/introduction-service/1.1/fake-invitation-request"
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/0.1/invitation"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/1.0/fake-forward-invitation"
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/0.1/forward-invitation"
in updated_typeset
)
assert (
"https://didcom.org/introduction-service/0.1/invitation-request"
in updated_typeset
)
assert (
"https://didcom.org/introduction-service/0.1/invitation" in updated_typeset
)
assert (
"https://didcom.org/introduction-service/0.1/forward-invitation"
in updated_typeset
)

def test_oob_create_msg_types_for_minor_version(self):
MSG_PATH = "aries_cloudagent.protocols.out_of_band.v1_0.messages"
test_typesets = (
{
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/invitation": f"{MSG_PATH}.invitation.Invitation",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/handshake-reuse": f"{MSG_PATH}.reuse.HandshakeReuse",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/handshake-reuse-accepted": f"{MSG_PATH}.reuse_accept.HandshakeReuseAccept",
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/problem_report": f"{MSG_PATH}.problem_report.OOBProblemReport",
"https://didcom.org/out-of-band/1.1/invitation": f"{MSG_PATH}.invitation.Invitation",
"https://didcom.org/out-of-band/1.1/handshake-reuse": f"{MSG_PATH}.reuse.HandshakeReuse",
"https://didcom.org/out-of-band/1.1/handshake-reuse-accepted": f"{MSG_PATH}.reuse_accept.HandshakeReuseAccept",
"https://didcom.org/out-of-band/1.1/problem_report": f"{MSG_PATH}.problem_report.OOBProblemReport",
},
)
test_version_def = {
"current_minor_version": 1,
"major_version": 1,
"minimum_minor_version": 0,
"path": "v0_1",
}
updated_typesets = self.registry.create_msg_types_for_minor_version(
test_typesets, test_version_def
)
updated_typeset = updated_typesets[0]
assert "https://didcom.org/out-of-band/1.0/invitation" in updated_typeset
assert "https://didcom.org/out-of-band/1.0/handshake-reuse" in updated_typeset
assert (
"https://didcom.org/out-of-band/1.0/handshake-reuse-accepted"
in updated_typeset
)
assert "https://didcom.org/out-of-band/1.0/problem_report" in updated_typeset
assert "https://didcom.org/out-of-band/1.1/invitation" in updated_typeset
assert "https://didcom.org/out-of-band/1.1/handshake-reuse" in updated_typeset
assert (
"https://didcom.org/out-of-band/1.1/handshake-reuse-accepted"
in updated_typeset
)
assert "https://didcom.org/out-of-band/1.1/problem_report" in updated_typeset
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.0/invitation"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.0/handshake-reuse"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.0/handshake-reuse-accepted"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.0/problem_report"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/invitation"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/handshake-reuse"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/handshake-reuse-accepted"
in updated_typeset
)
assert (
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/introduction-service/1.1/fake-invitation-request"
"did:sov:BzCbsNYhMrjHiqZDTUASHg;spec/out-of-band/1.1/problem_report"
in updated_typeset
)

Expand Down
15 changes: 12 additions & 3 deletions aries_cloudagent/core/tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,19 @@ def test_get_version_from_message_type(self):
)

def test_get_version_from_message(self):
assert test_module.get_version_from_message(HandshakeReuse()) == "1.0"
assert test_module.get_version_from_message(HandshakeReuse()) == "1.1"

async def test_get_proto_default_version(self):
async def test_get_proto_default_version_from_msg_class(self):
profile = make_profile()
assert (
await test_module.get_proto_default_version(profile, HandshakeReuse)
await test_module.get_proto_default_version_from_msg_class(
profile, HandshakeReuse
)
) == "1.1"

def test_get_proto_default_version(self):
assert (
test_module.get_proto_default_version(
"aries_cloudagent.protocols.out_of_band.definition"
)
) == "1.1"
36 changes: 26 additions & 10 deletions aries_cloudagent/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,20 @@ def get_version_from_message(msg: AgentMessage) -> str:
return get_version_from_message_type(msg_type)


async def get_proto_default_version(
async def get_proto_default_version_from_msg_class(
profile: Profile, msg_class: type, major_version: int = 1
) -> str:
"""Return default protocol version from version_definition."""
version_definition = await get_version_def_from_msg_class(
profile, msg_class, major_version
)
default_major_version = version_definition["major_version"]
default_minor_version = version_definition["current_minor_version"]
return f"{default_major_version}.{default_minor_version}"
return _get_default_version_from_version_def(version_definition)


def get_proto_default_version(def_path: str, major_version: int = 1) -> str:
"""Return default protocol version from version_definition."""
version_definition = _get_version_def_from_path(def_path, major_version)
return _get_default_version_from_version_def(version_definition)


def _get_path_from_msg_class(msg_class: type) -> str:
Expand All @@ -116,10 +120,26 @@ def _get_path_from_msg_class(msg_class: type) -> str:
return (path.replace("/", ".")) + "definition"


def _get_version_def_from_path(definition_path: str, major_version: int = 1):
version_definition = None
definition = ClassLoader.load_module(definition_path)
for protocol_version in definition.versions:
if major_version == protocol_version["major_version"]:
version_definition = protocol_version
break
return version_definition


def _get_default_version_from_version_def(version_definition) -> str:
default_major_version = version_definition["major_version"]
default_minor_version = version_definition["current_minor_version"]
return f"{default_major_version}.{default_minor_version}"


async def get_version_def_from_msg_class(
profile: Profile, msg_class: type, major_version: int = 1
):
"""Return version_definition of a protocol."""
"""Return version_definition of a protocol from msg_class."""
cache = profile.inject_or(BaseCache)
version_definition = None
if cache:
Expand All @@ -129,11 +149,7 @@ async def get_version_def_from_msg_class(
if version_definition:
return version_definition
definition_path = _get_path_from_msg_class(msg_class)
definition = ClassLoader.load_module(definition_path)
for protocol_version in definition.versions:
if major_version == protocol_version["major_version"]:
version_definition = protocol_version
break
version_definition = _get_version_def_from_path(definition_path, major_version)
if not version_definition:
raise ProtocolDefinitionValidationError(
f"Unable to load protocol version_definition for {str(msg_class)}"
Expand Down