more cleanup

This commit is contained in:
Believethehype
2024-10-17 16:12:18 +02:00
parent a3ad0bebc7
commit db7fb286e0
11 changed files with 76 additions and 94 deletions

View File

@@ -5,9 +5,6 @@ from nostr_dvm.utils.nostr_utils import send_event
from nostr_dvm.utils.print_utils import bcolors
async def announce_dm_relays(dvm_config, client):
tags = []
@@ -30,7 +27,7 @@ async def announce_dm_relays(dvm_config, client):
async def nip65_announce_relays(dvm_config, client):
# todo we might want to call the dm relays seperately but for now we do it together with the inbox relays
# todo we might want to call the dm relays seperately but for now we do it together with the inbox relays
await announce_dm_relays(dvm_config, client)
tags = []
@@ -51,4 +48,3 @@ async def nip65_announce_relays(dvm_config, client):
else:
print(
bcolors.RED + "[" + dvm_config.NIP89.NAME + "] Could not announce NIP 65 for " + dvm_config.NIP89.NAME + bcolors.ENDC)

View File

@@ -1,10 +1,9 @@
import os
from datetime import timedelta
from hashlib import sha256
from pathlib import Path
import dotenv
from nostr_sdk import Filter, Tag, Keys, EventBuilder, Client, EventId, PublicKey, Event, Timestamp, SingleLetterTag, \
from nostr_sdk import Filter, Tag, Keys, EventBuilder, Client, EventId, PublicKey, Timestamp, SingleLetterTag, \
Alphabet, Kind
from nostr_dvm.utils import definitions
@@ -88,7 +87,8 @@ async def nip88_delete_announcement(eid: str, keys: Keys, dtag: str, client: Cli
await send_event(event, client, config)
async def nip88_has_active_subscription(user: PublicKey, tiereventdtag, client: Client, receiver_public_key_hex, checkCanceled = True):
async def nip88_has_active_subscription(user: PublicKey, tiereventdtag, client: Client, receiver_public_key_hex,
checkCanceled=True):
subscription_status = {
"isActive": False,
"validUntil": 0,
@@ -140,9 +140,11 @@ async def nip88_announce_tier(dvm_config, client):
# 5% go to NostrSDK developers
# 10% optionally go to clients that support this subscription DVM
zaptag1 = Tag.parse(["zap", dvm_config.PUBLIC_KEY, "wss://damus.io", "16"])
zaptag2 = Tag.parse(["zap", "npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8", "wss://damus.io", "1"]) # NostrDVM
zaptag3 = Tag.parse(["zap", "npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet", "wss://damus.io", "1"]) # NostrSDK
zaptag4 = Tag.parse(["zap", "", "wss://damus.io", "2"]) # Client might use this for splits
zaptag2 = Tag.parse(
["zap", "npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8", "wss://damus.io", "1"]) # NostrDVM
zaptag3 = Tag.parse(
["zap", "npub1drvpzev3syqt0kjrls50050uzf25gehpz9vgdw08hvex7e0vgfeq0eseet", "wss://damus.io", "1"]) # NostrSDK
zaptag4 = Tag.parse(["zap", "", "wss://damus.io", "2"]) # Client might use this for splits
p_tag = Tag.parse(["p", dvm_config.NIP88.PAYMENT_VERIFIER_PUBKEY])
tags = [title_tag, image_tag, zaptag1, zaptag2, zaptag3, zaptag4, d_tag, p_tag]
@@ -182,7 +184,6 @@ async def nip88_announce_tier(dvm_config, client):
else:
print("[" + dvm_config.identifier + "] Announced NIP 88 Tier")
return annotier_id
# Relay and payment-verification

View File

@@ -1,5 +1,4 @@
import os
from datetime import timedelta
from hashlib import sha256
from pathlib import Path
@@ -33,8 +32,8 @@ async def nip89_announce_tasks(dvm_config, client):
event = EventBuilder(EventDefinitions.KIND_ANNOUNCEMENT, content, [k_tag, d_tag]).to_event(keys)
eventid = await send_event(event, client=client, dvm_config=dvm_config)
print(bcolors.BLUE + "[" + dvm_config.NIP89.NAME + "] Announced NIP 89 for " + dvm_config.NIP89.NAME +" (" + eventid.id.to_hex() +")" + bcolors.ENDC)
print(
bcolors.BLUE + "[" + dvm_config.NIP89.NAME + "] Announced NIP 89 for " + dvm_config.NIP89.NAME + " (" + eventid.id.to_hex() + ")" + bcolors.ENDC)
async def fetch_nip89_parameters_for_deletion(keys, eventid, client, dvmconfig, pow=False):
@@ -70,6 +69,7 @@ async def nip89_delete_announcement(eid: str, keys: Keys, dtag: str, client: Cli
print(f"POW event: {event.as_json()}")
await send_event(event, client, config)
async def nip89_delete_announcement_pow(eid: str, keys: Keys, dtag: str, client: Client, config):
e_tag = Tag.parse(["e", eid])
a_tag = Tag.parse(

View File

@@ -1,5 +1,6 @@
import base64
import hashlib
from nostr_sdk import EventBuilder, Tag, Kind, Keys
@@ -7,6 +8,7 @@ def sha256sum(filename):
with open(filename, 'rb', buffering=0) as f:
return hashlib.file_digest(f, 'sha256').hexdigest()
async def generate_nip98_header(pkeys_hex, url="", kind="POST", filepath=""):
keys = Keys.parse(pkeys_hex)
utag = Tag.parse(["u", url])
@@ -20,4 +22,3 @@ async def generate_nip98_header(pkeys_hex, url="", kind="POST", filepath=""):
encoded_nip98_event = base64.b64encode(event.as_json().encode('utf-8')).decode('utf-8')
return "Nostr " + encoded_nip98_event

View File

@@ -6,10 +6,10 @@ from typing import List
import dotenv
from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, Keys, nip04_decrypt, Metadata, Options, \
Nip19Event, SingleLetterTag, RelayOptions, RelayLimits, SecretKey, NostrSigner, Connection, ConnectionTarget, \
EventSource, EventBuilder, Kind, nip04_encrypt
Nip19Event, SingleLetterTag, RelayLimits, SecretKey, NostrSigner, Connection, ConnectionTarget, \
EventSource, EventBuilder, Kind
from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout, relay_timeout_long
from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout
async def get_event_by_id(event_id_str: str, client: Client, config=None) -> Event | None:
@@ -25,13 +25,13 @@ async def get_event_by_id(event_id_str: str, client: Client, config=None) -> Eve
events = await client.get_events_of([id_filter], relay_timeout)
if len(events) > 0:
return events[0]
else:
print("Event not found")
return None
async def get_events_async(client, filter, timeout):
source_l = EventSource.relays(timedelta(seconds=timeout))
events = await client.get_events_of([filter], source_l)
@@ -72,7 +72,7 @@ async def get_events_by_ids(event_ids, client: Client, config=None) -> List | No
async def get_events_by_id(event_ids: list, client: Client, config=None) -> list[Event] | None:
id_filter = Filter().ids(event_ids)
#events = asyncio.run(get_events_async(client, id_filter, config.RELAY_TIMEOUT))
# events = asyncio.run(get_events_async(client, id_filter, config.RELAY_TIMEOUT))
events = await client.get_events_of([id_filter], relay_timeout)
if len(events) > 0:
return events
@@ -100,7 +100,6 @@ async def get_referenced_event_by_id(event_id, client, dvm_config, kinds) -> Eve
job_id_filter = Filter().event(event_id).limit(1)
events = await client.get_events_of([job_id_filter], relay_timeout)
if len(events) > 0:
return events[0]
else:
@@ -154,6 +153,7 @@ async def get_dm_relays(event_to_send: Event, client: Client, dvm_config):
relays.append(rtag)
return relays
async def get_main_relays(event_to_send: Event, client: Client, dvm_config):
ptags = []
for tag in event_to_send.tags():
@@ -162,8 +162,8 @@ async def get_main_relays(event_to_send: Event, client: Client, dvm_config):
ptags.append(ptag)
if len(await client.relays()) == 0:
for relay in dvm_config.RELAY_LIST:
await client.add_relay(relay)
for relay in dvm_config.RELAY_LIST:
await client.add_relay(relay)
await client.connect()
filter = Filter().kinds([EventDefinitions.KIND_FOLLOW_LIST]).authors(ptags)
@@ -176,16 +176,13 @@ async def get_main_relays(event_to_send: Event, client: Client, dvm_config):
content = json.loads(followlist.content())
relays = []
for relay in content:
relays.append(relay)
relays.append(relay)
return relays
except:
return []
async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
# 1. OK, Let's overcomplicate things.
# 2. If our event has a relays tag, we just send the event to these relay in the classical way.
relays = []
@@ -197,10 +194,10 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
try:
relays.append(tag.as_vec()[index])
except:
print("[" + dvm_config.NIP89.NAME + "] " + tag.as_vec()[index] + " couldn't be added to outbox relays")
print("[" + dvm_config.NIP89.NAME + "] " + tag.as_vec()[
index] + " couldn't be added to outbox relays")
break
# 3. If we couldn't find relays, we look in the receivers inbox
if len(relays) == 0:
relays = await get_inbox_relays(event, client, dvm_config)
@@ -210,18 +207,16 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
print("[" + dvm_config.NIP89.NAME + "] No Inbox found, replying to generic relays")
relays = await get_main_relays(event, client, dvm_config)
#eventid = await send_event(event, client, dvm_config)
#return eventid
# eventid = await send_event(event, client, dvm_config)
# return eventid
# 5. Otherwise, we create a new Outbox client with the inbox relays and send the event there
relaylimits = RelayLimits.disable()
connection = Connection().embedded_tor().target(ConnectionTarget.ONION)
#connection = Connection().addr("127.0.0.1:9050").target(ConnectionTarget.ONION)
# connection = Connection().addr("127.0.0.1:9050").target(ConnectionTarget.ONION)
opts = ((
Options().wait_for_send(False).send_timeout(timedelta(seconds=5)).relay_limits(
relaylimits)).connection(connection).connection_timeout(timedelta(seconds=30)))
Options().wait_for_send(False).send_timeout(timedelta(seconds=5)).relay_limits(
relaylimits)).connection(connection).connection_timeout(timedelta(seconds=30)))
sk = SecretKey.from_hex(dvm_config.PRIVATE_KEY)
keys = Keys.parse(sk.to_hex())
@@ -234,7 +229,7 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
await outboxclient.add_relay(relay)
except:
print("[" + dvm_config.NIP89.NAME + "] " + relay + " couldn't be added to outbox relays")
#
#
await outboxclient.connect()
try:
print("Connected, sending event")
@@ -252,7 +247,6 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
except:
print("Error removing relay: " + relay)
relays = await get_main_relays(event, client, dvm_config)
for relay in relays:
await outboxclient.add_relay(relay)
@@ -264,13 +258,10 @@ async def send_event_outbox(event: Event, client, dvm_config) -> EventId:
event_id = None
print(e)
await outboxclient.shutdown()
return event_id
async def send_event(event: Event, client: Client, dvm_config):
try:
relays = []
@@ -370,7 +361,7 @@ def check_and_decrypt_own_tags(event, dvm_config):
return event
async def update_profile_lnaddress(private_key, dvm_config, lud16="",):
async def update_profile_lnaddress(private_key, dvm_config, lud16="", ):
keys = Keys.parse(private_key)
opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=5))
.skip_disconnected_relays(True))
@@ -417,17 +408,13 @@ async def update_profile(dvm_config, client, lud16=""):
await client.set_metadata(metadata)
async def send_nip04_dm(client: Client, msg, receiver: PublicKey, dvm_config ):
async def send_nip04_dm(client: Client, msg, receiver: PublicKey, dvm_config):
signer = NostrSigner.keys(Keys.parse(dvm_config.PRIVATE_KEY))
content = await signer.nip04_encrypt(receiver, msg)
ptag = Tag.parse(["p", receiver.to_hex()])
event = EventBuilder(Kind(4), content, [ptag]).to_event(Keys.parse(dvm_config.PRIVATE_KEY))
await client.send_event(event)
# relays = await get_dm_relays(event, client, dvm_config)
#
# outboxclient = Client(signer)
@@ -448,7 +435,6 @@ async def send_nip04_dm(client: Client, msg, receiver: PublicKey, dvm_config ):
# print(e)
def check_and_set_private_key(identifier):
if not os.getenv("DVM_PRIVATE_KEY_" + identifier.upper()):
pk = Keys.generate().secret_key().to_hex()

