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__':
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():
print(f'loading environment from {env_path.resolve()}')
dotenv.load_dotenv(env_path, verbose=True, override=True)

View File

@ -43,9 +43,6 @@ def send_file_to_server(filepath, address):
return result
# headers = {'Content-type': 'application/x-www-form-urlencoded'}
"""
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).

View File

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