allow change lnaddress, remove one step of tutorial.

This commit is contained in:
Believethehype 2024-09-17 16:39:42 +02:00
parent 9f4e7c5d40
commit 92a993e184
5 changed files with 48 additions and 177 deletions

View File

@ -1,6 +1,8 @@
# LIGHTNING/ZAP FUNCTIONS
import json
import os
import random
import string
import urllib.parse
from pathlib import Path
@ -347,20 +349,26 @@ def get_price_per_sat(currency):
return price_currency_per_sat
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain, newname = " ", currentname=" "):
if newname == " ":
newname = identifier
def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain):
print(os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper()))
data = {
'name': identifier,
'name': newname,
'domain': nostdressdomain,
'kind': "lnbits",
'host': os.getenv("LNBITS_HOST"),
'key': os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper()),
'pin': pin,
'npub': npub,
'currentname': " "
'currentname': currentname
}
try:
url = "https://" + nostdressdomain + "/api/easy/"
res = requests.post(url, data=data)
@ -368,10 +376,22 @@ def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain):
obj = json.loads(res.text)
if obj.get("ok"):
return identifier + "@" + nostdressdomain, obj["pin"]
return data["name"] + "@" + nostdressdomain, obj["pin"]
except Exception as e:
print(e)
return "", ""
print("Creating random name..")
data["name"] = data["name"] + "_" + randomword(10)
try:
url = "https://" + nostdressdomain + "/api/easy/"
res = requests.post(url, data=data)
print(res.text)
obj = json.loads(res.text)
if obj.get("ok"):
return data["name"] + "@" + nostdressdomain, obj["pin"]
except Exception as e:
return "", ""
def check_and_set_ln_bits_keys(identifier, npub):
@ -385,7 +405,7 @@ def check_and_set_ln_bits_keys(identifier, npub):
pin = ""
if os.getenv("NOSTDRESS_DOMAIN") and success != "failed":
print(os.getenv("NOSTDRESS_DOMAIN"))
lnaddress, pin = make_ln_address_nostdress(identifier, npub, " ", os.getenv("NOSTDRESS_DOMAIN"))
lnaddress, pin = make_ln_address_nostdress(identifier, npub, " ", os.getenv("NOSTDRESS_DOMAIN"), identifier)
add_key_to_env_file("LNADDRESS_" + identifier.upper(), lnaddress)
add_key_to_env_file("LNADDRESS_PIN_" + identifier.upper(), pin)
@ -397,6 +417,15 @@ def check_and_set_ln_bits_keys(identifier, npub):
os.getenv("LNADDRESS_" + identifier.upper()))
def change_ln_address(identifier, new_identifier):
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()
lnaddress, pin = make_ln_address_nostdress(identifier, npub, pin, os.getenv("NOSTDRESS_DOMAIN"), new_identifier, currentname=previous_identifier)
add_key_to_env_file("LNADDRESS_" + identifier.upper(), lnaddress)
add_key_to_env_file("LNADDRESS_PIN_" + identifier.upper(), pin)
print("changed lnaddress")
def add_key_to_env_file(value, oskey):
env_path = Path('.env')
if env_path.is_file():

View File

@ -163,4 +163,3 @@ if __name__ == '__main__':
asyncio.run(test_gallery())
# works

View File

