optional: update profile on lnaddress change

This commit is contained in:
Believethehype 2024-09-17 17:10:02 +02:00
parent 92a993e184
commit ed8c44977c
3 changed files with 27 additions and 5 deletions

View File

@ -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:

View File

@ -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')

View File

@ -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)