diff --git a/cashu/wallet/nostr.py b/cashu/wallet/nostr.py index acfb8d81..83dfe21d 100644 --- a/cashu/wallet/nostr.py +++ b/cashu/wallet/nostr.py @@ -1,6 +1,5 @@ import asyncio import threading -import time import click from loguru import logger @@ -91,7 +90,7 @@ async def send_nostr( client.dm(token, pubkey_to) print(f"Token sent to {pubkey_to.bech32()}") - await asyncio.sleep(5) + await asyncio.sleep(1) client.close() return token, pubkey_to.bech32() @@ -113,22 +112,34 @@ async def receive_nostr( # print(f"Your nostr private key (do not share!): {client.private_key.bech32()}") await asyncio.sleep(2) - def get_token_callback(event: Event, decrypted_content): + def get_token_callback(event: Event, decrypted_content: str): logger.debug( f"From {event.public_key[:3]}..{event.public_key[-3:]}: {decrypted_content}" ) - try: - # call the receive method - tokenObj = deserialize_token_from_string(decrypted_content) - asyncio.run( - receive( - wallet, - tokenObj, + # split the content into words + words = decrypted_content.split(" ") + for w in words: + try: + logger.trace( + f"Nostr: setting last check timestamp to {event.created_at}" ) - ) - except Exception as e: - logger.error(e) - pass + # call the receive method + tokenObj = deserialize_token_from_string(w) + asyncio.run( + receive( + wallet, + tokenObj, + ) + ) + asyncio.run( + set_nostr_last_check_timestamp( + timestamp=event.created_at, db=wallet.db + ) + ) + + except Exception as e: + logger.debug(e) + pass # determine timestamp of last check so we don't scan all historical DMs last_check = await get_nostr_last_check_timestamp(db=wallet.db) @@ -136,7 +147,6 @@ def get_token_callback(event: Event, decrypted_content): if last_check: last_check -= 60 * 60 # 1 hour tolerance - await set_nostr_last_check_timestamp(timestamp=int(time.time()), db=wallet.db) logger.debug("Starting Nostr DM thread") t = threading.Thread( target=client.get_dm,