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 run_hook('init_transaction_dialog', self, buttons)
107 tx_dict = self.tx.as_dict()
108 input_info = json.loads(tx_dict["input_info"])
109 self.parent.sign_raw_transaction(self.tx, input_info)
114 name = 'signed_%s.txn' % (self.tx.hash()[0:8]) if self.tx.is_complete() else 'unsigned.txn'
115 fileName = self.parent.getSaveFileName(_("Select where to save your signed transaction"), name, "*.txn")
117 with open(fileName, "w+") as f:
118 f.write(json.dumps(self.tx.as_dict(),indent=4) + '\n')
119 self.show_message(_("Transaction saved successfully"))
125 is_relevant, is_mine, v, fee = self.wallet.get_tx_value(self.tx)
127 if self.tx.is_complete():
128 status = _("Status: Signed")
129 self.sign_button.hide()
130 tx_hash = self.tx.hash()
132 if tx_hash in self.wallet.transactions.keys():
133 conf, timestamp = self.wallet.verifier.get_confirmations(tx_hash)
135 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
138 status = _("Status: %d confirmations")%conf
139 self.broadcast_button.hide()
143 self.broadcast_button.show()
145 status = _("Status: Unsigned")
147 if not self.wallet.is_watching_only():
148 self.sign_button.show()
150 self.sign_button.hide()
151 self.broadcast_button.hide()
154 self.tx_hash_e.setText(tx_hash)
155 self.status_label.setText(status)
157 if time_str is not None:
158 self.date_label.setText(_("Date: %s")%time_str)
159 self.date_label.show()
161 self.date_label.hide()
164 # if we are not synchronized, we cannot tell
165 if self.parent.network is None or not self.parent.network.is_running() or not self.parent.network.is_connected():
167 if not self.wallet.up_to_date:
173 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v-fee) + ' ' + self.parent.base_unit())
174 self.fee_label.setText(_("Transaction fee")+': %s'% self.parent.format_amount(fee) + ' ' + self.parent.base_unit())
176 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
177 self.fee_label.setText(_("Transaction fee")+': '+ _("unknown"))
179 self.amount_label.setText(_("Amount received:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
181 self.amount_label.setText(_("Transaction unrelated to your wallet"))
184 def exec_menu(self, position,l):
185 item = l.itemAt(position)
187 addr = unicode(item.text(0))
189 menu.addAction(_("Copy to clipboard"), lambda: self.parent.app.clipboard().setText(addr))
190 menu.exec_(l.viewport().mapToGlobal(position))
193 def add_io(self, vbox):
195 if self.tx.locktime > 0:
196 vbox.addWidget(QLabel("LockTime: %d\n" % self.tx.locktime))
198 vbox.addWidget(QLabel(_("Inputs")))
200 if x.get('is_coinbase'):
203 _hash = x.get('prevout_hash')
204 return _hash[0:16] + '...' + _hash[-8:] + ":%d"%x.get('prevout_n') + u'\t' + "%s"%x.get('address')
205 lines = map(format_input, self.tx.inputs )
207 i_text.setText('\n'.join(lines))
208 i_text.setReadOnly(True)
209 i_text.setMaximumHeight(100)
210 vbox.addWidget(i_text)
212 vbox.addWidget(QLabel(_("Outputs")))
213 lines = map(lambda x: x[0] + u'\t\t' + self.parent.format_amount(x[1]), self.tx.outputs)
215 o_text.setText('\n'.join(lines))
216 o_text.setReadOnly(True)
217 o_text.setMaximumHeight(100)
218 vbox.addWidget(o_text)
222 def show_message(self, msg):
223 QMessageBox.information(self, _('Message'), msg, _('OK'))