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

fix: quickstart: Add fixes based upon previous DMP changes #36

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions didcomm_messaging/quickstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from didcomm_messaging.multiformats import multibase, multicodec
from didcomm_messaging.packaging import PackagingService
from didcomm_messaging.resolver import PrefixResolver
from didcomm_messaging.resolver.web import DIDWeb
from didcomm_messaging.resolver.peer import Peer2, Peer4
from didcomm_messaging.routing import RoutingService

Expand Down Expand Up @@ -89,22 +90,28 @@ async def setup_default(did: DID, did_secrets: Tuple[Key, Key]) -> DIDCommMessag
#
# At present, the PrefixResolver is used to determine which library should
# be used to convert a DID into a DIDDocument.
resolver = PrefixResolver({"did:peer:2": Peer2(), "did:peer:4": Peer4()})
resolver = PrefixResolver(
{
"did:peer:2": Peer2(),
"did:peer:4": Peer4(),
"did:web:": DIDWeb(),
}
)

# The Packaging Service is where a lot of the magic happens. Similar to a
# shipping box, the PackagingService will "pack" and "unpack" an encrypted
# message. When packing a message, the PackagingService will encrypt the
# message to a single target, however. If the message needs to be forwarded
# (because the recipient is behind a relay), then those messages will need
# to be handled by the RoutingService.
packer = PackagingService(resolver, crypto, secrets)
packer = PackagingService()

# The RoutingService handles the routing of messages through relays. When a
# message needs to be forwarded, the RoutingService will handle wrapping
# each encrypted message within a forward message. The built-in
# RoutingService allows for multiple levels of relays, so you don't need to
# worry about how a message is routed to the recipient.
router = RoutingService(packaging=packer, resolver=resolver)
router = RoutingService()

# Once everything is setup, we need to store the DID Secrets within the
# SecretsService. We do this by taking the secrets that were passed in,
Expand Down Expand Up @@ -180,10 +187,10 @@ async def send_http_message(

# If the HTTP enpoint responded with a message, decode it
if len(packed) > 0:
unpacked = await dmp.packaging.unpack(packed)
msg = unpacked[0].decode()
unpacked = await dmp.unpack(packed)
msg = unpacked.message
LOG.debug("Raw message from remote %s", msg)
return json.loads(msg)
return msg
return


Expand Down Expand Up @@ -396,9 +403,8 @@ async def handle_websocket(
try:
# Unpack/Decrypt the message, decode it, and load the JSON into
# a native python object.
unpacked_message, metadata = await dmp.packaging.unpack(message)
msg = unpacked_message.decode()
msg = json.loads(msg)
unpacked_message = await dmp.unpack(message)
msg = unpacked_message.message
LOG.debug("Received websocket message %s", msg["type"])

# If the message is not from the relay, process it via the callback
Expand Down