async def handle(cls, agent_name: str, wire_message: bytes, my_label: str = None, my_endpoint: str = None) -> bool: unpacked = await WalletAgent.unpack_message(agent_name, wire_message) kwargs = json.loads(unpacked['message']) message = Message(**kwargs) print('***** feature 0037 handle message *****') if 'sender_verkey' in unpacked: sender_verkey = unpacked['sender_verkey'] else: sender_verkey = None if 'recipient_verkey' in unpacked: recipient_verkey = unpacked['recipient_verkey'] else: recipient_verkey = None print('sender_verkey: ' + sender_verkey) print('recipient_verkey: ' + recipient_verkey) print(json.dumps(message.to_dict(), indent=2, sort_keys=True)) print('***************************************') if message.get('@type', None) is None: return False if not cls.endorsement(message): return False state_machine_id = cls.get_state_machine_id(unpacked['sender_verkey']) if message.type in [ PresentProofProtocol.REQUEST_PRESENTATION, AckMessage.ACK ]: if message.type == PresentProofProtocol.REQUEST_PRESENTATION: machine_class = PresentProofProtocol.ProverStateMachine await WalletAgent.start_state_machine( status=PresentProofStatus.Null, ttl=PresentProofProtocol.STATE_MACHINE_TTL, agent_name=agent_name, machine_class=machine_class, machine_id=state_machine_id) await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message) return True elif message.type in [PresentProofProtocol.PRESENTATION]: await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message) return True elif message.type == PresentProofProtocol.PROBLEM_REPORT: await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message) return True else: return False
def extract_verkey_endpoint(msg: Message, key: str) -> (Optional, Optional): """ Extract verkey and endpoint that will be used to send message back to the sender of this message. Might return None. """ did_doc = BasicMessage.extract_did_doc(msg, key) vks = did_doc.get('publicKey') vk = vks[0].get('publicKeyBase58') if vks and isinstance( vks, list) and len(vks) > 0 else None endpoints = msg.get(key, {}).get(DIDDoc.DID_DOC, {}).get('service') endpoint = endpoints[0].get( 'serviceEndpoint') if endpoints and isinstance( endpoints, list) and len(endpoints) > 0 else None return vk, endpoint
async def handle(cls, agent_name: str, wire_message: bytes, my_label: str = None, my_endpoint: str = None) -> bool: unpacked = await WalletAgent.unpack_message(agent_name, wire_message) kwargs = json.loads(unpacked['message']) message = Message(**kwargs) if message.get('@type', None) is None: return False if not cls.endorsement(message): return False for protocol_version in ["1.1", "1.0"]: type_issue_credential = cls.set_protocol_version( cls.ISSUE_CREDENTIAL, protocol_version) type_offer_credential = cls.set_protocol_version( cls.OFFER_CREDENTIAL, protocol_version) type_request_credential = cls.set_protocol_version( cls.REQUEST_CREDENTIAL, protocol_version) state_machine_id = cls.get_state_machine_id( unpacked['sender_verkey']) if message.type in [type_issue_credential, type_offer_credential]: machine_class = IssueCredentialProtocol.HolderSateMachine if message.type == type_offer_credential: await WalletAgent.start_state_machine( status=IssueCredentialStatus.Null, ttl=IssueCredentialProtocol.STATE_MACHINE_TTL, agent_name=agent_name, machine_class=machine_class, machine_id=state_machine_id, protocol_version=protocol_version) await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message) return True elif message.type in [type_request_credential, AckMessage.ACK]: await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message) return True return False
async def handle(cls, agent_name: str, wire_message: bytes, my_label: str=None, my_endpoint: str=None) -> bool: unpacked = await WalletAgent.unpack_message(agent_name, wire_message) kwargs = json.loads(unpacked['message']) message = Message(**kwargs) if message.get('@type', None) is None: return False if message.type == cls.REQUEST: state_machine_id = unpacked['sender_verkey'] machine_class = DIDExchange.DIDExchangeInviterStateMachine await WalletAgent.start_state_machine( agent_name=agent_name, machine_class=machine_class, machine_id=state_machine_id, endpoint=my_endpoint, label=my_label, status=DIDExchangeStatus.Invited ) await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message ) return True elif message.type == DIDExchange.RESPONSE: state_machine_id = message['connection~sig']['signer'] await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message ) return True elif message.type in [TrustPing.PING, TrustPing.PING_RESPONSE]: state_machine_id = unpacked['sender_verkey'] await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message ) return True elif message.type == DIDExchange.PROBLEM_REPORT: state_machine_id = message.to_dict().get('connection~sig', {}).get('signer') if state_machine_id: await WalletAgent.invoke_state_machine( agent_name=agent_name, id_=state_machine_id, content_type=cls.WIRED_CONTENT_TYPE, data=wire_message ) return True else: logging.error('Problem report', message.as_json()) return True else: return False
async def __receive_connection_ack(self, msg: Message): if self.status == DIDExchangeStatus.Responded: try: TrustPing.Ping.validate(msg) if msg.get('response_requested'): pong = TrustPing.Pong.build(msg.id) to_did = msg.context['to_did'] await DIDExchange.send_message_to_agent(to_did, pong, self.get_wallet()) except: err_msg = DIDExchange.build_problem_report_for_connections( DIDExchange.RESPONSE_FOR_UNKNOWN_REQUEST, 'Uncknown ack thread id', thread_id=msg.id ) to_did = msg.context['to_did'] await DIDExchange.send_message_to_agent(to_did, err_msg, self.get_wallet()) else: await self.done() else: raise ImpossibleStatus()
def extract_did_doc(msg: Message, key: str) -> dict: return msg.get(key, {}).get(DIDDoc.DID_DOC, {}) or msg.get( key, {}).get(DIDDoc.VCX_DID_DOC, {})