Skip to content
This repository has been archived by the owner on May 8, 2020. It is now read-only.

Commit

Permalink
Make serializer out byte and not str.
Browse files Browse the repository at this point in the history
Fixed minor inconsistent typing issues.
Deleted unused helpers.py file.
Fixed minor pytlint reported issues.

Signed-off-by: fabienpe <fabienpe@users.noreply.github.com>
  • Loading branch information
fabienpe committed May 28, 2019
1 parent 3371a44 commit 296580c
Show file tree
Hide file tree
Showing 18 changed files with 109 additions and 124 deletions.
10 changes: 5 additions & 5 deletions python/agent.py
Expand Up @@ -12,9 +12,9 @@
from indy import wallet, did, error, crypto, pairwise

import indy_sdk_utils as utils
from serializer import json_serializer as Serializer
from python_agent_utils.messages.message import Message
from router.family_router import FamilyRouter
from serializer.json_serializer import JSONSerializer as Serializer


class WalletConnectionException(Exception):
Expand Down Expand Up @@ -183,7 +183,7 @@ async def unpack_agent_message(self, wire_msg_bytes):
to_key = unpacked['recipient_verkey']
to_did = await utils.did_for_key(self.wallet_handle, unpacked['recipient_verkey'])

msg = Serializer.unpack(unpacked['message'])
msg = Serializer.deserialize(unpacked['message'])

