electrum-bitcoinprivate/gui/kivy/uix/dialogs/tx_dialog.py

171 lines
6.1 KiB
Python
Raw Normal View History

2016-02-12 23:15:06 -08:00
from kivy.app import App
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.lang import Builder
2016-02-13 01:33:49 -08:00
from kivy.clock import Clock
2016-02-17 01:53:14 -08:00
from kivy.uix.label import Label
2016-02-12 23:15:06 -08:00
from electrum_gui.kivy.i18n import _
from datetime import datetime
Builder.load_string('''
<TxDialog@Popup>
id: popup
title: _('Transaction')
2016-02-16 01:56:58 -08:00
is_mine: True
2016-02-12 23:15:06 -08:00
can_sign: False
can_broadcast: False
fee_str: ''
2016-02-16 01:56:58 -08:00
date_str: ''
2016-02-12 23:15:06 -08:00
amount_str: ''
2016-02-17 01:53:14 -08:00
tx_hash: ''
2016-02-12 23:15:06 -08:00
status_str: ''
2016-02-16 01:56:58 -08:00
description: ''
2016-02-17 01:53:14 -08:00
outputs_str: ''
ScrollView:
BoxLayout:
orientation: 'vertical'
2016-02-16 01:56:58 -08:00
spacing: '10dp'
2016-02-17 01:53:14 -08:00
padding: '10dp'
GridLayout:
size_hint: 1, None
height: self.minimum_height
cols: 2
spacing: '10dp'
TopLabel:
text: _('Status')
TopLabel:
text: root.status_str
TopLabel:
text: _('Description') if root.description else ''
TopLabel:
text: root.description
TopLabel:
text: _('Date') if root.date_str else ''
TopLabel:
text: root.date_str
TopLabel:
text: _('Amount sent') if root.is_mine else _('Amount received')
TopLabel:
text: root.amount_str
TopLabel:
text: _('Transaction fee') if root.fee_str else ''
TopLabel:
text: root.fee_str
2016-02-16 01:56:58 -08:00
TopLabel:
2016-02-17 01:53:14 -08:00
text: _('Outputs') + ':'
2016-02-17 02:40:05 -08:00
OutputList:
2016-02-17 01:53:14 -08:00
id: outputs
2016-02-16 01:56:58 -08:00
TopLabel:
2016-02-17 01:53:14 -08:00
text: _('Transaction ID') + ':' if root.tx_hash else ''
2016-02-16 01:56:58 -08:00
TopLabel:
2016-02-17 01:53:14 -08:00
font_size: '6pt'
text: '[ref=x]%s[/ref]' %' '.join(map(''.join, zip(*[iter(root.tx_hash)]*4))) if root.tx_hash else ''
padding: '10dp', '10dp'
on_ref_press:
app._clipboard.copy(self.text)
app.show_info(_('Transaction ID copied to clipboard'))
canvas.before:
Color:
rgb: .3, .3, .3
Rectangle:
size: self.size
pos: self.pos
Widget:
size_hint: 1, 0.2
2016-02-16 01:56:58 -08:00
2016-02-17 01:53:14 -08:00
BoxLayout:
size_hint: 1, None
2016-02-16 01:56:58 -08:00
height: '48dp'
2016-02-17 01:53:14 -08:00
Button:
size_hint: 0.5, None
height: '48dp'
text: _('Sign') if root.can_sign else _('Broadcast') if root.can_broadcast else ''
opacity: 1 if root.can_sign or root.can_broadcast else 0
disabled: not( root.can_sign or root.can_broadcast )
on_release:
if root.can_sign: root.do_sign()
if root.can_broadcast: root.do_broadcast()
IconButton:
size_hint: 0.5, None
height: '48dp'
icon: 'atlas://gui/kivy/theming/light/qrcode'
on_release: root.show_qr()
Button:
size_hint: 0.5, None
height: '48dp'
text: _('Close')
on_release: popup.dismiss()
2016-02-12 23:15:06 -08:00
''')
2016-02-17 02:40:05 -08:00
2016-02-12 23:15:06 -08:00
class TxDialog(Factory.Popup):
def __init__(self, app, tx):
Factory.Popup.__init__(self)
self.app = app
self.wallet = self.app.wallet
self.tx = tx
self.update()
def update(self):
self.can_broadcast = False
if self.tx.is_complete():
2016-02-17 01:53:14 -08:00
self.tx_hash = self.tx.hash()
self.description = self.wallet.get_label(self.tx_hash)
if self.tx_hash in self.wallet.transactions.keys():
conf, timestamp = self.wallet.get_confirmations(self.tx_hash)
2016-02-13 02:00:21 -08:00
self.status_str = _("%d confirmations")%conf if conf else _('Pending')
2016-02-12 23:15:06 -08:00
if timestamp:
2016-02-16 01:56:58 -08:00
self.date_str = datetime.fromtimestamp(timestamp).isoformat(' ')[:-3]
2016-02-12 23:15:06 -08:00
else:
self.can_broadcast = self.app.network is not None
2016-02-13 02:16:45 -08:00
self.status_str = _('Signed')
2016-02-12 23:15:06 -08:00
else:
s, r = self.tx.signature_count()
2016-02-13 02:00:21 -08:00
self.status_str = _("Unsigned") if s == 0 else _('Partially signed') + ' (%d/%d)'%(s,r)
2016-02-12 23:15:06 -08:00
is_relevant, is_mine, v, fee = self.wallet.get_wallet_delta(self.tx)
2016-02-16 01:56:58 -08:00
self.is_mine = is_mine
2016-02-12 23:15:06 -08:00
if is_relevant:
if is_mine:
if fee is not None:
2016-02-16 01:56:58 -08:00
self.amount_str = self.app.format_amount_and_units(-v+fee)
self.fee_str = self.app.format_amount_and_units(-fee)
2016-02-12 23:15:06 -08:00
else:
2016-02-16 01:56:58 -08:00
self.amount_str = self.app.format_amount_and_units(-v)
self.fee_str = _("unknown")
2016-02-12 23:15:06 -08:00
else:
2016-02-16 01:56:58 -08:00
self.amount_str = self.app.format_amount_and_units(v)
2016-02-12 23:15:06 -08:00
self.fee_str = ''
else:
self.amount_str = _("Transaction unrelated to your wallet")
self.fee_str = ''
self.can_sign = self.wallet.can_sign(self.tx)
2016-02-17 02:40:05 -08:00
self.ids.outputs.clear_widgets()
2016-02-17 01:53:14 -08:00
for (type, address, amount) in self.tx.outputs():
2016-02-17 02:40:05 -08:00
self.ids.outputs.add_output(address, amount)
2016-02-17 01:53:14 -08:00
2016-02-12 23:15:06 -08:00
def do_sign(self):
2016-02-13 01:33:49 -08:00
self.app.protected(_("Enter your PIN code in order to sign this transaction"), self._do_sign, ())
2016-02-12 23:15:06 -08:00
def _do_sign(self, password):
2016-02-13 02:16:45 -08:00
self.status_str = _('Signing') + '...'
2016-02-13 01:33:49 -08:00
Clock.schedule_once(lambda dt: self.__do_sign(password), 0.1)
def __do_sign(self, password):
2016-02-12 23:15:06 -08:00
self.app.wallet.sign_transaction(self.tx, password)
self.update()
def do_broadcast(self):
self.app.show_info(_('Broadcasting'))
ok, txid = self.app.wallet.sendtx(self.tx)
self.app.show_info(txid)
def show_qr(self):
from electrum.bitcoin import base_encode
text = str(self.tx).decode('hex')
text = base_encode(text, base=43)
self.app.qr_dialog(_("Raw Transaction"), text)