-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
309 lines (232 loc) · 10.5 KB
/
main.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
CrazyToolBox, a web3 utilities GUI toolbox.
© 2023 Telefónica Digital España S.L.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import threading
import os
from PySide6.QtCore import QObject, Signal, Slot, QFile, QTimer
from PySide6.QtUiTools import QUiLoader
from PySide6.QtGui import QAction
from PySide6.QtWidgets import (
QApplication,
QCheckBox,
QComboBox,
QHBoxLayout,
QPlainTextEdit,
QPushButton,
QSizePolicy,
QStackedWidget,
QTabWidget,
QTextEdit,
)
import pyperclip
from src.custom_types import MOST_USED_SOLIDITY_DATA_TYPES, CURRENCY_TYPES, MOST_USED_CURRENCY_TYPES
from src.toolbox_core import ToolBoxCore
MAX_PARAMS = 8
#####################
# UTILS #
#####################
def update_items():
global weiConverterFrom, weiConverterTo, advancedModeCheckbox
weiConverterFrom.clear()
weiConverterTo.clear()
items_to_add = CURRENCY_TYPES if advancedModeCheckbox.isChecked() else MOST_USED_CURRENCY_TYPES
weiConverterFrom.addItems(items_to_add)
weiConverterTo.addItems(items_to_add)
def add_list_widget_item(function_selector_encoder):
global paramsLayout, result_to_copy, result_signal
if paramsLayout.count() >= MAX_PARAMS - 1:
result_signal.result_to_copy.emit("")
result_signal.result.emit("Max params reached! To add more params, use the advanced mode.")
if paramsLayout.count() >= MAX_PARAMS:
return
combo_widget = QComboBox()
combo_widget.addItem("-")
combo_widget.addItems(MOST_USED_SOLIDITY_DATA_TYPES)
combo_widget.setMaximumHeight(30)
combo_widget.setMinimumHeight(30)
combo_widget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Fixed)
combo_widget.currentIndexChanged.connect(
lambda: function_selector_encoder(
functionSelectorNameInput.toPlainText(), [paramsLayout.itemAt(i).widget().currentText() for i in range(paramsLayout.count())]
)
)
paramsLayout.addWidget(combo_widget)
enable_disable_buttons()
def remove_list_widget_item():
global paramsLayout, addParamButton, removeParamButton
item = paramsLayout.itemAt(paramsLayout.count() - 1)
if item is not None:
widget = item.widget()
paramsLayout.removeWidget(widget)
widget.deleteLater()
enable_disable_buttons()
def enable_disable_buttons():
global paramsLayout, addParamButton, removeParamButton
if paramsLayout.count() >= MAX_PARAMS:
addParamButton.setEnabled(False)
removeParamButton.setEnabled(True)
elif paramsLayout.count() <= 0:
addParamButton.setEnabled(True)
removeParamButton.setEnabled(False)
else:
addParamButton.setEnabled(True)
removeParamButton.setEnabled(True)
def copy_result():
global result_to_copy, copyButton
if result_to_copy != "":
# Convert markdown to plain text
result_to_copy_plain = result_to_copy.replace("<br />", "\n").replace("**", "").replace("__", "")
# Copy to clipboard
pyperclip.copy(result_to_copy_plain)
copyButton.setText("Copied!")
QTimer.singleShot(1000, lambda: copyButton.setText("Copy"))
def reset_gui():
global result_signal
result_signal.result_to_copy.emit("")
result_signal.result.emit("")
result_signal.function_signature.emit("")
def render_licenses():
# Load the UI
licenses_ui_file = QFile(os.path.join(os.path.dirname(__file__), "view", "licenses.ui"))
licenses_ui_file.open(QFile.ReadOnly)
licenses_loader = QUiLoader()
# Set the window
licenses_window = licenses_loader.load(licenses_ui_file)
licenseAttributionsText = licenses_window.findChild(QTextEdit, "licenseAttributionsText")
closeButton = licenses_window.findChild(QPushButton, "closeButton")
license_attributions = open(os.path.join(os.path.dirname(__file__), "license_attributions.txt"), "r", encoding="utf-8").read()
licenseAttributionsText.setPlainText(license_attributions)
closeButton.clicked.connect(lambda: licenses_window.close())
# Show the window
licenses_window.show()
#####################
# SIGNALS #
#####################
class ResultSignal(QObject):
result_to_copy = Signal(str)
result = Signal(str)
function_signature = Signal(str)
@Slot()
def update_output_label(text):
global outputLabel
outputLabel.setMarkdown(text)
@Slot()
def update_result_to_copy(text):
global result_to_copy, copyButton
result_to_copy = text
if result_to_copy != "":
copyButton.setEnabled(True)
else:
copyButton.setEnabled(False)
@Slot()
def update_function_signature(text):
global functionSignatureOutput
functionSignatureOutput.setPlainText(text)
if __name__ == "__main__":
# Load the UI
app = QApplication([])
ui_file = QFile(os.path.join(os.path.dirname(__file__), "view", "main.ui"))
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
# Set the window
window = loader.load(ui_file)
# Connect the signals
result_signal = ResultSignal()
result_signal.result_to_copy.connect(update_result_to_copy)
result_signal.result.connect(update_output_label)
result_signal.function_signature.connect(update_function_signature)
# Create the core
toolBoxCore = ToolBoxCore(result_signal)
# General
result_to_copy = ""
copyButton = window.findChild(QPushButton, "copyButton")
outputLabel = window.findChild(QTextEdit, "outputLabel")
tabWidget = window.findChild(QTabWidget, "tabWidget")
licenseAttributionsAction = window.findChild(QAction, "licenseAttributionsAction")
copyButton.clicked.connect(copy_result)
tabWidget.currentChanged.connect(reset_gui)
licenseAttributionsAction.triggered.connect(render_licenses)
# Wei converter
weiConverterInput = window.findChild(QPlainTextEdit, "weiConverterInput")
weiConverterFrom = window.findChild(QComboBox, "weiConverterFrom")
weiConverterTo = window.findChild(QComboBox, "weiConverterTo")
advancedModeCheckbox = window.findChild(QCheckBox, "advancedModeCheckbox")
weiConverterInput.textChanged.connect(
lambda: toolBoxCore.wei_converter(weiConverterInput.toPlainText(), weiConverterFrom.currentText(), weiConverterTo.currentText())
)
weiConverterFrom.currentIndexChanged.connect(
lambda: toolBoxCore.wei_converter(weiConverterInput.toPlainText(), weiConverterFrom.currentText(), weiConverterTo.currentText())
)
weiConverterTo.currentIndexChanged.connect(
lambda: toolBoxCore.wei_converter(weiConverterInput.toPlainText(), weiConverterFrom.currentText(), weiConverterTo.currentText())
)
advancedModeCheckbox.stateChanged.connect(update_items)
# Function selector encoder view
advancedModeCheckbox2 = window.findChild(QCheckBox, "advancedModeCheckbox2")
fsEncoderStackedWidget = window.findChild(QStackedWidget, "fsEncoderStackedWidget")
fsEncoderStackedWidget.currentChanged.connect(reset_gui)
advancedModeCheckbox2.stateChanged.connect(
lambda: fsEncoderStackedWidget.setCurrentIndex(1 if advancedModeCheckbox2.isChecked() else 0)
)
# Function selector encoder basic
addParamButton = window.findChild(QPushButton, "addParamButton")
removeParamButton = window.findChild(QPushButton, "removeParamButton")
functionSelectorNameInput = window.findChild(QPlainTextEdit, "functionSelectorNameInput")
functionSignatureOutput = window.findChild(QPlainTextEdit, "functionSignatureOutput")
paramsLayout = window.findChild(QHBoxLayout, "paramsLayout")
addParamButton.clicked.connect(lambda: add_list_widget_item(toolBoxCore.function_selector_encoder))
removeParamButton.clicked.connect(remove_list_widget_item)
functionSelectorNameInput.textChanged.connect(
lambda: toolBoxCore.function_selector_encoder(
functionSelectorNameInput.toPlainText(), [paramsLayout.itemAt(i).widget().currentText() for i in range(paramsLayout.count())]
)
)
# Function selector encoder advanced
functionSelectorSignatureInput = window.findChild(QPlainTextEdit, "functionSelectorSignatureInput")
functionSelectorSignatureInput.textChanged.connect(
lambda: toolBoxCore.function_selector_encoder_advanced(functionSelectorSignatureInput.toPlainText())
)
# Function selector decoder
functionSelectorDecoderInput = window.findChild(QPlainTextEdit, "functionSelectorDecoderInput")
functionSelectorDecoderInput.textChanged.connect(
lambda: QTimer.singleShot(1000, lambda: toolBoxCore.function_selector_decoder(functionSelectorDecoderInput.toPlainText()))
)
# Transaction input decoder
transactionInputDecoderInput = window.findChild(QPlainTextEdit, "transactionInputDecoderInput")
transactionInputDecoderInput.textChanged.connect(
lambda: QTimer.singleShot(
1000,
lambda: threading.Thread(
target=toolBoxCore.decode_transaction_input, args=(transactionInputDecoderInput.toPlainText(),)
).start(),
)
)
# Keccak256 hash
keccak256HashInput = window.findChild(QPlainTextEdit, "keccak256HashInput")
keccak256HashInput.textChanged.connect(lambda: toolBoxCore.keccak256_hash(keccak256HashInput.toPlainText()))
# EIP55 Validator
eip55ValidatorInput = window.findChild(QPlainTextEdit, "eip55ValidatorInput")
eip55ValidatorInput.textChanged.connect(lambda: toolBoxCore.eip55_validator(eip55ValidatorInput.toPlainText()))
# Get signature owner
signatureMessageHashInput = window.findChild(QPlainTextEdit, "signatureMessageHashInput")
signatureInput = window.findChild(QPlainTextEdit, "signatureInput")
signatureMessageHashInput.textChanged.connect(
lambda: toolBoxCore.get_signer_owner(signatureMessageHashInput.toPlainText(), signatureInput.toPlainText())
)
signatureInput.textChanged.connect(
lambda: toolBoxCore.get_signer_owner(signatureMessageHashInput.toPlainText(), signatureInput.toPlainText())
)
# Show the window
window.show()
app.exec()