forked from openwallet-foundation/acapy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openwallet-foundation#23 from burdettadam/feature/…
…didresolver feature/did_resolver
- Loading branch information
Showing
11 changed files
with
352 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
""" | ||
the did resolver. | ||
responsible for keeping track of all resolvers. more importantly | ||
retrieving did's from different sources provided by the method type. | ||
""" | ||
|
||
import logging | ||
from itertools import chain | ||
from typing import Union | ||
|
||
from ..core.profile import Profile | ||
from ..resolver.base import BaseDIDResolver, DIDMethodNotSupported, DIDNotFound | ||
from ..resolver.did import DID, DIDUrl # , DID_PATTERN | ||
from ..resolver.diddoc import ResolvedDIDDoc # , ExternalResourceError | ||
from .did_resolver_registry import DIDResolverRegistry | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class DIDResolver: | ||
"""did resolver singleton.""" | ||
|
||
def __init__(self, registry: DIDResolverRegistry): | ||
"""Initialize a `didresolver` instance.""" | ||
self.did_resolver_registry = registry | ||
|
||
async def resolve( | ||
self, profile: Profile, did: Union[str, DID] | ||
) -> ResolvedDIDDoc: | ||
"""Retrieve did doc from public registry.""" | ||
# TODO Cache results | ||
if isinstance(did, str): | ||
did = DID(did) | ||
for resolver in self._match_did_to_resolver(did): | ||
try: | ||
LOGGER.debug("Resolving DID %s with %s", did, resolver) | ||
return await resolver.resolve(profile, did) | ||
except DIDNotFound: | ||
LOGGER.debug("DID %s not found by resolver %s", did, resolver) | ||
|
||
raise DIDNotFound(f"DID {did} could not be resolved.") | ||
|
||
def _match_did_to_resolver(self, did: DID) -> BaseDIDResolver: | ||
"""Generate supported DID Resolvers. | ||
Native resolvers are yielded first, in registered order followed by | ||
non-native resolvers in registered order. | ||
""" | ||
valid_resolvers = list( | ||
filter( | ||
lambda resolver: resolver.supports(did.method), | ||
self.did_resolver_registry.resolvers, | ||
) | ||
) | ||
native_resolvers = filter(lambda resolver: resolver.native, valid_resolvers) | ||
non_native_resolvers = filter( | ||
lambda resolver: not resolver.native, valid_resolvers | ||
) | ||
resolvers = list(chain(native_resolvers, non_native_resolvers)) | ||
if not resolvers: | ||
raise DIDMethodNotSupported(f"{did.method} not supported") | ||
return resolvers | ||
|
||
async def dereference( | ||
self, profile: Profile, did_url: str | ||
) -> ResolvedDIDDoc: | ||
"""Dereference a DID URL to its corresponding DID Doc object.""" | ||
# TODO Use cached DID Docs when possible | ||
did_url = DIDUrl.parse(did_url) | ||
doc = await self.resolve(profile, did_url.did) | ||
return doc.dereference(did_url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.