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 util import MyTreeWidget
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)
73 buttons = QHBoxLayout()
74 vbox.addLayout( buttons )
78 self.sign_button = b = QPushButton(_("Sign"))
79 b.clicked.connect(self.sign)
82 self.broadcast_button = b = QPushButton(_("Broadcast"))
83 b.clicked.connect(lambda: self.parent.broadcast_transaction(self.tx))
88 self.save_button = b = QPushButton(_("Save"))
89 b.clicked.connect(self.save)
92 cancelButton = QPushButton(_("Close"))
93 cancelButton.clicked.connect(lambda: self.done(0))
94 buttons.addWidget(cancelButton)
95 cancelButton.setDefault(True)
103 tx_dict = self.tx.as_dict()
104 input_info = json.loads(tx_dict["input_info"])
105 self.parent.sign_raw_transaction(self.tx, input_info)
110 name = 'signed_%s.txn' % (self.tx.hash()[0:8]) if self.tx.is_complete() else 'unsigned.txn'
111 fileName = self.parent.getSaveFileName(_("Select where to save your signed transaction"), name, "*.txn")
113 with open(fileName, "w+") as f:
114 f.write(json.dumps(self.tx.as_dict(),indent=4) + '\n')
115 self.show_message(_("Transaction saved successfully"))
121 is_relevant, is_mine, v, fee = self.wallet.get_tx_value(self.tx)
123 if self.tx.is_complete():
124 status = _("Status: Signed")
125 self.sign_button.hide()
126 tx_hash = self.tx.hash()
128 if tx_hash in self.wallet.transactions.keys():
129 conf, timestamp = self.wallet.verifier.get_confirmations(tx_hash)
131 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
134 status = _("Status: %d confirmations")%conf
135 self.broadcast_button.hide()
139 self.broadcast_button.show()
141 status = _("Status: Unsigned")
143 if not self.wallet.is_watching_only():
144 self.sign_button.show()
146 self.sign_button.hide()
147 self.broadcast_button.hide()
150 self.tx_hash_e.setText(tx_hash)
151 self.status_label.setText(status)
153 if time_str is not None:
154 self.date_label.setText(_("Date: %s")%time_str)
155 self.date_label.show()
157 self.date_label.hide()
160 # if we are not synchronized, we cannot tell
161 if self.parent.network is None or not self.parent.network.is_running() or not self.parent.network.is_connected():
163 if not self.wallet.up_to_date:
169 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v-fee) + ' ' + self.parent.base_unit())
170 self.fee_label.setText(_("Transaction fee")+': %s'% self.parent.format_amount(fee) + ' ' + self.parent.base_unit())
172 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
173 self.fee_label.setText(_("Transaction fee")+': '+ _("unknown"))
175 self.amount_label.setText(_("Amount received:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
177 self.amount_label.setText(_("Transaction unrelated to your wallet"))
180 def exec_menu(self, position,l):
181 item = l.itemAt(position)
183 addr = unicode(item.text(0))
185 menu.addAction(_("Copy to clipboard"), lambda: self.parent.app.clipboard().setText(addr))
186 menu.exec_(l.viewport().mapToGlobal(position))
189 def add_io(self, vbox):
191 if self.tx.locktime > 0:
192 vbox.addWidget(QLabel("LockTime: %d\n" % self.tx.locktime))
194 vbox.addWidget(QLabel(_("Inputs")))
196 if x.get('is_coinbase'):
199 _hash = x.get('prevout_hash')
200 return _hash[0:16] + '...' + _hash[-8:] + ":%d"%x.get('prevout_n') + u'\t' + "%s"%x.get('address')
201 lines = map(format_input, self.tx.inputs )
203 i_text.setText('\n'.join(lines))
204 i_text.setReadOnly(True)
205 i_text.setMaximumHeight(100)
206 vbox.addWidget(i_text)
208 vbox.addWidget(QLabel(_("Outputs")))
209 lines = map(lambda x: x[0] + u'\t\t' + self.parent.format_amount(x[1]), self.tx.outputs)
211 o_text.setText('\n'.join(lines))
212 o_text.setReadOnly(True)
213 o_text.setMaximumHeight(100)
214 vbox.addWidget(o_text)
218 def show_message(self, msg):
219 QMessageBox.information(self, _('Message'), msg, _('OK'))