Merge pull request #2 from believethehype/dev

cleanup
This commit is contained in:
believethehype 2023-11-24 22:07:23 +01:00 committed by GitHub
commit 55c93db89f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 72 deletions

45
bot.py
View File

@ -3,16 +3,15 @@ import time
from datetime import timedelta from datetime import timedelta
from threading import Thread from threading import Thread
from nostr_sdk import Keys, Client, Timestamp, Filter, nip04_decrypt, HandleNotification, EventBuilder, PublicKey, \ from nostr_sdk import (Keys, Client, Timestamp, Filter, nip04_decrypt, HandleNotification, EventBuilder, PublicKey,
Event, Options Options)
from utils.admin_utils import admin_make_database_updates from utils.admin_utils import admin_make_database_updates
from utils.backend_utils import get_amount_per_task from utils.backend_utils import get_amount_per_task
from utils.database_utils import get_or_add_user, update_user_balance, create_sql_table, update_sql_table, User from utils.database_utils import get_or_add_user, update_user_balance, create_sql_table, update_sql_table, User
from utils.definitions import EventDefinitions from utils.definitions import EventDefinitions
from utils.nostr_utils import send_event, get_event_by_id from utils.nostr_utils import send_event
from utils.zap_utils import parse_amount_from_bolt11_invoice, check_for_zapplepay, decrypt_private_zap_message, \ from utils.zap_utils import parse_zap_event_tags
parse_zap_event_tags
class Bot: class Bot:
@ -67,27 +66,26 @@ class Bot:
decrypted_text = nip04_decrypt(self.keys.secret_key(), nostr_event.pubkey(), nostr_event.content()) decrypted_text = nip04_decrypt(self.keys.secret_key(), nostr_event.pubkey(), nostr_event.content())
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)
# We do a selection of tasks now, maybe change this later, Idk. # We do a selection of tasks now, maybe change this later, Idk.
if decrypted_text[0].isdigit(): if decrypted_text[0].isdigit():
index = int(decrypted_text.split(' ')[0]) - 1 index = int(decrypted_text.split(' ')[0]) - 1
task = self.dvm_config.SUPPORTED_DVMS[index].TASK task = self.dvm_config.SUPPORTED_DVMS[index].TASK
print("[" + self.NAME + "] Request from " + str(user.name) + " (" + str(user.nip05) + ", Balance: " print("[" + self.NAME + "] Request from " + str(user.name) + " (" + str(user.nip05) + ", Balance: "
+ str(user.balance)+ " Sats) Task: " + str(task)) + str(user.balance) + " Sats) Task: " + str(task))
duration = 1 duration = 1
required_amount = get_amount_per_task(self.dvm_config.SUPPORTED_DVMS[index].TASK, required_amount = get_amount_per_task(self.dvm_config.SUPPORTED_DVMS[index].TASK,
self.dvm_config, duration) self.dvm_config, duration)
if user.isblacklisted: if user.isblacklisted:
# For some reason an admin might blacklist npubs, e.g. for abusing the service # For some reason an admin might blacklist npubs, e.g. for abusing the service
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(), evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
"Your are currently blocked from all services.", "Your are currently blocked from all "
None).to_event(self.keys) "services.",None).to_event(self.keys)
send_event(evt, client=self.client, dvm_config=dvm_config) send_event(evt, client=self.client, dvm_config=dvm_config)
elif user.iswhitelisted or user.balance >= required_amount or required_amount == 0: elif user.iswhitelisted or user.balance >= required_amount or required_amount == 0:
if not user.iswhitelisted: if not user.iswhitelisted:
balance = max(user.balance - required_amount, 0) balance = max(user.balance - required_amount, 0)
@ -95,7 +93,7 @@ class Bot:
iswhitelisted=user.iswhitelisted, isblacklisted=user.isblacklisted, iswhitelisted=user.iswhitelisted, isblacklisted=user.isblacklisted,
nip05=user.nip05, lud16=user.lud16, name=user.name, nip05=user.nip05, lud16=user.lud16, name=user.name,
lastactive=Timestamp.now().as_secs()) lastactive=Timestamp.now().as_secs())
time.sleep(2.0)
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(), evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
"Your Job is now scheduled. New balance is " + "Your Job is now scheduled. New balance is " +
str(balance) str(balance)
@ -103,6 +101,7 @@ class Bot:
"processing.", "processing.",
nostr_event.id()).to_event(self.keys) nostr_event.id()).to_event(self.keys)
else: else:
time.sleep(2.0)
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(), evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
"Your Job is now scheduled. As you are " "Your Job is now scheduled. As you are "
"whitelisted, your balance remains at" "whitelisted, your balance remains at"
@ -112,7 +111,6 @@ class Bot:
nostr_event.id()).to_event(self.keys) nostr_event.id()).to_event(self.keys)
print("[" + self.NAME + "] Replying " + user.name + " with \"scheduled\" confirmation") print("[" + self.NAME + "] Replying " + user.name + " with \"scheduled\" confirmation")
time.sleep(2.0)
send_event(evt, client=self.client, dvm_config=dvm_config) send_event(evt, client=self.client, dvm_config=dvm_config)
i_tag = decrypted_text.replace(decrypted_text.split(' ')[0] + " ", "") i_tag = decrypted_text.replace(decrypted_text.split(' ')[0] + " ", "")
@ -137,14 +135,12 @@ class Bot:
str(int(required_amount - user.balance)) str(int(required_amount - user.balance))
+ " Sats, then try again.", + " Sats, then try again.",
nostr_event.id()).to_event(self.keys) nostr_event.id()).to_event(self.keys)
time.sleep(2.0)
send_event(evt, client=self.client, dvm_config=dvm_config) send_event(evt, client=self.client, dvm_config=dvm_config)
# TODO if we receive the result from one of the dvms, need some better management, maybe check for keys # TODO if we receive the result from one of the dvms, need some better management, maybe check for keys
elif decrypted_text.startswith('{"result":'): elif decrypted_text.startswith('{"result":'):
dvm_result = json.loads(decrypted_text) dvm_result = json.loads(decrypted_text)
user_npub_hex = dvm_result["sender"] user_npub_hex = dvm_result["sender"]
user = get_or_add_user(db=self.dvm_config.DB, npub=user_npub_hex, user = get_or_add_user(db=self.dvm_config.DB, npub=user_npub_hex,
@ -154,7 +150,7 @@ class Bot:
PublicKey.from_hex(user.npub), PublicKey.from_hex(user.npub),
dvm_result["result"], dvm_result["result"],
None).to_event(self.keys) None).to_event(self.keys)
time.sleep(2.0)
send_event(reply_event, client=self.client, dvm_config=dvm_config) send_event(reply_event, client=self.client, dvm_config=dvm_config)
else: else:
@ -165,22 +161,20 @@ class Bot:
message += str(index) + " " + p.NAME + " " + p.TASK + " " + str(p.COST) + " Sats" + "\n" message += str(index) + " " + p.NAME + " " + p.TASK + " " + str(p.COST) + " Sats" + "\n"
index += 1 index += 1
time.sleep(1.0)
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(), evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
message + "\nSelect an Index and provide an input (" message + "\nSelect an Index and provide an input ("
"e.g. 1 A purple ostrich)", "e.g. 1 A purple ostrich)",
None).to_event(self.keys) nostr_event.id()).to_event(self.keys)
#nostr_event.id()).to_event(self.keys)
time.sleep(3)
send_event(evt, client=self.client, dvm_config=dvm_config) send_event(evt, client=self.client, dvm_config=dvm_config)
except Exception as e: except Exception as e:
pass
# TODO we still receive (broken content) events after fetching the metadata, but we don't listen to them.
# probably in client.get_events_of in fetch_user_metadata
print("Error in bot " + str(e)) print("Error in bot " + str(e))
def handle_zap(zap_event): def handle_zap(zap_event):
print("[" + self.NAME + "] Zap received") print("[" + self.NAME + "] Zap received")
try: try:
invoice_amount, zapped_event, sender, anon = parse_zap_event_tags(zap_event, invoice_amount, zapped_event, sender, anon = parse_zap_event_tags(zap_event,
self.keys, self.NAME, self.keys, self.NAME,
@ -190,14 +184,16 @@ class Bot:
if zapped_event is not None: if zapped_event is not None:
if not anon: if not anon:
print("[" + self.NAME + "] Note Zap received for Bot balance: " + str(invoice_amount) + " Sats from " + str( print("[" + self.NAME + "] Note Zap received for Bot balance: " + str(
invoice_amount) + " Sats from " + str(
user.name)) user.name))
update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client, update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client,
config=self.dvm_config) config=self.dvm_config)
# a regular note # a regular note
elif not anon: elif not anon:
print("[" + self.NAME + "] Profile Zap received for Bot balance: " + str(invoice_amount) + " Sats from " + str( print("[" + self.NAME + "] Profile Zap received for Bot balance: " + str(
invoice_amount) + " Sats from " + str(
user.name)) user.name))
update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client, update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client,
config=self.dvm_config) config=self.dvm_config)
@ -213,4 +209,3 @@ class Bot:
bot = Bot bot = Bot
nostr_dvm_thread = Thread(target=bot, args=[self.dvm_config]) nostr_dvm_thread = Thread(target=bot, args=[self.dvm_config])
nostr_dvm_thread.start() nostr_dvm_thread.start()

