3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2012 thomasv@gitorious
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 import sys, time, datetime, re, threading
20 from electrum.i18n import _, set_language
21 from electrum.util import print_error, print_msg
22 import os.path, json, ast, traceback
30 sys.exit("Error: Could not import PyQt4 on Linux systems, you may try 'sudo apt-get install python-qt4'")
32 from PyQt4.QtGui import *
33 from PyQt4.QtCore import *
34 import PyQt4.QtCore as QtCore
36 from electrum import transaction
39 class TxDialog(QDialog):
41 def __init__(self, tx, parent):
43 tx_dict = tx.as_dict()
45 self.wallet = parent.wallet
47 QDialog.__init__(self)
48 self.setMinimumWidth(600)
49 self.setWindowTitle(_('Transaction'))
55 vbox.addWidget(QLabel("Transaction ID:"))
56 self.tx_hash_e = QLineEdit()
57 self.tx_hash_e.setReadOnly(True)
58 vbox.addWidget(self.tx_hash_e)
59 self.status_label = QLabel()
60 vbox.addWidget(self.status_label)
62 self.date_label = QLabel()
63 vbox.addWidget(self.date_label)
64 self.amount_label = QLabel()
65 vbox.addWidget(self.amount_label)
66 self.fee_label = QLabel()
67 vbox.addWidget(self.fee_label)
69 self.io = self.io_widget(tx)
70 vbox.addWidget( self.io )
72 buttons = QHBoxLayout()
73 vbox.addLayout( buttons )
77 self.sign_button = b = QPushButton(_("Sign"))
78 b.clicked.connect(self.sign)
81 self.broadcast_button = b = QPushButton(_("Broadcast"))
82 b.clicked.connect(self.broadcast)
86 self.save_button = b = QPushButton(_("Save"))
87 b.clicked.connect(self.save)
90 cancelButton = QPushButton(_("Close"))
91 cancelButton.clicked.connect(lambda: self.done(0))
92 buttons.addWidget(cancelButton)
100 tx_dict = self.tx.as_dict()
101 input_info = json.loads(tx_dict["input_info"])
102 self.parent.sign_raw_transaction(self.tx, input_info, self)
107 fileName = self.parent.getSaveFileName(_("Select where to save your signed transaction"), 'signed_%s.txn' % (self.tx.hash()[0:8]), "*.txn")
109 with open(fileName, "w+") as f:
110 f.write(json.dumps(self.tx.as_dict(),indent=4) + '\n')
111 self.show_message(_("Transaction saved successfully"))
115 tx_hash = self.tx.hash()
117 is_relevant, is_mine, v, fee = self.wallet.get_tx_value(self.tx)
119 if self.tx.is_complete:
120 status = "Status: Signed"
121 self.sign_button.hide()
123 if tx_hash in self.wallet.transactions.keys():
124 conf, timestamp = self.wallet.verifier.get_confirmations(tx_hash)
126 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
129 status = "Status: %d confirmations"%conf
130 self.broadcast_button.hide()
134 self.broadcast_button.show()
136 status = "Status: Unsigned"
138 self.sign_button.show()
139 self.broadcast_button.hide()
141 self.tx_hash_e.setText(tx_hash)
142 self.status_label.setText(status)
144 if time_str is not None:
145 self.date_label.setText("Date: %s"%time_str)
146 self.date_label.show()
148 self.date_label.hide()
153 self.amount_label.setText("Amount sent: %s"% self.parent.format_amount(v-fee))
154 self.fee_label.setText("Transaction fee: %s"% self.parent.format_amount(fee))
156 self.amount_label.setText("Amount sent: %s"% self.parent.format_amount(v))
157 self.fee_label.setText("Transaction fee: unknown")
159 self.amount_label.setText("Amount received: %s"% self.parent.format_amount(v))
161 self.amount_label.setText("Transaction unrelated to your wallet")
165 def io_widget(self, tx):
166 tabs = QTabWidget(self)
169 grid_ui = QGridLayout(tab1)
170 grid_ui.setColumnStretch(0,1)
171 tabs.addTab(tab1, _('Outputs') )
173 tree_widget = QTreeWidget(self)
174 tree_widget.setColumnCount(2)
175 tree_widget.setHeaderLabels( [_('Address'), _('Amount')] )
176 tree_widget.setColumnWidth(0, 300)
177 tree_widget.setColumnWidth(1, 50)
179 for address, value in tx.outputs:
180 item = QTreeWidgetItem( [address, "%s" % ( self.parent.format_amount(value))] )
181 tree_widget.addTopLevelItem(item)
183 tree_widget.setMaximumHeight(100)
185 grid_ui.addWidget(tree_widget)
188 grid_ui = QGridLayout(tab2)
189 grid_ui.setColumnStretch(0,1)
190 tabs.addTab(tab2, _('Inputs') )
192 tree_widget = QTreeWidget(self)
193 tree_widget.setColumnCount(2)
194 tree_widget.setHeaderLabels( [ _('Address'), _('Previous output')] )
196 for input_line in tx.inputs:
197 item = QTreeWidgetItem( [ str(input_line["address"]), str(input_line.get("prevout_hash"))] )
198 tree_widget.addTopLevelItem(item)
200 tree_widget.setMaximumHeight(100)
202 grid_ui.addWidget(tree_widget)
207 result, result_message = self.wallet.sendtx( self.tx )
209 self.show_message("Transaction successfully sent: %s" % (result_message))
213 self.show_message("There was a problem sending your transaction:\n %s" % (result_message))
215 def show_message(self, msg):
216 QMessageBox.information(self, _('Message'), msg, _('OK'))