async def find_inbound_connection( self, receipt: MessageReceipt) -> ConnectionRecord: """ Deserialize an incoming message and further populate the request context. Args: receipt: The message receipt Returns: The `ConnectionRecord` associated with the expanded message, if any """ cache_key = None connection = None resolved = False if receipt.sender_verkey and receipt.recipient_verkey: cache_key = (f"connection_by_verkey::{receipt.sender_verkey}" f"::{receipt.recipient_verkey}") cache: BaseCache = await self.context.inject(BaseCache, required=False) if cache: async with cache.acquire(cache_key) as entry: if entry.result: cached = entry.result receipt.sender_did = cached["sender_did"] receipt.recipient_did_public = cached[ "recipient_did_public"] receipt.recipient_did = cached["recipient_did"] connection = await ConnectionRecord.retrieve_by_id( self.context, cached["id"]) else: connection = await self.resolve_inbound_connection( receipt) if connection: cache_val = { "id": connection.connection_id, "sender_did": receipt.sender_did, "recipient_did": receipt.recipient_did, "recipient_did_public": receipt.recipient_did_public, } await entry.set_result(cache_val, 3600) resolved = True if not connection and not resolved: connection = await self.resolve_inbound_connection(receipt) return connection
async def resolve_inbound_connection( self, receipt: MessageReceipt) -> ConnectionRecord: """ Populate the receipt DID information and find the related `ConnectionRecord`. Args: receipt: The message receipt Returns: The `ConnectionRecord` associated with the expanded message, if any """ if receipt.sender_verkey: try: receipt.sender_did = await self.find_did_for_key( receipt.sender_verkey) except StorageNotFoundError: self._logger.warning( "No corresponding DID found for sender verkey: %s", receipt.sender_verkey, ) if receipt.recipient_verkey: try: wallet: BaseWallet = await self.context.inject(BaseWallet) my_info = await wallet.get_local_did_for_verkey( receipt.recipient_verkey) receipt.recipient_did = my_info.did if "public" in my_info.metadata and my_info.metadata[ "public"] is True: receipt.recipient_did_public = True except InjectorError: self._logger.warning( "Cannot resolve recipient verkey, no wallet defined by " "context: %s", receipt.recipient_verkey, ) except WalletNotFoundError: self._logger.warning( "No corresponding DID found for recipient verkey: %s", receipt.recipient_verkey, ) return await self.find_connection(receipt.sender_did, receipt.recipient_did, receipt.recipient_verkey, True)