		balance = rows[0][1].split(",")

		upbal = str(int(balance[-1]) + int(theamt))
		print(upbal)

		upball = rows[0][1] + "," + upbal
		print(upball)

		cur.close()

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("INSERT INTO wallets WHERE hash = '" + thewall + "' (balance) VALUES ('" + upball + "')")
		con.commit()
		cur.close()





		from flask import Flask, render_template
from flask import Flask, redirect
from flask import request
from flask import jsonify
from flask import Flask, g
from random import seed
from random import random
import os
import sqlite3
import base64
import lnurl
import requests 
import hashlib
import time

#DATABASE = 'database.db'

INVOICE_KEY = "MzAwMjE6MWUyOTlkZDc5MzFiMTQxNThjNDc4OWJiNGZkMjhlMzMxNzk1NGY3ZjRkYTgzYmNlOWFjNTJmZDUyYTc1MGI3ZQ=="
API_ENDPOINT = "https://lntxbot.bigsun.xyz"

app = Flask(__name__)


DEFAULT_PATH = "database.sqlite3"


def db_connect(db_path=DEFAULT_PATH):
    con = sqlite3.connect(db_path)
    return con

def encrypt_string(hash_string):
    sha_signature = \
        hashlib.sha256(hash_string.encode()).hexdigest()
    return sha_signature


@app.route('/')
def home():

	return render_template('index.html')

@app.route('/deletewallet')
def deletewallet():

	thewal = request.args.get('wal');

	con = db_connect() 
	cur = con.cursor() 
	print(thewal)
	cur.execute("select * from wallets WHERE hash = '" + str(thewal) + "'")
	rowss = cur.fetchall()

	if len(rowss) > 0:
		
		cur.close()
		print(rowss)

		userupt = "del" + rowss[0][5]

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("UPDATE wallets SET user = '"+userupt+"' WHERE hash = '" + rowss[0][0] + "'")

		con.commit()
		cur.close()

		return render_template('deletewallet.html', user = rowss[0][5])
	else:	
		return render_template('index.html')





	
@app.route('/getinvoice')
def getinvoice():
    
	amt = request.args.get('amt');
	nme = request.args.get('nme');

	dataj = {'amt': amt, 'memo': nme} 
	headers = {'Authorization': 'Basic %s' %  INVOICE_KEY}


	r = requests.post(url = API_ENDPOINT + "/addinvoice", json = dataj, headers = headers) 
	  
	data = r.json() 

	pay_req = data['pay_req']
	payment_hash = data['payment_hash']

	return jsonify({"pay_req": pay_req, "payment_hash": payment_hash}), 200


@app.route('/paymentcheck')
def paymentcheck():
	thehash = request.args.get('hash');
	theamt = request.args.get('amt');
	thenme = request.args.get('nme');

	headers = {'Authorization': 'Basic %s' %  INVOICE_KEY}
	r = requests.post(url = API_ENDPOINT + "/invoicestatus/" + thehash, headers = headers) 
	data = r.json() 
	print(data)

    
	if data == "":
		return jsonify({"paid": "false"}), 200
	else:
		#database add

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("INSERT INTO accounts (userhash, wallets) VALUES ('" + thehash + "','" + thehash + ",')")
		con.commit()
		cur.close()

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("INSERT INTO wallets (hash, balance, name) VALUES ('" + thehash + "','" + theamt + ",','" + thenme + "')")
		con.commit()
		cur.close()

		return jsonify({"paid": "true","userhash": thehash}), 200


@app.route('/paymentadd')
def paymentadd():
	thehash = request.args.get('hash');
	theamt = request.args.get('amt');
	thenme = request.args.get('nme');
	thewall = request.args.get('wal');

	headers = {'Authorization': 'Basic %s' %  INVOICE_KEY}
	r = requests.post(url = API_ENDPOINT + "/invoicestatus/" + thehash, headers = headers) 
	data = r.json() 
	print(data)

    
	if data == "":
		return jsonify({"paid": "false"}), 200
	else:
		con = db_connect() 
		cur = con.cursor() 

		cur.execute("select * from wallets WHERE hash = '" + str(thewall) + "'")
	 
		rows = cur.fetchall()
		print(rows)
        
		lastamt = rows[0][1].split(",")
		newamt = int(lastamt[-1]) + int(theamt)
		updamt = rows[0][1] + "," + str(newamt)

		thetime = time.time()
		thereceive = "!" + thenme + "," + str(thetime) + "," + str(theamt)

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("UPDATE wallets SET received = '" + rows[0][3] + thereceive + "' WHERE hash = '" + str(thewall) + "'")
		
		con.commit()
		cur.close()

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("UPDATE wallets SET balance = '" + updamt + "' WHERE hash = '" + str(thewall) + "'")
		
		con.commit()
		cur.close()
		
		return jsonify({"paid": "true","wal": thewall}), 200


  
