-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwallet.py
175 lines (136 loc) · 4.92 KB
/
wallet.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import subprocess
import sys, os
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from flask import Flask, render_template, request, send_file
import arweave
from utils import run_cmd
wallet_path = "wallet.json"
app = Flask(__name__)
try:
hn = run_cmd("hostname -I").split(" ")[0]
except Exception as e:
hn = run_cmd("hostname")
def valid_filename(filename):
return filename.endswith(".json") and len(filename.split(".")) == 2
@app.route("/", methods=["GET"])
def home():
# if len(wallet) > 0:
# w = wallet[0]
# else:
# w = None
# print(w)
# w = arweave.Wallet(wallet_path) if os.path.exists(wallet_path) else None
status = "a wallet.json already exists, it will be replaced if a new one is uploaded" if os.path.exists("wallet.json") else "unable to locate wallet.json, please upload one"
return render_template(
"index.html",
data={
"status":status
},
)
# @app.route('/gallery')
# def gallery():
# w = wallet[0]
# my_addr = wallet.address if wallet else 'NO WALLET'
# client = GraphqlClient(endpoint="https://arweave.net/graphql")
# query = """
# query {
# transactions(owners:["8iD-Gy_sKx98oth27JhjjP2V_xUSIGqs_8-skb63YHg"]) {
# edges {
# node {
# id
# }
# }
# }
# }
# """
# data = client.execute(query=query)
# print(data)
# return render_template('gallery.html', data="")
@app.route("/upload", methods=["POST"])
def upload():
jwk_file = request.files["jwk"]
filename = jwk_file.filename
if not valid_filename(filename):
return "Invalid File, should be a valid wallet.json file"
# Need to add more checks to verify if the wallet json is valid
jwk_file.save(wallet_path)
global wallet
# Init throws an error if wallet is invalid, catch this exception to use the old wallet or replace with new one
# wallet = arweave.Wallet(wallet_path)
return "JWK Uploaded"
@app.route("/download", methods=["GET"])
def download():
# download wallet.json file
return send_file(wallet_path, as_attachment=True)
# def run_server():
# global pid
# os.system("cd cam-py && gunicorn -w 1 wallet:app -b 0.0.0.0:8080")
# app.run(host='0.0.0.0', port=8080) # was getting no perms on port 80
class ServerWorker(QThread):
server_started = pyqtSignal(bool)
def __init__(self):
super().__init__()
def run(self):
try:
subprocess.Popen("gunicorn -w 1 wallet:app -b 0.0.0.0:8080", shell=True)
self.server_started.emit(True)
except Exception as e:
print(e)
self.server_started.emit(False)
class WalletScreen(QWidget):
def __init__(self, name="", icon_path=""):
super().__init__()
self.name = name
self.icon = QIcon(icon_path)
self.wallet = None
self.status = QLabel("Wallet Status: Not Loaded", self)
self.status.setAlignment(Qt.AlignCenter)
self.address = QLabel(f"Address: NA", self)
self.address.setAlignment(Qt.AlignCenter)
self.balance = QLabel(f"Balance: NA", self)
self.balance.setAlignment(Qt.AlignCenter)
self.portal_url = QLabel("Portal at ...", self)
self.portal_url.setAlignment(Qt.AlignCenter)
l = QLabel("or", self)
l.setAlignment(Qt.AlignCenter)
self.portal_url_1 = QLabel("...", self)
self.portal_url_1.setAlignment(Qt.AlignCenter)
self.layout = QVBoxLayout()
self.layout.addWidget(self.status)
self.layout.addWidget(self.address)
self.layout.addWidget(self.balance)
self.layout.addWidget(self.portal_url)
self.layout.addWidget(l)
self.layout.addWidget(self.portal_url_1)
self.layout.addStretch(1)
self.setLayout(self.layout)
def set_wallet(self, wallet):
self.wallet = wallet
self.refresh_data()
def start_server(self):
self.worker = ServerWorker()
self.worker.server_started.connect(self.server_started)
self.status.setText("Starting Server...")
self.worker.start()
def stop_server(self):
try:
if self.worker:
os.system("pkill gunicorn")
self.worker.terminate()
except Exception as e:
print(e)
def server_started(self, status):
if status:
self.status.setText("Server Started")
self.portal_url.setText("Portal at http://" + hn + ":8080")
self.portal_url_1.setText("http://clickoor.local:8080")
else:
self.status.setText("Failed to start server")
self.portal_url.setText("Portal at [error]")
self.portal_url_1.setText("[error]")
def refresh_data(self):
if self.wallet:
self.address.setText(f"Address: {self.wallet.address}")
self.balance.setText(f"Balance: {self.wallet.balance} AR")