error handling if no .env file is provided

This commit is contained in:
Believethehype 2023-12-19 11:36:25 +01:00
parent 3043c188f5
commit 70e684f0b1
4 changed files with 18 additions and 25 deletions

View File

@ -146,6 +146,10 @@ def playground():
if __name__ == '__main__': if __name__ == '__main__':
env_path = Path('.env') env_path = Path('.env')
if not env_path.is_file():
with open('.env', 'w') as f:
print("Wrting new .env file")
f.write('')
if env_path.is_file(): if env_path.is_file():
print(f'loading environment from {env_path.resolve()}') print(f'loading environment from {env_path.resolve()}')
dotenv.load_dotenv(env_path, verbose=True, override=True) dotenv.load_dotenv(env_path, verbose=True, override=True)

View File

@ -43,9 +43,6 @@ def send_file_to_server(filepath, address):
return result return result
# headers = {'Content-type': 'application/x-www-form-urlencoded'}
""" """
check_n_server_status(request_form, address) check_n_server_status(request_form, address)
Function that requests the status of the current process with the jobID (we use the Nostr event as jobID). Function that requests the status of the current process with the jobID (we use the Nostr event as jobID).

View File

@ -9,12 +9,12 @@ from Crypto.Cipher import AES
from Crypto.Util.Padding import pad from Crypto.Util.Padding import pad
from bech32 import bech32_decode, convertbits, bech32_encode from bech32 import bech32_decode, convertbits, bech32_encode
from nostr_sdk import nostr_sdk, PublicKey, SecretKey, Event, EventBuilder, Tag, Keys from nostr_sdk import nostr_sdk, PublicKey, SecretKey, Event, EventBuilder, Tag, Keys
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
import lnurl import lnurl
from hashlib import sha256 from hashlib import sha256
import dotenv import dotenv
# TODO tor connection to lnbits # TODO tor connection to lnbits
# proxies = { # proxies = {
# 'http': 'socks5h://127.0.0.1:9050', # 'http': 'socks5h://127.0.0.1:9050',
@ -23,6 +23,7 @@ import dotenv
proxies = {} proxies = {}
def parse_zap_event_tags(zap_event, keys, name, client, config): def parse_zap_event_tags(zap_event, keys, name, client, config):
zapped_event = None zapped_event = None
invoice_amount = 0 invoice_amount = 0
@ -126,10 +127,11 @@ def create_bolt11_lud16(lud16, amount):
except: except:
return None return None
def create_lnbits_account(name): def create_lnbits_account(name):
if os.getenv("LNBITS_ADMIN_ID") is None or os.getenv("LNBITS_ADMIN_ID") == "": if os.getenv("LNBITS_ADMIN_ID") is None or os.getenv("LNBITS_ADMIN_ID") == "":
print("No admin id set, no wallet created.") print("No admin id set, no wallet created.")
return return "","","","", "failed"
data = { data = {
'admin_id': os.getenv("LNBITS_ADMIN_ID"), 'admin_id': os.getenv("LNBITS_ADMIN_ID"),
'wallet_name': name, 'wallet_name': name,
@ -144,9 +146,11 @@ def create_lnbits_account(name):
walletjson = json.loads(r.text) walletjson = json.loads(r.text)
print(walletjson) print(walletjson)
if walletjson.get("wallets"): if walletjson.get("wallets"):
return walletjson['wallets'][0]['inkey'], walletjson['wallets'][0]['adminkey'], walletjson['wallets'][0]['id'], walletjson['id'], "success" return walletjson['wallets'][0]['inkey'], walletjson['wallets'][0]['adminkey'], walletjson['wallets'][0][
'id'], walletjson['id'], "success"
except: except:
print("error creating wallet") print("error creating wallet")
return "", "", "", "", "failed"
def check_bolt11_ln_bits_is_paid(payment_hash: str, config): def check_bolt11_ln_bits_is_paid(payment_hash: str, config):
@ -278,9 +282,6 @@ def zap(lud16: str, amount: int, content, zapped_event: Event, keys, dvm_config,
return None return None
def get_price_per_sat(currency): def get_price_per_sat(currency):
import requests import requests
@ -298,13 +299,11 @@ def get_price_per_sat(currency):
return price_currency_per_sat return price_currency_per_sat
def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain): def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain):
#env_path = Path('.env') # env_path = Path('.env')
#if env_path.is_file(): # if env_path.is_file():
# dotenv.load_dotenv(env_path, verbose=True, override=True) # dotenv.load_dotenv(env_path, verbose=True, override=True)
print(os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper())) print(os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper()))
data = { data = {
'name': identifier, 'name': identifier,
@ -314,10 +313,9 @@ def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain):
'key': os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper()), 'key': os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper()),
'pin': pin, 'pin': pin,
'npub': npub, 'npub': npub,
'currentname': " " 'currentname': " "
} }
try: try:
url = "https://" + nostdressdomain + "/api/easy/" url = "https://" + nostdressdomain + "/api/easy/"
res = requests.post(url, data=data) res = requests.post(url, data=data)
@ -330,8 +328,8 @@ def make_ln_address_nostdress(identifier, npub, pin, nostdressdomain):
print(e) print(e)
return "", "" return "", ""
def check_and_set_ln_bits_keys(identifier, npub):
def check_and_set_ln_bits_keys(identifier, npub):
if not os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper()): if not os.getenv("LNBITS_INVOICE_KEY_" + identifier.upper()):
invoicekey, adminkey, walletid, userid, success = create_lnbits_account(identifier) invoicekey, adminkey, walletid, userid, success = create_lnbits_account(identifier)
add_key_to_env_file("LNBITS_INVOICE_KEY_" + identifier.upper(), invoicekey) add_key_to_env_file("LNBITS_INVOICE_KEY_" + identifier.upper(), invoicekey)
@ -341,7 +339,7 @@ def check_and_set_ln_bits_keys(identifier, npub):
lnaddress = "" lnaddress = ""
pin = "" pin = ""
if os.getenv("NOSTDRESS_DOMAIN"): if os.getenv("NOSTDRESS_DOMAIN") and success != "failed":
print(os.getenv("NOSTDRESS_DOMAIN")) 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"))
add_key_to_env_file("LNADDRESS_" + identifier.upper(), lnaddress) add_key_to_env_file("LNADDRESS_" + identifier.upper(), lnaddress)
@ -356,14 +354,8 @@ def check_and_set_ln_bits_keys(identifier, npub):
os.getenv("LNADDRESS_" + identifier.upper())) os.getenv("LNADDRESS_" + identifier.upper()))
def add_key_to_env_file(value, oskey): def add_key_to_env_file(value, oskey):
env_path = Path('.env') env_path = Path('.env')
if env_path.is_file(): if env_path.is_file():
dotenv.load_dotenv(env_path, verbose=True, override=True) dotenv.load_dotenv(env_path, verbose=True, override=True)
dotenv.set_key(env_path, value, oskey) dotenv.set_key(env_path, value, oskey)

View File

@ -1,6 +1,6 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
VERSION = '0.0.7' VERSION = '0.0.8'
DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines' DESCRIPTION = 'A framework to build and run Nostr NIP90 Data Vending Machines'
LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. ' LONG_DESCRIPTION = ('A framework to build and run Nostr NIP90 Data Vending Machines. '
'This is an early stage release. Interfaces might change/brick') 'This is an early stage release. Interfaces might change/brick')