diff --git a/examples/tts_dvm/test_client.py b/examples/tts_dvm/test_client.py index 9133237..c4951fb 100644 --- a/examples/tts_dvm/test_client.py +++ b/examples/tts_dvm/test_client.py @@ -13,7 +13,7 @@ from nostr_dvm.utils.nostr_utils import send_event, check_and_set_private_key from nostr_dvm.utils.definitions import EventDefinitions -def nostr_client_test_tts(prompt): +async def nostr_client_test_tts(prompt): keys = Keys.parse(check_and_set_private_key("test_client")) iTag = Tag.parse(["i", prompt, "text"]) @@ -34,10 +34,10 @@ def nostr_client_test_tts(prompt): signer = NostrSigner.keys(keys) client = Client(signer) for relay in relay_list: - client.add_relay(relay) - client.connect() + await client.add_relay(relay) + await client.connect() config = DVMConfig - send_event(event, client=client, dvm_config=config) + await send_event(event, client=client, dvm_config=config) return event.as_json() async def nostr_client(): @@ -61,13 +61,13 @@ async def nostr_client(): await client.subscribe([dm_zap_filter, dvm_filter]) - nostr_client_test_tts("Hello, this is a test. Mic check one, two.") + await nostr_client_test_tts("Hello, this is a test. Mic check one, two.") print("Sending Job Request") #nostr_client_test_image_private("a beautiful ostrich watching the sunset") class NotificationHandler(HandleNotification): - def handle(self, relay_url, subscription_id, event: Event): + async def handle(self, relay_url, subscription_id, event: Event): print(f"Received new event from {relay_url}: {event.as_json()}") if event.kind() == 7000: print("[Nostr Client]: " + event.as_json()) diff --git a/nostr_dvm/subscription.py b/nostr_dvm/subscription.py index 26cc09d..b4c481f 100644 --- a/nostr_dvm/subscription.py +++ b/nostr_dvm/subscription.py @@ -223,7 +223,7 @@ class Subscription: await client.add_relay(relay) await client.connect() recipeid = await client.send_event(event) - await client.disconnect() + await client.shutdown() recipe = recipeid.to_hex() return recipe diff --git a/nostr_dvm/tasks/discovery_censor_wot.py b/nostr_dvm/tasks/discovery_censor_wot.py index 326b257..e112afa 100644 --- a/nostr_dvm/tasks/discovery_censor_wot.py +++ b/nostr_dvm/tasks/discovery_censor_wot.py @@ -81,7 +81,7 @@ class DiscoverReports(DVMTaskInterface): cli = Client.with_opts(signer, opts) # cli.add_relay("wss://relay.nostr.band") for relay in self.dvm_config.RELAY_LIST: - cli.add_relay(relay) + await cli.add_relay(relay) # add nostr band, too. ropts = RelayOptions().ping(False) await cli.add_relay_with_opts("wss://nostr.band", ropts) diff --git a/nostr_dvm/tasks/discovery_inactive_follows.py b/nostr_dvm/tasks/discovery_inactive_follows.py index f2f0d24..67472b5 100644 --- a/nostr_dvm/tasks/discovery_inactive_follows.py +++ b/nostr_dvm/tasks/discovery_inactive_follows.py @@ -140,11 +140,11 @@ class DiscoverInactiveFollows(DVMTaskInterface): for i in range(i, i + st): filter1 = Filter().author(PublicKey.from_hex(users[i])).since(notactivesince).limit(1) filters.append(filter1) - event_from_authors = cli.get_events_of(filters, timedelta(seconds=10)) + event_from_authors = await cli.get_events_of(filters, timedelta(seconds=10)) for author in event_from_authors: instance.dic[author.author().to_hex()] = "True" print(str(i) + "/" + str(len(users))) - cli.disconnect() + await cli.shutdown() threads = [] begin = 0 diff --git a/nostr_dvm/tasks/discovery_nonfollowers.py b/nostr_dvm/tasks/discovery_nonfollowers.py index bce3be0..0b173c1 100644 --- a/nostr_dvm/tasks/discovery_nonfollowers.py +++ b/nostr_dvm/tasks/discovery_nonfollowers.py @@ -80,7 +80,7 @@ class DiscoverNonFollowers(DVMTaskInterface): step = 20 followers_filter = Filter().author(PublicKey.from_hex(options["user"])).kind(Kind(3)) - followers = cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) + followers = await cli.get_events_of([followers_filter], timedelta(seconds=self.dvm_config.RELAY_TIMEOUT)) if len(followers) > 0: result_list = [] @@ -111,7 +111,7 @@ class DiscoverNonFollowers(DVMTaskInterface): cli = Client.with_opts(signer, opts) for relay in self.dvm_config.RELAY_LIST: await cli.add_relay(relay) - cli.connect() + await cli.connect() for i in range(i, i + st): filters = [] @@ -141,7 +141,7 @@ class DiscoverNonFollowers(DVMTaskInterface): print("DIDNT FIND " + best_entry.author().to_nostr_uri()) print(str(i) + "/" + str(len(users))) - cli.disconnect() + await cli.shutdown() threads = [] begin = 0 diff --git a/nostr_dvm/tasks/search_users.py b/nostr_dvm/tasks/search_users.py index 7659e29..a306e92 100644 --- a/nostr_dvm/tasks/search_users.py +++ b/nostr_dvm/tasks/search_users.py @@ -114,7 +114,7 @@ class SearchUser(DVMTaskInterface): else: break - await cli.disconnect() + await cli.shutdown() return json.dumps(result_list) def post_process(self, result, event): diff --git a/setup.py b/setup.py index f10f6cc..804fcd5 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -VERSION = '0.6.0' +VERSION = '0.6.1' 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. See the github repository for more information') diff --git a/tests/db.py b/tests/db.py index 0ab2483..58947ca 100644 --- a/tests/db.py +++ b/tests/db.py @@ -6,9 +6,9 @@ print(keys.public_key().to_bech32()) -def reconcile_db(): +async def reconcile_db(): # Create/open SQLite database - database = NostrDatabase.sqlite("nostr.db") + database = await NostrDatabase.sqlite("nostr.db") # NOT AVAILABLE ON WINDOWS AT THE MOMENT! # Create/open nostrdb database @@ -16,21 +16,21 @@ def reconcile_db(): client = ClientBuilder().database(database).build() - client.add_relay("wss://relay.damus.io") - client.add_relay("wss://atl.purplerelay.com") - client.connect() + await client.add_relay("wss://relay.damus.io") + await client.add_relay("wss://atl.purplerelay.com") + await client.connect() # Negentropy reconciliation f = Filter().author(keys.public_key()) opts = NegentropyOptions() - client.reconcile(f, opts) + await client.reconcile(f, opts) - do_some_work() + await do_some_work() -def do_some_work(): - database = NostrDatabase.sqlite("nostr.db") +async def do_some_work(): + database = await NostrDatabase.sqlite("nostr.db") f = Filter().author(keys.public_key()).limit(10) - events = database.query([f]) + events = await database.query([f]) for event in events: print(event.as_json()) diff --git a/tests/discovery.py b/tests/discovery.py index d9035ad..a977c7a 100644 --- a/tests/discovery.py +++ b/tests/discovery.py @@ -21,7 +21,7 @@ from nostr_dvm.utils.nip89_utils import create_amount_tag, NIP89Config, check_an from nostr_dvm.utils.nostr_utils import check_and_set_private_key from nostr_dvm.utils.zap_utils import check_and_set_ln_bits_keys -rebroadcast_NIP89 = False # Announce NIP89 on startup +rebroadcast_NIP89 = True # Announce NIP89 on startup rebroadcast_NIP65_Relay_List = False update_profile = False diff --git a/tests/test_events.py b/tests/test_events.py index 217cc8e..191759a 100644 --- a/tests/test_events.py +++ b/tests/test_events.py @@ -1,3 +1,4 @@ +import asyncio from datetime import timedelta from pathlib import Path @@ -9,22 +10,45 @@ from nostr_dvm.utils import definitions, dvmconfig from nostr_dvm.utils.nostr_utils import check_and_set_private_key -relay_list = dvmconfig.DVMConfig.RELAY_LIST -keys = Keys.parse(check_and_set_private_key("test_client")) -wait_for_send = False -skip_disconnected_relays = True -opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=5)) - .skip_disconnected_relays(skip_disconnected_relays)) -signer = NostrSigner.keys(keys) -client = Client.with_opts(signer, opts) +async def test(): -for relay in relay_list: - client.add_relay(relay) -client.connect() + relay_list = dvmconfig.DVMConfig.RELAY_LIST + keys = Keys.parse(check_and_set_private_key("test_client")) + wait_for_send = False + skip_disconnected_relays = True + opts = (Options().wait_for_send(wait_for_send).send_timeout(timedelta(seconds=5)) + .skip_disconnected_relays(skip_disconnected_relays)) + + signer = NostrSigner.keys(keys) + client = Client.with_opts(signer, opts) + + for relay in relay_list: + await client.add_relay(relay) + client.connect() + + await test_referred_events(client,"c70fbd4dbaad22c427d4359981d3bdddd3971ed1a38227ca2f8e5e760f58103c", + definitions.EventDefinitions.ANY_RESULT) + + # shows kind 7000 reaction but not kind 6300 result (d05e7ae9271fe2d8968cccb67c01e3458dbafa4a415e306d49b22729b088c8a1) + await test_referred_events(client, "5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e", + definitions.EventDefinitions.ANY_RESULT) + + bech32evnt = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_bech32() + print(bech32evnt) + + test = Nip19Event.from_bech32( + "nevent1qqsrjcpejsrlt3u7dy42y6rc97svrq9ver08xy4jr2ll55ynq3sxafcppamhxue69uhkummnw3ezumt0d5pzpmnqx2pla0zvxxcfjqeeysy29ll3mtmf4s3yff0y45r7egau080vqvzqqqqqqyu4q839") + print(test.event_id().to_hex()) + + nostruri = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_nostr_uri() + print(nostruri) + + await test_search_by_user_since_days(client, + PublicKey.from_bech32("npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8"), 60, "Bitcoin") -def test_referred_events(event_id, kinds=None): +async def test_referred_events(client, event_id, kinds=None): if kinds is None: kinds = [] @@ -34,7 +58,7 @@ def test_referred_events(event_id, kinds=None): else: job_id_filter = Filter().event(EventId.from_hex(event_id)) - events = client.get_events_of([job_id_filter], timedelta(seconds=5)) + events = await client.get_events_of([job_id_filter], timedelta(seconds=5)) if len(events) > 0: for event in events: @@ -45,13 +69,13 @@ def test_referred_events(event_id, kinds=None): return None -def test_search_by_user_since_days(pubkey, days, prompt): +async def test_search_by_user_since_days(client, pubkey, days, prompt): since_seconds = int(days) * 24 * 60 * 60 dif = Timestamp.now().as_secs() - since_seconds since = Timestamp.from_secs(dif) filterts = Filter().search(prompt).author(pubkey).kinds([1]).since(since) - events = client.get_events_of([filterts], timedelta(seconds=5)) + events = await client.get_events_of([filterts], timedelta(seconds=5)) if len(events) > 0: for event in events: @@ -71,21 +95,7 @@ if __name__ == '__main__': else: raise FileNotFoundError(f'.env file not found at {env_path} ') + asyncio.run(test()) + # works - test_referred_events("c70fbd4dbaad22c427d4359981d3bdddd3971ed1a38227ca2f8e5e760f58103c", definitions.EventDefinitions.ANY_RESULT) - - #shows kind 7000 reaction but not kind 6300 result (d05e7ae9271fe2d8968cccb67c01e3458dbafa4a415e306d49b22729b088c8a1) - test_referred_events("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e", definitions.EventDefinitions.ANY_RESULT) - - bech32evnt = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_bech32() - print(bech32evnt) - - test = Nip19Event.from_bech32("nevent1qqsrjcpejsrlt3u7dy42y6rc97svrq9ver08xy4jr2ll55ynq3sxafcppamhxue69uhkummnw3ezumt0d5pzpmnqx2pla0zvxxcfjqeeysy29ll3mtmf4s3yff0y45r7egau080vqvzqqqqqqyu4q839") - print(test.event_id().to_hex()) - - nostruri = EventId.from_hex("5635e5dd930b3c831f6ab1e348bb488f3c9aca2f13190e93ab5e5e1e1ba1835e").to_nostr_uri() - print(nostruri) - - test_search_by_user_since_days(PublicKey.from_bech32("npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8"), 60, "Bitcoin") -