Skip to content

Commit

Permalink
fix: Address PR concerns
Browse files Browse the repository at this point in the history
Signed-off-by: Colton Wolkins (Indicio work address) <[email protected]>
  • Loading branch information
TheTechmage committed Jan 18, 2024
1 parent be7d0a5 commit 4d298fe
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions didcomm_messaging/quickstart.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
"""Quickstart helpers for beginner users of DIDComm."""
from typing import Optional, Dict, List, Any, Union, Callable, Awaitable, Sequence
from typing import (
Optional,
Dict,
List,
Any,
Union,
Callable,
Awaitable,
Sequence,
Tuple,
)
import aiohttp
import attr
import attrs
Expand Down Expand Up @@ -106,7 +116,7 @@ def set_id(method):
return DIDDocument.deserialize(doc)


def generate_did():
def generate_did() -> Tuple[DID, Tuple[Key, Key]]:
"""Use Askar to generate encryption/verification keys, then return a DID from both."""

verkey = Key.generate(KeyAlg.ED25519)
Expand Down Expand Up @@ -139,7 +149,9 @@ def generate_did():
return did, (verkey, xkey)


async def setup_default(did, did_secrets, enable_compatibility_prefix=False):
async def setup_default(
did: DID, did_secrets: Tuple[Key, Key], enable_compatibility_prefix: bool = False
) -> DIDCommMessaging:
"""Setup a pre-configured DIDCommMessaging instance."""

# The Crypto Service is used to encrypt, decrypt, sign and verify messages.
Expand Down Expand Up @@ -215,7 +227,7 @@ async def setup_default(did, did_secrets, enable_compatibility_prefix=False):

async def send_http_message(
dmp: DIDCommMessaging, my_did: DID, message: Message, target: DID
):
) -> Optional[Message]:
"""Send a message via HTTP."""

# Get the message as a dictionary
Expand All @@ -234,31 +246,31 @@ async def send_http_message(
endpoint = packy.get_endpoint("http")

async with aiohttp.ClientSession() as session:
LOG.info("posting message type %s to %s" % (message_wrapper.type, endpoint))
LOG.info("posting message type %s to %s", message_wrapper.type, endpoint)

async with session.post(endpoint, data=packed) as resp:
LOG.debug("posted message: %s" % (message))
LOG.debug("message ID: %s" % (message_wrapper.id))
LOG.debug("posted message: %s", message)
LOG.debug("message ID: %s", message_wrapper.id)
packed = await resp.text()

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


async def setup_relay(
dmp: DIDCommMessaging, my_did: DID, relay_did: DID, keys: Sequence[Key]
) -> Union[DID, None]:
"""Negotiate services with an outbound relay.
"""Negotiate services with an inbound relay.
Returns a DID upon successful negotiation.
"""

# Request mediation from the outbound relay
# Request mediation from the inbound relay
message = Message(
type="https://didcomm.org/coordinate-mediation/3.0/mediate-request",
id=str(uuid.uuid4()),
Expand Down Expand Up @@ -289,7 +301,7 @@ async def setup_relay(
}
],
)
LOG.info("relayed did: ", new_did)
LOG.info("relayed did: %s", new_did)

# A couple of helpers variables to simplify the next few lines
resolver = dmp.resolver
Expand Down Expand Up @@ -369,7 +381,7 @@ async def fetch_relayed_messages(
LOG.info("Received message %s", attach["id"][:-58])

# Decrypt/Unpack the encrypted message attachment
unpacked = await dmp.packaging.unpack(json.dumps(attach["data"]["json"]))
unpacked = await dmp.unpack(json.dumps(attach["data"]["json"]))
msg = unpacked.message
msg = Message.from_json(msg)

Expand All @@ -379,7 +391,7 @@ async def fetch_relayed_messages(

if msg.type == "https://didcomm.org/basicmessage/2.0/message":
logmsg = msg.body["content"].replace("\n", " ").replace("\r", "")
LOG.info(f"Got message: {logmsg}")
LOG.info(f"Got message: %s", logmsg)

message = Message(
type="https://didcomm.org/messagepickup/3.0/messages-received",
Expand Down

0 comments on commit 4d298fe

Please sign in to comment.