From 1ac3a10e4475873109b98d8beceaa8c115a5eed7 Mon Sep 17 00:00:00 2001 From: fabienpe Date: Thu, 25 Apr 2019 15:23:48 +0200 Subject: [PATCH] Fixed unbound variable msg in start() which can make code crash in some cases. Fixed additional minor PEP8 issues. Signed-off-by: fabienpe --- python/agent.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/python/agent.py b/python/agent.py index 9a27cb5c..1990834e 100644 --- a/python/agent.py +++ b/python/agent.py @@ -1,24 +1,25 @@ """ Module defining Agent class and related methods """ -import json -import base64 import asyncio +import base64 +import json import struct -import traceback import time +import traceback import aiohttp -from indy import wallet, did, error, crypto, pairwise, non_secrets +from indy import wallet, did, error, crypto, pairwise import indy_sdk_utils as utils -from serializer import json_serializer as Serializer -from helpers import bytes_to_str, serialize_bytes_json, str_to_bytes from message import Message from router.family_router import FamilyRouter +from serializer import json_serializer as Serializer + class WalletConnectionException(Exception): pass + class Agent: """ Agent class storing all needed elements for agent operation. """ @@ -53,6 +54,7 @@ async def start(self): wire_msg_bytes = await self.message_queue.get() # Try to unpack message assuming it's not encrypted + msg = "" try: msg = Serializer.unpack(wire_msg_bytes) except Exception as e: @@ -71,8 +73,8 @@ async def start(self): await self.route_message_to_module(msg) except Exception as e: - print("\n\n--- Message Processing failed --- \n\n") - traceback.print_exc() + print("\n\n--- Message Processing failed --- \n\n") + traceback.print_exc() async def connect_wallet(self, agent_name, passphrase, ephemeral=False): """ Create if not already exists and open wallet. @@ -105,7 +107,7 @@ async def connect_wallet(self, agent_name, passphrase, ephemeral=False): await wallet.create_wallet(wallet_config, wallet_credentials) except error.IndyError as e: if e.error_code is error.ErrorCode.WalletAlreadyExistsError: - pass # This is ok, and expected. + pass # This is ok, and expected. else: print("Unexpected Indy Error: {}".format(e)) except Exception as e: @@ -162,7 +164,6 @@ async def unpack_and_verify_signed_agent_message_field(self, signed_field): fieldjson = data_bytes[8:] return json.loads(fieldjson), sig_verified - async def unpack_agent_message(self, wire_msg_bytes): if isinstance(wire_msg_bytes, str): wire_msg_bytes = bytes(wire_msg_bytes, 'utf-8') @@ -174,6 +175,7 @@ async def unpack_agent_message(self, wire_msg_bytes): ) from_key = None + from_did = None if 'sender_verkey' in unpacked: from_key = unpacked['sender_verkey'] from_did = await utils.did_for_key(self.wallet_handle, unpacked['sender_verkey']) @@ -184,14 +186,14 @@ async def unpack_agent_message(self, wire_msg_bytes): msg = Serializer.unpack(unpacked['message']) msg.context = { - 'from_did': from_did, # Could be None - 'to_did': to_did, # Could be None - 'from_key': from_key, # Could be None + 'from_did': from_did, # Could be None + 'to_did': to_did, # Could be None + 'from_key': from_key, # Could be None 'to_key': to_key } return msg - async def send_message_to_agent(self, to_did, msg:Message): + async def send_message_to_agent(self, to_did, msg: Message): print("Sending:", msg) their_did = to_did