Skip to content

Commit

Permalink
Merge pull request #18 from believethehype/nostrsdk080
Browse files Browse the repository at this point in the history
update to nostrsdk 0.8.0
  • Loading branch information
believethehype authored Jan 19, 2024
2 parents b268513 + 0513dcf commit ccb9b28
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 57 deletions.
2 changes: 1 addition & 1 deletion examples/ollama_dvm/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def handle(self, relay_url, event):
print("[Nostr Client]: " + event.content())

elif event.kind() == 4:
dec_text = nip04_decrypt(sk, event.pubkey(), event.content())
dec_text = nip04_decrypt(sk, event.author(), event.content())
print("[Nostr Client]: " + f"Received new msg: {dec_text}")

elif event.kind() == 9735:
Expand Down
2 changes: 1 addition & 1 deletion examples/tts_dvm/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def handle(self, relay_url, event):
print("[Nostr Client]: " + event.content())

elif event.kind() == 4:
dec_text = nip04_decrypt(sk, event.pubkey(), event.content())
dec_text = nip04_decrypt(sk, event.author(), event.content())
print("[Nostr Client]: " + f"Received new msg: {dec_text}")

elif event.kind() == 9735:
Expand Down
48 changes: 24 additions & 24 deletions nostr_dvm/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ def handle_msg(self, relay_url, msg):
return

def handle_dm(nostr_event):
sender = nostr_event.pubkey().to_hex()
sender = nostr_event.author().to_hex()
if sender == self.keys.public_key().to_hex():
return

try:
decrypted_text = nip04_decrypt(self.keys.secret_key(), nostr_event.pubkey(), nostr_event.content())
decrypted_text = nip04_decrypt(self.keys.secret_key(), nostr_event.author(), nostr_event.content())
user = get_or_add_user(db=self.dvm_config.DB, npub=sender, client=self.client, config=self.dvm_config)
print("[" + self.NAME + "] Message from " + user.name + ": " + decrypted_text)

Expand Down Expand Up @@ -152,7 +152,7 @@ def handle_dm(nostr_event):

