it's zappening.

This commit is contained in:
Believethehype
2023-11-26 21:47:33 +01:00
parent fe054c76db
commit 15284f5944
5 changed files with 116 additions and 95 deletions

View File

@@ -51,47 +51,53 @@ def get_task(event, client, dvmconfig):
def check_task_is_supported(event, client, get_duration=False, config=None):
dvm_config = config
input_value = ""
input_type = ""
duration = 1
task = get_task(event, client=client, dvmconfig=dvm_config)
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..")
return False, "", 0
else:
input_value = tag.as_vec()[1]
input_type = tag.as_vec()[2]
if input_type == "event":
evt = get_event_by_id(input_value, client=client, config=dvm_config)
if evt is None:
print("Event not found")
return False, "", 0
elif input_type == 'url' and check_url_is_readable(input_value) is None:
print("Url not readable / supported")
try:
dvm_config = config
input_value = ""
input_type = ""
duration = 1
task = get_task(event, client=client, dvmconfig=dvm_config)
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..")
return False, "", 0
else:
input_value = tag.as_vec()[1]
input_type = tag.as_vec()[2]
if input_type == "event":
evt = get_event_by_id(input_value, client=client, config=dvm_config)
if evt is None:
print("Event not found")
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
elif tag.as_vec()[0] == 'output':
output = tag.as_vec()[1]
if not (output == "text/plain"
or output == "text/json" or output == "json"
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
for dvm in dvm_config.SUPPORTED_DVMS:
if dvm.TASK == task:
if not dvm.is_input_supported(input_type, event.content()):
return False, task, duration
elif tag.as_vec()[0] == 'output':
output = tag.as_vec()[1]
if not (output == "text/plain"
or output == "text/json" or output == "json"
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
if task not in (x.TASK for x in dvm_config.SUPPORTED_DVMS):
return False, task, duration
for dvm in dvm_config.SUPPORTED_DVMS:
if dvm.TASK == task:
if not dvm.is_input_supported(input_type, event.content()):
return False, task, duration
return True, task, duration
if task not in (x.TASK for x in dvm_config.SUPPORTED_DVMS):
return False, task, duration
return True, task, duration
except Exception as e:
print("Check task: " + str(e))
def check_url_is_readable(url):

View File

@@ -1,5 +1,6 @@
import json
from datetime import timedelta
from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey
from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt
def get_event_by_id(event_id: str, client: Client, config=None) -> Event | None:
@@ -57,3 +58,41 @@ def send_event(event: Event, client: Client, dvm_config) -> EventId:
client.remove_relay(relay)
return event_id
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]
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
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])
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

View File

@@ -45,6 +45,7 @@ def parse_zap_event_tags(zap_event, keys, name, client, config):
zapped_event = None
invoice_amount = 0
anon = False
message = ""
sender = zap_event.pubkey()
for tag in zap_event.tags():
@@ -59,7 +60,7 @@ def parse_zap_event_tags(zap_event, keys, name, client, config):
for z_tag in zap_request_event.tags():
if z_tag.as_vec()[0] == 'anon':
if len(z_tag.as_vec()) > 1:
print("[" + name + "] Private Zap received.")
#print("[" + name + "] Private Zap received.")
decrypted_content = decrypt_private_zap_message(z_tag.as_vec()[1],
keys.secret_key(),
zap_request_event.pubkey())
@@ -67,14 +68,14 @@ def parse_zap_event_tags(zap_event, keys, name, client, config):
if decrypted_private_event.kind() == 9733:
sender = decrypted_private_event.pubkey().to_hex()
message = decrypted_private_event.content()
if message != "":
print("Zap Message: " + message)
#if message != "":
# print("Zap Message: " + message)
else:
anon = True
print(
"[" + name + "] Anonymous Zap received. Unlucky, I don't know from whom, and never will")
return invoice_amount, zapped_event, sender, anon
return invoice_amount, zapped_event, sender, message, anon
def create_bolt11_ln_bits(sats: int, config: DVMConfig) -> (str, str):
@@ -142,10 +143,6 @@ def enrypt_private_zap_message(message, privatekey, publickey):
encrypted_msg_bech32 = bech32_encode("pzap", convertbits(encrypted_msg, 8, 5, True))
iv_bech32 = bech32_encode("iv", convertbits(iv, 8, 5, True))
print("Encrypted Message:", encrypted_msg_bech32)
print("IV:", iv_bech32)
return encrypted_msg_bech32 + "_" + iv_bech32