Skip to content

Commit

Permalink
Add buttons to psbt dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
giacomocaironi committed Jan 8, 2023
1 parent 14f5134 commit a98ac31
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 21 deletions.
49 changes: 48 additions & 1 deletion hwilib/_gui.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env python3

import base64
import json
import logging
import sys
Expand All @@ -26,7 +27,7 @@
exit(-1)

from PySide2.QtGui import QRegExpValidator
from PySide2.QtWidgets import QApplication, QDialog, QDialogButtonBox, QLineEdit, QMessageBox, QMainWindow
from PySide2.QtWidgets import QApplication, QDialog, QDialogButtonBox, QFileDialog, QLineEdit, QMessageBox, QMainWindow, QMenu
from PySide2.QtCore import QCoreApplication, QRegExp, Signal, Slot

import bitbox02.util
Expand Down Expand Up @@ -127,6 +128,52 @@ def __init__(self, client):
self.ui.sign_psbt_button.clicked.connect(self.sign_psbt_button_clicked)
self.ui.buttonBox.clicked.connect(self.accept)

menu = QMenu()
self.ui.import_toolbutton.setMenu(menu)
menu = self.ui.import_toolbutton.menu()
menu.addAction("From binary").triggered.connect(self.import_binary_clicked)
menu.addAction("From base64").triggered.connect(self.import_base64_clicked)

menu = QMenu()
self.ui.export_toolbutton.setMenu(menu)
menu = self.ui.export_toolbutton.menu()
menu.addAction("To binary").triggered.connect(self.export_binary_clicked)
menu.addAction("To base64").triggered.connect(self.export_base64_clicked)

@Slot()
def import_base64_clicked(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open file')
if filename:
with open(filename, 'r', encoding='utf-8') as f:
b64 = f.read()
self.ui.psbt_in_textedit.setPlainText(b64)

@Slot()
def import_binary_clicked(self):
filename, _ = QFileDialog.getOpenFileName(self, 'Open file', "", "PSBT (*.psbt)")
if filename:
with open(filename, 'rb') as f:
bin = f.read()
b64 = base64.b64encode(bin).decode()
self.ui.psbt_in_textedit.setPlainText(b64)

@Slot()
def export_base64_clicked(self):
filename, _ = QFileDialog.getSaveFileName(self, 'Save file')
if filename:
with open(filename, 'w') as f:
b64 = self.ui.psbt_out_textedit.toPlainText()
f.write(b64)

@Slot()
def export_binary_clicked(self):
filename, _ = QFileDialog.getSaveFileName(self, 'Save file', "untitled.psbt")
if filename:
with open(filename, 'wb') as f:
b64 = self.ui.psbt_out_textedit.toPlainText()
bin = base64.b64decode(b64.encode())
f.write(bin)

@Slot()
def sign_psbt_button_clicked(self):
psbt_str = self.ui.psbt_in_textedit.toPlainText()
Expand Down
60 changes: 40 additions & 20 deletions hwilib/ui/signpsbtdialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="import_toolbutton">
<property name="text">
<string>Import PSBT</string>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand All @@ -69,6 +79,26 @@
</item>
</layout>
</item>
<item row="1" column="2">
<widget class="QPlainTextEdit" name="psbt_in_textedit">
<property name="minimumSize">
<size>
<width>300</width>
<height>120</height>
</size>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
<item row="3" column="0">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
Expand All @@ -94,6 +124,16 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="export_toolbutton">
<property name="text">
<string>Export PSBT</string>
</property>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
Expand All @@ -109,16 +149,6 @@
</item>
</layout>
</item>
<item row="1" column="2">
<widget class="QPlainTextEdit" name="psbt_in_textedit">
<property name="minimumSize">
<size>
<width>300</width>
<height>120</height>
</size>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPlainTextEdit" name="psbt_out_textedit">
<property name="minimumSize">
Expand Down Expand Up @@ -175,16 +205,6 @@
</item>
</layout>
</item>
<item row="4" column="2">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down

0 comments on commit a98ac31

Please sign in to comment.