Skip to content

Commit

Permalink
feat: Support did:cheqd resolver
Browse files Browse the repository at this point in the history
Signed-off-by: DaevMithran <[email protected]>
  • Loading branch information
DaevMithran committed Nov 4, 2024
1 parent 68e7f88 commit b653306
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
26 changes: 26 additions & 0 deletions acapy_agent/messaging/valid.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,29 @@ def __init__(self):
)


class CheqdDID(Regexp):
"""Validate value against cheqd DID."""

EXAMPLE = "did:cheqd:testnet:099be283-4302-40cc-9850-22016bcd1d86"

UUID = r"([a-z,0-9,-]{36,36})"
ID_CHAR = r"(?:[a-zA-Z0-9]{21,22}|" + UUID + ")"
NETWORK = r"(testnet|mainnet)"
METHOD_ID = r"(?:" + ID_CHAR + r"*:)*(" + ID_CHAR + r"+)"
QUERY = r"([?][^#]*)?"
PARAMS = r"((;[a-zA-Z0-9_.:%-]+=[a-zA-Z0-9_.:%-]*)*)"

PATTERN = re.compile(rf"^did:cheqd:{NETWORK}:{METHOD_ID}{PARAMS}{QUERY}$")

def __init__(self):
"""Initialize the instance."""

super().__init__(
CheqdDID.PATTERN,
error="Value {input} is not an cheqd decentralized identifier (DID)",
)


class DIDValidation(Regexp):
"""Validate value against any valid DID spec."""

Expand Down Expand Up @@ -940,6 +963,9 @@ def __init__(
INDY_DID_VALIDATE = IndyDID()
INDY_DID_EXAMPLE = IndyDID.EXAMPLE

CHEQD_DID_VALIDATE = CheqdDID()
CHEQD_DID_EXAMPLE = CheqdDID.EXAMPLE

GENERIC_DID_VALIDATE = MaybeIndyDID()
GENERIC_DID_EXAMPLE = MaybeIndyDID.EXAMPLE

Expand Down
38 changes: 23 additions & 15 deletions acapy_agent/resolver/default/cheqd.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,51 @@
"""Key DID Resolver.
Resolution is performed using the IndyLedger class.
"""
"""Cheqd DID Resolver."""

from aiohttp import ClientSession
from typing import Optional, Pattern, Sequence, Text

from ...messaging.valid import CheqdDID
from ...config.injection_context import InjectionContext
from ...core.profile import Profile
from ...did.did_key import DIDKey
from ...messaging.valid import DIDKey as DIDKeyType
from ..base import BaseDIDResolver, DIDNotFound, ResolverType


class KeyDIDResolver(BaseDIDResolver):
class CheqdDIDResolver(BaseDIDResolver):
"""Key DID Resolver."""

DID_RESOLVER_BASE_URL = "https://resolver.cheqd.net/1.0/identifiers/"

def __init__(self):
"""Initialize Key Resolver."""
"""Initialize Cheqd Resolver."""
super().__init__(ResolverType.NATIVE)

async def setup(self, context: InjectionContext):
"""Perform required setup for Key DID resolution."""
"""Perform required setup for Cheqd DID resolution."""

@property
def supported_did_regex(self) -> Pattern:
"""Return supported_did_regex of Key DID Resolver."""
return DIDKeyType.PATTERN
"""Return supported_did_regex of Cheqd DID Resolver."""
return CheqdDID.PATTERN

async def _resolve(
self,
profile: Profile,
did: str,
service_accept: Optional[Sequence[Text]] = None,
) -> dict:
"""Resolve a Key DID."""
"""Resolve a Cheqd DID."""
try:
did_key = DIDKey.from_did(did)
async with ClientSession() as session:
try:
async with session.get(
self.DID_RESOLVER_BASE_URL + did,
) as response:
if response.status == 200:
did_cheqd = await response.json()
else:
raise Exception(response)
except Exception:
raise

except Exception as e:
raise DIDNotFound(f"Unable to resolve did: {did}") from e

return did_key.did_doc
return did_cheqd.did_doc

0 comments on commit b653306

Please sign in to comment.