@ -1,161 +0,0 @@
{
"cells": [
{
"metadata": {},
"cell_type": "markdown",
"source": "First we will load our .env file that we created in the last tutorial.",
"id": "de06960a2f8dae68"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T14:12:25.797327Z",
"start_time": "2024-09-16T14:12:25.792936Z"
}
},
"cell_type": "code",
"source": [
"import dotenv\n",
"from pathlib import Path\n",
"\n",
"env_path = Path('.env')\n",
"if not env_path.is_file():\n",
" with open('.env', 'w') as f:\n",
" print(\"Writing new .env file\")\n",
" f.write('')\n",
"if env_path.is_file():\n",
" print(f'loading environment from {env_path.resolve()}')\n",
" dotenv.load_dotenv(env_path, verbose=True, override=True)\n",
"else:\n",
" raise FileNotFoundError(f'.env file not found at {env_path} ')"
],
"id": "7a3909a4945f62c3",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loading environment from /Users/tobias/Documents/dvm/tutorials/.env\n"
]
}
],
"execution_count": 6
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Ok, lets start with the very basics. We create the configuration for own DVM. The configuration contains infos like the lightning address, the private keys and some parameters that might come in handy for each DVM.\n",
"\n",
"In order to create a new Config, including everything we need to start, like a nostr private key, a lnbits wallet, and a lightning address, all we need to do is set an identifier and call the build_default_config function.\n",
"\n",
"Note: For the sake of this tutorial our identifier is tutorial01_ + some random word, to make sure ln addresses will not be overwritten. You can also pick an identifier of your choice (if its not taken on the nostdress server)."
],
"id": "75ae876bf5da9d35"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T14:12:27.820688Z",
"start_time": "2024-09-16T14:12:27.817674Z"
}
},
"cell_type": "code",
"source": [
"from nostr_dvm.utils.dvmconfig import build_default_config\n",
"from helper import randomword\n",
"identifier = \"tutorial01_\" + randomword(10)\n",
"print(identifier)"
],
"id": "5f5cfdf34488074a",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tutorial01_knicqxeqwq\n"
]
}
],
"execution_count": 7
},
{
"metadata": {},
"cell_type": "markdown",
"source": [
"Ok, now something important. I need you to do a manual step. Put what is printed above (tutorial01_somerandomword) in the next line down there:\n",
"\n",
"This way we make sure we don't create multiple configs for the same dvm. We just want to give it one fixed name and that's the one we created above. Or you can still pick your own one, if its unique.\n",
"\n",
"identifier = tutorial01_... "
],
"id": "e0f9f41907628fd7"
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2024-09-16T14:12:34.572391Z",
"start_time": "2024-09-16T14:12:34.047756Z"
}
},
"cell_type": "code",
"source": [
"identifier = \"tutorial01_xxxxxxxx\"\n",
"\n",
"dvm_config = build_default_config(identifier)"
],
"id": "d9ef15853d433c66",
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loading environment from /Users/tobias/Documents/dvm/tutorials/.env\n",
"https://demo.lnbits.com/usermanager/api/v1/users\n",
"{'id': 'b719b9d29e9d49a7a05d7eebfa4b6ebb', 'name': 'tutorial01_knicqxeqwq', 'admin': '43a50a6074f6427385b49460cf60c6a1', 'email': '', 'password': '', 'extra': None, 'wallets': [{'id': '8743fff6b78e429894062bca8ab076f2', 'admin': '43a50a6074f6427385b49460cf60c6a1', 'name': 'tutorial01_knicqxeqwq', 'user': 'b719b9d29e9d49a7a05d7eebfa4b6ebb', 'adminkey': '846aa1465c4641729a275e4988a8a458', 'inkey': '65851d26d8254836ae1a06e954a5311f'}]}\n",
"nostrdvm.com\n",
"65851d26d8254836ae1a06e954a5311f\n",
"{\"ok\":true,\"pin\":\"f0247b53c856e1a4d879e13c8daf88e16da32740fd23cf24e3ded8c0b6e7745b\"}\n",
"\n"
]
}
],
"execution_count": 8
},
{
"metadata": {},
"cell_type": "markdown",
"source": "If everything worked, your .env file should now be updated with the information. If you run this code box again, the dvm will know it already is configured. That's why we set the name manually. We don't want a random name each time we call it, but the identifier stays with the DVM.",
"id": "536ad95eaa6e628f"
},
{
"metadata": {},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "ea6243e7373d4b70"
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

View File

@ -2,21 +2,21 @@
# Go to the very bottom of this code and replace the identifier with the one from last exercise.
# We use a GenericDVM kind to start with. Now what's this? We have many predefined tasks in the task folder, but
# the genericDVM gives you some control for simple manipulation without caring about the tasks. Important is that
# we set the Kind of the GenericDVM. In Line 20 you see that we give it Kind 5050 (Text generation).
# we set the Kind of the GenericDVM. In Line 28 you see that we give it Kind 5050 (Text generation).
# 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 os
from pathlib import Path
import dotenv
from nostr_dvm.tasks.generic_dvm import GenericDVM
from nostr_sdk import Kind
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.nip89_utils import NIP89Config
from nostr_dvm.utils.zap_utils import change_ln_address
def run_dvm(identifier):
@ -74,5 +74,9 @@ if __name__ == '__main__':
raise FileNotFoundError(f'.env file not found at {env_path} ')
# Replace the identifier with the one from the last notebook, or a new dvmconfig will be stored
identifier = "tutorial01_xxxxxxxx"
identifier = "tutorial01"
# psst, you can change your lightning address here:
# change_ln_address(identifier, "a_cool_new_address")
run_dvm(identifier)

View File

@ -1,5 +1,5 @@
#Welcome to part 4. This actually is is a simplistic client that will interact with our DVM.
# We will address the DVM we created in part 03, so make sure it's still running and run this Script in a new instance.
#Welcome to part 3. This actually is is a simplistic client that will interact with our DVM.
# We will address the DVM we created in part 02, so make sure it's still running and run this Script in a new instance.
# Copy the DVM's hex key that pops up at the beginning and replace the one down in the main function with your DVM's key.
# This way we will tag it and it will know it should reply to us.