Skip to content

Commit

Permalink
refactor: rename some components for clarity
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Bluhm <[email protected]>
  • Loading branch information
dbluhm committed Oct 30, 2023
1 parent 648a69e commit a61a95e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
6 changes: 3 additions & 3 deletions didcomm_messaging/didcomm.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pydid import DIDUrl, VerificationMethod
from didcomm_messaging.crypto import P, S, CryptoService, SecretsManager
from didcomm_messaging.jwe import JweEnvelope, from_b64url
from didcomm_messaging.resolver import Resolver
from didcomm_messaging.resolver import DIDResolver


@dataclass
Expand All @@ -24,12 +24,12 @@ class DIDCommMessagingError(Exception):
"""Represents an error from the DIDComm Messaging interface."""


class DIDCommMessaging(Generic[P, S]):
class PackagingService(Generic[P, S]):
"""DIDComm Messaging interface."""

def __init__(
self,
resolver: Resolver,
resolver: DIDResolver,
crypto: CryptoService[P, S],
secrets: SecretsManager[S],
):
Expand Down
16 changes: 8 additions & 8 deletions didcomm_messaging/resolver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
from pydid import DIDDocument, DIDUrl, Resource, VerificationMethod


class ResolutionError(Exception):
class DIDResolutionError(Exception):
"""Represents an error from a DID Resolver."""


class DIDNotFound(ResolutionError):
class DIDNotFound(DIDResolutionError):
"""Represents a DID not found error."""


class DIDMethodNotSupported(ResolutionError):
class DIDMethodNotSupported(DIDResolutionError):
"""Represents a DID method not supported error."""


class Resolver(ABC):
class DIDResolver(ABC):
"""DID Resolver interface."""

@abstractmethod
Expand All @@ -33,7 +33,7 @@ async def resolve_and_dereference(self, did_url: str) -> Resource:
"""Resolve a DID URL and dereference the identifier."""
url = DIDUrl.parse(did_url)
if not url.did:
raise ResolutionError("Invalid DID URL; must be absolute")
raise DIDResolutionError("Invalid DID URL; must be absolute")

doc = await self.resolve_and_parse(url.did)
return doc.dereference(url)
Expand All @@ -44,15 +44,15 @@ async def resolve_and_dereference_verification_method(
"""Resolve a DID URL and dereference the identifier."""
resource = await self.resolve_and_dereference(did_url)
if not isinstance(resource, VerificationMethod):
raise ResolutionError("Resource is not a verification method")
raise DIDResolutionError("Resource is not a verification method")

return resource


class PrefixResolver(Resolver):
class PrefixResolver(DIDResolver):
"""DID Resolver delegates to sub-resolvers by DID prefix."""

def __init__(self, resolvers: Dict[str, Resolver]):
def __init__(self, resolvers: Dict[str, DIDResolver]):
"""Initialize the resolver."""
self.resolvers = resolvers

Expand Down
6 changes: 3 additions & 3 deletions didcomm_messaging/resolver/peer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""did:peer resolver."""

from didcomm_messaging.resolver import Resolver
from didcomm_messaging.resolver import DIDResolver

try:
from did_peer_2 import resolve as resolve_peer_2
Expand All @@ -12,15 +12,15 @@
)


class Peer2(Resolver):
class Peer2(DIDResolver):
"""did:peer:2 resolver."""

async def resolve(self, did: str) -> dict:
"""Resolve a did:peer:2 DID."""
return resolve_peer_2(did)


class Peer4(Resolver):
class Peer4(DIDResolver):
"""did:peer:4 resolver."""

async def resolve(self, did: str) -> dict:
Expand Down
8 changes: 4 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from aries_askar import Key, KeyAlg
from didcomm_messaging.crypto.askar import AskarCryptoService, AskarSecretKey
from didcomm_messaging.crypto.basic import InMemorySecretsManager
from didcomm_messaging.didcomm import DIDCommMessaging
from didcomm_messaging.didcomm import PackagingService
from didcomm_messaging.multiformats import multibase
from didcomm_messaging.multiformats import multicodec
from didcomm_messaging.resolver.peer import Peer2, Peer4
Expand All @@ -15,7 +15,7 @@ async def main():
"""An example of using DIDComm Messaging."""
secrets = InMemorySecretsManager()
crypto = AskarCryptoService()
didcomm = DIDCommMessaging(
packer = PackagingService(
PrefixResolver({"did:peer:2": Peer2(), "did:peer:4": Peer4()}), crypto, secrets
)
verkey = Key.generate(KeyAlg.ED25519)
Expand All @@ -39,9 +39,9 @@ async def main():
await secrets.add_secret(AskarSecretKey(verkey, f"{did}#key-1"))
await secrets.add_secret(AskarSecretKey(xkey, f"{did}#key-2"))
print(did)
packed = await didcomm.pack(b"hello world", [did], did)
packed = await packer.pack(b"hello world", [did], did)
print(json.dumps(json.loads(packed), indent=2))
unpacked = await didcomm.unpack(packed)
unpacked = await packer.unpack(packed)
print(unpacked)


Expand Down

0 comments on commit a61a95e

Please sign in to comment.