@app.route('/lnurlwallet')
def lnurlwallet():

	#put in a function
	thestr = request.args.get('lightning');
	lnurll = lnurl.decode(thestr)
	r = requests.get(url = lnurll) 

	data = r.json() 
	print(data)

	callback = data['callback']
	maxwithdraw = data['maxWithdrawable']
	withdraw = int(maxwithdraw/1000)
	k1 = data['k1']

	#get invoice
	dataj = {'amt': str(withdraw)} 
	headers = {'Authorization': 'Basic %s' %  INVOICE_KEY}

	rr = requests.post(url = API_ENDPOINT + "/addinvoice", json = dataj, headers = headers) 
	  
	dataa = rr.json() 

	#get callback

	pay_req = dataa['pay_req']
	payment_hash = dataa['payment_hash']

	invurl =  callback + '&k1=' + k1 + '&pr=' + pay_req

	rrr = requests.get(url = invurl) 
	dataaa = rrr.json() 

	#database add
	return redirect("wallet?id=" + thestr, code=302)
		
		


@app.route('/wallet')
def wallet():

	theid = request.args.get('usr');
	thewal = request.args.get('wal');
	theamt = request.args.get('amt');
	thenme = request.args.get('nme');

	#Checks if the user exists in "accounts"
	con = db_connect() 
	cur = con.cursor() 
	print(thewal)
	cur.execute("select * from accounts WHERE userhash = '" + str(theid) + "'")
	rows = cur.fetchall()

	#Yes, check the user has a wallet
	if len(rows) > 0:
		cur.close()

		con = db_connect() 
		cur = con.cursor() 
		print(thewal)
		cur.execute("select * from wallets WHERE user = '" + str(rows[0][0]) + "'")
		rowss = cur.fetchall()

		#Yes, return the wallet(s)
		if len(rowss) > 0:


			for length.rowss in rows:
				#for dis
				cur.close()
				print(rowss)


				walb = rowss[i][1].split(",")[-1]
				#end for with array


			return render_template('wallet.html', walnme = rowss[0][4], user = rowss[0][5], walbal = walb, theid = theid, thewal = thewal, thesent = rowss[0][2] , thereceive = rowss[0][3], adminkey = rowss[0][6], inkey = rowss[0][7])
		
		#No, make the first wallet
		else:
			cur.close()
			con = db_connect() 
			cur = con.cursor() 

			cur.execute("INSERT INTO accounts (userhash) VALUES ('" + theid + "')")
			con.commit()
			cur.close()

			con = db_connect() 
			cur = con.cursor() 

			adminkey = encrypt_string(theid)
			inkey = encrypt_string(adminkey)

			cur.execute("INSERT INTO wallets (hash, balance, sent, received, name, user, adminkey, inkey) VALUES ('" + thewal + "',',0','0','0','" + thenme + "','" + theid + "','" + adminkey + "','" + inkey + "')")
			con.commit()
			cur.close()
			
			return render_template('wallet.html', walnme = thenme, walbal = '0', theid = theid, thewal = thewal, adminkey = adminkey, inkey = inkey)
	#No
	else:
		return render_template('index.html')


if __name__ == '__main__':
    app.run(debug=True)



    

@app.route('/paymentcheck')
def paymentcheck():
	thehash = request.args.get('hash');
	theamt = request.args.get('amt');
	thenme = request.args.get('nme');

	headers = {'Authorization': 'Basic %s' %  INVOICE_KEY}
	r = requests.post(url = API_ENDPOINT + "/invoicestatus/" + thehash, headers = headers) 
	data = r.json() 
	print(data)

    
	if data == "":
		return jsonify({"paid": "false"}), 200
	else:
		#database add

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("INSERT INTO accounts (userhash, wallets) VALUES ('" + thehash + "','" + thehash + ",')")
		con.commit()
		cur.close()

		con = db_connect() 
		cur = con.cursor() 

		cur.execute("INSERT INTO wallets (hash, balance, name) VALUES ('" + thehash + "','" + theamt + ",','" + thenme + "')")
		con.commit()
		cur.close()

		return jsonify({"paid": "true","userhash": thehash}), 200