mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-05-14 09:30:00 +02:00
fixing the decrypted tags thing
This commit is contained in:
parent
08137790d4
commit
248963ce90
12
bot.py
12
bot.py
@ -136,15 +136,16 @@ class Bot:
|
|||||||
bid_tag = Tag.parse(['bid', bid, bid])
|
bid_tag = Tag.parse(['bid', bid, bid])
|
||||||
relays_tag = Tag.parse(["relays", json.dumps(self.dvm_config.RELAY_LIST)])
|
relays_tag = Tag.parse(["relays", json.dumps(self.dvm_config.RELAY_LIST)])
|
||||||
alt_tag = Tag.parse(["alt", self.dvm_config.SUPPORTED_DVMS[index].TASK])
|
alt_tag = Tag.parse(["alt", self.dvm_config.SUPPORTED_DVMS[index].TASK])
|
||||||
|
p_tag = Tag.parse(['p', dvm_keys.public_key().to_hex()])
|
||||||
|
|
||||||
encrypted_params_string = json.dumps([i_tag.as_vec(), bid_tag.as_vec(),
|
encrypted_params_string = json.dumps([i_tag.as_vec(), bid_tag.as_vec(),
|
||||||
relays_tag.as_vec(), alt_tag.as_vec()])
|
relays_tag.as_vec(), alt_tag.as_vec(), p_tag.as_vec()])
|
||||||
|
|
||||||
print(encrypted_params_string)
|
print(encrypted_params_string)
|
||||||
|
|
||||||
encrypted_params = nip04_encrypt(self.keys.secret_key(), dvm_keys.public_key(),
|
encrypted_params = nip04_encrypt(self.keys.secret_key(), dvm_keys.public_key(),
|
||||||
encrypted_params_string)
|
encrypted_params_string)
|
||||||
p_tag = Tag.parse(['p', dvm_keys.public_key().to_hex()])
|
|
||||||
encrypted_tag = Tag.parse(['encrypted'])
|
encrypted_tag = Tag.parse(['encrypted'])
|
||||||
nip90request = EventBuilder(self.dvm_config.SUPPORTED_DVMS[index].KIND, encrypted_params,
|
nip90request = EventBuilder(self.dvm_config.SUPPORTED_DVMS[index].KIND, encrypted_params,
|
||||||
[p_tag, encrypted_tag]).to_event(self.keys)
|
[p_tag, encrypted_tag]).to_event(self.keys)
|
||||||
@ -220,14 +221,14 @@ class Bot:
|
|||||||
|
|
||||||
elif status == "payment-required" or status == "partial":
|
elif status == "payment-required" or status == "partial":
|
||||||
amount = 0
|
amount = 0
|
||||||
|
|
||||||
for tag in nostr_event.tags():
|
for tag in nostr_event.tags():
|
||||||
if tag.as_vec()[0] == "amount":
|
if tag.as_vec()[0] == "amount":
|
||||||
amount_msats = int(tag.as_vec()[1])
|
amount_msats = int(tag.as_vec()[1])
|
||||||
amount = int(amount_msats / 1000)
|
amount = int(amount_msats / 1000)
|
||||||
|
|
||||||
entry = next((x for x in self.job_list if x['event_id'] == etag), None)
|
entry = next((x for x in self.job_list if x['event_id'] == etag), None)
|
||||||
if entry is not None and entry['is_paid'] is False and entry['dvm_key'] == ptag:
|
if entry is not None and entry['is_paid'] is False and entry['dvm_key'] == ptag:
|
||||||
|
print("PAYMENT: " + nostr_event.as_json())
|
||||||
#if we get a bolt11, we pay and move on
|
#if we get a bolt11, we pay and move on
|
||||||
if len(tag.as_vec()) > 2:
|
if len(tag.as_vec()) > 2:
|
||||||
bolt11 = tag.as_vec()[2]
|
bolt11 = tag.as_vec()[2]
|
||||||
@ -236,7 +237,8 @@ class Bot:
|
|||||||
else:
|
else:
|
||||||
user = get_or_add_user(db=self.dvm_config.DB, npub=nostr_event.pubkey().to_hex(),
|
user = get_or_add_user(db=self.dvm_config.DB, npub=nostr_event.pubkey().to_hex(),
|
||||||
client=self.client, config=self.dvm_config)
|
client=self.client, config=self.dvm_config)
|
||||||
bolt11 = zap(user.lud16, amount, "Zap", nostr_event, self.keys, self.dvm_config, "private")
|
print("PAYING: " + user.name)
|
||||||
|
bolt11 = zap(user.lud16, amount, "Zap", nostr_event, self.keys, self.dvm_config, "public")
|
||||||
if bolt11 == None:
|
if bolt11 == None:
|
||||||
print("Receiver has no Lightning address")
|
print("Receiver has no Lightning address")
|
||||||
return
|
return
|
||||||
|
49
dvm.py
49
dvm.py
@ -83,24 +83,24 @@ class DVM:
|
|||||||
return
|
return
|
||||||
|
|
||||||
def handle_nip90_job_event(nip90_event):
|
def handle_nip90_job_event(nip90_event):
|
||||||
"""
|
|
||||||
:type nip90_event: Event
|
|
||||||
"""
|
|
||||||
|
|
||||||
tags: typing.List[Tag]
|
nip90_event = check_and_decrypt_tags(nip90_event, self.dvm_config)
|
||||||
tags, p_tag_str = check_and_decrypt_tags(nip90_event, self.dvm_config)
|
if nip90_event is None:
|
||||||
if p_tag_str is None:
|
|
||||||
return
|
return
|
||||||
nip90_event.tags = tags
|
|
||||||
cashu = ""
|
|
||||||
for tag in tags:
|
|
||||||
if tag.as_vec()[0] == "cashu":
|
|
||||||
cashu = tag.as_vec()[0]
|
|
||||||
|
|
||||||
user = get_or_add_user(self.dvm_config.DB, nip90_event.pubkey().to_hex(), client=self.client,
|
user = get_or_add_user(self.dvm_config.DB, nip90_event.pubkey().to_hex(), client=self.client,
|
||||||
config=self.dvm_config)
|
config=self.dvm_config)
|
||||||
|
|
||||||
task_supported, task, duration = check_task_is_supported(nip90_event, tags, client=self.client,
|
|
||||||
|
cashu = ""
|
||||||
|
p_tag_str = ""
|
||||||
|
for tag in nip90_event.tags():
|
||||||
|
if tag.as_vec()[0] == "cashu":
|
||||||
|
cashu = tag.as_vec()[0]
|
||||||
|
elif tag.as_vec()[0] == "p":
|
||||||
|
p_tag_str = tag.as_vec()[0]
|
||||||
|
|
||||||
|
task_supported, task, duration = check_task_is_supported(nip90_event, client=self.client,
|
||||||
get_duration=(not user.iswhitelisted),
|
get_duration=(not user.iswhitelisted),
|
||||||
config=self.dvm_config)
|
config=self.dvm_config)
|
||||||
|
|
||||||
@ -134,6 +134,7 @@ class DVM:
|
|||||||
|
|
||||||
do_work(nip90_event)
|
do_work(nip90_event)
|
||||||
# if task is directed to us via p tag and user has balance, do the job and update balance
|
# if task is directed to us via p tag and user has balance, do the job and update balance
|
||||||
|
|
||||||
elif p_tag_str == Keys.from_sk_str(
|
elif p_tag_str == Keys.from_sk_str(
|
||||||
self.dvm_config.PRIVATE_KEY).public_key().to_hex() and user.balance >= amount:
|
self.dvm_config.PRIVATE_KEY).public_key().to_hex() and user.balance >= amount:
|
||||||
balance = max(user.balance - amount, 0)
|
balance = max(user.balance - amount, 0)
|
||||||
@ -155,7 +156,7 @@ class DVM:
|
|||||||
# else send a payment required event to user
|
# else send a payment required event to user
|
||||||
else:
|
else:
|
||||||
bid = 0
|
bid = 0
|
||||||
for tag in nip90_event.tags:
|
for tag in nip90_event.tags():
|
||||||
if tag.as_vec()[0] == 'bid':
|
if tag.as_vec()[0] == 'bid':
|
||||||
bid = int(tag.as_vec()[1])
|
bid = int(tag.as_vec()[1])
|
||||||
|
|
||||||
@ -177,14 +178,15 @@ class DVM:
|
|||||||
False, amount, client=self.client, dvm_config=self.dvm_config)
|
False, amount, client=self.client, dvm_config=self.dvm_config)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print("Task not supported on this DVM, skipping..")
|
print("[" + self.dvm_config.NIP89.name + "] Task " + task + " not supported on this DVM, skipping..")
|
||||||
|
|
||||||
def handle_zap(zap_event):
|
def handle_zap(zap_event):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
invoice_amount, zapped_event, sender, message, anon = parse_zap_event_tags(zap_event,
|
invoice_amount, zapped_event, sender, message, anon = parse_zap_event_tags(zap_event,
|
||||||
self.keys, self.dvm_config.NIP89.name,
|
self.keys,
|
||||||
self.client, self.dvm_config)
|
self.dvm_config.NIP89.name,
|
||||||
|
self.client, self.dvm_config)
|
||||||
user = get_or_add_user(db=self.dvm_config.DB, npub=sender, client=self.client, config=self.dvm_config)
|
user = get_or_add_user(db=self.dvm_config.DB, npub=sender, client=self.client, config=self.dvm_config)
|
||||||
|
|
||||||
if zapped_event is not None:
|
if zapped_event is not None:
|
||||||
@ -198,16 +200,17 @@ class DVM:
|
|||||||
amount = int(float(tag.as_vec()[1]) / 1000)
|
amount = int(float(tag.as_vec()[1]) / 1000)
|
||||||
elif tag.as_vec()[0] == 'e':
|
elif tag.as_vec()[0] == 'e':
|
||||||
job_event = get_event_by_id(tag.as_vec()[1], client=self.client, config=self.dvm_config)
|
job_event = get_event_by_id(tag.as_vec()[1], client=self.client, config=self.dvm_config)
|
||||||
tags: typing.List[Tag]
|
if job_event is not None:
|
||||||
tags, p_tag_str = check_and_decrypt_tags(job_event, self.dvm_config)
|
job_event = check_and_decrypt_tags(job_event, self.dvm_config)
|
||||||
job_event.tags = tags
|
else:
|
||||||
|
return
|
||||||
|
|
||||||
if p_tag_str is None:
|
if p_tag_str is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
# if a reaction by us got zapped
|
# if a reaction by us got zapped
|
||||||
|
|
||||||
task_supported, task, duration = check_task_is_supported(job_event, tags,
|
task_supported, task, duration = check_task_is_supported(job_event,
|
||||||
client=self.client,
|
client=self.client,
|
||||||
get_duration=False,
|
get_duration=False,
|
||||||
config=self.dvm_config)
|
config=self.dvm_config)
|
||||||
@ -263,7 +266,7 @@ class DVM:
|
|||||||
print("[" + self.dvm_config.NIP89.name + "] Error during content decryption: " + str(e))
|
print("[" + self.dvm_config.NIP89.name + "] Error during content decryption: " + str(e))
|
||||||
|
|
||||||
def check_event_has_not_unfinished_job_input(nevent, append, client, dvmconfig):
|
def check_event_has_not_unfinished_job_input(nevent, append, client, dvmconfig):
|
||||||
task_supported, task, duration = check_task_is_supported(nevent, nevent.tags, client, False,
|
task_supported, task, duration = check_task_is_supported(nevent, client, False,
|
||||||
config=dvmconfig)
|
config=dvmconfig)
|
||||||
if not task_supported:
|
if not task_supported:
|
||||||
return False
|
return False
|
||||||
@ -372,7 +375,7 @@ class DVM:
|
|||||||
status_tag = Tag.parse(["status", status])
|
status_tag = Tag.parse(["status", status])
|
||||||
|
|
||||||
tags = [e_tag, alt_tag, status_tag]
|
tags = [e_tag, alt_tag, status_tag]
|
||||||
for tag in original_event.tags:
|
for tag in original_event.tags():
|
||||||
|
|
||||||
if tag.as_vec()[0] == "p":
|
if tag.as_vec()[0] == "p":
|
||||||
p_tag = tag
|
p_tag = tag
|
||||||
|
@ -46,7 +46,7 @@ class ImageGenerationDALLE(DVMTaskInterface):
|
|||||||
model = "dall-e-3"
|
model = "dall-e-3"
|
||||||
quality = "standard"
|
quality = "standard"
|
||||||
|
|
||||||
for tag in event.tags:
|
for tag in event.tags():
|
||||||
if tag.as_vec()[0] == 'i':
|
if tag.as_vec()[0] == 'i':
|
||||||
input_type = tag.as_vec()[2]
|
input_type = tag.as_vec()[2]
|
||||||
if input_type == "text":
|
if input_type == "text":
|
||||||
|
@ -55,7 +55,7 @@ class ImageGenerationSDXL(DVMTaskInterface):
|
|||||||
lora_weight = ""
|
lora_weight = ""
|
||||||
strength = ""
|
strength = ""
|
||||||
guidance_scale = ""
|
guidance_scale = ""
|
||||||
for tag in event.tags:
|
for tag in event.tags():
|
||||||
if tag.as_vec()[0] == 'i':
|
if tag.as_vec()[0] == 'i':
|
||||||
input_type = tag.as_vec()[2]
|
input_type = tag.as_vec()[2]
|
||||||
if input_type == "text":
|
if input_type == "text":
|
||||||
|
@ -43,7 +43,7 @@ class TextExtractionPDF(DVMTaskInterface):
|
|||||||
input_content = ""
|
input_content = ""
|
||||||
url = ""
|
url = ""
|
||||||
|
|
||||||
for tag in event.tags:
|
for tag in event.tags():
|
||||||
if tag.as_vec()[0] == 'i':
|
if tag.as_vec()[0] == 'i':
|
||||||
input_type = tag.as_vec()[2]
|
input_type = tag.as_vec()[2]
|
||||||
input_content = tag.as_vec()[1]
|
input_content = tag.as_vec()[1]
|
||||||
|
@ -42,7 +42,7 @@ class Translation(DVMTaskInterface):
|
|||||||
text = ""
|
text = ""
|
||||||
translation_lang = "en"
|
translation_lang = "en"
|
||||||
|
|
||||||
for tag in event.tags:
|
for tag in event.tags():
|
||||||
if tag.as_vec()[0] == 'i':
|
if tag.as_vec()[0] == 'i':
|
||||||
input_type = tag.as_vec()[2]
|
input_type = tag.as_vec()[2]
|
||||||
|
|
||||||
|
@ -71,6 +71,7 @@ def nostr_client_test_image(prompt):
|
|||||||
|
|
||||||
def nostr_client_test_image_private(prompt, cashutoken):
|
def nostr_client_test_image_private(prompt, cashutoken):
|
||||||
keys = Keys.from_sk_str(os.getenv("NOSTR_TEST_CLIENT_PRIVATE_KEY"))
|
keys = Keys.from_sk_str(os.getenv("NOSTR_TEST_CLIENT_PRIVATE_KEY"))
|
||||||
|
receiver_keys = Keys.from_sk_str(os.getenv("NOSTR_PRIVATE_KEY"))
|
||||||
|
|
||||||
|
|
||||||
# TODO more advanced logic, more parsing, params etc, just very basic test functions for now
|
# TODO more advanced logic, more parsing, params etc, just very basic test functions for now
|
||||||
@ -92,7 +93,7 @@ def nostr_client_test_image_private(prompt, cashutoken):
|
|||||||
relays_tag.as_vec(), alt_tag.as_vec(), cashu_tag.as_vec()])
|
relays_tag.as_vec(), alt_tag.as_vec(), cashu_tag.as_vec()])
|
||||||
|
|
||||||
|
|
||||||
encrypted_params = nip04_encrypt(keys.secret_key(), keys.public_key(),
|
encrypted_params = nip04_encrypt(keys.secret_key(), receiver_keys.public_key(),
|
||||||
encrypted_params_string)
|
encrypted_params_string)
|
||||||
|
|
||||||
p_tag = Tag.parse(['p', keys.public_key().to_hex()])
|
p_tag = Tag.parse(['p', keys.public_key().to_hex()])
|
||||||
@ -107,31 +108,6 @@ def nostr_client_test_image_private(prompt, cashutoken):
|
|||||||
send_event(nip90request, client=client, dvm_config=config)
|
send_event(nip90request, client=client, dvm_config=config)
|
||||||
return nip90request.as_json()
|
return nip90request.as_json()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
iTag = Tag.parse(["i", prompt, "text"])
|
|
||||||
outTag = Tag.parse(["output", "image/png;format=url"])
|
|
||||||
paramTag1 = Tag.parse(["param", "size", "1024x1024"])
|
|
||||||
tTag = Tag.parse(["t", "bitcoin"])
|
|
||||||
|
|
||||||
bidTag = Tag.parse(['bid', str(1000 * 1000), str(1000 * 1000)])
|
|
||||||
relaysTag = Tag.parse(['relays', "wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
|
|
||||||
"wss://nostr-pub.wellorder.net"])
|
|
||||||
alttag = Tag.parse(["alt", "This is a NIP90 DVM AI task to translate a given Input"])
|
|
||||||
event = EventBuilder(EventDefinitions.KIND_NIP90_GENERATE_IMAGE, str("Generate an Image."),
|
|
||||||
[iTag, outTag, tTag, paramTag1, bidTag, relaysTag, alttag]).to_event(keys)
|
|
||||||
|
|
||||||
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
|
|
||||||
"wss://nostr-pub.wellorder.net"]
|
|
||||||
|
|
||||||
client = Client(keys)
|
|
||||||
for relay in relay_list:
|
|
||||||
client.add_relay(relay)
|
|
||||||
client.connect()
|
|
||||||
config = DVMConfig
|
|
||||||
send_event(event, client=client, dvm_config=config)
|
|
||||||
return event.as_json()
|
|
||||||
|
|
||||||
def nostr_client():
|
def nostr_client():
|
||||||
keys = Keys.from_sk_str(os.getenv("NOSTR_TEST_CLIENT_PRIVATE_KEY"))
|
keys = Keys.from_sk_str(os.getenv("NOSTR_TEST_CLIENT_PRIVATE_KEY"))
|
||||||
sk = keys.secret_key()
|
sk = keys.secret_key()
|
||||||
@ -156,8 +132,8 @@ def nostr_client():
|
|||||||
|
|
||||||
nostr_client_test_image("a beautiful purple ostrich watching the sunset")
|
nostr_client_test_image("a beautiful purple ostrich watching the sunset")
|
||||||
|
|
||||||
cashutoken = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJpZCI6IlhXQzAvRXRhcVM4QyIsImFtb3VudCI6NCwiQyI6IjAzNzZhYTQ4YTJiMDU1NTlmYzQ4MTU2NjJjZThhMjZmZGM5OTQzYzY2Yzg0OWEzNTg3NDgwYWRmYzE0ZTEwNTRiZCIsInNlY3JldCI6IlIzTGhSZDI5UktJTzRkMHdNZ0Z0K2ZKWlVoYi90K0RmZXMxdFVrZVBVV0E9In0seyJpZCI6IlhXQzAvRXRhcVM4QyIsImFtb3VudCI6MTYsIkMiOiIwMmYyNTdhYzYzOTU4NGY1YTE5NTNkMGI0ODI3OWJkN2EyMjdmZTBkYzI0OWY0MjQwNjgzMDZlOTI0ZGE3ZGVhZDciLCJzZWNyZXQiOiJ4Tmhwdm50SkNwcXFiYmFjWDA0NzluVld4SGo5U05jaVBvdTNYQ3JWcmRjPSJ9LHsiaWQiOiJYV0MwL0V0YXFTOEMiLCJhbW91bnQiOjMyLCJDIjoiMDIyYjhiY2JkYTQ1OTNlMGZlNTY4ZWYyOTM2OWNmZjNmMzY2NzdlZDAzYTQ4ODMxNzYwNDQxN2JkNGE3MTYzZDYyIiwic2VjcmV0IjoiTEprUVlheWNyUE9yZ3hZcHhlcDZVV3U0RjZ3QUVydnZJNHZiRmN0R3h6MD0ifV0sIm1pbnQiOiJodHRwczovL2xuYml0cy5iaXRjb2luZml4ZXN0aGlzLm9yZy9jYXNodS9hcGkvdjEvOXVDcDIyUllWVXE4WjI0bzVCMlZ2VyJ9XX0" =
|
# cashutoken = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJpZCI6IlhXQzAvRXRhcVM4QyIsImFtb3VudCI6NCwiQyI6IjAzNzZhYTQ4YTJiMDU1NTlmYzQ4MTU2NjJjZThhMjZmZGM5OTQzYzY2Yzg0OWEzNTg3NDgwYWRmYzE0ZTEwNTRiZCIsInNlY3JldCI6IlIzTGhSZDI5UktJTzRkMHdNZ0Z0K2ZKWlVoYi90K0RmZXMxdFVrZVBVV0E9In0seyJpZCI6IlhXQzAvRXRhcVM4QyIsImFtb3VudCI6MTYsIkMiOiIwMmYyNTdhYzYzOTU4NGY1YTE5NTNkMGI0ODI3OWJkN2EyMjdmZTBkYzI0OWY0MjQwNjgzMDZlOTI0ZGE3ZGVhZDciLCJzZWNyZXQiOiJ4Tmhwdm50SkNwcXFiYmFjWDA0NzluVld4SGo5U05jaVBvdTNYQ3JWcmRjPSJ9LHsiaWQiOiJYV0MwL0V0YXFTOEMiLCJhbW91bnQiOjMyLCJDIjoiMDIyYjhiY2JkYTQ1OTNlMGZlNTY4ZWYyOTM2OWNmZjNmMzY2NzdlZDAzYTQ4ODMxNzYwNDQxN2JkNGE3MTYzZDYyIiwic2VjcmV0IjoiTEprUVlheWNyUE9yZ3hZcHhlcDZVV3U0RjZ3QUVydnZJNHZiRmN0R3h6MD0ifV0sIm1pbnQiOiJodHRwczovL2xuYml0cy5iaXRjb2luZml4ZXN0aGlzLm9yZy9jYXNodS9hcGkvdjEvOXVDcDIyUllWVXE4WjI0bzVCMlZ2VyJ9XX0"
|
||||||
nostr_client_test_image_private("a beautiful ostrich watching the sunset", cashutoken )
|
#nostr_client_test_image_private("a beautiful ostrich watching the sunset", cashutoken )
|
||||||
class NotificationHandler(HandleNotification):
|
class NotificationHandler(HandleNotification):
|
||||||
def handle(self, relay_url, event):
|
def handle(self, relay_url, event):
|
||||||
print(f"Received new event from {relay_url}: {event.as_json()}")
|
print(f"Received new event from {relay_url}: {event.as_json()}")
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
|
import typing
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
|
from nostr_sdk import Event, Tag
|
||||||
|
|
||||||
from utils.definitions import EventDefinitions
|
from utils.definitions import EventDefinitions
|
||||||
from utils.nostr_utils import get_event_by_id
|
from utils.nostr_utils import get_event_by_id
|
||||||
|
|
||||||
@ -50,17 +54,16 @@ def get_task(event, client, dvmconfig):
|
|||||||
return "unknown type"
|
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:
|
try:
|
||||||
dvm_config = config
|
dvm_config = config
|
||||||
input_value = ""
|
input_value = ""
|
||||||
input_type = ""
|
input_type = ""
|
||||||
duration = 1
|
duration = 1
|
||||||
|
|
||||||
task = get_task(event, client=client, dvmconfig=dvm_config)
|
task = get_task(event, client=client, dvmconfig=dvm_config)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
for tag in tags:
|
for tag in event.tags():
|
||||||
if tag.as_vec()[0] == 'i':
|
if tag.as_vec()[0] == 'i':
|
||||||
if len(tag.as_vec()) < 3:
|
if len(tag.as_vec()) < 3:
|
||||||
print("Job Event missing/malformed i tag, skipping..")
|
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
|
return False, "", 0
|
||||||
elif input_type == 'url' and check_url_is_readable(input_value) is None:
|
elif input_type == 'url' and check_url_is_readable(input_value) is None:
|
||||||
print("Url not readable / supported")
|
print("Url not readable / supported")
|
||||||
return False, task, duration#
|
return False, task, duration #
|
||||||
|
|
||||||
elif tag.as_vec()[0] == 'output':
|
elif tag.as_vec()[0] == 'output':
|
||||||
# TODO move this to individual modules
|
# 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" or "image/jpg"
|
||||||
or output == "image/png;format=url" or output == "image/jpg;format=url"
|
or output == "image/png;format=url" or output == "image/jpg;format=url"
|
||||||
or output == ""):
|
or output == ""):
|
||||||
print("Output format not supported, skipping..")
|
print("Output format not supported, skipping..")
|
||||||
return False, "", 0
|
return False, "", 0
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Check task 2: " + str(e))
|
print("Check task 2: " + str(e))
|
||||||
|
|
||||||
|
|
||||||
for dvm in dvm_config.SUPPORTED_DVMS:
|
for dvm in dvm_config.SUPPORTED_DVMS:
|
||||||
|
print(dvm.TASK)
|
||||||
if dvm.TASK == task:
|
if dvm.TASK == task:
|
||||||
if not dvm.is_input_supported(input_type, event.content()):
|
if not dvm.is_input_supported(input_type, event.content()):
|
||||||
return False, task, duration
|
return False, task, duration
|
||||||
@ -130,10 +133,11 @@ def check_url_is_readable(url):
|
|||||||
|
|
||||||
|
|
||||||
def get_amount_per_task(task, dvm_config, duration=1):
|
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:
|
if dvm.TASK == task:
|
||||||
amount = dvm.COST * duration
|
amount = dvm.COST * duration
|
||||||
return amount
|
return amount
|
||||||
else:
|
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
|
return None
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
|
import typing
|
||||||
from datetime import timedelta
|
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:
|
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):
|
def check_and_decrypt_tags(event, dvm_config):
|
||||||
tags = []
|
try:
|
||||||
is_encrypted = False
|
tags = []
|
||||||
p = ""
|
is_encrypted = False
|
||||||
for tag in event.tags():
|
p = ""
|
||||||
if tag.as_vec()[0] == 'encrypted':
|
sender = event.pubkey()
|
||||||
is_encrypted = True
|
for tag in event.tags():
|
||||||
elif tag.as_vec()[0] == 'p':
|
if tag.as_vec()[0] == 'encrypted':
|
||||||
p = tag.as_vec()[1]
|
is_encrypted = True
|
||||||
|
elif tag.as_vec()[0] == 'p':
|
||||||
|
p = tag.as_vec()[1]
|
||||||
|
|
||||||
if is_encrypted:
|
if is_encrypted:
|
||||||
if p != Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex():
|
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, "
|
print("[" + dvm_config.NIP89.name + "] Task encrypted and not addressed to this DVM, "
|
||||||
"skipping..")
|
"skipping..")
|
||||||
return None, None
|
return None
|
||||||
|
|
||||||
elif p == Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex():
|
elif p == Keys.from_sk_str(dvm_config.PRIVATE_KEY).public_key().to_hex():
|
||||||
encrypted_tag = Tag.parse(["encrypted"])
|
print("encrypted")
|
||||||
p_tag = Tag.parse(["p", p])
|
#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(),
|
tags_str = nip04_decrypt(Keys.from_sk_str(dvm_config.PRIVATE_KEY).secret_key(),
|
||||||
event.pubkey(), event.content())
|
event.pubkey(), event.content())
|
||||||
params = json.loads(tags_str)
|
#TODO add outer p tag so it doesnt have to be sent twice
|
||||||
|
params = json.loads(tags_str)
|
||||||
for element in params:
|
eventasjson = json.loads(event.as_json())
|
||||||
tags.append(Tag.parse(element))
|
eventasjson['tags'] = params
|
||||||
|
eventasjson['content'] = ""
|
||||||
# Keep the encrypted tag
|
event = Event.from_json(json.dumps(eventasjson))
|
||||||
tags.append(p_tag)
|
print(event.as_json())
|
||||||
tags.append(encrypted_tag)
|
except Exception as e:
|
||||||
|
print(e)
|
||||||
return tags, p
|
|
||||||
|
|
||||||
else:
|
|
||||||
return event.tags, p
|
|
||||||
|
|
||||||
|
return event
|
||||||
|
Loading…
x
Reference in New Issue
Block a user