added some support for ens names w/ burnettk

This commit is contained in:
jasquat 2023-05-11 15:47:26 -04:00
parent 6bd6260f2f
commit 5b77d28df8
No known key found for this signature in database
2 changed files with 41 additions and 11 deletions

View File

@ -5,4 +5,4 @@
curl http://wakunode2.spiffworkflow.org -XPOST -H 'content-type: application/json' -d '{"jsonrpc":"2.0","method":"wakuext_sendOneToOneMessage","params":[{"id": "zQ3shh9nNUbuLzjzvjgUwxAKb3HEDMrVrNzyTW1ZUrFgcLhxc", "message": "hello dude what is happening now thursday demo"}],"id":1}'
# get public key from ens name
curl http://localhost:8545 -XPOST -H 'content-type: application/json' -d '{"jsonrpc":"2.0","method":"ens_publicKeyOf","params":["1", "cammellos.eth"], "id":1}'
curl http://localhost:8545 -XPOST -H 'content-type: application/json' -d '{"jsonrpc":"2.0","method":"ens_publicKeyOf","params":[1, "cammellos.eth"], "id":1}'

View File

@ -1,7 +1,7 @@
"""SendMessage."""
import json
from dataclasses import dataclass
from typing import Optional
from typing import Optional, Tuple
import requests
from requests.exceptions import HTTPError
@ -33,33 +33,47 @@ class SendMessage:
message_type: str
recipient: list[str]
def send_message(self, message_type_to_use: str, rec: str, message_to_send: Optional[str] = None) -> None:
def send_message(
self, message_type_to_use: str, rec: str, message_to_send: Optional[str] = None, params: Optional[list] = None
) -> Tuple[dict, int, bool]:
url = f'{current_app.config["CONNECTOR_PROXY_WAKU_BASE_URL"]}'
headers = {"Accept": "application/json", "Content-type": "application/json"}
if params is None:
params = [{"id": rec, "message": message_to_send}]
request_body = {
"jsonrpc": "2.0",
"method": message_type_to_use,
"params": [{"id": rec, "message": message_to_send}],
"params": params,
"id": 1,
}
response = {}
status_code = None
successful = False
try:
raw_response = requests.post(url, json.dumps(request_body), headers=headers)
raw_response.raise_for_status()
status_code = raw_response.status_code
parsed_response = json.loads(raw_response.text)
response = parsed_response
if not self.response_has_error(response) and status_code == 200:
successful = True
except HTTPError as ex:
status_code = ex.response.status_code
response['error'] = str(ex)
except Exception as ex:
response['error'] = str(ex)
status_code = 500
return (response, status_code)
return (response, status_code, successful)
def execute(self, config, task_data):
def response_has_error(self, response: dict) -> bool:
if 'error' in response:
return True
if 'result' in response:
return response['result'] == "0x0400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
return False
def execute(self, _config, _task_data):
"""Execute."""
responses = []
all_calls_returned_200 = True
@ -69,14 +83,30 @@ class SendMessage:
# alongside the contact request. But the message also appeared in your messages foreever and ever, which was not
# very compatible with sending a contact request with every message. Now, if you are not already a contact, you get
# a prompt to approve the request, and then you get all of the messages from the "spiff" user.
response, status_code = self.send_message('wakuext_addContact', rec)
if status_code == 200:
response, status_code = self.send_message(self.message_type, rec, self.message)
if status_code != 200:
all_calls_returned_200 = False
receiver_name = rec
status_code = None
response = None
successful = False
ens_lookup_failed = False
if receiver_name.endswith('.eth'):
response, status_code, successful = self.send_message('ens_publicKeyOf', receiver_name, params=[1, receiver_name])
if successful:
receiver_name = response['result']
else:
all_calls_returned_200 = False
ens_lookup_failed = True
if not ens_lookup_failed:
response, status_code, successful = self.send_message('wakuext_addContact', receiver_name)
if successful:
response, status_code, successful = self.send_message(self.message_type, receiver_name, self.message)
else:
all_calls_returned_200 = False
responses.append({
"response": response,
"successful": successful,
"status": status_code,
})
return ({