Skip to content

Commit

Permalink
Merge #648: Add buttons to psbt dialog
Browse files Browse the repository at this point in the history
a98ac31 Add buttons to psbt dialog (Giacomo Caironi)

Pull request description:

  Add buttons to import/export a psbt from/to a file

ACKs for top commit:
  achow101:
    ACK a98ac31

Tree-SHA512: 0f00f2488caead8c3fb83a61b98f4a738ba1e9e20ad05d12ba65866a54be5ae8b2f468032c8ad14adbecba246f5c43284bd18a5cae3677f15794158f55d4af1d
  • Loading branch information
achow101 committed Jul 22, 2023
2 parents ad28d6d + a98ac31 commit 7fe1410
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

def do_command(f, *args, **kwargs):
Expand Down Expand Up @@ -125,6 +126,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 7fe1410

Please sign in to comment.