mirror of
https://github.com/believethehype/nostrdvm.git
synced 2025-03-26 17:41:43 +01:00
move more functions to async
This commit is contained in:
parent
a2c68409f1
commit
f17621e56c
@ -531,9 +531,9 @@ class Bot:
|
||||
send_event(evt, client=self.client, dvm_config=dvm_config)
|
||||
|
||||
async def answer_nip89(nostr_event, index, giftwrap, sender):
|
||||
info = print_dvm_info(self.client, index)
|
||||
info = await print_dvm_info(self.client, index)
|
||||
if info is None:
|
||||
info = "No NIP89 Info found for " + self.dvm_config.SUPPORTED_DVMS[index].NAME
|
||||
info = "No NIP89 Info found for " + self.dvm_config.SUPPORTED_DVMS[index].NAME
|
||||
time.sleep(2.0)
|
||||
|
||||
if giftwrap:
|
||||
@ -664,10 +664,10 @@ class Bot:
|
||||
|
||||
return tags
|
||||
|
||||
def print_dvm_info(client, index):
|
||||
async def print_dvm_info(client, index):
|
||||
pubkey = self.dvm_config.SUPPORTED_DVMS[index].dvm_config.PUBLIC_KEY
|
||||
kind = self.dvm_config.SUPPORTED_DVMS[index].KIND
|
||||
nip89content_str = nip89_fetch_events_pubkey(client, pubkey, kind)
|
||||
nip89content_str = await nip89_fetch_events_pubkey(client, pubkey, kind)
|
||||
print(nip89content_str)
|
||||
if nip89content_str is not None:
|
||||
nip89content = json.loads(nip89content_str)
|
||||
|
@ -157,7 +157,7 @@ class DVM:
|
||||
"I Don't have information about subscription status, checking on the Nostr. This might take a few seconds",
|
||||
self.dvm_config)
|
||||
|
||||
subscription_status = nip88_has_active_subscription(PublicKey.parse(user.npub),
|
||||
subscription_status = await nip88_has_active_subscription(PublicKey.parse(user.npub),
|
||||
self.dvm_config.NIP88.DTAG, self.client,
|
||||
self.dvm_config.PUBLIC_KEY)
|
||||
|
||||
|
@ -300,7 +300,7 @@ class Subscription:
|
||||
success = True
|
||||
if subscription is None or subscription.end <= Timestamp.now().as_secs():
|
||||
# rather check nostr if our db is right
|
||||
subscription_status = nip88_has_active_subscription(
|
||||
subscription_status = await nip88_has_active_subscription(
|
||||
PublicKey.parse(subscriber),
|
||||
tier_dtag, self.client, recipient, checkCanceled=False)
|
||||
|
||||
@ -424,7 +424,7 @@ class Subscription:
|
||||
if subscription.active:
|
||||
if subscription.end < Timestamp.now().as_secs():
|
||||
# We could directly zap, but let's make another check if our subscription expired
|
||||
subscription_status = nip88_has_active_subscription(
|
||||
subscription_status = await nip88_has_active_subscription(
|
||||
PublicKey.parse(subscription.subscriber),
|
||||
subscription.tier_dtag, self.client, subscription.recipent)
|
||||
|
||||
|
@ -104,19 +104,19 @@ async def admin_make_database_updates(adminconfig: AdminConfig = None, dvmconfig
|
||||
event_id = adminconfig.EVENTID
|
||||
keys = Keys.parse(
|
||||
adminconfig.PRIVKEY) # Private key from sender of Event (e.g. the key of an nip89 announcement you want to delete)
|
||||
fetch_nip89_parameters_for_deletion(keys, event_id, client, dvmconfig, adminconfig.POW)
|
||||
await fetch_nip89_parameters_for_deletion(keys, event_id, client, dvmconfig, adminconfig.POW)
|
||||
|
||||
if adminconfig.DELETE_NIP88:
|
||||
event_id = adminconfig.EVENTID
|
||||
keys = Keys.parse(
|
||||
adminconfig.PRIVKEY) # Private key from sender of Event (e.g. the key of an nip89 announcement you want to delete)
|
||||
fetch_nip88_parameters_for_deletion(keys, event_id, client, dvmconfig)
|
||||
await fetch_nip88_parameters_for_deletion(keys, event_id, client, dvmconfig)
|
||||
|
||||
if adminconfig.FETCH_NIP88:
|
||||
event_id = adminconfig.EVENTID
|
||||
keys = Keys.parse(
|
||||
adminconfig.PRIVKEY)
|
||||
fetch_nip88_event(keys, event_id, client, dvmconfig)
|
||||
await fetch_nip88_event(keys, event_id, client, dvmconfig)
|
||||
|
||||
if adminconfig.UPDATE_PROFILE:
|
||||
update_profile(dvmconfig, client, lud16=dvmconfig.LN_ADDRESS)
|
||||
await update_profile(dvmconfig, client, lud16=dvmconfig.LN_ADDRESS)
|
||||
|
@ -9,8 +9,8 @@ from nostr_dvm.utils.nip89_utils import NIP89Config, nip89_fetch_events_pubkey
|
||||
from nostr_dvm.utils.output_utils import PostProcessFunctionType
|
||||
|
||||
|
||||
def build_external_dvm(pubkey, task, kind, fix_cost, per_unit_cost, config,
|
||||
external_post_process=PostProcessFunctionType.NONE):
|
||||
async def build_external_dvm(pubkey, task, kind, fix_cost, per_unit_cost, config,
|
||||
external_post_process=PostProcessFunctionType.NONE):
|
||||
dvm_config = DVMConfig()
|
||||
dvm_config.PUBLIC_KEY = PublicKey.from_hex(pubkey).to_hex()
|
||||
dvm_config.FIX_COST = fix_cost
|
||||
@ -23,12 +23,11 @@ def build_external_dvm(pubkey, task, kind, fix_cost, per_unit_cost, config,
|
||||
signer = NostrSigner.keys(keys)
|
||||
client = Client.with_opts(signer, opts)
|
||||
|
||||
|
||||
for relay in config.RELAY_LIST:
|
||||
client.add_relay(relay)
|
||||
client.connect()
|
||||
await client.add_relay(relay)
|
||||
await client.connect()
|
||||
|
||||
nip89content_str = nip89_fetch_events_pubkey(client, pubkey, kind)
|
||||
nip89content_str = await nip89_fetch_events_pubkey(client, pubkey, kind)
|
||||
name = "External DVM"
|
||||
image = "https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg"
|
||||
about = "An External DVM with no info"
|
||||
@ -52,7 +51,7 @@ def build_external_dvm(pubkey, task, kind, fix_cost, per_unit_cost, config,
|
||||
if nip89content.get("cashuAccepted"):
|
||||
cashu_accepted = nip89content["cashuAccepted"]
|
||||
else:
|
||||
print("No NIP89 set for "+ name)
|
||||
print("No NIP89 set for " + name)
|
||||
nip89info = {
|
||||
"name": name,
|
||||
"image": image,
|
||||
@ -69,4 +68,4 @@ def build_external_dvm(pubkey, task, kind, fix_cost, per_unit_cost, config,
|
||||
interface.SUPPORTS_ENCRYPTION = encryption_supported
|
||||
interface.ACCEPTS_CASHU = cashu_accepted
|
||||
|
||||
return interface
|
||||
return interface
|
||||
|
@ -35,9 +35,9 @@ def nip88_create_d_tag(name, pubkey, image):
|
||||
return d_tag
|
||||
|
||||
|
||||
def fetch_nip88_parameters_for_deletion(keys, eventid, client, dvmconfig):
|
||||
async def fetch_nip88_parameters_for_deletion(keys, eventid, client, dvmconfig):
|
||||
idfilter = Filter().id(EventId.from_hex(eventid)).limit(1)
|
||||
nip88events = client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT))
|
||||
nip88events = await client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT))
|
||||
d_tag = ""
|
||||
if len(nip88events) == 0:
|
||||
print("Event not found. Potentially gone.")
|
||||
@ -52,15 +52,15 @@ def fetch_nip88_parameters_for_deletion(keys, eventid, client, dvmconfig):
|
||||
return
|
||||
|
||||
if event.author().to_hex() == keys.public_key().to_hex():
|
||||
nip88_delete_announcement(event.id().to_hex(), keys, d_tag, client, dvmconfig)
|
||||
await nip88_delete_announcement(event.id().to_hex(), keys, d_tag, client, dvmconfig)
|
||||
print("NIP88 announcement deleted from known relays!")
|
||||
else:
|
||||
print("Privatekey does not belong to event")
|
||||
|
||||
|
||||
def fetch_nip88_event(keys, eventid, client, dvmconfig):
|
||||
async def fetch_nip88_event(keys, eventid, client, dvmconfig):
|
||||
idfilter = Filter().id(EventId.parse(eventid)).limit(1)
|
||||
nip88events = client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT))
|
||||
nip88events = await client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT))
|
||||
d_tag = ""
|
||||
if len(nip88events) == 0:
|
||||
print("Event not found. Potentially gone.")
|
||||
@ -80,15 +80,15 @@ def fetch_nip88_event(keys, eventid, client, dvmconfig):
|
||||
print("Privatekey does not belong to event")
|
||||
|
||||
|
||||
def nip88_delete_announcement(eid: str, keys: Keys, dtag: str, client: Client, config):
|
||||
async def nip88_delete_announcement(eid: str, keys: Keys, dtag: str, client: Client, config):
|
||||
e_tag = Tag.parse(["e", eid])
|
||||
a_tag = Tag.parse(
|
||||
["a", str(EventDefinitions.KIND_NIP88_TIER_EVENT) + ":" + keys.public_key().to_hex() + ":" + dtag])
|
||||
event = EventBuilder(Kind(5), "", [e_tag, a_tag]).to_event(keys)
|
||||
send_event(event, client, config)
|
||||
await send_event(event, client, config)
|
||||
|
||||
|
||||
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,
|
||||
@ -99,7 +99,7 @@ def nip88_has_active_subscription(user: PublicKey, tiereventdtag, client: Client
|
||||
subscriptionfilter = Filter().kind(definitions.EventDefinitions.KIND_NIP88_PAYMENT_RECIPE).pubkey(
|
||||
PublicKey.parse(receiver_public_key_hex)).custom_tag(SingleLetterTag.uppercase(Alphabet.P),
|
||||
[user.to_hex()]).limit(1)
|
||||
evts = client.get_events_of([subscriptionfilter], timedelta(seconds=3))
|
||||
evts = await client.get_events_of([subscriptionfilter], timedelta(seconds=3))
|
||||
if len(evts) > 0:
|
||||
print(evts[0].as_json())
|
||||
matchesdtag = False
|
||||
|
@ -37,9 +37,9 @@ async def nip89_announce_tasks(dvm_config, client):
|
||||
|
||||
|
||||
|
||||
def fetch_nip89_parameters_for_deletion(keys, eventid, client, dvmconfig, pow=False):
|
||||
async def fetch_nip89_parameters_for_deletion(keys, eventid, client, dvmconfig, pow=False):
|
||||
idfilter = Filter().id(EventId.from_hex(eventid)).limit(1)
|
||||
nip89events = client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT))
|
||||
nip89events = await client.get_events_of([idfilter], timedelta(seconds=dvmconfig.RELAY_TIMEOUT))
|
||||
d_tag = ""
|
||||
if len(nip89events) == 0:
|
||||
print("Event not found. Potentially gone.")
|
||||
@ -54,47 +54,47 @@ def fetch_nip89_parameters_for_deletion(keys, eventid, client, dvmconfig, pow=Fa
|
||||
return
|
||||
|
||||
if event.author().to_hex() == keys.public_key().to_hex():
|
||||
nip89_delete_announcement(event.id().to_hex(), keys, d_tag, client, dvmconfig)
|
||||
await nip89_delete_announcement(event.id().to_hex(), keys, d_tag, client, dvmconfig)
|
||||
if pow:
|
||||
nip89_delete_announcement_pow(event.id().to_hex(), keys, d_tag, client, dvmconfig)
|
||||
await nip89_delete_announcement_pow(event.id().to_hex(), keys, d_tag, client, dvmconfig)
|
||||
print("NIP89 announcement deleted from known relays!")
|
||||
else:
|
||||
print("Privatekey does not belong to event")
|
||||
|
||||
|
||||
def nip89_delete_announcement(eid: str, keys: Keys, dtag: str, client: Client, config):
|
||||
async def nip89_delete_announcement(eid: str, keys: Keys, dtag: str, client: Client, config):
|
||||
e_tag = Tag.parse(["e", eid])
|
||||
a_tag = Tag.parse(
|
||||
["a", str(EventDefinitions.KIND_ANNOUNCEMENT.as_u64()) + ":" + keys.public_key().to_hex() + ":" + dtag])
|
||||
event = EventBuilder(Kind(5), "", [e_tag, a_tag]).to_event(keys)
|
||||
print(f"POW event: {event.as_json()}")
|
||||
send_event(event, client, config)
|
||||
await send_event(event, client, config)
|
||||
|
||||
def nip89_delete_announcement_pow(eid: str, keys: Keys, dtag: str, client: 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(
|
||||
["a", str(EventDefinitions.KIND_ANNOUNCEMENT.as_u64()) + ":" + keys.public_key().to_hex() + ":" + dtag])
|
||||
event = EventBuilder(Kind(5), "", [e_tag, a_tag]).to_pow_event(keys, 28)
|
||||
print(f"POW event: {event.as_json()}")
|
||||
send_event(event, client, config)
|
||||
await send_event(event, client, config)
|
||||
|
||||
|
||||
def nip89_fetch_all_dvms(client):
|
||||
async def nip89_fetch_all_dvms(client):
|
||||
ktags = []
|
||||
for i in range(5000, 5999):
|
||||
ktags.append(str(i))
|
||||
|
||||
filter = Filter().kind(EventDefinitions.KIND_ANNOUNCEMENT).custom_tag(SingleLetterTag.lowercase(Alphabet.K), ktags)
|
||||
events = client.get_events_of([filter], timedelta(seconds=5))
|
||||
events = await client.get_events_of([filter], timedelta(seconds=5))
|
||||
for event in events:
|
||||
print(event.as_json())
|
||||
|
||||
|
||||
def nip89_fetch_events_pubkey(client, pubkey, kind):
|
||||
async def nip89_fetch_events_pubkey(client, pubkey, kind):
|
||||
ktags = [str(kind.as_u64())]
|
||||
nip89filter = (Filter().kind(EventDefinitions.KIND_ANNOUNCEMENT).author(PublicKey.parse(pubkey)).
|
||||
custom_tag(SingleLetterTag.lowercase(Alphabet.K), ktags))
|
||||
events = client.get_events_of([nip89filter], timedelta(seconds=4))
|
||||
events = await client.get_events_of([nip89filter], timedelta(seconds=4))
|
||||
|
||||
dvms = {}
|
||||
for event in events:
|
||||
|
@ -291,7 +291,7 @@ def check_and_decrypt_own_tags(event, dvm_config):
|
||||
return event
|
||||
|
||||
|
||||
def update_profile(dvm_config, client, lud16=""):
|
||||
async def update_profile(dvm_config, client, lud16=""):
|
||||
keys = Keys.parse(dvm_config.PRIVATE_KEY)
|
||||
nip89content = json.loads(dvm_config.NIP89.CONTENT)
|
||||
if nip89content.get("name"):
|
||||
@ -310,7 +310,7 @@ def update_profile(dvm_config, client, lud16=""):
|
||||
# .set_banner("https://example.com/banner.png") \
|
||||
print("[" + dvm_config.NIP89.NAME + "] Setting profile metadata for " + keys.public_key().to_bech32() + "...")
|
||||
print(metadata.as_json())
|
||||
client.set_metadata(metadata)
|
||||
await client.set_metadata(metadata)
|
||||
|
||||
|
||||
def check_and_set_private_key(identifier):
|
||||
|
Loading…
x
Reference in New Issue
Block a user