adapt and update to nostr-sdk 0.7.0

This commit is contained in:
Believethehype
2024-01-05 14:54:21 +01:00
parent f6c2358959
commit fa964a6609
9 changed files with 62 additions and 33 deletions

View File

@@ -4,7 +4,7 @@ from pathlib import Path
from threading import Thread
import dotenv
from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt
from nostr_sdk import Keys, Client, ClientSigner, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt
from nostr_dvm.utils.dvmconfig import DVMConfig
from nostr_dvm.utils.nostr_utils import send_event, check_and_set_private_key
@@ -24,7 +24,9 @@ def nostr_client_test_llm(prompt):
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
for relay in relay_list:
client.add_relay(relay)
client.connect()

View File

@@ -4,7 +4,8 @@ from pathlib import Path
from threading import Thread
import dotenv
from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt
from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt, \
ClientSigner
from nostr_dvm.utils.dvmconfig import DVMConfig
from nostr_dvm.utils.nostr_utils import send_event, check_and_set_private_key
@@ -41,7 +42,9 @@ def nostr_client():
sk = keys.secret_key()
pk = keys.public_key()
print(f"Nostr Test Client public key: {pk.to_bech32()}, Hex: {pk.to_hex()} ")
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
dvmconfig = DVMConfig()
for relay in dvmconfig.RELAY_LIST:
client.add_relay(relay)

View File

@@ -5,7 +5,7 @@ import time
from datetime import timedelta
from nostr_sdk import (Keys, Client, Timestamp, Filter, nip04_decrypt, HandleNotification, EventBuilder, PublicKey,
Options, Tag, Event, nip04_encrypt)
Options, Tag, Event, nip04_encrypt, ClientSigner)
from nostr_dvm.utils.admin_utils import admin_make_database_updates
from nostr_dvm.utils.database_utils import get_or_add_user, update_user_balance, create_sql_table, update_sql_table
@@ -35,7 +35,8 @@ class Bot:
skip_disconnected_relays = True
opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
.skip_disconnected_relays(skip_disconnected_relays))
self.client = Client.with_opts(self.keys, opts)
signer = ClientSigner.KEYS(self.keys)
self.client = Client.with_opts(signer, opts)
pk = self.keys.public_key()

View File

@@ -5,7 +5,7 @@ from datetime import timedelta
from sys import platform
from nostr_sdk import PublicKey, Keys, Client, Tag, Event, EventBuilder, Filter, HandleNotification, Timestamp, \
init_logger, LogLevel, Options, nip04_encrypt
init_logger, LogLevel, Options, nip04_encrypt, ClientSigner
import time
@@ -43,7 +43,10 @@ class DVM:
opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=self.dvm_config.RELAY_TIMEOUT))
.skip_disconnected_relays(skip_disconnected_relays))
self.client = Client.with_opts(self.keys, opts)
signer = ClientSigner.KEYS(self.keys)
self.client = Client.with_opts(signer,opts)
self.job_list = []
self.jobs_on_hold_list = []
pk = self.keys.public_key()

View File

@@ -1,7 +1,7 @@
import json
from datetime import timedelta
from nostr_sdk import PublicKey, Options, Keys, Client
from nostr_sdk import PublicKey, Options, Keys, Client, ClientSigner
from nostr_dvm.interfaces.dvmtaskinterface import DVMTaskInterface
from nostr_dvm.utils.dvmconfig import DVMConfig
@@ -20,10 +20,12 @@ def build_external_dvm(pubkey, task, kind, fix_cost, per_unit_cost, config,
opts = (Options().wait_for_send(True).send_timeout(timedelta(seconds=config.RELAY_TIMEOUT))
.skip_disconnected_relays(True))
keys = Keys.from_sk_str(config.PRIVATE_KEY)
client = Client.with_opts(keys, opts)
signer = ClientSigner.KEYS(keys)
client = Client.with_opts(signer, opts)
for relay in config.RELAY_LIST:
client.add_relay(relay)
client.add_relay(relay)
client.connect()
nip89content_str = nip89_fetch_events_pubkey(client, pubkey, kind)

View File

@@ -2,7 +2,7 @@ import json
import os
import requests
from nostr_sdk import Keys, PublicKey, Client, nip04_encrypt, EventBuilder, Tag
from nostr_sdk import Keys, PublicKey, Client, nip04_encrypt, EventBuilder, Tag, ClientSigner
from nostr_dvm.utils.dvmconfig import DVMConfig
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
@@ -20,7 +20,8 @@ def nwc_zap(connectionstr, bolt11, keys):
}
}
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
client.add_relay(relay)
client.connect()