View File

@@ -1,10 +1,8 @@
import json
import os
from datetime import timedelta
import requests
from nostr_sdk import Keys, PublicKey, Client, nip04_encrypt, EventBuilder, Tag, NostrSigner, Filter, Timestamp, \
NostrWalletConnectUri, Nwc
from nostr_sdk import Keys, PublicKey, NostrWalletConnectUri, Nwc
from nostr_dvm.utils.dvmconfig import DVMConfig
from nostr_dvm.utils.nostr_utils import check_and_set_private_key

View File

@@ -1,19 +1,19 @@
import json
import datetime as datetime
import json
import os
import random
from types import NoneType
import emoji
import pandas
import requests
from nostr_sdk import Tag, PublicKey, EventId, Keys, nip04_encrypt, EventBuilder, LogLevel
from nostr_sdk import Tag, PublicKey, EventId, Keys, EventBuilder, LogLevel
from pyupload.uploader import CatboxUploader
import pandas
from nostr_dvm.utils.print_utils import bcolors
from nostr_dvm.utils.definitions import EventDefinitions
from nostr_dvm.utils.nip98_utils import generate_nip98_header
from nostr_dvm.utils.nostr_utils import send_event_outbox
from nostr_dvm.utils.print_utils import bcolors
'''
Post process results to either given output format or a Nostr readable plain text.
@@ -293,11 +293,9 @@ def build_status_reaction(status, task, amount, content, dvm_config):
return alt_description, reaction
async def send_job_status_reaction(original_event_id_hex, original_event_author_hex, client, dvm_config,
content=None,
status="processing", user=None):
alt_description, reaction = build_status_reaction(status, "generic", 0, content, dvm_config)
e_tag = Tag.parse(["e", original_event_id_hex])
@@ -322,4 +320,4 @@ async def send_job_status_reaction(original_event_id_hex, original_event_author_
print(bcolors.YELLOW + "[" + dvm_config.NIP89.NAME + "]" + " Sent Kind " + str(
EventDefinitions.KIND_FEEDBACK.as_u16()) + " Reaction: " + status + " " + reaction_event.id().to_hex() + bcolors.ENDC)
return reaction_event.as_json()
return reaction_event.as_json()

View File

@@ -12,4 +12,4 @@ class bcolors:
MAGENTA = '\033[95m'
GREY = '\033[90m'
BLACK = '\033[90m'
DEFAULT = '\033[99m'
DEFAULT = '\033[99m'

View File

@@ -1,21 +1,17 @@
import json
import os
import re
import sys
from typing import Any
from urllib.request import urlopen, Request
import requests
import json
import yt_dlp
import sys
import os
import re
import requests
import bs4
import requests
import yt_dlp
from tqdm import tqdm
from pathlib import Path
browser = "chrome" #"firefox"
browser = "chrome" # "firefox"
def download_xvideo(url, target_location) -> None:
response = requests.get(url, stream=True)
@@ -119,7 +115,6 @@ def TiktokDownloadAll(linkList, path) -> str:
def YTDownload(link, path, audio_only=True):
if audio_only:
return get_audio([link])
else:
@@ -140,6 +135,7 @@ def get_media_duration(url):
except:
return None
def get_media_info(url):
try:
# See help(yt_dlp.YoutubeDL) for a list of available options and public functions
@@ -186,7 +182,6 @@ def get_video(URLS):
"outtmpl": 'outputs/video.mp4',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download(URLS)
return "outputs/video.mp4"

View File

@@ -57,12 +57,15 @@ def create_subscription_sql_table(db):
print(e)
def add_to_subscription_sql_table(db, id, recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag, zaps,
def add_to_subscription_sql_table(db, id, recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag,
zaps,
recipe, active, lastupdate, tier):
try:
con = sqlite3.connect(db)
cur = con.cursor()
data = (id, recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag, zaps, recipe, active, lastupdate, tier)
data = (
id, recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag, zaps, recipe, active, lastupdate,
tier)
print(id)
print(recipient)
print(subscriber)
@@ -118,7 +121,9 @@ def get_all_subscriptions_from_sql_table(db):
records = cursor.fetchall()
subscriptions = []
for row in records:
subscriptions.append(Subscription(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
subscriptions.append(
Subscription(row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10],
row[11], row[12], row[13], row[14]))
cursor.close()
return subscriptions
@@ -127,7 +132,8 @@ def get_all_subscriptions_from_sql_table(db):
finally:
if con:
con.close()
#print("The SQLite connection is closed")
# print("The SQLite connection is closed")
def delete_from_subscription_sql_table(db, id):
try:
@@ -139,12 +145,16 @@ def delete_from_subscription_sql_table(db, id):
except Error as e:
print(e)
def update_subscription_sql_table(db, id, recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag, zaps,
def update_subscription_sql_table(db, id, recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag,
zaps,
recipe, active, lastupdate, tier):
try:
con = sqlite3.connect(db)
cur = con.cursor()
data = (recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag, zaps, recipe, active, lastupdate, tier, id)
data = (
recipient, subscriber, nwc, cadence, amount, unit, begin, end, tier_dtag, zaps, recipe, active, lastupdate,
tier, id)
cur.execute(""" UPDATE subscriptions
SET recipient = ? ,
@@ -167,7 +177,3 @@ def update_subscription_sql_table(db, id, recipient, subscriber, nwc, cadence, a
con.close()
except Error as e:
print("Error Updating DB: " + str(e))

View File

@@ -4,9 +4,11 @@ import os
import random
import string
import urllib.parse
from hashlib import sha256
from pathlib import Path
import bech32
import dotenv
import requests
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
@@ -14,10 +16,7 @@ from bech32 import bech32_decode, convertbits, bech32_encode
from nostr_sdk import PublicKey, SecretKey, Event, EventBuilder, Tag, Keys, generate_shared_key, Kind, \
Timestamp
from nostr_dvm.utils.nostr_utils import get_event_by_id, check_and_decrypt_own_tags, update_profile, \
update_profile_lnaddress
from hashlib import sha256
import dotenv
from nostr_dvm.utils.nostr_utils import get_event_by_id, check_and_decrypt_own_tags, update_profile_lnaddress
# TODO tor connection to lnbits
# proxies = {
@@ -277,8 +276,7 @@ def zaprequest(lud16: str, amount: int, content, zapped_event, zapped_user, keys
callback = ob["callback"]
print(ob["callback"])
#encoded_lnurl = lnurl.encode(url)
# encoded_lnurl = lnurl.encode(url)
url_bytes = url.encode()
encoded_lnurl = bech32.bech32_encode('lnurl', bech32.convertbits(url_bytes, 8, 5))
@@ -351,12 +349,13 @@ def get_price_per_sat(currency):
return price_currency_per_sat
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain, newname = " ", currentname=" "):
def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain, newname=" ", currentname=" "):
if newname == " ":
newname = identifier
@@ -423,7 +422,8 @@ async def change_ln_address(identifier, new_identifier, dvm_config, updateprofil
previous_identifier = os.getenv("LNADDRESS_" + identifier.upper()).split("@")[0]
pin = os.getenv("LNADDRESS_PIN_" + identifier.upper())
npub = Keys.parse(os.getenv("DVM_PRIVATE_KEY_" + identifier.upper())).public_key().to_hex()
lnaddress, pin = make_ln_address_nostdress(identifier, npub, pin, os.getenv("NOSTDRESS_DOMAIN"), new_identifier, currentname=previous_identifier)
lnaddress, pin = make_ln_address_nostdress(identifier, npub, pin, os.getenv("NOSTDRESS_DOMAIN"), new_identifier,
currentname=previous_identifier)
add_key_to_env_file("LNADDRESS_" + identifier.upper(), lnaddress)
add_key_to_env_file("LNADDRESS_PIN_" + identifier.upper(), pin)
print("changed lnaddress")
@@ -431,6 +431,7 @@ async def change_ln_address(identifier, new_identifier, dvm_config, updateprofil
private_key = os.getenv("DVM_PRIVATE_KEY_" + identifier.upper())
await update_profile_lnaddress(private_key, dvm_config, lud16=lnaddress)
def add_key_to_env_file(value, oskey):
env_path = Path('.env')
if env_path.is_file():