77
dvm.py
View File

@ -2,7 +2,7 @@ import json
from datetime import timedelta from datetime import timedelta
from nostr_sdk import PublicKey, Keys, Client, Tag, Event, EventBuilder, Filter, HandleNotification, Timestamp, \ from nostr_sdk import PublicKey, Keys, Client, Tag, Event, EventBuilder, Filter, HandleNotification, Timestamp, \
init_logger, LogLevel, nip04_decrypt, EventId, Options init_logger, LogLevel, nip04_decrypt, Options
import time import time
@ -10,12 +10,11 @@ from utils.definitions import EventDefinitions, RequiredJobToWatch, JobToWatch
from utils.dvmconfig import DVMConfig from utils.dvmconfig import DVMConfig
from utils.admin_utils import admin_make_database_updates, AdminConfig from utils.admin_utils import admin_make_database_updates, AdminConfig
from utils.backend_utils import get_amount_per_task, check_task_is_supported, get_task from utils.backend_utils import get_amount_per_task, check_task_is_supported, get_task
from utils.database_utils import update_sql_table, get_from_sql_table, \ from utils.database_utils import update_sql_table, \
create_sql_table, get_or_add_user, update_user_balance create_sql_table, get_or_add_user, update_user_balance
from utils.nostr_utils import get_event_by_id, get_referenced_event_by_id, send_event from utils.nostr_utils import get_event_by_id, get_referenced_event_by_id, send_event
from utils.output_utils import post_process_result, build_status_reaction from utils.output_utils import post_process_result, build_status_reaction
from utils.zap_utils import check_bolt11_ln_bits_is_paid, parse_amount_from_bolt11_invoice, \ from utils.zap_utils import check_bolt11_ln_bits_is_paid, create_bolt11_ln_bits, parse_zap_event_tags
check_for_zapplepay, decrypt_private_zap_message, create_bolt11_ln_bits, parse_zap_event_tags
use_logger = False use_logger = False
if use_logger: if use_logger:
@ -30,10 +29,10 @@ class DVM:
job_list: list job_list: list
jobs_on_hold_list: list jobs_on_hold_list: list
def __init__(self, dvmconfig, adminconfig=None): def __init__(self, dvm_config, admin_config=None):
self.dvm_config = dvmconfig self.dvm_config = dvm_config
self.admin_config = adminconfig self.admin_config = admin_config
self.keys = Keys.from_sk_str(dvmconfig.PRIVATE_KEY) self.keys = Keys.from_sk_str(dvm_config.PRIVATE_KEY)
wait_for_send = True wait_for_send = True
skip_disconnected_relays = True skip_disconnected_relays = True
opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
@ -75,7 +74,8 @@ class DVM:
def handle(self, relay_url, nostr_event): def handle(self, relay_url, nostr_event):
if EventDefinitions.KIND_NIP90_EXTRACT_TEXT <= nostr_event.kind() <= EventDefinitions.KIND_NIP90_GENERIC: if EventDefinitions.KIND_NIP90_EXTRACT_TEXT <= nostr_event.kind() <= EventDefinitions.KIND_NIP90_GENERIC:
print( print(
"[" + self.dvm_config.NIP89.name + "] " + f"Received new NIP90 Job Request from {relay_url}: {nostr_event.as_json()}") "[" + self.dvm_config.NIP89.name + "] " + f"Received new NIP90 Job Request from {relay_url}:"
f" {nostr_event.as_json()}")
handle_nip90_job_event(nostr_event) handle_nip90_job_event(nostr_event)
elif nostr_event.kind() == EventDefinitions.KIND_ZAP: elif nostr_event.kind() == EventDefinitions.KIND_ZAP:
handle_zap(nostr_event) handle_zap(nostr_event)
@ -109,9 +109,10 @@ class DVM:
if user.iswhitelisted or task_is_free: if user.iswhitelisted or task_is_free:
print( print(
"[" + self.dvm_config.NIP89.name + "] Free task or Whitelisted for task " + task + ". Starting processing..") "[" + self.dvm_config.NIP89.name + "] Free task or Whitelisted for task " + task +
send_job_status_reaction(nip90_event, "processing", True, 0, client=self.client, ". Starting processing..")
dvm_config=self.dvm_config) send_job_status_reaction(nip90_event, "processing", True, 0,
client=self.client, dvm_config=self.dvm_config)
do_work(nip90_event, is_from_bot=False) do_work(nip90_event, is_from_bot=False)
else: else:
@ -121,7 +122,8 @@ class DVM:
bid = int(tag.as_vec()[1]) bid = int(tag.as_vec()[1])
print( print(
"[" + self.dvm_config.NIP89.name + "] Payment required: New Nostr " + task + " Job event: " + nip90_event.as_json()) "[" + self.dvm_config.NIP89.name + "] Payment required: New Nostr " + task + " Job event: "
+ nip90_event.as_json())
if bid > 0: if bid > 0:
bid_offer = int(bid / 1000) bid_offer = int(bid / 1000)
if bid_offer >= amount: if bid_offer >= amount:
@ -131,7 +133,8 @@ class DVM:
else: # If there is no bid, just request server rate from user else: # If there is no bid, just request server rate from user
print( print(
"[" + self.dvm_config.NIP89.name + "] Requesting payment for Event: " + nip90_event.id().to_hex()) "[" + self.dvm_config.NIP89.name + "] Requesting payment for Event: " +
nip90_event.id().to_hex())
send_job_status_reaction(nip90_event, "payment-required", send_job_status_reaction(nip90_event, "payment-required",
False, amount, client=self.client, dvm_config=self.dvm_config) False, amount, client=self.client, dvm_config=self.dvm_config)
else: else:
@ -192,17 +195,18 @@ class DVM:
print("[" + self.dvm_config.NIP89.name + "] Invoice was not paid sufficiently") print("[" + self.dvm_config.NIP89.name + "] Invoice was not paid sufficiently")
elif zapped_event.kind() in EventDefinitions.ANY_RESULT: elif zapped_event.kind() in EventDefinitions.ANY_RESULT:
print("Someone zapped the result of an exisiting Task. Nice") print("[" + self.dvm_config.NIP89.name + "] "
"Someone zapped the result of an exisiting Task. Nice")
elif not anon: elif not anon:
print("Note Zap received for Bot balance: " + str(invoice_amount) + " Sats from " + str( print("[" + self.dvm_config.NIP89.name + "] Note Zap received for Bot balance: " +
user.name)) str(invoice_amount) + " Sats from " + str(user.name))
update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client, update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client,
config=self.dvm_config) config=self.dvm_config)
# a regular note # a regular note
elif not anon: elif not anon:
print("Profile Zap received for Bot balance: " + str(invoice_amount) + " Sats from " + str( print("[" + self.dvm_config.NIP89.name + "] Profile Zap received for Bot balance: " +
user.name)) str(invoice_amount) + " Sats from " + str(user.name))
update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client, update_user_balance(self.dvm_config.DB, sender, invoice_amount, client=self.client,
config=self.dvm_config) config=self.dvm_config)
@ -210,6 +214,7 @@ class DVM:
print(f"Error during content decryption: {e}") print(f"Error during content decryption: {e}")
def handle_dm(dm_event): def handle_dm(dm_event):
# Note that DVMs only listen and answer to Bots, if at all.
decrypted_text = nip04_decrypt(self.keys.secret_key(), dm_event.pubkey(), dm_event.content()) decrypted_text = nip04_decrypt(self.keys.secret_key(), dm_event.pubkey(), dm_event.content())
ob = json.loads(decrypted_text) ob = json.loads(decrypted_text)
@ -220,17 +225,19 @@ class DVM:
if str(ob['input']).startswith("http"): if str(ob['input']).startswith("http"):
input_type = "url" input_type = "url"
# TODO Handle additional inputs/params
j_tag = Tag.parse(["j", self.dvm_config.SUPPORTED_DVMS[0].TASK]) j_tag = Tag.parse(["j", self.dvm_config.SUPPORTED_DVMS[0].TASK])
i_tag = Tag.parse(["i", ob['input'], input_type]) i_tag = Tag.parse(["i", ob['input'], input_type])
tags = [j_tag, i_tag]
tags.append(Tag.parse(["y", dm_event.pubkey().to_hex()])) y_tag = Tag.parse(["y", dm_event.pubkey().to_hex()])
tags.append(Tag.parse(["z", ob['sender']])) z_tag = Tag.parse(["z", ob['sender']])
tags = [j_tag, i_tag, y_tag, z_tag]
job_event = EventBuilder(EventDefinitions.KIND_DM, "", tags).to_event(self.keys) job_event = EventBuilder(EventDefinitions.KIND_DM, "", tags).to_event(self.keys)
do_work(job_event, is_from_bot=True) do_work(job_event, is_from_bot=True)
def check_event_has_not_unfinished_job_input(nevent, append, client, dvmconfig): def check_event_has_not_unfinished_job_input(nevent, append, client, dvm_config):
task_supported, task, duration = check_task_is_supported(nevent, client, False, config=dvmconfig) task_supported, task, duration = check_task_is_supported(nevent, client, False, config=dvm_config)
if not task_supported: if not task_supported:
return False return False
@ -245,13 +252,13 @@ class DVM:
if input_type == "job": if input_type == "job":
evt = get_referenced_event_by_id(event_id=input, client=client, evt = get_referenced_event_by_id(event_id=input, client=client,
kinds=EventDefinitions.ANY_RESULT, kinds=EventDefinitions.ANY_RESULT,
dvm_config=dvmconfig) dvm_config=dvm_config)
if evt is None: if evt is None:
if append: if append:
job = RequiredJobToWatch(event=nevent, timestamp=Timestamp.now().as_secs()) job = RequiredJobToWatch(event=nevent, timestamp=Timestamp.now().as_secs())
self.jobs_on_hold_list.append(job) self.jobs_on_hold_list.append(job)
send_job_status_reaction(nevent, "chain-scheduled", True, 0, client=client, send_job_status_reaction(nevent, "chain-scheduled", True, 0,
dvm_config=dvmconfig) client=client, dvm_config=dvm_config)
return False return False
else: else:
@ -286,8 +293,10 @@ class DVM:
if is_from_bot: if is_from_bot:
# Reply to Bot # Reply to Bot
original_sender = ""
receiver_key = PublicKey
for tag in original_event.tags(): for tag in original_event.tags():
if tag.as_vec()[0] == "y": # TODO we temporally use internal tags to move information if tag.as_vec()[0] == "y": # TODO we temporally use internal tags in Kind4 to move information
receiver_key = PublicKey.from_hex(tag.as_vec()[1]) receiver_key = PublicKey.from_hex(tag.as_vec()[1])
elif tag.as_vec()[0] == "z": elif tag.as_vec()[0] == "z":
original_sender = tag.as_vec()[1] original_sender = tag.as_vec()[1]
@ -297,7 +306,7 @@ class DVM:
"sender": original_sender "sender": original_sender
} }
message = json.dumps(params) message = json.dumps(params)
print(message) print("[" + self.dvm_config.NIP89.name + "] " + message)
response_event = EventBuilder.new_encrypted_direct_msg(self.keys, receiver_key, message, response_event = EventBuilder.new_encrypted_direct_msg(self.keys, receiver_key, message,
None).to_event(self.keys) None).to_event(self.keys)
send_event(response_event, client=self.client, dvm_config=self.dvm_config) send_event(response_event, client=self.client, dvm_config=self.dvm_config)
@ -449,29 +458,29 @@ class DVM:
send_job_status_reaction(event, "processing", True, 0, send_job_status_reaction(event, "processing", True, 0,
client=self.client, client=self.client,
dvm_config=self.dvm_config) dvm_config=self.dvm_config)
print("do work from joblist") print("[" + self.dvm_config.NIP89.name + "] doing work from joblist")
do_work(event, is_from_bot=False) do_work(event, is_from_bot=False)
elif check_bolt11_ln_bits_is_paid(job.payment_hash, self.dvm_config) is None: # invoice expired elif check_bolt11_ln_bits_is_paid(job.payment_hash, self.dvm_config) is None: # invoice expired
try: try:
self.job_list.remove(job) self.job_list.remove(job)
except: except:
print("Error removing Job from List after payment") print("[" + self.dvm_config.NIP89.name + "] Error removing Job from List after payment")
if Timestamp.now().as_secs() > job.expires: if Timestamp.now().as_secs() > job.expires:
try: try:
self.job_list.remove(job) self.job_list.remove(job)
except: except:
print("Error removing Job from List after expiry") print("[" + self.dvm_config.NIP89.name + "] Error removing Job from List after expiry")
for job in self.jobs_on_hold_list: for job in self.jobs_on_hold_list:
if check_event_has_not_unfinished_job_input(job.event, False, client=self.client, if check_event_has_not_unfinished_job_input(job.event, False, client=self.client,
dvmconfig=self.dvm_config): dvm_config=self.dvm_config):
handle_nip90_job_event(nip90_event=job.event) handle_nip90_job_event(nip90_event=job.event)
try: try:
self.jobs_on_hold_list.remove(job) self.jobs_on_hold_list.remove(job)
except: except:
print("Error removing Job on Hold from List after expiry") print("[" + self.dvm_config.NIP89.name + "] Error removing Job on Hold from List after expiry")
if Timestamp.now().as_secs() > job.timestamp + 60 * 20: # remove jobs to look for after 20 minutes.. if Timestamp.now().as_secs() > job.timestamp + 60 * 20: # remove jobs to look for after 20 minutes..
self.jobs_on_hold_list.remove(job) self.jobs_on_hold_list.remove(job)