msg.context = {
'from_did': from_did, # Could be None
Expand Down Expand Up @@ -214,7 +214,7 @@ async def send_message_to_endpoint_and_key(self, their_ver_key, their_endpoint,
# If my_ver_key is omitted, anoncrypt is used inside pack.
wire_message = await crypto.pack_message(
self.wallet_handle,
Serializer.pack(msg),
Serializer.serialize(msg).decode('utf-8'),
[their_ver_key],
my_ver_key
)
Expand All @@ -237,7 +237,7 @@ async def send_admin_message(self, msg: Message):
if self.agent_admin_key and self.admin_key:
msg = await crypto.pack_message(
self.wallet_handle,
Serializer.pack(msg),
Serializer.serialize(msg).decode('utf-8'),
[self.admin_key],
self.agent_admin_key
)
Expand All @@ -251,7 +251,7 @@ async def unpack_wire_msg(self, wire_msg) -> Optional:
# Try to unpack message assuming it's not encrypted
msg = ""
try:
msg = Serializer.unpack(wire_msg)
msg = Serializer.deserialize(wire_msg)
except Exception as e:
print("Message encrypted, attempting to unpack...")

Expand Down
21 changes: 0 additions & 21 deletions python/helpers.py

This file was deleted.

5 changes: 2 additions & 3 deletions python/message.py
Expand Up @@ -2,9 +2,8 @@
received messages.
"""
import json
from collections import UserDict

import uuid
from collections import UserDict


class Message(UserDict):
Expand All @@ -19,7 +18,7 @@ def __init__(self, *args, **kwargs):
other things: ambiguous data. Interpretation defined by type and id.
"""
UserDict.__init__(self,*args, **kwargs)
UserDict.__init__(self, *args, **kwargs)
self.context = {}
# Assign it an ID
if '@id' not in self.data:
Expand Down
4 changes: 2 additions & 2 deletions python/modules/__init__.py
Expand Up @@ -6,14 +6,14 @@ class Module:
PROBLEM_REPORT = 'problem_report'

@staticmethod
def build_problem_report_for_connections(family, problem_code, problem_str):
def build_problem_report_for_connections(family, problem_code, problem_str) -> Message:
return Message({
"@type": "{}/problem_report".format(family),
"problem-code": problem_code,
"explain": problem_str
})

async def validate_common_message_blocks(self, msg, family):
async def validate_common_message_blocks(self, msg, family) -> int:
"""
Validate threading, timing blocks in messages and return error message to sender when invalid
"""
Expand Down
11 changes: 6 additions & 5 deletions python/modules/admin.py
@@ -1,11 +1,12 @@
import aiohttp_jinja2
import json
import socket

import aiohttp_jinja2
from indy import pairwise
from indy_sdk_utils import get_wallet_records

from router.simple_router import SimpleRouter
from indy_sdk_utils import get_wallet_records
from python_agent_utils.messages.message import Message
from router.simple_router import SimpleRouter
from . import Module


Expand All @@ -22,10 +23,10 @@ def __init__(self, agent):
self.router = SimpleRouter()
self.router.register(self.STATE_REQUEST, self.state_request)

async def route(self, msg: Message) -> Message:
async def route(self, msg: Message) -> None:
return await self.router.route(msg)

async def state_request(self, _) -> Message:
async def state_request(self, _) -> None:
print("Processing state_request")

if self.agent.initialized:
Expand Down
6 changes: 3 additions & 3 deletions python/modules/admin_walletconnection.py
Expand Up @@ -25,12 +25,12 @@ def __init__(self, agent):
self.router.register(AdminWalletConnection.CONNECT, self.connect)
self.router.register(AdminWalletConnection.DISCONNECT, self.disconnect)

async def route(self, msg: Message) -> Message:
async def route(self, msg: Message) -> None:
""" Route a message to its registered callback
"""
return await self.router.route(msg)

async def connect(self, msg: Message) -> Message:
async def connect(self, msg: Message) -> None:
""" Connect to an existing wallet.
"""
try:
Expand All @@ -46,7 +46,7 @@ async def connect(self, msg: Message) -> Message:
# Prompt a STATE message.
return await self.agent.modules[Admin.FAMILY].state_request(None)

async def disconnect(self, _) -> Message:
async def disconnect(self, _) -> None:
""" Disconnect from an existing wallet.
"""
await self.agent.disconnect_wallet()
Expand Down
8 changes: 6 additions & 2 deletions python/modules/basicmessage.py
Expand Up @@ -26,7 +26,9 @@ def __init__(self, agent):
self.router.register(AdminBasicMessage.SEND_MESSAGE, self.send_message)
self.router.register(AdminBasicMessage.GET_MESSAGES, self.get_messages)

async def route(self, msg: Message) -> Message:
async def route(self, msg: Message) -> None:
""" Route a message to its registered callback.
"""
return await self.router.route(msg)

async def send_message(self, msg: Message) -> Message:
Expand Down Expand Up @@ -119,7 +121,9 @@ def __init__(self, agent):
self.router = SimpleRouter()
self.router.register(BasicMessage.MESSAGE, self.receive_message)

async def route(self, msg: Message) -> Message:
async def route(self, msg: Message) -> None:
""" Route a message to its registered callback.
"""
return await self.router.route(msg)

async def receive_message(self, msg: Message):
Expand Down
39 changes: 19 additions & 20 deletions python/modules/connection.py
Expand Up @@ -12,10 +12,9 @@
from indy import did, pairwise, non_secrets, error

import indy_sdk_utils as utils
import serializer.json_serializer as Serializer
from python_agent_utils.messages.did_doc import DIDDoc
from python_agent_utils.messages.connection import Connection as ConnectionMessage
from router.simple_router import SimpleRouter
from serializer.json_serializer import JSONSerializer as Serializer
from . import Module
from python_agent_utils.messages.message import Message

Expand Down Expand Up @@ -58,10 +57,10 @@ def __init__(self, agent):
self.router.register(AdminConnection.SEND_REQUEST, self.send_request)
self.router.register(AdminConnection.SEND_RESPONSE, self.send_response)

async def route(self, msg: Message) -> Message:
async def route(self, msg: Message) -> None:
return await self.router.route(msg)

async def generate_invite(self, msg: Message) -> Message:
async def generate_invite(self, msg: Message) -> None:
""" Generate new connection invitation.
This interaction represents an out-of-band communication channel. In the future and in
Expand Down Expand Up @@ -108,7 +107,7 @@ async def generate_invite(self, msg: Message) -> Message:
})

b64_invite = \
base64.urlsafe_b64encode(bytes(Serializer.pack(invite_msg), 'utf-8')).decode('ascii')
base64.urlsafe_b64encode(Serializer.serialize(invite_msg)).decode('ascii')

await self.agent.send_admin_message(
Message({
Expand All @@ -117,7 +116,7 @@ async def generate_invite(self, msg: Message) -> Message:
})
)

async def receive_invite(self, msg: Message) -> Message:
async def receive_invite(self, msg: Message) -> None:
""" Receive and save invite.
This interaction represents an out-of-band communication channel. In the future and in
Expand Down Expand Up @@ -158,7 +157,7 @@ async def receive_invite(self, msg: Message) -> Message:
if not matches:
raise BadInviteException("Invite string is improperly formatted")

invite_msg = Serializer.unpack(
invite_msg = Serializer.deserialize(
base64.urlsafe_b64decode(matches.group(2)).decode('utf-8')
)

Expand All @@ -179,11 +178,11 @@ async def receive_invite(self, msg: Message) -> Message:
self.agent.wallet_handle,
'invitations',
invite_msg['recipientKeys'][0],
Serializer.pack(pending_connection),
Serializer.serialize(pending_connection).decode('utf-8'),
'{}'
)

async def send_request(self, msg: Message):
async def send_request(self, msg: Message) -> None:
""" Recall invite message from wallet and prepare and send request to the inviter.
send_request message format:
Expand All @@ -204,7 +203,7 @@ async def send_request(self, msg: Message):
}
}
"""
pending_connection = Serializer.unpack(
pending_connection = Serializer.deserialize(
json.loads(
await non_secrets.get_wallet_record(
self.agent.wallet_handle,
Expand Down Expand Up @@ -273,11 +272,11 @@ async def send_request(self, msg: Message):
await non_secrets.update_wallet_record_value(self.agent.wallet_handle,
'invitations',
pending_connection['connection_key'],
Serializer.pack(pending_connection))
Serializer.serialize(pending_connection).decode('utf-8'))

await self.agent.send_admin_message(pending_connection)

async def send_response(self, msg: Message) -> Message:
async def send_response(self, msg: Message) -> None:
""" Send response to request.
send_response message format:
Expand Down Expand Up @@ -335,7 +334,7 @@ async def send_response(self, msg: Message) -> Message:
response_msg['connection~sig'] = await self.agent.sign_agent_message_field(response_msg['connection'], pairwise_meta["connection_key"])
del response_msg['connection']

pending_connection = Serializer.unpack(
pending_connection = Serializer.deserialize(
json.loads(
await non_secrets.get_wallet_record(self.agent.wallet_handle,
'invitations',
Expand Down Expand Up @@ -374,10 +373,10 @@ def __init__(self, agent):
self.router.register(Connection.REQUEST, self.request_received)
self.router.register(Connection.RESPONSE, self.response_received)

async def route(self, msg: Message) -> Message:
async def route(self, msg: Message) -> None:
return await self.router.route(msg)

async def request_received(self, msg: Message) -> Message:
async def request_received(self, msg: Message) -> None:
""" Received connection request.
Request format:
Expand Down Expand Up @@ -407,7 +406,7 @@ async def request_received(self, msg: Message) -> Message:
"""
r = await self.validate_common_message_blocks(msg, Connection.FAMILY)
if not r:
return r
return

try:
ConnectionMessage.Request.validate(msg)
Expand Down Expand Up @@ -476,7 +475,7 @@ async def request_received(self, msg: Message) -> Message:
self.agent.wallet_handle,
'invitations',
connection_key,
Serializer.pack(pending_connection),
Serializer.serialize(pending_connection).decode('utf-8'),
'{}'
)
except error.IndyError as indy_error:
Expand All @@ -485,7 +484,7 @@ async def request_received(self, msg: Message) -> Message:
raise indy_error
await self.agent.send_admin_message(pending_connection)

async def response_received(self, msg: Message) -> Message:
async def response_received(self, msg: Message) -> None:
""" Process response
Response format:
Expand All @@ -499,7 +498,7 @@ async def response_received(self, msg: Message) -> Message:
"""
r = await self.validate_common_message_blocks(msg, Connection.FAMILY)
if not r:
return r
return

my_did = msg.context['to_did']
if my_did is None:
Expand Down Expand Up @@ -571,7 +570,7 @@ async def response_received(self, msg: Message) -> Message:
})
)

pending_connection = Serializer.unpack(
pending_connection = Serializer.deserialize(
json.loads(
await non_secrets.get_wallet_record(self.agent.wallet_handle,
'invitations',
Expand Down
10 changes: 5 additions & 5 deletions python/modules/protocol_discovery.py
Expand Up @@ -20,10 +20,10 @@ def __init__(self, agent):
self.router = SimpleRouter()
self.router.register(AdminProtocolDiscovery.SEND_QUERY, self.send_query)

async def route(self, msg: Message):
async def route(self, msg: Message) -> None:
return await self.router.route(msg)

async def send_query(self, msg: Message):
async def send_query(self, msg: Message) -> None:
query_msg = Message({
'@type': ProtocolDiscovery.QUERY,
'query': msg['query']
Expand Down Expand Up @@ -54,10 +54,10 @@ def __init__(self, agent):
self.router.register(ProtocolDiscovery.QUERY, self.query_received)
self.router.register(ProtocolDiscovery.DISCLOSE, self.disclose_received)

async def route(self, msg: Message):
async def route(self, msg: Message) -> None:
return await self.router.route(msg)

async def query_received(self, msg: Message):
async def query_received(self, msg: Message) -> None:
await self.agent.send_admin_message(
Message({
'@type': AdminProtocolDiscovery.QUERY_RECEIVED,
Expand Down Expand Up @@ -92,7 +92,7 @@ def map_query(char):
})
)

async def disclose_received(self, msg: Message):
async def disclose_received(self, msg: Message) -> None:
await self.agent.send_admin_message(
Message({
'@type': AdminProtocolDiscovery.DISCLOSE_RECEIVED,
Expand Down

0 comments on commit 296580c

Please sign in to comment.