-WIP-electrum-btcp/gui/qt/seed_dialog.py

170 lines
5.4 KiB
Python
Raw Normal View History

#!/usr/bin/env python
#
# Electrum - lightweight Bitcoin client
# Copyright (C) 2013 ecdsa@github
#
2016-02-23 02:36:42 -08:00
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
2016-02-23 02:36:42 -08:00
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
2016-02-23 02:36:42 -08:00
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from PyQt4.QtGui import *
from PyQt4.QtCore import *
2013-09-11 04:55:49 -07:00
from electrum.i18n import _
from util import *
from qrtextedit import ShowQRTextEdit, ScanQRTextEdit
2016-01-13 02:20:58 -08:00
class SeedLayoutBase(object):
2016-09-28 08:36:06 -07:00
2016-09-28 12:31:47 -07:00
def _seed_layout(self, seed=None, title=None, icon=True):
if seed:
2016-01-13 02:20:58 -08:00
self.seed_e = ShowQRTextEdit()
self.seed_e.setText(seed)
else:
2016-01-13 02:20:58 -08:00
self.seed_e = ScanQRTextEdit()
self.seed_e.setTabChangesFocus(True)
self.seed_e.setMaximumHeight(75)
hbox = QHBoxLayout()
2016-09-28 12:31:47 -07:00
if icon:
logo = QLabel()
logo.setPixmap(QPixmap(":icons/seed.png").scaledToWidth(64))
logo.setMaximumWidth(60)
hbox.addWidget(logo)
2016-01-13 02:20:58 -08:00
hbox.addWidget(self.seed_e)
if not title:
return hbox
vbox = QVBoxLayout()
vbox.addWidget(WWLabel(title))
vbox.addLayout(hbox)
return vbox
2014-04-19 11:23:27 -07:00
def layout(self):
2016-01-13 02:20:58 -08:00
return self.layout_
2014-04-19 11:23:27 -07:00
def seed_edit(self):
return self.seed_e
2014-04-19 11:23:27 -07:00
2016-01-13 02:20:58 -08:00
class SeedDisplayLayout(SeedLayoutBase):
2016-09-28 12:31:47 -07:00
def __init__(self, seed, title=None, icon=True):
self.layout_ = self._seed_layout(seed=seed, title=title, icon=icon)
2016-01-13 02:20:58 -08:00
def seed_warning_msg(seed):
return ''.join([
"<p>",
_("Please save these %d words on paper (order is important). "),
_("This seed will allow you to recover your wallet in case "
"of computer failure."),
"</p>",
2016-10-01 08:46:26 -07:00
"<b>" + _("WARNING") + ":</b>",
"<ul>",
"<li>" + _("Never disclose your seed.") + "</li>",
"<li>" + _("Never type it on a website.") + "</li>",
2016-10-01 08:46:26 -07:00
"<li>" + _("Do not store it electronically.") + "</li>",
"</ul>"
]) % len(seed.split())
class CreateSeedLayout(SeedLayoutBase):
def __init__(self, seed):
title = _("Your wallet generation seed is:")
2016-01-13 02:20:58 -08:00
vbox = QVBoxLayout()
vbox.addLayout(self._seed_layout(seed=seed, title=title))
msg = seed_warning_msg(seed)
2016-01-13 02:20:58 -08:00
vbox.addWidget(WWLabel(msg))
self.layout_ = vbox
class TextInputLayout(SeedLayoutBase):
def __init__(self, parent, title, is_valid):
self.is_valid = is_valid
self.parent = parent
2016-09-28 12:31:47 -07:00
self.layout_ = self._seed_layout(title=title, icon=False)
self.seed_e.textChanged.connect(self.on_edit)
def get_text(self):
return clean_text(self.seed_edit())
def on_edit(self):
self.parent.next_button.setEnabled(self.is_valid(self.get_text()))
class SeedInputLayout(SeedLayoutBase):
def __init__(self, parent, title, is_seed):
vbox = QVBoxLayout()
vbox.addLayout(self._seed_layout(title=title))
2016-09-30 00:46:47 -07:00
hbox = QHBoxLayout()
hbox.addStretch(1)
2016-09-30 00:50:25 -07:00
hbox.addWidget(QLabel(''))
self.seed_type_label = QLabel('')
2016-09-30 00:46:47 -07:00
hbox.addWidget(self.seed_type_label)
vbox.addLayout(hbox)
self.layout_ = vbox
self.parent = parent
self.is_seed = is_seed
self.seed_e.textChanged.connect(self.on_edit)
def get_seed(self):
return clean_text(self.seed_edit())
def on_edit(self):
from electrum.bitcoin import seed_type
s = self.get_seed()
b = self.is_seed(s)
t = seed_type(s)
label = _('Seed Type') + ': ' + t if t else ''
self.seed_type_label.setText(label)
self.parent.next_button.setEnabled(b)
class ShowSeedLayout(SeedLayoutBase):
def __init__(self, seed, passphrase):
title = _("Your wallet generation seed is:")
vbox = QVBoxLayout()
vbox.addLayout(self._seed_layout(seed=seed, title=title))
if passphrase:
hbox = QHBoxLayout()
passphrase_e = QLineEdit()
passphrase_e.setText(passphrase)
passphrase_e.setReadOnly(True)
2016-10-11 02:21:08 -07:00
hbox.addWidget(QLabel(_("Your seed extension is") + ':'))
hbox.addWidget(passphrase_e)
vbox.addLayout(hbox)
msg = seed_warning_msg(seed)
vbox.addWidget(WWLabel(msg))
self.layout_ = vbox
class SeedDialog(WindowModalDialog):
def __init__(self, parent, seed, passphrase):
WindowModalDialog.__init__(self, parent, ('Electrum - ' + _('Seed')))
self.setMinimumWidth(400)
vbox = QVBoxLayout(self)
vbox.addLayout(ShowSeedLayout(seed, passphrase).layout())
vbox.addLayout(Buttons(CloseButton(self)))