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
37 from electrum.plugins import run_hook
39 from util import MyTreeWidget
41 class TxDialog(QDialog):
43 def __init__(self, tx, parent):
45 tx_dict = tx.as_dict()
47 self.wallet = parent.wallet
49 QDialog.__init__(self)
50 self.setMinimumWidth(600)
51 self.setWindowTitle(_("Transaction"))
57 vbox.addWidget(QLabel(_("Transaction ID:")))
58 self.tx_hash_e = QLineEdit()
59 self.tx_hash_e.setReadOnly(True)
60 vbox.addWidget(self.tx_hash_e)
61 self.status_label = QLabel()
62 vbox.addWidget(self.status_label)
64 self.date_label = QLabel()
65 vbox.addWidget(self.date_label)
66 self.amount_label = QLabel()
67 vbox.addWidget(self.amount_label)
68 self.fee_label = QLabel()
69 vbox.addWidget(self.fee_label)
75 buttons = QHBoxLayout()
76 vbox.addLayout( buttons )
80 self.sign_button = b = QPushButton(_("Sign"))
81 b.clicked.connect(self.sign)
84 self.broadcast_button = b = QPushButton(_("Broadcast"))
85 b.clicked.connect(lambda: self.parent.broadcast_transaction(self.tx))
90 self.save_button = b = QPushButton(_("Save"))
91 b.clicked.connect(self.save)
94 cancelButton = QPushButton(_("Close"))
95 cancelButton.clicked.connect(lambda: self.done(0))
96 buttons.addWidget(cancelButton)
97 cancelButton.setDefault(True)
99 b = QPushButton(_("Show QR code"))
100 b.clicked.connect(self.show_qr)
101 buttons.insertWidget(1,b)
107 json_text = json.dumps(self.tx.as_dict()).replace(' ', '')
108 self.parent.show_qrcode(json_text, 'Transaction')
109 except Exception as e:
110 self.parent.show_message(str(e))
114 tx_dict = self.tx.as_dict()
115 input_info = json.loads(tx_dict["input_info"])
116 self.parent.sign_raw_transaction(self.tx, input_info)
121 name = 'signed_%s.txn' % (self.tx.hash()[0:8]) if self.tx.is_complete() else 'unsigned.txn'
122 fileName = self.parent.getSaveFileName(_("Select where to save your signed transaction"), name, "*.txn")
124 with open(fileName, "w+") as f:
125 f.write(json.dumps(self.tx.as_dict(),indent=4) + '\n')
126 self.show_message(_("Transaction saved successfully"))
132 is_relevant, is_mine, v, fee = self.wallet.get_tx_value(self.tx)
134 if self.tx.is_complete():
135 status = _("Status: Signed")
136 self.sign_button.hide()
137 tx_hash = self.tx.hash()
139 if tx_hash in self.wallet.transactions.keys():
140 conf, timestamp = self.wallet.verifier.get_confirmations(tx_hash)
142 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
145 status = _("Status: %d confirmations")%conf
146 self.broadcast_button.hide()
150 self.broadcast_button.show()
152 status = _("Status: Unsigned")
154 if not self.wallet.is_watching_only():
155 self.sign_button.show()
157 self.sign_button.hide()
158 self.broadcast_button.hide()
161 self.tx_hash_e.setText(tx_hash)
162 self.status_label.setText(status)
164 if time_str is not None:
165 self.date_label.setText(_("Date: %s")%time_str)
166 self.date_label.show()
168 self.date_label.hide()
171 # if we are not synchronized, we cannot tell
172 if self.parent.network is None or not self.parent.network.is_running() or not self.parent.network.is_connected():
174 if not self.wallet.up_to_date:
180 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v-fee) + ' ' + self.parent.base_unit())
181 self.fee_label.setText(_("Transaction fee")+': %s'% self.parent.format_amount(fee) + ' ' + self.parent.base_unit())
183 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
184 self.fee_label.setText(_("Transaction fee")+': '+ _("unknown"))
186 self.amount_label.setText(_("Amount received:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
188 self.amount_label.setText(_("Transaction unrelated to your wallet"))
191 def exec_menu(self, position,l):
192 item = l.itemAt(position)
194 addr = unicode(item.text(0))
196 menu.addAction(_("Copy to clipboard"), lambda: self.parent.app.clipboard().setText(addr))
197 menu.exec_(l.viewport().mapToGlobal(position))
200 def add_io(self, vbox):
202 if self.tx.locktime > 0:
203 vbox.addWidget(QLabel("LockTime: %d\n" % self.tx.locktime))
205 vbox.addWidget(QLabel(_("Inputs")))
207 if x.get('is_coinbase'):
210 _hash = x.get('prevout_hash')
211 return _hash[0:16] + '...' + _hash[-8:] + ":%d"%x.get('prevout_n') + u'\t' + "%s"%x.get('address')
212 lines = map(format_input, self.tx.inputs )
214 i_text.setText('\n'.join(lines))
215 i_text.setReadOnly(True)
216 i_text.setMaximumHeight(100)
217 vbox.addWidget(i_text)
219 vbox.addWidget(QLabel(_("Outputs")))
220 lines = map(lambda x: x[0] + u'\t\t' + self.parent.format_amount(x[1]), self.tx.outputs)
222 o_text.setText('\n'.join(lines))
223 o_text.setReadOnly(True)
224 o_text.setMaximumHeight(100)
225 vbox.addWidget(o_text)
229 def show_message(self, msg):
230 QMessageBox.information(self, _('Message'), msg, _('OK'))