forked from 21isenough/LightningATM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lntxbot.py
executable file
·121 lines (99 loc) · 3.4 KB
/
lntxbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/python3
import json
import os
import logging
import time
import requests
import math
import qr
import config
import utils
# import display
display_config = config.conf["atm"]["display"]
display = getattr(__import__("displays", fromlist=[display_config]), display_config)
logger = logging.getLogger("LNTXBOT")
# TODO: Add variable to set certificate check to true or false
# TODO: Add evaluation for credentials after scanning
# TODO: Remove display calls from here to app.py
def payout(amt, payment_request):
"""Attempts to pay a BOLT11 invoice
"""
data = {
"invoice": payment_request,
"amount": math.floor(amt),
}
response = requests.post(
str(config.conf["lntxbot"]["url"]) + "/payinvoice",
headers={"Authorization": "Basic %s" % config.conf["lntxbot"]["creds"]},
data=json.dumps(data),
)
response = response.json()
if response["payment_error"]:
display.update_payment_failed()
else:
display.update_thankyou_screen()
def request_lnurl(amt):
"""Request a new lnurl for 'amt' from the server
"""
data = {
"satoshis": str(math.floor(amt)),
}
response = requests.post(
str(config.conf["lntxbot"]["url"]) + "/generatelnurlwithdraw",
headers={"Authorization": "Basic %s" % config.conf["lntxbot"]["creds"]},
data=json.dumps(data),
)
return response.json()
def get_lnurl_balance():
"""Query the lnurl balance from the server and return the
["BTC"]["AvailableBalance"] value
"""
response = requests.post(
str(config.conf["lntxbot"]["url"]) + "/balance",
headers={"Authorization": "Basic %s" % config.conf["lntxbot"]["creds"]},
)
return response.json()["BTC"]["AvailableBalance"]
def wait_for_balance_update(start_balance, timeout):
"""Loops waiting for any balance change on the lnurl and returns True if this
happens before the timeout
"""
start_time = time.time()
success = False
# loop while we wait for the balance to get updated
while True and (time.time() < start_time + timeout):
new_balance = get_lnurl_balance()
if start_balance == new_balance:
print("Balance: " + str(start_balance) + " (no changes)")
time.sleep(3)
else:
print(
"Balance: " + str(start_balance) + " | New Balance:" + str(new_balance)
)
success = True
break
return success
def process_using_lnurl(amt):
"""Processes receiving an amount using the lnurl scheme
"""
# get the new lnurl
display.update_lnurl_generation()
logger.info("LNURL requested")
lnurl = request_lnurl(amt)
# Check EPD_SIZE is defined
utils.check_epd_size()
# create a qr code image and print it to terminal
qr_img = utils.generate_lnurl_qr(lnurl["lnurl"])
# draw the qr code on the e-ink screen
display.draw_lnurl_qr(qr_img)
# get the balance? back from the bot
start_balance = get_lnurl_balance()
print(start_balance)
# loop while we wait for a balance update or until timeout reached
success = wait_for_balance_update(start_balance, timeout=90)
if success:
display.update_thankyou_screen()
logger.info("LNURL withdrawal succeeded")
return
else:
# TODO: I think we should handle a failure here
logger.error("LNURL withdrawal failed (within 90 seconds)")