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_nvc.i18n import _, set_language
21 from electrum_nvc.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_nvc import transaction
37 from electrum_nvc.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 self.buttons = 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)
100 b.setIcon(QIcon(":icons/qrcode.png"))
101 b.clicked.connect(self.show_qr)
102 buttons.insertWidget(1,b)
104 run_hook('transaction_dialog', self)
110 text = self.tx.raw.decode('hex')
112 self.parent.show_qrcode(text, 'Transaction')
113 except Exception as e:
114 self.show_message(str(e))
118 self.parent.sign_raw_transaction(self.tx)
123 name = 'signed_%s.txn' % (self.tx.hash()[0:8]) if self.tx.is_complete() else 'unsigned.txn'
124 fileName = self.parent.getSaveFileName(_("Select where to save your signed transaction"), name, "*.txn")
126 with open(fileName, "w+") as f:
127 f.write(json.dumps(self.tx.as_dict(),indent=4) + '\n')
128 self.show_message(_("Transaction saved successfully"))
134 is_relevant, is_mine, v, fee = self.wallet.get_tx_value(self.tx)
135 if self.wallet.can_sign(self.tx):
136 self.sign_button.show()
138 self.sign_button.hide()
140 if self.tx.is_complete():
142 tx_hash = self.tx.hash()
144 if tx_hash in self.wallet.transactions.keys():
145 conf, timestamp = self.wallet.verifier.get_confirmations(tx_hash)
147 time_str = datetime.datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
150 status = _("%d confirmations")%conf
151 self.broadcast_button.hide()
155 self.broadcast_button.show()
157 s, r = self.tx.signature_count()
158 status = _("Unsigned") if s == 0 else _('Partially signed (%d/%d)'%(s,r))
160 self.broadcast_button.hide()
163 self.tx_hash_e.setText(tx_hash)
164 self.status_label.setText(_('Status:') + ' ' + status)
166 if time_str is not None:
167 self.date_label.setText(_("Date: %s")%time_str)
168 self.date_label.show()
170 self.date_label.hide()
172 # if we are not synchronized, we cannot tell
173 if self.parent.network is None or not self.parent.network.is_running() or not self.parent.network.is_connected():
175 if not self.wallet.up_to_date:
181 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v-fee) + ' ' + self.parent.base_unit())
182 self.fee_label.setText(_("Transaction fee")+': %s'% self.parent.format_amount(fee) + ' ' + self.parent.base_unit())
184 self.amount_label.setText(_("Amount sent:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
185 self.fee_label.setText(_("Transaction fee")+': '+ _("unknown"))
187 self.amount_label.setText(_("Amount received:")+' %s'% self.parent.format_amount(v) + ' ' + self.parent.base_unit())
189 self.amount_label.setText(_("Transaction unrelated to your wallet"))
191 run_hook('transaction_dialog_update', self)
195 def add_io(self, vbox):
197 if self.tx.locktime > 0:
198 vbox.addWidget(QLabel("LockTime: %d\n" % self.tx.locktime))
200 vbox.addWidget(QLabel(_("Inputs")))
202 if x.get('is_coinbase'):
205 _hash = x.get('prevout_hash')
206 return _hash[0:16] + '...' + _hash[-8:] + ":%d"%x.get('prevout_n') + u'\t' + "%s"%x.get('address')
207 lines = map(format_input, self.tx.inputs )
209 i_text.setText('\n'.join(lines))
210 i_text.setReadOnly(True)
211 i_text.setMaximumHeight(100)
212 vbox.addWidget(i_text)
214 vbox.addWidget(QLabel(_("Outputs")))
215 lines = map(lambda x: x[1] + u'\t\t' + self.parent.format_amount(x[2]), self.tx.get_outputs())
217 o_text.setText('\n'.join(lines))
218 o_text.setReadOnly(True)
219 o_text.setMaximumHeight(100)
220 vbox.addWidget(o_text)
224 def show_message(self, msg):
225 QMessageBox.information(self, _('Message'), msg, _('OK'))