View File

@@ -1,6 +1,6 @@
from setuptools import setup, find_packages
VERSION = '0.1.5'
VERSION = '0.1.6'
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. '
'This is an early stage release. Interfaces might change/brick')
@@ -14,7 +14,7 @@ setup(
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
packages=find_packages(include=['nostr_dvm/**']),
install_requires=["nostr-sdk==0.6.0",
install_requires=["nostr-sdk==0.7.0",
"bech32==1.2.0",
"pycryptodome==3.19.0",
"python-dotenv==1.0.0",

View File

@@ -5,7 +5,7 @@ from threading import Thread
import dotenv
from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt, \
nip04_encrypt
nip04_encrypt, ClientSigner
from nostr_dvm.utils.dvmconfig import DVMConfig
from nostr_dvm.utils.nostr_utils import send_event, check_and_set_private_key
@@ -31,7 +31,9 @@ def nostr_client_test_translation(input, kind, lang, sats, satsmax):
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
for relay in relay_list:
client.add_relay(relay)
client.connect()
@@ -58,20 +60,22 @@ def nostr_client_test_image(prompt):
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
for relay in relay_list:
client.add_relay(relay)
client.connect()
config = DVMConfig
send_event(event, client=client, dvm_config=config)
return event.as_json()
def nostr_client_test_tts(prompt):
keys = Keys.from_sk_str(check_and_set_private_key("test_client"))
iTag = Tag.parse(["i", prompt, "text"])
paramTag1 = Tag.parse(["param", "language", "en"])
bidTag = Tag.parse(['bid', str(1000 * 1000), str(1000 * 1000)])
relaysTag = Tag.parse(['relays', "wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"])
@@ -82,7 +86,8 @@ def nostr_client_test_tts(prompt):
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
"wss://nostr-pub.wellorder.net"]
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
for relay in relay_list:
client.add_relay(relay)
client.connect()
@@ -95,7 +100,6 @@ def nostr_client_test_image_private(prompt, cashutoken):
keys = Keys.from_sk_str(check_and_set_private_key("test_client"))
receiver_keys = Keys.from_sk_str(check_and_set_private_key("replicate_sdxl"))
# TODO more advanced logic, more parsing, params etc, just very basic test functions for now
relay_list = ["wss://relay.damus.io", "wss://blastr.f7z.xyz", "wss://relayable.org",
@@ -114,15 +118,15 @@ def nostr_client_test_image_private(prompt, cashutoken):
encrypted_params_string = json.dumps([i_tag.as_vec(), outTag.as_vec(), paramTag1.as_vec(), bid_tag.as_vec(),
relays_tag.as_vec(), alt_tag.as_vec(), cashu_tag.as_vec()])
encrypted_params = nip04_encrypt(keys.secret_key(), receiver_keys.public_key(),
encrypted_params_string)
encrypted_tag = Tag.parse(['encrypted'])
nip90request = EventBuilder(EventDefinitions.KIND_NIP90_GENERATE_IMAGE, encrypted_params,
[pTag, encrypted_tag]).to_event(keys)
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
for relay in relay_list:
client.add_relay(relay)
client.connect()
@@ -130,12 +134,15 @@ def nostr_client_test_image_private(prompt, cashutoken):
send_event(nip90request, client=client, dvm_config=config)
return nip90request.as_json()
def nostr_client():
keys = Keys.from_sk_str(check_and_set_private_key("test_client"))
sk = keys.secret_key()
pk = keys.public_key()
print(f"Nostr Client public key: {pk.to_bech32()}, Hex: {pk.to_hex()} ")
client = Client(keys)
signer = ClientSigner.KEYS(keys)
client = Client(signer)
dvmconfig = DVMConfig()
for relay in dvmconfig.RELAY_LIST:
client.add_relay(relay)
@@ -148,15 +155,15 @@ def nostr_client():
EventDefinitions.KIND_FEEDBACK]).since(Timestamp.now())) # public events
client.subscribe([dm_zap_filter, dvm_filter])
#nostr_client_test_translation("This is the result of the DVM in spanish", "text", "es", 20, 20)
#nostr_client_test_translation("note1p8cx2dz5ss5gnk7c59zjydcncx6a754c0hsyakjvnw8xwlm5hymsnc23rs", "event", "es", 20,20)
#nostr_client_test_translation("44a0a8b395ade39d46b9d20038b3f0c8a11168e67c442e3ece95e4a1703e2beb", "event", "zh", 20, 20)
# nostr_client_test_translation("This is the result of the DVM in spanish", "text", "es", 20, 20)
# nostr_client_test_translation("note1p8cx2dz5ss5gnk7c59zjydcncx6a754c0hsyakjvnw8xwlm5hymsnc23rs", "event", "es", 20,20)
# nostr_client_test_translation("44a0a8b395ade39d46b9d20038b3f0c8a11168e67c442e3ece95e4a1703e2beb", "event", "zh", 20, 20)
nostr_client_test_image("a beautiful purple ostrich watching the sunset")
#nostr_client_test_tts("Hello, this is a test. Mic check one, two.")
# nostr_client_test_tts("Hello, this is a test. Mic check one, two.")
#cashutoken = "cashuAeyJ0b2tlbiI6W3sicHJvb2ZzIjpbeyJpZCI6InZxc1VRSVorb0sxOSIsImFtb3VudCI6MSwiQyI6IjAyNWU3ODZhOGFkMmExYTg0N2YxMzNiNGRhM2VhMGIyYWRhZGFkOTRiYzA4M2E2NWJjYjFlOTgwYTE1NGIyMDA2NCIsInNlY3JldCI6InQ1WnphMTZKMGY4UElQZ2FKTEg4V3pPck5rUjhESWhGa291LzVzZFd4S0U9In0seyJpZCI6InZ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b0sxOSIsImFtb3VudCI6MTYsIkMiOiIwMmRiZTA3ZjgwYmMzNzE0N2YyMDJkNTZiMGI3ZTIzZTdiNWNkYTBhNmI3Yjg3NDExZWYyOGRiZDg2NjAzNzBlMWIiLCJzZWNyZXQiOiJHYUNIdHhzeG9HM3J2WWNCc0N3V0YxbU1NVXczK0dDN1RKRnVwOHg1cURzPSJ9XSwibWludCI6Imh0dHBzOi8vbG5iaXRzLmJpdGNvaW5maXhlc3RoaXMub3JnL2Nhc2h1L2FwaS92MS9ScDlXZGdKZjlxck51a3M1eVQ2SG5rIn1dfQ=="
# nostr_client_test_image_private("a beautiful ostrich watching the sunset")
class NotificationHandler(HandleNotification):
def handle(self, relay_url, event):
print(f"Received new event from {relay_url}: {event.as_json()}")

View File

@@ -2,8 +2,9 @@ from datetime import timedelta
from pathlib import Path
import dotenv
import nostr_sdk
from nostr_sdk import Keys, Client, Tag, EventBuilder, Filter, HandleNotification, Timestamp, nip04_decrypt, \
nip04_encrypt, EventId, Options, PublicKey
nip04_encrypt, EventId, Options, PublicKey, Event, ClientSigner
from nostr_dvm.utils import definitions, dvmconfig
from nostr_dvm.utils.nostr_utils import check_and_set_private_key
@@ -16,7 +17,9 @@ skip_disconnected_relays = True
opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=5))
.skip_disconnected_relays(skip_disconnected_relays))
client = Client.with_opts(keys, opts)
signer = ClientSigner.KEYS(keys)
client = Client.with_opts(signer, opts)
for relay in relay_list:
client.add_relay(relay)
client.connect()
@@ -75,3 +78,10 @@ if __name__ == '__main__':
#shows kind 7000 reaction but not kind 6300 result (d05e7ae9271fe2d8968cccb67c01e3458dbafa4a415e306d49b22729b088c8a1)
test_referred_events("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e", None)
bech32evnt = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_bech32()
print(bech32evnt)
nostruri = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_nostr_uri()
print(nostruri)