From 225981eea32f8e0bcf85e928d84d172376c3e938 Mon Sep 17 00:00:00 2001 From: Believethehype Date: Wed, 27 Dec 2023 14:19:19 +0100 Subject: [PATCH] added examples for nwc (currently not yet used by the framework) --- nostr_dvm/tasks/texttospeech.py | 4 +- nostr_dvm/utils/nwc_tools.py | 86 +++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 nostr_dvm/utils/nwc_tools.py diff --git a/nostr_dvm/tasks/texttospeech.py b/nostr_dvm/tasks/texttospeech.py index 996ff13..3b10d67 100644 --- a/nostr_dvm/tasks/texttospeech.py +++ b/nostr_dvm/tasks/texttospeech.py @@ -100,7 +100,9 @@ class TextToSpeech(DVMTaskInterface): print(TTS().list_models()) try: - tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2").to(device) + model = "tts_models/multilingual/multi-dataset/your_tts" + #model = "tts_models/multilingual/multi-dataset/xtts_v2" + tts = TTS(model).to(device) tts.tts_to_file( text=options["prompt"], diff --git a/nostr_dvm/utils/nwc_tools.py b/nostr_dvm/utils/nwc_tools.py new file mode 100644 index 0000000..57c9afe --- /dev/null +++ b/nostr_dvm/utils/nwc_tools.py @@ -0,0 +1,86 @@ +import json +import os + +import requests +from nostr_sdk import Keys, PublicKey, Client, nip04_encrypt, EventBuilder, Tag + +from nostr_dvm.utils.dvmconfig import DVMConfig +from nostr_dvm.utils.nostr_utils import check_and_set_private_key +from nostr_dvm.utils.zap_utils import zaprequest + + +def nwc_zap(connectionstr, bolt11, keys): + target_pubkey, relay, secret = parse_connection_str(connectionstr) + SecretSK = Keys.from_sk_str(secret) + + content = { + "method": "pay_invoice", + "params": { + "invoice": bolt11 + } + } + + client = Client(keys) + client.add_relay(relay) + client.connect() + + client_public_key = PublicKey.from_hex(target_pubkey) + encrypted_content = nip04_encrypt(SecretSK.secret_key(), client_public_key, json.dumps(content)) + + pTag = Tag.parse(["p", client_public_key.to_hex()]) + event = EventBuilder(23194, encrypted_content, + [pTag]).to_event(keys) + + event_id = client.send_event(event) + print(event_id.to_hex()) + + +def parse_connection_str(connectionstring): + split = connectionstring.split("?") + targetpubkey = split[0].split(":")[1] + split2 = split[1].split("&") + relay = split2[0].split("=")[1] + relay = relay.replace("%3A%2F%2F", "://") + secret = split2[1].split("=")[1] + return targetpubkey, relay, secret + + +def make_nwc_account(identifier, nwcdomain): + pubkey = Keys.from_sk_str(os.getenv("DVM_PRIVATE_KEY_" + identifier.upper())).public_key().to_hex() + data = { + 'name': identifier, + 'host': os.getenv("LNBITS_HOST"), + 'key': os.getenv("LNBITS_ADMIN_KEY_" + identifier.upper()), + 'pubkey': pubkey, + } + + try: + url = nwcdomain + header = {"content-type": "application/json"} + res = requests.post(url, headers=header, json=data) + obj = json.loads(res.text) + if obj['params']['connectionURI'] != "Users already exists": + return obj['params']['connectionURI'] + else: + return "" + + except Exception as e: + print(e) + return "" + + +def nwc_test(nwc_server): + connectionstring = make_nwc_account("test", nwc_server + "/api/new") + print(connectionstring) + # TODO Store the connection string in a db, use here if you already have one + # connectionstring = "nostr+walletconnect:..." + if connectionstring != "": + # we use the keys from a test user + keys = Keys.from_sk_str(check_and_set_private_key("test")) + + # we zap npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8's profile 21 sats and say Cool stuff + pubkey = PublicKey.from_bech32("npub1nxa4tywfz9nqp7z9zp7nr7d4nchhclsf58lcqt5y782rmf2hefjquaa6q8") + bolt11 = zaprequest("hype@bitcoinfixesthis.org", 21, "Cool Stuff", None, + pubkey, keys, DVMConfig.RELAY_LIST) + + nwc_zap(connectionstring, bolt11, keys)