View File

@ -8,7 +8,7 @@ from dataclasses import dataclass
from datetime import timedelta from datetime import timedelta
from logging import Filter from logging import Filter
from nostr_sdk import Timestamp, Keys, PublicKey, EventBuilder, Filter, Client, Options from nostr_sdk import Timestamp, Keys, PublicKey, EventBuilder, Filter
from utils.nostr_utils import send_event from utils.nostr_utils import send_event
@ -195,11 +195,6 @@ def get_or_add_user(db, npub, client, config):
return user return user
class DvmConfig:
pass
def fetch_user_metadata(npub, client): def fetch_user_metadata(npub, client):
name = "" name = ""
nip05 = "" nip05 = ""
@ -208,8 +203,6 @@ def fetch_user_metadata(npub, client):
print(f"\nGetting profile metadata for {pk.to_bech32()}...") print(f"\nGetting profile metadata for {pk.to_bech32()}...")
profile_filter = Filter().kind(0).author(pk).limit(1) profile_filter = Filter().kind(0).author(pk).limit(1)
events = client.get_events_of([profile_filter], timedelta(seconds=5)) events = client.get_events_of([profile_filter], timedelta(seconds=5))
#TODO, it seems our client is still subscribed after that
if len(events) > 0: if len(events) > 0:
latest_entry = events[0] latest_entry = events[0]
latest_time = 0 latest_time = 0

