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

143 lines
4.1 KiB
Python
Raw Normal View History

2015-12-20 08:37:07 -08:00
from kivy.app import App
from kivy.factory import Factory
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from decimal import Decimal
2016-02-13 01:33:49 -08:00
from kivy.clock import Clock
2015-12-20 08:37:07 -08:00
2019-12-24 03:21:35 -08:00
from electrum_bitcoinprivate.util import InvalidPassword
from electrum_bitcoinprivate_gui.kivy.i18n import _
2015-12-20 08:37:07 -08:00
Builder.load_string('''
<PasswordDialog@Popup>
id: popup
2019-12-24 03:21:35 -08:00
title: 'Electrum-bitcoinprivate'
2016-02-13 01:00:20 -08:00
message: ''
2015-10-13 10:09:12 -07:00
BoxLayout:
size_hint: 1, 1
2015-10-13 10:09:12 -07:00
orientation: 'vertical'
2016-02-13 01:00:20 -08:00
Widget:
size_hint: 1, 0.05
2016-02-13 01:00:20 -08:00
Label:
font_size: '20dp'
2016-02-13 01:00:20 -08:00
text: root.message
text_size: self.width, None
size: self.texture_size
Widget:
size_hint: 1, 0.05
Label:
id: a
font_size: '50dp'
text: '*'*len(kb.password) + '-'*(6-len(kb.password))
size: self.texture_size
2016-02-13 01:00:20 -08:00
Widget:
size_hint: 1, 0.05
GridLayout:
id: kb
size_hint: 1, None
height: self.minimum_height
2015-12-20 08:37:07 -08:00
update_amount: popup.update_password
password: ''
2015-12-20 08:37:07 -08:00
on_password: popup.on_password(self.password)
spacing: '2dp'
cols: 3
KButton:
text: '1'
KButton:
text: '2'
KButton:
text: '3'
KButton:
text: '4'
KButton:
text: '5'
KButton:
text: '6'
KButton:
text: '7'
KButton:
text: '8'
KButton:
text: '9'
KButton:
text: 'Clear'
KButton:
text: '0'
KButton:
text: '<'
2015-12-20 08:37:07 -08:00
''')
class PasswordDialog(Factory.Popup):
def init(self, app, wallet, message, on_success, on_failure, is_change=0):
self.app = app
self.wallet = wallet
2016-02-13 01:00:20 -08:00
self.message = message
self.on_success = on_success
self.on_failure = on_failure
2016-03-06 02:46:09 -08:00
self.ids.kb.password = ''
self.success = False
self.is_change = is_change
self.pw = None
self.new_password = None
2019-12-24 03:21:35 -08:00
self.title = 'Electrum-bitcoinprivate' + (' - ' + self.wallet.basename() if self.wallet else '')
def check_password(self, password):
if self.is_change > 1:
return True
try:
self.wallet.check_password(password)
return True
except InvalidPassword as e:
return False
def on_dismiss(self):
if not self.success:
if self.on_failure:
self.on_failure()
else:
# keep dialog open
return True
else:
if self.on_success:
args = (self.pw, self.new_password) if self.is_change else (self.pw,)
Clock.schedule_once(lambda dt: self.on_success(*args), 0.1)
2015-12-20 08:37:07 -08:00
def update_password(self, c):
kb = self.ids.kb
text = kb.password
if c == '<':
text = text[:-1]
elif c == 'Clear':
text = ''
else:
text += c
kb.password = text
def on_password(self, pw):
if len(pw) == 6:
if self.check_password(pw):
if self.is_change == 0:
self.success = True
self.pw = pw
self.message = _('Please wait...')
self.dismiss()
elif self.is_change == 1:
self.pw = pw
self.message = _('Enter new PIN')
self.ids.kb.password = ''
self.is_change = 2
elif self.is_change == 2:
self.new_password = pw
self.message = _('Confirm new PIN')
self.ids.kb.password = ''
self.is_change = 3
elif self.is_change == 3:
self.success = pw == self.new_password
self.dismiss()
else:
self.app.show_error(_('Wrong PIN'))
self.ids.kb.password = ''