forked from 21isenough/LightningATM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qr.py
executable file
·93 lines (73 loc) · 2.87 KB
/
qr.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
#!/usr/bin/python3
import zbarlight
import logging
import requests
import time
import config
from PIL import Image
from io import BytesIO
from picamera import PiCamera
logger = logging.getLogger("QR")
def scan():
with PiCamera() as camera:
try:
camera.start_preview()
time.sleep(1)
logger.info("Start scanning for QR code")
except:
logger.error("PiCamera.start_preview() raised an exception")
stream = BytesIO()
qr_codes = None
# Set timeout to 10 seconds
timeout = time.time() + 10
while qr_codes is None and (time.time() < timeout):
stream.seek(0)
# Start camera stream (make sure RaspberryPi camera is focused correctly
# manually adjust it, if not)
camera.capture(stream, "jpeg")
qr_codes = zbarlight.scan_codes("qrcode", Image.open(stream))
time.sleep(0.05)
camera.stop_preview()
# break immediately if we didn't get a qr code scan
if not qr_codes:
logger.info("No QR within 10 seconds detected")
return False
# decode the first qr_code to get the data
qr_code = qr_codes[0].decode()
return qr_code
def scan_attempts(target_attempts):
"""Scan and evaluate users credentials
"""
attempts = 0
while attempts < target_attempts:
qrcode = scan()
if qrcode:
logger.info("QR code successfuly detected.")
return qrcode
else:
attempts += 1
logger.error("{}. attempt!".format(attempts))
logger.error("{} failed scanning attempts.".format(target_attempts))
def scan_credentials():
credentials = scan_attempts(4)
if credentials:
if ("lnd-config" in credentials) and ("lnd.config" in credentials):
logger.info("BTCPayServer LND Credentials detected.")
try:
r = requests.get(credentials.lstrip("config="))
data = r.json()
data = data["configurations"][0]
config.update_config("btcpay", "url", data["uri"] + "v1")
config.update_config("lnd", "macaroon", data["adminMacaroon"])
config.update_config("atm", "activewallet", "btcpay_lnd")
except:
logger.error("QR not valid (they expire after 10 minutes)")
elif ("lntxbot" in credentials) and ("@" in credentials):
logger.info("Lntxbot Credentials detected.")
config.update_config("lntxbot", "creds", credentials.split("@")[0])
config.update_config("lntxbot", "url", credentials.split("@")[1])
config.update_config("atm", "activewallet", "lntxbot")
else:
logger.error("No credentials to a known wallet detected.")
else:
logger.error("No credentials to a known wallet could be detected.")