elif decrypted_text.lower().startswith("balance"):
time.sleep(3.0)
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
evt = EventBuilder.encrypted_direct_msg(self.keys, nostr_event.author(),
"Your current balance is " + str(
user.balance) + " Sats. Zap me to add to your balance. I will use your balance interact with the DVMs for you.\n"
"I support both public and private Zaps, as well as Zapplepay.\n"
Expand All @@ -172,12 +172,12 @@ def handle_dm(nostr_event):
else:
time.sleep(2.0)
message = "Error: " + cashu_message + ". Token has not been redeemed."
evt = EventBuilder.new_encrypted_direct_msg(self.keys, PublicKey.from_hex(sender), message,
evt = EventBuilder.encrypted_direct_msg(self.keys, PublicKey.from_hex(sender), message,
None).to_event(self.keys)
send_event(evt, client=self.client, dvm_config=self.dvm_config)
elif decrypted_text.lower().startswith("what's the second best"):
time.sleep(3.0)
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
evt = EventBuilder.encrypted_direct_msg(self.keys, nostr_event.author(),
"No, there is no second best.\n\nhttps://cdn.nostr.build/p/mYLv.mp4",
nostr_event.id()).to_event(self.keys)
send_event(evt, client=self.client, dvm_config=self.dvm_config)
Expand Down Expand Up @@ -212,7 +212,7 @@ def handle_nip90_feedback(nostr_event):
if is_encrypted:
if ptag == self.keys.public_key().to_hex():
tags_str = nip04_decrypt(Keys.from_sk_str(dvm_config.PRIVATE_KEY).secret_key(),
nostr_event.pubkey(), nostr_event.content())
nostr_event.author(), nostr_event.content())
params = json.loads(tags_str)
params.append(Tag.parse(["p", ptag]).as_vec())
params.append(Tag.parse(["encrypted"]).as_vec())
Expand All @@ -236,17 +236,17 @@ def handle_nip90_feedback(nostr_event):

if status == "success" or status == "error" or status == "processing" or status == "partial" and content != "":
entry = next((x for x in self.job_list if x['event_id'] == etag), None)
if entry is not None and entry['dvm_key'] == nostr_event.pubkey().to_hex():
if entry is not None and entry['dvm_key'] == nostr_event.author().to_hex():
user = get_or_add_user(db=self.dvm_config.DB, npub=entry['npub'],
client=self.client, config=self.dvm_config)
time.sleep(2.0)
reply_event = EventBuilder.new_encrypted_direct_msg(self.keys,
reply_event = EventBuilder.encrypted_direct_msg(self.keys,
PublicKey.from_hex(entry['npub']),
content,
None).to_event(self.keys)
print(status + ": " + content)
print(
"[" + self.NAME + "] Received reaction from " + nostr_event.pubkey().to_hex() + " message to orignal sender " + user.name)
"[" + self.NAME + "] Received reaction from " + nostr_event.author().to_hex() + " message to orignal sender " + user.name)
send_event(reply_event, client=self.client, dvm_config=dvm_config)

elif status == "payment-required" or status == "partial":
Expand All @@ -256,7 +256,7 @@ def handle_nip90_feedback(nostr_event):
amount = int(amount_msats / 1000)
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'] == nostr_event.pubkey().to_hex():
'dvm_key'] == nostr_event.author().to_hex():
# if we get a bolt11, we pay and move on
user = get_or_add_user(db=self.dvm_config.DB, npub=entry["npub"],
client=self.client, config=self.dvm_config)
Expand All @@ -266,7 +266,7 @@ def handle_nip90_feedback(nostr_event):
iswhitelisted=user.iswhitelisted, isblacklisted=user.isblacklisted,
nip05=user.nip05, lud16=user.lud16, name=user.name,
lastactive=Timestamp.now().as_secs())
evt = EventBuilder.new_encrypted_direct_msg(self.keys,
evt = EventBuilder.encrypted_direct_msg(self.keys,
PublicKey.from_hex(entry["npub"]),
"Paid " + str(
amount) + " Sats from balance to DVM. New balance is " +
Expand All @@ -280,7 +280,7 @@ def handle_nip90_feedback(nostr_event):
else:
print("Bot payment-required")
time.sleep(2.0)
evt = EventBuilder.new_encrypted_direct_msg(self.keys,
evt = EventBuilder.encrypted_direct_msg(self.keys,
PublicKey.from_hex(entry["npub"]),
"Current balance: " + str(
user.balance) + " Sats. Balance of " + str(
Expand All @@ -295,7 +295,7 @@ def handle_nip90_feedback(nostr_event):
bolt11 = tag.as_vec()[2]
# else we create a zap
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.author().to_hex(),
client=self.client, config=self.dvm_config)
print("Paying: " + user.name)
bolt11 = zaprequest(user.lud16, amount, "Zap", nostr_event, self.keys,
Expand Down Expand Up @@ -332,7 +332,7 @@ def handle_nip90_response_event(nostr_event: Event):

entry = next((x for x in self.job_list if x['event_id'] == etag), None)
if entry is not None and entry[
'dvm_key'] == nostr_event.pubkey().to_hex():
'dvm_key'] == nostr_event.author().to_hex():
print(entry)
user = get_or_add_user(db=self.dvm_config.DB, npub=entry['npub'],
client=self.client, config=self.dvm_config)
Expand All @@ -341,12 +341,12 @@ def handle_nip90_response_event(nostr_event: Event):
content = nostr_event.content()
if is_encrypted:
if ptag == self.keys.public_key().to_hex():
content = nip04_decrypt(self.keys.secret_key(), nostr_event.pubkey(), content)
content = nip04_decrypt(self.keys.secret_key(), nostr_event.author(), content)
else:
return

dvms = [x for x in self.dvm_config.SUPPORTED_DVMS if
x.PUBLIC_KEY == nostr_event.pubkey().to_hex() and x.KIND == nostr_event.kind() - 1000]
x.PUBLIC_KEY == nostr_event.author().to_hex() and x.KIND == nostr_event.kind() - 1000]
if len(dvms) > 0:
dvm = dvms[0]
if dvm.dvm_config.EXTERNAL_POST_PROCESS_TYPE != PostProcessFunctionType.NONE:
Expand All @@ -357,7 +357,7 @@ def handle_nip90_response_event(nostr_event: Event):

print("[" + self.NAME + "] Received results, message to orignal sender " + user.name)
time.sleep(1.0)
reply_event = EventBuilder.new_encrypted_direct_msg(self.keys,
reply_event = EventBuilder.encrypted_direct_msg(self.keys,
PublicKey.from_hex(user.npub),
content,
None).to_event(self.keys)
Expand All @@ -383,8 +383,8 @@ def handle_zap(zap_event):
entry = next((x for x in self.job_list if x['event_id'] == etag), None)
print(entry)
# print(entry['dvm_key'])
# print(str(zapped_event.pubkey().to_hex()))
# print(str(zap_event.pubkey().to_hex()))
# print(str(zapped_event.author().to_hex()))
# print(str(zap_event.author().to_hex()))
print(sender)
if entry is not None and entry['is_paid'] is True and entry['dvm_key'] == sender:
# if we get a bolt11, we pay and move on
Expand Down Expand Up @@ -425,7 +425,7 @@ def answer_overview(nostr_event):
index += 1

time.sleep(3.0)
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
evt = EventBuilder.encrypted_direct_msg(self.keys, nostr_event.author(),
message + "\nSelect an Index and provide an input ("
"e.g. \"2 A purple ostrich\")\nType \"index info\" to learn "
"more about each DVM. (e.g. \"2 info\")\n\n"
Expand All @@ -436,7 +436,7 @@ def answer_overview(nostr_event):

def answer_blacklisted(nostr_event):
# 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.encrypted_direct_msg(self.keys, nostr_event.author(),
"Your are currently blocked from all "
"services.", None).to_event(self.keys)
send_event(evt, client=self.client, dvm_config=dvm_config)
Expand All @@ -445,10 +445,10 @@ def answer_nip89(nostr_event, index):
info = print_dvm_info(self.client, index)
time.sleep(2.0)
if info is not None:
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
evt = EventBuilder.encrypted_direct_msg(self.keys, nostr_event.author(),
info, None).to_event(self.keys)
else:
evt = EventBuilder.new_encrypted_direct_msg(self.keys, nostr_event.pubkey(),
evt = EventBuilder.encrypted_direct_msg(self.keys, nostr_event.author(),
"No NIP89 Info found for " +
self.dvm_config.SUPPORTED_DVMS[index].NAME,
None).to_event(self.keys)
Expand All @@ -463,7 +463,7 @@ def build_params(decrypted_text, nostr_event, index):
if len(split) == 1:
remaining_text = decrypted_text.replace(split[0], "")
params = remaining_text.split(" -")
tag = Tag.parse(["param", "user", nostr_event.pubkey().to_hex()])
tag = Tag.parse(["param", "user", nostr_event.author().to_hex()])
tags.append(tag)
for i in params:
print(i)
Expand Down
18 changes: 9 additions & 9 deletions nostr_dvm/dvm.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def handle_nip90_job_event(nip90_event):
if nip90_event is None:
return

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.author().to_hex(), client=self.client,
config=self.dvm_config)
cashu = ""
p_tag_str = ""
Expand Down Expand Up @@ -334,12 +334,12 @@ def check_and_return_event(data, original_event: Event):
dvm_config=self.dvm_config,
)
if amount > 0 and self.dvm_config.LNBITS_ADMIN_KEY != "":
user = get_or_add_user(self.dvm_config.DB, original_event.pubkey().to_hex(),
user = get_or_add_user(self.dvm_config.DB, original_event.author().to_hex(),
client=self.client, config=self.dvm_config)
print(user.lud16 + " " + str(amount))
bolt11 = zaprequest(user.lud16, amount, "Couldn't finish job, returning sats",
original_event,
self.keys, self.dvm_config, zaptype="private")
original_event, "",
self.keys, self.dvm_config.RELAY_LIST, zaptype="private")
if bolt11 is None:
print("Receiver has no Lightning address, can't zap back.")
return
Expand All @@ -352,7 +352,7 @@ def send_nostr_reply_event(content, original_event_as_str):
original_event = Event.from_json(original_event_as_str)
request_tag = Tag.parse(["request", original_event_as_str])
e_tag = Tag.parse(["e", original_event.id().to_hex()])
p_tag = Tag.parse(["p", original_event.pubkey().to_hex()])
p_tag = Tag.parse(["p", original_event.author().to_hex()])
alt_tag = Tag.parse(["alt", "This is the result of a NIP90 DVM AI task with kind " + str(
original_event.kind()) + ". The task was: " + original_event.content()])
status_tag = Tag.parse(["status", "success"])
Expand All @@ -372,7 +372,7 @@ def send_nostr_reply_event(content, original_event_as_str):

if encrypted:
print(content)
content = nip04_encrypt(self.keys.secret_key(), PublicKey.from_hex(original_event.pubkey().to_hex()),
content = nip04_encrypt(self.keys.secret_key(), PublicKey.from_hex(original_event.author().to_hex()),
content)

reply_event = EventBuilder(original_event.kind() + 1000, str(content), reply_tags).to_event(self.keys)
Expand All @@ -389,7 +389,7 @@ def send_job_status_reaction(original_event, status, is_paid=True, amount=0, cli
alt_description, reaction = build_status_reaction(status, task, amount, content)

e_tag = Tag.parse(["e", original_event.id().to_hex()])
p_tag = Tag.parse(["p", original_event.pubkey().to_hex()])
p_tag = Tag.parse(["p", original_event.author().to_hex()])
alt_tag = Tag.parse(["alt", alt_description])
status_tag = Tag.parse(["status", status])
reply_tags = [e_tag, alt_tag, status_tag]
Expand Down Expand Up @@ -464,7 +464,7 @@ def send_job_status_reaction(original_event, status, is_paid=True, amount=0, cli
str_tags.append(element.as_vec())

content = json.dumps(str_tags)
content = nip04_encrypt(self.keys.secret_key(), PublicKey.from_hex(original_event.pubkey().to_hex()),
content = nip04_encrypt(self.keys.secret_key(), PublicKey.from_hex(original_event.author().to_hex()),
content)
reply_tags = encryption_tags

Expand Down Expand Up @@ -527,7 +527,7 @@ def do_work(job_event, amount):
dvm_config=self.dvm_config)
# Zapping back the user on error
if amount > 0 and self.dvm_config.LNBITS_ADMIN_KEY != "":
user = get_or_add_user(self.dvm_config.DB, job_event.pubkey().to_hex(),
user = get_or_add_user(self.dvm_config.DB, job_event.author().to_hex(),
client=self.client, config=self.dvm_config)
print(user.lud16 + " " + str(amount))
bolt11 = zaprequest(user.lud16, amount, "Couldn't finish job, returning sats", job_event, user.npub,
Expand Down
2 changes: 1 addition & 1 deletion nostr_dvm/tasks/discovery_inactive_follows.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def create_request_from_nostr_event(self, event, client=None, dvm_config=None):
request_form = {"jobID": event.id().to_hex()}

# default values
user = event.pubkey().to_hex()
user = event.author().to_hex()
since_days = 90

for tag in event.tags():
Expand Down
Loading

0 comments on commit ccb9b28

Please sign in to comment.