diff --git a/nostr_dvm/utils/zap_utils.py b/nostr_dvm/utils/zap_utils.py index 5ced977..c6c5c56 100644 --- a/nostr_dvm/utils/zap_utils.py +++ b/nostr_dvm/utils/zap_utils.py @@ -8,7 +8,8 @@ import requests from Crypto.Cipher import AES from Crypto.Util.Padding import pad from bech32 import bech32_decode, convertbits, bech32_encode -from nostr_sdk import nostr_sdk, PublicKey, SecretKey, Event, EventBuilder, Tag, Keys, generate_shared_key, Kind +from nostr_sdk import nostr_sdk, PublicKey, SecretKey, Event, EventBuilder, Tag, Keys, generate_shared_key, Kind, \ + Timestamp from nostr_dvm.utils.nostr_utils import get_event_by_id, check_and_decrypt_own_tags import lnurl @@ -268,13 +269,27 @@ def zaprequest(lud16: str, amount: int, content, zapped_event, zapped_user, keys tags = [amount_tag, relays_tag, p_tag, lnurl_tag] if zaptype == "private": - key_str = keys.secret_key().to_hex() + zapped_event.id().to_hex() + str(zapped_event.created_at().as_secs()) - encryption_key = sha256(key_str.encode('utf-8')).hexdigest() + if zapped_event is not None: + key_str = keys.secret_key().to_hex() + zapped_event.id().to_hex() + str( + zapped_event.created_at().as_secs()) + else: + key_str = keys.secret_key().to_hex() + str(Timestamp.now().as_secs()) + + encryption_key = sha256(key_str.encode('utf-8')).hexdigest() + tags = [p_tag] + if zapped_event is not None: + tags.append(e_tag) + print("hello") zap_request = EventBuilder(Kind(9733), content, - [p_tag, e_tag]).to_event(keys).as_json() + tags).to_event(keys).as_json() + print("hello2") keys = Keys.parse(encryption_key) - encrypted_content = enrypt_private_zap_message(zap_request, keys.secret_key(), zapped_event.author()) + if zapped_event is not None: + encrypted_content = enrypt_private_zap_message(zap_request, keys.secret_key(), zapped_event.author()) + else: + encrypted_content = enrypt_private_zap_message(zap_request, keys.secret_key(), zapped_user) + anon_tag = Tag.parse(['anon', encrypted_content]) tags.append(anon_tag) content = "" @@ -288,7 +303,7 @@ def zaprequest(lud16: str, amount: int, content, zapped_event, zapped_user, keys return ob["pr"] except Exception as e: - print("ZAP REQUEST: " + e) + print("ZAP REQUEST: " + str(e)) return None diff --git a/tests/nwc.py b/tests/nwc.py new file mode 100644 index 0000000..7ad7e39 --- /dev/null +++ b/tests/nwc.py @@ -0,0 +1,35 @@ +import os +from pathlib import Path + +import dotenv +from nostr_sdk import Keys, PublicKey + +from nostr_dvm.utils import dvmconfig +from nostr_dvm.utils.nwc_tools import nwc_zap +from nostr_dvm.utils.zap_utils import create_bolt11_lud16, zaprequest + + +def playground(): + + connectionstr = os.getenv("TEST_NWC") + keys = Keys.parse(os.getenv("TEST_USER")) + + bolt11 = zaprequest("bot@nostrdvm.com", 5, "test", None, PublicKey.parse("npub1cc79kn3phxc7c6mn45zynf4gtz0khkz59j4anew7dtj8fv50aqrqlth2hf"), keys, dvmconfig.DVMConfig.RELAY_LIST, zaptype="private") + + print(bolt11) + result = nwc_zap(connectionstr, bolt11, keys, externalrelay=None) + print(result) + + +if __name__ == '__main__': + env_path = Path('.env') + if not env_path.is_file(): + with open('.env', 'w') as f: + print("Writing new .env file") + f.write('') + if env_path.is_file(): + print(f'loading environment from {env_path.resolve()}') + dotenv.load_dotenv(env_path, verbose=True, override=True) + else: + raise FileNotFoundError(f'.env file not found at {env_path} ') + playground() \ No newline at end of file