View File

@ -1,5 +1,4 @@
from nostr_sdk import Tag, Keys, EventBuilder from nostr_sdk import Tag, Keys, EventBuilder
from utils.nostr_utils import send_event from utils.nostr_utils import send_event

View File

@ -1,5 +1,5 @@
from datetime import timedelta from datetime import timedelta
from nostr_sdk import Keys, Filter, Client, Alphabet, EventId, Options, Event, PublicKey from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey
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:

View File

@ -30,7 +30,8 @@ def post_process_result(anno, original_event):
for i in str(each_row).split('\n'): for i in str(each_row).split('\n'):
result = result + i + "\n" result = result + i + "\n"
result = replace_broken_words( result = replace_broken_words(
str(result).replace("\"", "").replace('[', "").replace(']', "").lstrip(None)) str(result).replace("\"", "").replace('[', "").replace(']',
"").lstrip(None))
return result return result
elif output_format == "text/vtt": elif output_format == "text/vtt":
@ -47,7 +48,8 @@ def post_process_result(anno, original_event):
result = result + str(convertstart) + " --> " + str( result = result + str(convertstart) + " --> " + str(
convertend) + "\n" + cleared_name + "\n\n" convertend) + "\n" + cleared_name + "\n\n"
result = replace_broken_words( result = replace_broken_words(
str(result).replace("\"", "").replace('[', "").replace(']', "").lstrip(None)) str(result).replace("\"", "").replace('[', "").replace(']',
"").lstrip(None))
return result return result
elif output_format == "text/json" or output_format == "json": elif output_format == "text/json" or output_format == "json":

View File

@ -100,7 +100,8 @@ def check_bolt11_ln_bits_is_paid(payment_hash: str, config: DVMConfig):
def check_for_zapplepay(pubkey_hex: str, content: str): def check_for_zapplepay(pubkey_hex: str, content: str):
try: try:
# Special case Zapplepay # Special case Zapplepay
if pubkey_hex == PublicKey.from_bech32("npub1wxl6njlcgygduct7jkgzrvyvd9fylj4pqvll6p32h59wyetm5fxqjchcan").to_hex(): if (pubkey_hex == PublicKey.from_bech32("npub1wxl6njlcgygduct7jkgzrvyvd9fylj4pqvll6p32h59wyetm5fxqjchcan")
.to_hex()):
real_sender_bech32 = content.replace("From: nostr:", "") real_sender_bech32 = content.replace("From: nostr:", "")
pubkey_hex = PublicKey.from_bech32(real_sender_bech32).to_hex() pubkey_hex = PublicKey.from_bech32(real_sender_bech32).to_hex()
return pubkey_hex return pubkey_hex