Skip to content

Commit

Permalink
Merge branch 'main' into endorser-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ianco authored Sep 2, 2022
2 parents 2d508e0 + b72db27 commit be4fd96
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 93 deletions.
8 changes: 4 additions & 4 deletions DIDResolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ In practice, DIDs and DID Documents are used for a variety of purposes but espec

## `DIDResolver`

In ACA-Py, the `DIDResolver` provides the interface to resolve DIDs using registered method resolvers. Method resolver registration happens on startup through the `DIDResolverRegistry`. This registry enables additional resolvers to be loaded via plugin.
In ACA-Py, the `DIDResolver` provides the interface to resolve DIDs using registered method resolvers. Method resolver registration happens on startup in a `did_resolvers` list. This registry enables additional resolvers to be loaded via plugin.

#### Example usage:
```python=
Expand Down Expand Up @@ -73,17 +73,17 @@ The following is an example method resolver implementation. In this example, we

```python=
from aries_cloudagent.config.injection_context import InjectionContext
from aries_cloudagent.resolver.did_resolver_registry import DIDResolverRegistry
from ..resolver.did_resolver import DIDResolver
from .example_resolver import ExampleResolver
async def setup(context: InjectionContext):
"""Setup the plugin."""
registry = context.inject(DIDResolverRegistry)
registry = context.inject(DIDResolver)
resolver = ExampleResolver()
await resolver.setup(context)
registry.register(resolver)
registry.append(resolver)
```

#### `example_resolver.py`
Expand Down
7 changes: 1 addition & 6 deletions aries_cloudagent/config/default_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from ..core.protocol_registry import ProtocolRegistry
from ..core.goal_code_registry import GoalCodeRegistry
from ..resolver.did_resolver import DIDResolver
from ..resolver.did_resolver_registry import DIDResolverRegistry
from ..tails.base import BaseTailsServer

from ..protocols.actionmenu.v1_0.base_service import BaseMenuService
Expand Down Expand Up @@ -50,12 +49,8 @@ async def build_context(self) -> InjectionContext:
# Global event bus
context.injector.bind_instance(EventBus, EventBus())

# Global did resolver registry
did_resolver_registry = DIDResolverRegistry()
context.injector.bind_instance(DIDResolverRegistry, did_resolver_registry)

# Global did resolver
context.injector.bind_instance(DIDResolver, DIDResolver(did_resolver_registry))
context.injector.bind_instance(DIDResolver, DIDResolver([]))

await self.bind_providers(context)
await self.load_plugins(context)
Expand Down
4 changes: 2 additions & 2 deletions aries_cloudagent/core/tests/test_conductor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from ...protocols.coordinate_mediation.v1_0.models.mediation_record import (
MediationRecord,
)
from ...resolver.did_resolver import DIDResolver, DIDResolverRegistry
from ...resolver.did_resolver import DIDResolver
from ...multitenant.base import BaseMultitenantManager
from ...multitenant.manager import MultitenantManager
from ...storage.base import BaseStorage
Expand Down Expand Up @@ -92,7 +92,7 @@ async def build_context(self) -> InjectionContext:
context.injector.bind_instance(ProfileManager, InMemoryProfileManager())
context.injector.bind_instance(ProtocolRegistry, ProtocolRegistry())
context.injector.bind_instance(BaseWireFormat, self.wire_format)
context.injector.bind_instance(DIDResolver, DIDResolver(DIDResolverRegistry()))
context.injector.bind_instance(DIDResolver, DIDResolver([]))
context.injector.bind_instance(EventBus, MockEventBus())
return context

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
from .....multitenant.manager import MultitenantManager
from .....protocols.routing.v1_0.manager import RoutingManager
from .....resolver.did_resolver import DIDResolver
from .....resolver.did_resolver_registry import DIDResolverRegistry
from .....storage.error import StorageNotFoundError
from .....transport.inbound.receipt import MessageReceipt
from .....wallet.base import DIDInfo
Expand Down Expand Up @@ -2024,9 +2023,7 @@ async def test_fetch_connection_targets_no_my_did(self):

async def test_fetch_connection_targets_conn_invitation_did_no_resolver(self):
async with self.profile.session() as session:
self.context.injector.bind_instance(
DIDResolver, DIDResolver(DIDResolverRegistry())
)
self.context.injector.bind_instance(DIDResolver, DIDResolver([]))
await session.wallet.create_local_did(
method=DIDMethod.SOV,
key_type=KeyType.ED25519,
Expand Down Expand Up @@ -2320,9 +2317,7 @@ async def test_fetch_connection_targets_conn_invitation_unsupported_key_type(sel

async def test_fetch_connection_targets_oob_invitation_svc_did_no_resolver(self):
async with self.profile.session() as session:
self.context.injector.bind_instance(
DIDResolver, DIDResolver(DIDResolverRegistry())
)
self.context.injector.bind_instance(DIDResolver, DIDResolver([]))
await session.wallet.create_local_did(
method=DIDMethod.SOV,
key_type=KeyType.ED25519,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

from .....core.in_memory import InMemoryProfile
from .....did.did_key import DIDKey
from .....resolver.did_resolver_registry import DIDResolverRegistry
from .....resolver.did_resolver import DIDResolver
from .....storage.vc_holder.vc_record import VCRecord
from .....wallet.base import BaseWallet, DIDInfo
Expand Down Expand Up @@ -69,9 +68,7 @@ def event_loop(request):
def profile():
profile = InMemoryProfile.test_profile()
context = profile.context
did_resolver_registry = DIDResolverRegistry()
context.injector.bind_instance(DIDResolverRegistry, did_resolver_registry)
context.injector.bind_instance(DIDResolver, DIDResolver(did_resolver_registry))
context.injector.bind_instance(DIDResolver, DIDResolver([]))
context.injector.bind_instance(DocumentLoader, custom_document_loader)
context.settings["debug.auto_respond_presentation_request"] = True
return profile
Expand Down
14 changes: 7 additions & 7 deletions aries_cloudagent/resolver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,42 @@
from ..config.injection_context import InjectionContext
from ..config.provider import ClassProvider

from .did_resolver_registry import DIDResolverRegistry
from ..resolver.did_resolver import DIDResolver

LOGGER = logging.getLogger(__name__)


async def setup(context: InjectionContext):
"""Set up default resolvers."""
registry = context.inject_or(DIDResolverRegistry)
registry = context.inject_or(DIDResolver)
if not registry:
LOGGER.warning("No DID Resolver Registry instance found in context")
LOGGER.warning("No DID Resolver instance found in context")
return

key_resolver = ClassProvider(
"aries_cloudagent.resolver.default.key.KeyDIDResolver"
).provide(context.settings, context.injector)
await key_resolver.setup(context)
registry.register(key_resolver)
registry.register_resolver(key_resolver)

if not context.settings.get("ledger.disabled"):
indy_resolver = ClassProvider(
"aries_cloudagent.resolver.default.indy.IndyDIDResolver"
).provide(context.settings, context.injector)
await indy_resolver.setup(context)
registry.register(indy_resolver)
registry.register_resolver(indy_resolver)
else:
LOGGER.warning("Ledger is not configured, not loading IndyDIDResolver")

web_resolver = ClassProvider(
"aries_cloudagent.resolver.default.web.WebDIDResolver"
).provide(context.settings, context.injector)
await web_resolver.setup(context)
registry.register(web_resolver)
registry.register_resolver(web_resolver)

if context.settings.get("resolver.universal"):
universal_resolver = ClassProvider(
"aries_cloudagent.resolver.default.universal.UniversalResolver"
).provide(context.settings, context.injector)
await universal_resolver.setup(context)
registry.register(universal_resolver)
registry.register_resolver(universal_resolver)
13 changes: 8 additions & 5 deletions aries_cloudagent/resolver/did_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from datetime import datetime
from itertools import chain
import logging
from typing import Sequence, Tuple, Type, TypeVar, Union
from typing import List, Sequence, Tuple, Type, TypeVar, Union

from pydid import DID, DIDError, DIDUrl, Resource, NonconformantDocument
from pydid.doc.doc import IDNotFoundError
Expand All @@ -22,7 +22,6 @@
ResolutionResult,
ResolverError,
)
from .did_resolver_registry import DIDResolverRegistry

LOGGER = logging.getLogger(__name__)

Expand All @@ -33,9 +32,13 @@
class DIDResolver:
"""did resolver singleton."""

def __init__(self, registry: DIDResolverRegistry):
def __init__(self, resolvers: List[BaseDIDResolver] = None):
"""Create DID Resolver."""
self.did_resolver_registry = registry
self.resolvers = resolvers or []

def register_resolver(self, resolver: BaseDIDResolver):
"""Register a new resolver."""
self.resolvers.append(resolver)

async def _resolve(
self, profile: Profile, did: Union[str, DID]
Expand Down Expand Up @@ -90,7 +93,7 @@ async def _match_did_to_resolver(
"""
valid_resolvers = [
resolver
for resolver in self.did_resolver_registry.resolvers
for resolver in self.resolvers
if await resolver.supports(profile, did)
]
native_resolvers = filter(lambda resolver: resolver.native, valid_resolvers)
Expand Down
28 changes: 0 additions & 28 deletions aries_cloudagent/resolver/did_resolver_registry.py

This file was deleted.

21 changes: 6 additions & 15 deletions aries_cloudagent/resolver/tests/test_did_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
ResolverType,
)
from ..did_resolver import DIDResolver
from ..did_resolver_registry import DIDResolverRegistry

from . import DOC

Expand Down Expand Up @@ -88,10 +87,10 @@ async def _resolve(self, profile, did):

@pytest.fixture
def resolver():
did_resolver_registry = DIDResolverRegistry()
did_resolver_registry = []
for method in TEST_DID_METHODS:
resolver = MockResolver([method], DIDDocument.deserialize(DOC))
did_resolver_registry.register(resolver)
did_resolver_registry.append(resolver)
return DIDResolver(did_resolver_registry)


Expand All @@ -101,7 +100,7 @@ def profile():


def test_create_resolver(resolver):
assert len(resolver.did_resolver_registry.resolvers) == len(TEST_DID_METHODS)
assert len(resolver.resolvers) == len(TEST_DID_METHODS)


@pytest.mark.asyncio
Expand All @@ -121,11 +120,9 @@ async def test_match_did_to_resolver_x_not_supported(resolver):

@pytest.mark.asyncio
async def test_match_did_to_resolver_native_priority(profile):
registry = DIDResolverRegistry()
native = MockResolver(["sov"], native=True)
non_native = MockResolver(["sov"], native=False)
registry.register(non_native)
registry.register(native)
registry = [non_native, native]
resolver = DIDResolver(registry)
assert [native, non_native] == await resolver._match_did_to_resolver(
profile, TEST_DID0
Expand All @@ -134,15 +131,11 @@ async def test_match_did_to_resolver_native_priority(profile):

@pytest.mark.asyncio
async def test_match_did_to_resolver_registration_order(profile):
registry = DIDResolverRegistry()
native1 = MockResolver(["sov"], native=True)
registry.register(native1)
native2 = MockResolver(["sov"], native=True)
registry.register(native2)
non_native3 = MockResolver(["sov"], native=False)
registry.register(non_native3)
native4 = MockResolver(["sov"], native=True)
registry.register(native4)
registry = [native1, native2, non_native3, native4]
resolver = DIDResolver(registry)
assert [
native1,
Expand Down Expand Up @@ -200,8 +193,6 @@ async def test_resolve_did_x_not_supported(resolver, profile):
async def test_resolve_did_x_not_found(profile):
py_did = DID("did:cowsay:EiDahaOGH-liLLdDtTxEAdc8i-cfCz-WUcQdRJheMVNn3A")
cowsay_resolver_not_found = MockResolver(["cowsay"], resolved=DIDNotFound())
registry = DIDResolverRegistry()
registry.register(cowsay_resolver_not_found)
resolver = DIDResolver(registry)
resolver = DIDResolver([cowsay_resolver_not_found])
with pytest.raises(DIDNotFound):
await resolver.resolve(profile, py_did)
12 changes: 0 additions & 12 deletions aries_cloudagent/resolver/tests/test_did_resolver_registry.py

This file was deleted.

9 changes: 6 additions & 3 deletions aries_cloudagent/wallet/did_method.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def supports_key_type(self, key_type: KeyType) -> bool:
"""Check whether the current method supports the key type."""
return key_type in self.supported_key_types

def from_metadata(metadata: Mapping) -> "DIDMethod":
@classmethod
def from_metadata(cls, metadata: Mapping) -> "DIDMethod":
"""Get DID method instance from metadata object.
Returns SOV if no metadata was found for backwards compatability.
Expand All @@ -63,15 +64,17 @@ def from_metadata(metadata: Mapping) -> "DIDMethod":
# return default SOV for backward compat
return DIDMethod.SOV

def from_method(method: str) -> Optional["DIDMethod"]:
@classmethod
def from_method(cls, method: str) -> Optional["DIDMethod"]:
"""Get DID method instance from the method name."""
for did_method in DIDMethod:
if method == did_method.method_name:
return did_method

return None

def from_did(did: str) -> "DIDMethod":
@classmethod
def from_did(cls, did: str) -> "DIDMethod":
"""Get DID method instance from the method name."""
if not did.startswith("did:"):
# sov has no prefix
Expand Down

0 comments on commit be4fd96

Please sign in to comment.