async def request_received(self, msg: Message) -> Message: their_endpoint_uri = msg['endpoint'] my_did_str = msg['did'] my_did_info_str = await did.get_my_did_with_meta( self.agent.wallet_handle, my_did_str) my_did_info_json = json.loads(my_did_info_str) my_verkey = my_did_info_json['verkey'] metadata_str = my_did_info_json['metadata'] metadata_dict = json.loads(metadata_str) conn_name = metadata_dict['conn_name'] message_bytes = str_to_bytes(msg['content']) message_bytes = base64.b64decode(message_bytes) their_key_str, their_data_bytes = await crypto.auth_decrypt( self.agent.wallet_handle, my_verkey, message_bytes) # change verkey passed via send_invite to the agent without encryption my_new_verkey = await did.replace_keys_start(self.agent.wallet_handle, my_did_str, '{}') await did.replace_keys_apply(self.agent.wallet_handle, my_did_str) their_data_json = json.loads(bytes_to_str(their_data_bytes)) their_did_str = their_data_json['did'] identity_json = json.dumps({ "did": their_did_str, "verkey": their_key_str }) meta_json = json.dumps({ "conn_name": conn_name, "their_endpoint": their_endpoint_uri, "their_verkey": their_key_str, "my_verkey": my_new_verkey }) await did.store_their_did(self.agent.wallet_handle, identity_json) await pairwise.create_pairwise(self.agent.wallet_handle, their_did_str, my_did_str, meta_json) return Message({ 'type': CONN_UI.REQUEST_RECEIVED, 'content': { 'name': conn_name, 'endpoint_did': their_did_str, 'history': msg } })
async def response_received(self, msg: Message) -> Message: my_did_str = msg['did'] my_did_info_str = await did.get_my_did_with_meta( self.agent.wallet_handle, my_did_str) my_did_info_json = json.loads(my_did_info_str) my_verkey = my_did_info_json['verkey'] metadata_str = my_did_info_json['metadata'] metadata_dict = json.loads(metadata_str) conn_name = metadata_dict['conn_name'] their_endpoint = metadata_dict['their_endpoint'] message_bytes = str_to_bytes(msg['content']) message_bytes = base64.b64decode(message_bytes) their_key_str, their_data_bytes = await crypto.auth_decrypt( self.agent.wallet_handle, my_verkey, message_bytes) their_data_json = json.loads(bytes_to_str(their_data_bytes)) their_did_str = their_data_json['did'] identity_json = json.dumps({ "did": their_did_str, "verkey": their_key_str }) meta_json = json.dumps({ "conn_name": conn_name, "their_endpoint": their_endpoint, "their_verkey": their_key_str, "my_verkey": my_verkey }) await did.store_their_did(self.agent.wallet_handle, identity_json) await pairwise.create_pairwise(self.agent.wallet_handle, their_did_str, my_did_str, meta_json) # pairwise connection between agents is established to this point return Message({ 'type': CONN_UI.RESPONSE_RECEIVED, 'id': self.agent.ui_token, 'content': { 'name': conn_name, 'their_did': their_did_str, 'history': msg } })
async def unpack_agent_message(self, wire_msg_bytes): wire_msg = json.loads(wire_msg_bytes) # get key from wallet from to: did # TODO: add error reporting if DID not found #load my key list did_list_str = await did.list_my_dids_with_meta(self.wallet_handle) did_list = json.loads(did_list_str) my_verkey_to_did = {} for d in did_list: my_verkey_to_did[d['verkey']] = d['did'] if wire_msg['to'] not in my_verkey_to_did: raise Exception("Unknown recipient key") #load pairwise pairwise_list_str = await pairwise.list_pairwise(self.wallet_handle) pairwise_list = json.loads(pairwise_list_str) their_verkey_to_did = {} for p_str in pairwise_list: p = json.loads(p_str) p_meta = json.loads(p['metadata']) their_verkey_to_did[p_meta['their_verkey']] = p['their_did'] from_did = None if wire_msg['from'] in their_verkey_to_did: from_did = their_verkey_to_did[wire_msg['from']] # now process payload message_bytes = base64.b64decode(wire_msg['payload']) their_key_str, their_data_bytes = await crypto.auth_decrypt( self.wallet_handle, wire_msg['to'], message_bytes) msg_str = bytes_to_str(their_data_bytes) msg_json = json.loads(msg_str) msg = Message(msg_json) msg.context = { 'from_did': from_did, # will be none if unknown sender verkey 'from_key': their_key_str, 'to_key': wire_msg['to'], 'to_did': my_verkey_to_did[wire_msg['to']] } return msg
async def receive_message(self, msg: Message) -> Message: their_did_str = msg['from'] pairwise_conn_info_str = await pairwise.get_pairwise(self.agent.wallet_handle, their_did_str) pairwise_conn_info_json = json.loads(pairwise_conn_info_str) my_did_str = pairwise_conn_info_json['my_did'] metadata = json.loads(pairwise_conn_info_json['metadata']) my_did_info_str = await did.get_my_did_with_meta(self.agent.wallet_handle, my_did_str) my_did_info_json = json.loads(my_did_info_str) my_verkey = my_did_info_json['verkey'] message_bytes = str_to_bytes(msg['message']) message_bytes = base64.b64decode(message_bytes) their_key_str, their_data_bytes = await crypto.auth_decrypt( self.agent.wallet_handle, my_verkey, message_bytes) their_data_json = json.loads(bytes_to_str(their_data_bytes)) # store message in the wallet await non_secrets.add_wallet_record( self.agent.wallet_handle, "basicmessage", uuid.uuid4().hex, json.dumps({ 'from': their_did_str, 'timestamp': their_data_json['timestamp'], 'content': their_data_json['content'] }), json.dumps({ "their_did": their_did_str }) ) return Message({ '@type': ADMIN_BASICMESSAGE.MESSAGE_RECEIVED, 'id': self.agent.ui_token, 'with': their_did_str, 'message': { 'from': their_did_str, 'timestamp': their_data_json['timestamp'], 'content': their_data_json['content'] } })
def get_zip_code(self): """ Hit the GeoNames API to get a ZIP code for a given (lat, long) See more info about the API endpoint here: http://www.geonames.org/export/web-services.html """ # Make a request to the GeoNames API for the closest ZIP # to the given (lat, long) formatted_url = GEONAMES_HOST + '/' + \ POSTAL_CODE_MAPPING_ENDPOINT + \ '?lat=' + str(self.latitude) + \ '&lng=' + str(self.longitude) + \ '&username='******'postalCodes'][0]['postalCode'] return zip_code
def get_neighborhood(self): """ Hit the Socrata SF Data API to get a neighborhood for a given (lat, long) See more info about dataset here: https://data.sfgov.org/Geographic-Locations-and-Boundaries/SF-Find-Neighborhoods/pty2-tcw4 """ # Make a request to the SF Data Socrata API # endpoint for neighborhood data formatted_url = SF_DATA_HOST + '/resource/' + \ NEIGHBORHOOD_BOUNDARIES_RESOURCE_ID + \ ".json?$where=intersects(the_geom,'POINT+(" + \ str(self.longitude) + '+' + \ str(self.latitude) + ")')" # Pass the Socrata API token headers = {'X-App-Token': self.socrata_api_token} r = requests.get(formatted_url, headers=headers) unicode_content = helpers.bytes_to_str(r.content) neighborhood = json.loads(unicode_content)[0]['name'] return neighborhood
async def message_received(self, msg: Message) -> Message: my_did_str = msg['did'] their_did_str = "" conn_name = "" my_verkey = "" self.agent_pairwises_list_str = await pairwise.list_pairwise( self.agent.wallet_handle) self.agent_pairwises_list = json.loads(self.agent_pairwises_list_str) for self.agent_pairwise_str in self.agent_pairwises_list: self.agent_pairwise_json = json.loads(self.agent_pairwise_str) if not self.agent_pairwise_json['my_did'] == my_did_str: continue their_did_str = self.agent_pairwise_json['their_did'] metadata_str = self.agent_pairwise_json['metadata'] metadata_json = json.loads(metadata_str) conn_name = metadata_json['conn_name'] my_verkey = metadata_json['my_verkey'] message_bytes = str_to_bytes(msg['content']) message_bytes = base64.b64decode(message_bytes) their_key_str, their_data_bytes = await crypto.auth_decrypt( self.agent.wallet_handle, my_verkey, message_bytes) their_data_json = json.loads(bytes_to_str(their_data_bytes)) return Message({ 'type': CONN_UI.MESSAGE_RECEIVED, 'id': self.agent.ui_token, 'content': { 'name': conn_name, 'their_did': their_did_str, 'history': their_data_json } })