Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.88 KB | None | 0 0
  1. import sys, requests, json, threading, subprocess
  2. from PyQt5.QtWidgets import *
  3. from PyQt5.QtGui import QIcon, QPixmap
  4. from PyQt5.QtCore import pyqtSlot, Qt
  5. import psutil
  6.  
  7. def ProcessExists(processName):
  8. for proc in psutil.process_iter():
  9. try:
  10. if processName.lower() in proc.name().lower():
  11. return True
  12. except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
  13. pass
  14. return False
  15.  
  16. def GUICtrlUpdateStyle(control):
  17. style = control.type + '''#''' + control.objectName() + ''' {
  18. font-size: ''' + control.myfontsize + ''';
  19. font-weight: ''' + control.myfontweight + ''';
  20. background-color: ''' + control.mybackgroundcolor + ''';
  21. color: ''' + control.mycolor + ''';
  22. border: ''' + control.myborder + ''';
  23. }
  24. '''
  25. if control.type == 'QLineEdit':
  26. style += control.type + '''#''' + control.objectName() + ''' {
  27. padding: 0px 5px 0px 5px;
  28. }
  29. '''
  30. if control.type == 'QPushButton':
  31. style += control.type + '''#''' + control.objectName() + ''':hover {
  32. background-color: ''' + control.myhoverbackgroundcolor + ''';
  33. color: ''' + control.myhovercolor + ''';
  34. }
  35. '''
  36. control.setStyleSheet(style)
  37.  
  38. def GUICtrlSetBkColor(control, color):
  39. control.mybackgroundcolor = color
  40. GUICtrlUpdateStyle(control)
  41.  
  42. def GUICtrlSetFontWeight(control, weight):
  43. control.myfontweight = weight
  44. GUICtrlUpdateStyle(control)
  45.  
  46. def GUICtrlSetColor(control, color):
  47. control.mycolor = color
  48. GUICtrlUpdateStyle(control)
  49.  
  50. def GUICtrlSetFontSize(control, size):
  51. control.myfontsize = size
  52. GUICtrlUpdateStyle(control)
  53.  
  54. def ValidAmount(szAmount):
  55. szChrset = "0123456789."
  56. for iChr in range(0, len(szAmount), 1):
  57. if not szAmount[iChr:1] in szChrset:
  58. return 0
  59. return 1
  60.  
  61. def find_str(s, char):
  62. index = 0
  63.  
  64. if char in s:
  65. c = char[0]
  66. for ch in s:
  67. if ch == c:
  68. if s[index:index+len(char)] == char:
  69. return index
  70.  
  71. index += 1
  72.  
  73. return -1
  74.  
  75. def GetNodeInfo():
  76. response = requests.post('http://127.0.0.1:22869/rpc', data='{"method" : "explorer.info.node", "params" : null, "id" : "", "jsonrpc" : "2.0"}', headers={'Content-Type':'application/json'})
  77. return json.loads(response.text)
  78.  
  79. def GetWalletBalance():
  80. response = requests.post('http://127.0.0.1:38070/json_rpc', data='{"method" : "getBalance", "params" : {}, "id" : "", "jsonrpc" : "2.0"}', headers={'Content-Type':'application/json'})
  81. return json.loads(response.text)
  82.  
  83. class App(QWidget):
  84. def __init__(self):
  85. super().__init__()
  86. self.title = 'Galaxia Wallet'
  87. self.width = 800
  88. self.height = 470
  89. self.ctrlCount = 0
  90. self.timer = 0
  91. self.xi_daemon = 0
  92. self.XiNetworkState, self.walletBalance, self.walletBalanceLocked = 0, 0, 0
  93.  
  94. self.initUI()
  95.  
  96. def closeEvent(self, event):
  97. self.timer.cancel()
  98. requests.post('http://127.0.0.1:38070/json_rpc', data='{"method" : "shutdown", "params" : {}, "id" : "", "jsonrpc" : "2.0"}', headers={'Content-Type':'application/json'})
  99. if self.xi_daemon: self.xi_daemon.terminate()
  100.  
  101. def initUI(self):
  102. self.setWindowTitle(self.title)
  103. self.setFixedSize(self.width, self.height)
  104. self.tabsControls = {}
  105.  
  106. #Image background
  107. background = QLabel(self)
  108. background.setPixmap(QPixmap("bg.png"))
  109. #Background rects
  110. self.GUICtrlCreateBox('rgba(255, 255, 255, 10%)', 0, 0, 200, 145)
  111. self.GUICtrlCreateBox('rgba(255, 255, 255, 10%)', 0, 290, 200, 115)
  112. self.GUICtrlCreateBox('rgba(255, 255, 255, 10%)', 0, 410, 200, 60)
  113.  
  114. #Balance labels
  115. self.hLabelGalaxia = self.GUICtrlCreateLabel("GALAXIA", 0, 0, 200, 60, 0, 0, '32px')
  116. self.hLabelGalaxia.setAlignment(Qt.AlignHCenter)
  117. self.hLabelBalance = self.GUICtrlCreateLabel("Balance", 25, 60, 0, 0, 0, 0, '11px', 'normal')
  118. self.hLabelBalanceValue = self.GUICtrlCreateLabel('0.000000', 25, 70, 175, 35, 0, 'white', '22px', 'normal')
  119. self.hLabelBalanceLocked = self.GUICtrlCreateLabel("Locked balance", 25, 105, 0, 0, 0, 0, '11px' , 'normal')
  120. self.hLabelBalanceLockedValue = self.GUICtrlCreateLabel('0.000000', 25, 115, 175, 25, 0, 'white', '18px', 'normal')
  121. #Network status
  122. self.hLogoGreen = QPixmap('logo_green.png').scaledToWidth(50, Qt.SmoothTransformation)
  123. self.hLogoRed = QPixmap('logo_red.png').scaledToWidth(50, Qt.SmoothTransformation)
  124. self.hLogoYellow = QPixmap('logo_yellow.png').scaledToWidth(50, Qt.SmoothTransformation)
  125. self.hLabelNetworkIcon = self.GUICtrlCreateLabel('', 5, 415, 50, 50)
  126. self.hLabelNetworkIcon.setPixmap(self.hLogoRed)
  127. self.hLabelNetwork = self.GUICtrlCreateLabel("Network status", 60, 425, 0, 0, 'transparent', 0, '14px', 'bold')
  128. self.hLabelNetworkStatus = self.GUICtrlCreateLabel("Disconnected", 60, 435, 145, 30, 'transparent', '#fc7c7c', '11px', 'bold')
  129. #Nav
  130. self.activeTab = self.hButtonSend = self.GUICtrlCreateButton('Send', 0, 150, 200, 35, 'rgba(26, 188, 156, 50%)', 'white')
  131. self.hButtonReceive = self.GUICtrlCreateButton("Receive", 0, 185, 200, 35)
  132. self.hButtonHistory = self.GUICtrlCreateButton("Transactions", 0, 220, 200, 35)
  133. self.hButtonSettings = self.GUICtrlCreateButton("Settings", 0, 255, 200, 35)
  134. #Send TAB
  135. self.hInputAmount = self.GUICtrlCreateInput('', 215, 30, 250, 30, 'rgba(255, 0, 0, 15%)')
  136. self.hInputAddress = self.GUICtrlCreateInput('', 215, 80, 250, 30, 'rgba(255, 0, 0, 15%)')
  137. self.hInputPaymentID = self.GUICtrlCreateInput('', 215, 130, 125, 30)
  138.  
  139. self.hLabelAmount = self.GUICtrlCreateLabel("Amount", 215, 15)
  140. self.hLabelAmountErr = self.GUICtrlCreateLabel("Enter amount", 365, 60, 100, 20, 0, '#b53b3b')
  141. self.hLabelAddress = self.GUICtrlCreateLabel("Address", 215, 65)
  142. self.hLabelAddressErr = self.GUICtrlCreateLabel("Enter address", 365, 110, 100, 20, 0, '#b53b3b')
  143. self.hLabelPaymentID = self.GUICtrlCreateLabel("Payment ID (Optional)", 215, 115)
  144.  
  145. self.hButtonAmountAll = self.GUICtrlCreateButton("or All", 475, 30, 50, 30)
  146. self.hButtonAddressPaste = self.GUICtrlCreateButton("Paste", 475, 80, 50, 30)
  147. self.hButtonSendSend = self.GUICtrlCreateButton("Send", 215, 170, 50, 30)
  148.  
  149. self.tabsControls[self.hButtonSend.objectName()] = [self.hInputAmount, self.hInputAddress, self.hInputPaymentID, self.hLabelAmount, self.hLabelAmountErr,
  150. self.hLabelAddress, self.hLabelAddressErr, self.hLabelPaymentID, self.hButtonAmountAll, self.hButtonAddressPaste,
  151. self.hButtonSendSend]
  152. #Settings TAB
  153. self.hLabelStartupBk = self.GUICtrlCreateBox('rgba(255, 255, 255, 10%)', 215, 15, 25, 25)
  154. self.hCheckboxStartup = self.GUICtrlCreateCheckBox('', 215, 15)
  155. self.tabsControls[self.hButtonSettings.objectName()] = [self. hLabelStartupBk, self.hCheckboxStartup]
  156.  
  157. #Transactions TAB
  158. self.hTableTransactions = QTableWidget(100, 4, self)
  159. self.hTableTransactions.move(215, 15)
  160. self.hTableTransactions.setFixedSize(570, 200)
  161. self.hTableTransactions.setItem(1, 1, QTableWidgetItem('sounds aaa but aaaa'))
  162. self.hTableTransactions.verticalHeader().hide()
  163. self.hTableTransactions.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
  164. self.hTableTransactions.setHorizontalHeaderLabels(['Date', 'Tx hash', 'Amount', 'From address'])
  165. self.hTableTransactions.horizontalHeader().setSectionResizeMode(QHeaderView.Fixed)
  166. self.hTableTransactions.horizontalHeader().resizeSection(0, 50)
  167.  
  168. self.tabsControls[self.hButtonReceive.objectName()] = []
  169. self.tabsControls[self.hButtonHistory.objectName()] = [self.hTableTransactions]
  170.  
  171. for ctrl in self.tabsControls[self.hButtonSettings.objectName()]:
  172. ctrl.hide()
  173. for ctrl in self.tabsControls[self.hButtonHistory.objectName()]:
  174. ctrl.hide()
  175.  
  176. self.timer = threading.Timer(2.5, self.XiNetworkUpdate)
  177. self.timer.start()
  178. self.show()
  179.  
  180. def GUICtrlCreateButton(self, text, left, top, width = 0, height = 0, background = 0, color = 0, fontsize = 0, fontweight = 0):
  181. button = QPushButton(text, self)
  182. self.ctrlCount += 1
  183. button.setObjectName(str(self.ctrlCount))
  184. if width: button.setFixedWidth(width)
  185. if height: button.setFixedHeight(height)
  186. button.move(left, top)
  187. button.type = 'QPushButton'
  188. button.myfontsize = fontsize if fontsize else '14px'
  189. button.myfontweight = fontweight if fontweight else 'bold'
  190. button.mybackgroundcolor = background if background else 'rgba(255, 255, 255, 10%)'
  191. button.mycolor = color if color else 'rgb(26, 188, 156)'
  192. button.myborder = 'none'
  193. button.myhoverbackgroundcolor = 'rgba(26, 188, 156, 50%)'
  194. button.myhovercolor = 'white'
  195. GUICtrlUpdateStyle(button)
  196. button.clicked.connect(self.button_proc)
  197. return button
  198.  
  199. def GUICtrlCreateCheckBox(self, text, left, top):
  200. checkbox = QCheckBox(text, self)
  201. self.ctrlCount += 1
  202. checkbox.setObjectName(str(self.ctrlCount))
  203. checkbox.move(left, top)
  204. checkbox.type = 'QCheckBox'
  205. #checkbox.clicked.connect(self.checkbox_proc)
  206. return checkbox
  207.  
  208. def GUICtrlCreateBox(self, color, left, top, width, height):
  209. box = QLabel(self)
  210. box.move(left, top)
  211. box.setFixedSize(width, height)
  212. box.setStyleSheet('background-color: ' + color)
  213. box.setAlignment(Qt.AlignHCenter)
  214. box.setAlignment(Qt.AlignVCenter)
  215. return box
  216.  
  217. def GUICtrlCreateLabel(self, text, left, top, width = 0, height = 0, background = 0, color = 0, fontsize = 0, fontweight = 0):
  218. label = QLabel(text, self)
  219. self.ctrlCount += 1
  220. label.setObjectName(str(self.ctrlCount))
  221. if width: label.setFixedWidth(width)
  222. if height: label.setFixedHeight(height)
  223. label.move(left, top)
  224. label.type = 'QLabel'
  225. label.mywidth = str(width) if width else ''
  226. label.myheight = str(height) if height else ''
  227. label.myfontsize = fontsize if fontsize else '10px'
  228. label.myfontweight = fontweight if fontweight else 'bold'
  229. label.mybackgroundcolor = background if background else 'transparent'
  230. label.mycolor = color if color else 'rgb(26, 188, 156)'
  231. label.myborder = 'none'
  232. GUICtrlUpdateStyle(label)
  233. return label
  234.  
  235. def GUICtrlCreateInput(self, text, left, top, width, height, background = 0, color = 0, fontsize = 0, fontweight = 0):
  236. input = QLineEdit(self)
  237. self.ctrlCount += 1
  238. input.setObjectName(str(self.ctrlCount))
  239. input.move(left, top)
  240. input.type = 'QLineEdit'
  241. input.setFixedSize(width, height)
  242. input.myfontsize = fontisze if fontsize else '14px'
  243. input.myfontweight = fontweight if fontweight else 'bold'
  244. input.mybackgroundcolor = background if background else 'rgba(255, 255, 255, 10%)'
  245. input.mycolor = color if color else 'rgb(26, 188, 156)'
  246. input.myborder = 'none'
  247. GUICtrlUpdateStyle(input)
  248. input.setText(text)
  249. input.textEdited.connect(self.input_proc)
  250. input.editingFinished.connect(self.input_proc_end)
  251. return input
  252.  
  253. def XiNetworkSetState(self, iState, iPercent = 0):
  254. if iState != self.XiNetworkState:
  255. self.XiNetworkState = iState
  256. if iState == 0:
  257. GUICtrlSetColor(self.hLabelNetworkIcon, '#fc7c7c')
  258. GUICtrlSetColor(self.hLabelNetworkStatus, '#fc7c7c')
  259. self.hLabelNetworkStatus.setText("Disconnected")
  260. self.hLabelNetworkIcon.setPixmap(self.hLogoRed)
  261. elif iState == 1:
  262. GUICtrlSetColor(self.hLabelNetworkIcon, '#f7ff91')
  263. GUICtrlSetColor(self.hLabelNetworkStatus, '#f7ff91')
  264. self.hLabelNetworkStatus.setText("Syncing (" + '%.2f' % iPercent + "%)")
  265. self.hLabelNetworkIcon.setPixmap(self.hLogoYellow)
  266. elif iState == 2:
  267. GUICtrlSetColor(self.hLabelNetworkIcon, 'rgb(26, 188, 156)')
  268. GUICtrlSetColor(self.hLabelNetworkStatus, 'rgb(26, 188, 156)')
  269. self.hLabelNetworkStatus.setText("Synced")
  270. self.hLabelNetworkIcon.setPixmap(self.hLogoGreen)
  271.  
  272. def button_proc(self):
  273. obj = self.sender()
  274. if obj != self.activeTab:
  275. if obj == self.hButtonSend or obj == self.hButtonReceive or obj == self.hButtonHistory or obj == self.hButtonSettings:
  276. GUICtrlSetBkColor(self.activeTab, 'rgba(255, 255, 255, 10%)')
  277. GUICtrlSetColor(self.activeTab, 'rgb(26, 188, 156)')
  278. for ctrl in self.tabsControls[self.activeTab.objectName()]:
  279. ctrl.hide()
  280. GUICtrlSetBkColor(obj, 'rgba(26, 188, 156, 50%)')
  281. GUICtrlSetColor(obj, 'white')
  282. for ctrl in self.tabsControls[obj.objectName()]:
  283. ctrl.show()
  284. self.activeTab = obj
  285. return
  286.  
  287. def input_proc_end(self):
  288. obj = self.sender()
  289. if obj == self.hInputAmount:
  290. if obj.text() != '': obj.setText('%.6f' % float(obj.text()))
  291.  
  292. def input_proc(self):
  293. obj = self.sender()
  294. if obj == self.hInputAmount:
  295. dot = find_str(obj.text(), '.')
  296. if dot >= 0 and len(obj.text()) > 7 + dot:
  297. self.hLabelAmountErr.setText('Bad amount')
  298. GUICtrlSetBkColor(self.hInputAmount, 'rgba(255, 0, 0, 15%)')
  299. elif not ValidAmount(obj.text()):
  300. self.hLabelAmountErr.setText('Bad amount')
  301. GUICtrlSetBkColor(self.hInputAmount, 'rgba(255, 0, 0, 15%)')
  302. elif obj.text() == '' or float(obj.text()) == 0 :
  303. self.hLabelAmountErr.setText('Enter amount')
  304. GUICtrlSetBkColor(self.hInputAmount, 'rgba(255, 0, 0, 15%)')
  305. elif float(obj.text()) > self.walletBalance:
  306. self.hLabelAmountErr.setText('Not enought founds')
  307. GUICtrlSetBkColor(self.hInputAmount, 'rgba(255, 0, 0, 15%)')
  308. else:
  309. self.hLabelAmountErr.setText('')
  310. GUICtrlSetBkColor(self.hInputAmount, 'rgba(255, 255, 255, 10%)')
  311. elif obj == self.hInputAddress:
  312. if obj.text() == '':
  313. self.hLabelAddressErr.setText('Enter address')
  314. GUICtrlSetBkColor(self.hInputAddress, 'rgba(255, 0, 0, 15%)')
  315. elif len(obj.text()) != 98:
  316. self.hLabelAddressErr.setText('Bad address')
  317. GUICtrlSetBkColor(self.hInputAddress, 'rgba(255, 0, 0, 15%)')
  318. elif obj.text()[0:3] != 'gxi':
  319. self.hLabelAddressErr.setText('Bad address')
  320. GUICtrlSetBkColor(self.hInputAddress, 'rgba(255, 0, 0, 15%)')
  321. else:
  322. self.hLabelAddressErr.setText('')
  323. GUICtrlSetBkColor(self.hInputAddress, 'rgba(255, 255, 255, 10%)')
  324. return
  325. def XiNetworkUpdate(self):
  326. nodeSync, networkSync = 0, 0
  327. if not ProcessExists("xi-daemon"):
  328. self.XiNetworkSetState(0)
  329. self.xi_daemon = subprocess.Popen("xi-daemon --p2p-local-ip --rpc-server --block-explorer-enable --network Galaxia.MainNet")
  330. elif not ProcessExists("xi-pgservice"):
  331. self.XiNetworkSetState(0)
  332. subprocess.Popen("xi-pgservice.exe -w BunkierWallet.wallet --rpc-legacy-security --network Galaxia.MainNet")
  333. else:
  334. nodeInfo = GetNodeInfo()
  335. nodeSync = nodeInfo['result']['chain']['top_height']
  336. networkSync = nodeInfo['result']['p2p']['height']
  337. if networkSync and networkSync > 1:
  338. if nodeSync < networkSync:
  339. self.XiNetworkSetState(1, nodeSync / networkSync * 100)
  340. else:
  341. self.XiNetworkSetState(2)
  342. else:
  343. self.XiNetworkSetState(0)
  344. walletInfo = GetWalletBalance()
  345. self.walletBalance = walletInfo['result']['available_balance'] / 1000000
  346. self.walletBalanceLocked = walletInfo['result']['locked_amount'] / 1000000
  347. self.UpdateBalance()
  348. self.timer = threading.Timer(2.5, self.XiNetworkUpdate)
  349. self.timer.start()
  350.  
  351. def UpdateBalance(self):
  352. self.hLabelBalanceValue.setText('%.6f' % self.walletBalance)
  353. self.hLabelBalanceLockedValue.setText('%.6f' % self.walletBalanceLocked)
  354.  
  355. style = '''
  356. QHeaderView::section {
  357. background-color: transparent;
  358. color: white;
  359. padding-left: 4px;
  360. border: 1px solid rgba(26, 188, 156);
  361. }
  362. QHeaderView {
  363. background-color: rgba(255, 255, 255, 10%);
  364. }
  365. QHeaderView::section:checked
  366. {
  367. background-color: transparent;
  368. }
  369. QTableWidget {
  370. gridline-color: rgba(26, 188, 156);
  371. background-color: rgba(255, 255, 255, 10%);
  372. border: 1px solid rgba(26, 188, 156);
  373. color: rgb(26, 188, 156)
  374. }
  375. QCheckBox {
  376. background-color: transparent;
  377. color: rgb(26, 188, 156);
  378. width: 25px;
  379. height: 25px;
  380. font-size: 12px;
  381. }
  382. QCheckBox::indicator {
  383. width: 25px;
  384. height: 25px;
  385. background-color: transparent;
  386. }
  387. QCheckBox::indicator:checked {
  388. margin: 7.5px 7.5px 7.5px 7.5px;
  389. width: 10px;
  390. height: 10px;
  391. background-color: rgb(26, 188, 156);
  392. }
  393. QCheckBox::indicator:unchecked {
  394. width: 25px;
  395. height: 25px;
  396. }
  397. '''
  398.  
  399. if __name__ == '__main__':
  400. app = QApplication(sys.argv)
  401. app.setStyleSheet(style)
  402. ex = App()
  403. sys.exit(app.exec_())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement