fixing the decrypted tags thing

This commit is contained in:
Believethehype
2023-11-27 10:35:58 +01:00
parent 08137790d4
commit 248963ce90
9 changed files with 87 additions and 101 deletions

View File

@@ -1,4 +1,8 @@
import typing
import requests
from nostr_sdk import Event, Tag
from utils.definitions import EventDefinitions
from utils.nostr_utils import get_event_by_id
@@ -50,17 +54,16 @@ def get_task(event, client, dvmconfig):
return "unknown type"
def check_task_is_supported(event, tags, client, get_duration=False, config=None):
def check_task_is_supported(event: Event, client, get_duration=False, config=None):
try:
dvm_config = config
input_value = ""
input_type = ""
duration = 1
task = get_task(event, client=client, dvmconfig=dvm_config)
try:
for tag in tags:
for tag in event.tags():
if tag.as_vec()[0] == 'i':
if len(tag.as_vec()) < 3:
print("Job Event missing/malformed i tag, skipping..")
@@ -75,7 +78,7 @@ def check_task_is_supported(event, tags, client, get_duration=False, config=None
return False, "", 0
elif input_type == 'url' and check_url_is_readable(input_value) is None:
print("Url not readable / supported")
return False, task, duration#
return False, task, duration #
elif tag.as_vec()[0] == 'output':
# TODO move this to individual modules
@@ -85,13 +88,13 @@ def check_task_is_supported(event, tags, client, get_duration=False, config=None
or output == "image/png" or "image/jpg"
or output == "image/png;format=url" or output == "image/jpg;format=url"
or output == ""):
print("Output format not supported, skipping..")
return False, "", 0
print("Output format not supported, skipping..")
return False, "", 0
except Exception as e:
print("Check task 2: " + str(e))
for dvm in dvm_config.SUPPORTED_DVMS:
print(dvm.TASK)
if dvm.TASK == task:
if not dvm.is_input_supported(input_type, event.content()):
return False, task, duration
@@ -130,10 +133,11 @@ def check_url_is_readable(url):
def get_amount_per_task(task, dvm_config, duration=1):
for dvm in dvm_config.SUPPORTED_DVMS: #this is currently just one
for dvm in dvm_config.SUPPORTED_DVMS: # this is currently just one
if dvm.TASK == task:
amount = dvm.COST * duration
return amount
else:
print("["+dvm_config.SUPPORTED_DVMS[0].NAME +"] Task " + task + " is currently not supported by this instance, skipping")
print("[" + dvm_config.SUPPORTED_DVMS[
0].NAME + "] Task " + task + " is currently not supported by this instance, skipping")
return None

View File

@@ -1,6 +1,7 @@
import json
import typing
from datetime import timedelta
from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt
from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt, EventBuilder
def get_event_by_id(event_id: str, client: Client, config=None) -> Event | None:
@@ -61,38 +62,38 @@ def send_event(event: Event, client: Client, dvm_config) -> EventId:
def check_and_decrypt_tags(event, dvm_config):
tags = []
is_encrypted = False
p = ""
for tag in event.tags():
if tag.as_vec()[0] == 'encrypted':
is_encrypted = True
elif tag.as_vec()[0] == 'p':
p = tag.as_vec()[1]
try:
tags = []
is_encrypted = False
p = ""
sender = event.pubkey()
for tag in event.tags():
if tag.as_vec()[0] == 'encrypted':
is_encrypted = True
elif tag.as_vec()[0] == 'p':
p = tag.as_vec()[1]
if is_encrypted:
if p != Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex():
print("[" + dvm_config.NIP89.name + "] Task encrypted and not addressed to this DVM, "
"skipping..")
return None, None
if is_encrypted:
if p != Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex():
print("[" + dvm_config.NIP89.name + "] Task encrypted and not addressed to this DVM, "
"skipping..")
return None
elif p == Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex():
encrypted_tag = Tag.parse(["encrypted"])
p_tag = Tag.parse(["p", p])
elif p == Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex():
print("encrypted")
#encrypted_tag = Tag.parse(["encrypted"])
#p_tag = Tag.parse(["p", p])
tags_str = nip04_decrypt(Keys.from_sk_str(dvm_config.PRIVATE_KEY).secret_key(),
event.pubkey(), event.content())
params = json.loads(tags_str)
for element in params:
tags.append(Tag.parse(element))
# Keep the encrypted tag
tags.append(p_tag)
tags.append(encrypted_tag)
return tags, p
else:
return event.tags, p
tags_str = nip04_decrypt(Keys.from_sk_str(dvm_config.PRIVATE_KEY).secret_key(),
event.pubkey(), event.content())
#TODO add outer p tag so it doesnt have to be sent twice
params = json.loads(tags_str)
eventasjson = json.loads(event.as_json())
eventasjson['tags'] = params
eventasjson['content'] = ""
event = Event.from_json(json.dumps(eventasjson))
print(event.as_json())
except Exception as e:
print(e)
return event