From ed8c44977c005e451939457e5d947a9b8e36ce2c Mon Sep 17 00:00:00 2001 From: Believethehype <1097224+believethehype@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:10:02 +0200 Subject: [PATCH] optional: update profile on lnaddress change --- nostr_dvm/utils/nostr_utils.py | 19 ++++++++++++++++++- nostr_dvm/utils/zap_utils.py | 8 ++++++-- tutorials/02_run_dvm.py | 5 +++-- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/nostr_dvm/utils/nostr_utils.py b/nostr_dvm/utils/nostr_utils.py index 5e84dd3..c92c36f 100644 --- a/nostr_dvm/utils/nostr_utils.py +++ b/nostr_dvm/utils/nostr_utils.py @@ -12,7 +12,6 @@ from nostr_sdk import Filter, Client, Alphabet, EventId, Event, PublicKey, Tag, from nostr_dvm.utils.definitions import EventDefinitions, relay_timeout, relay_timeout_long - async def get_event_by_id(event_id: str, client: Client, config=None) -> Event | None: split = event_id.split(":") if len(split) == 3: @@ -358,6 +357,24 @@ def check_and_decrypt_own_tags(event, dvm_config): return event +async def update_profile_lnaddress(private_key, dvm_config, lud16="",): + keys = Keys.parse(private_key) + opts = (Options().wait_for_send(False).send_timeout(timedelta(seconds=5)) + .skip_disconnected_relays(True)) + + signer = NostrSigner.keys(keys) + client = Client.with_opts(signer, opts) + for relay in dvm_config.RELAY_LIST: + await client.add_relay(relay) + await client.connect() + + metadata = Metadata() \ + .set_lud16(lud16) \ + .set_nip05(lud16) + + await client.set_metadata(metadata) + + async def update_profile(dvm_config, client, lud16=""): keys = Keys.parse(dvm_config.PRIVATE_KEY) try: diff --git a/nostr_dvm/utils/zap_utils.py b/nostr_dvm/utils/zap_utils.py index eca17f8..bd57894 100644 --- a/nostr_dvm/utils/zap_utils.py +++ b/nostr_dvm/utils/zap_utils.py @@ -14,7 +14,8 @@ from bech32 import bech32_decode, convertbits, bech32_encode from nostr_sdk import 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 +from nostr_dvm.utils.nostr_utils import get_event_by_id, check_and_decrypt_own_tags, update_profile, \ + update_profile_lnaddress from hashlib import sha256 import dotenv @@ -417,7 +418,7 @@ def check_and_set_ln_bits_keys(identifier, npub): os.getenv("LNADDRESS_" + identifier.upper())) -def change_ln_address(identifier, new_identifier): +async def change_ln_address(identifier, new_identifier, dvm_config, updateprofile=False): previous_identifier = os.getenv("LNADDRESS_" + identifier.upper()).split("@")[0] pin = os.getenv("LNADDRESS_PIN_" + identifier.upper()) npub = Keys.parse(os.getenv("DVM_PRIVATE_KEY_" + identifier.upper())).public_key().to_hex() @@ -425,6 +426,9 @@ def change_ln_address(identifier, new_identifier): add_key_to_env_file("LNADDRESS_" + identifier.upper(), lnaddress) add_key_to_env_file("LNADDRESS_PIN_" + identifier.upper(), pin) print("changed lnaddress") + if updateprofile: + private_key = os.getenv("DVM_PRIVATE_KEY_" + identifier.upper()) + await update_profile_lnaddress(private_key, dvm_config, lud16=lnaddress) def add_key_to_env_file(value, oskey): env_path = Path('.env') diff --git a/tutorials/02_run_dvm.py b/tutorials/02_run_dvm.py index 753ffb5..ede1640 100644 --- a/tutorials/02_run_dvm.py +++ b/tutorials/02_run_dvm.py @@ -6,6 +6,7 @@ # On https://www.data-vending-machines.org/ there's an overview on all current kinds. # On https://github.com/nostr-protocol/data-vending-machines/ you can make a PR for your own kind, if you come up with one later. # Check the run_dvm function for more explanations +import asyncio import os from pathlib import Path @@ -14,7 +15,7 @@ import dotenv from nostr_dvm.tasks.generic_dvm import GenericDVM from nostr_sdk import Kind, Keys from nostr_dvm.utils.admin_utils import AdminConfig -from nostr_dvm.utils.dvmconfig import build_default_config +from nostr_dvm.utils.dvmconfig import build_default_config, DVMConfig from nostr_dvm.utils.nip89_utils import NIP89Config from nostr_dvm.utils.zap_utils import change_ln_address @@ -77,6 +78,6 @@ if __name__ == '__main__': identifier = "tutorial01" # psst, you can change your lightning address here: - # change_ln_address(identifier, "a_cool_new_address") + #asyncio.run(change_ln_address(identifier, "test", DVMConfig(), True)) run_dvm(identifier) \ No newline at end of file