tseed_dialog.py - electrum - Electrum Bitcoin wallet
HTML git clone https://git.parazyd.org/electrum
DIR Log
DIR Files
DIR Refs
DIR Submodules
---
tseed_dialog.py (10389B)
---
1 #!/usr/bin/env python
2 #
3 # Electrum - lightweight Bitcoin client
4 # Copyright (C) 2013 ecdsa@github
5 #
6 # Permission is hereby granted, free of charge, to any person
7 # obtaining a copy of this software and associated documentation files
8 # (the "Software"), to deal in the Software without restriction,
9 # including without limitation the rights to use, copy, modify, merge,
10 # publish, distribute, sublicense, and/or sell copies of the Software,
11 # and to permit persons to whom the Software is furnished to do so,
12 # subject to the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 # SOFTWARE.
25
26 from typing import TYPE_CHECKING
27
28 from PyQt5.QtCore import Qt
29 from PyQt5.QtGui import QPixmap
30 from PyQt5.QtWidgets import (QVBoxLayout, QCheckBox, QHBoxLayout, QLineEdit,
31 QLabel, QCompleter, QDialog, QStyledItemDelegate)
32
33 from electrum.i18n import _
34 from electrum.mnemonic import Mnemonic, seed_type
35 from electrum import old_mnemonic
36
37 from .util import (Buttons, OkButton, WWLabel, ButtonsTextEdit, icon_path,
38 EnterButton, CloseButton, WindowModalDialog, ColorScheme)
39 from .qrtextedit import ShowQRTextEdit, ScanQRTextEdit
40 from .completion_text_edit import CompletionTextEdit
41
42 if TYPE_CHECKING:
43 from electrum.simple_config import SimpleConfig
44
45
46 def seed_warning_msg(seed):
47 return ''.join([
48 "<p>",
49 _("Please save these {0} words on paper (order is important). "),
50 _("This seed will allow you to recover your wallet in case "
51 "of computer failure."),
52 "</p>",
53 "<b>" + _("WARNING") + ":</b>",
54 "<ul>",
55 "<li>" + _("Never disclose your seed.") + "</li>",
56 "<li>" + _("Never type it on a website.") + "</li>",
57 "<li>" + _("Do not store it electronically.") + "</li>",
58 "</ul>"
59 ]).format(len(seed.split()))
60
61
62 class SeedLayout(QVBoxLayout):
63
64 def seed_options(self):
65 dialog = QDialog()
66 vbox = QVBoxLayout(dialog)
67 if 'ext' in self.options:
68 cb_ext = QCheckBox(_('Extend this seed with custom words'))
69 cb_ext.setChecked(self.is_ext)
70 vbox.addWidget(cb_ext)
71 if 'bip39' in self.options:
72 def f(b):
73 self.is_seed = (lambda x: bool(x)) if b else self.saved_is_seed
74 self.is_bip39 = b
75 self.on_edit()
76 if b:
77 msg = ' '.join([
78 '<b>' + _('Warning') + ':</b> ',
79 _('BIP39 seeds can be imported in Electrum, so that users can access funds locked in other wallets.'),
80 _('However, we do not generate BIP39 seeds, because they do not meet our safety standard.'),
81 _('BIP39 seeds do not include a version number, which compromises compatibility with future software.'),
82 _('We do not guarantee that BIP39 imports will always be supported in Electrum.'),
83 ])
84 else:
85 msg = ''
86 self.seed_warning.setText(msg)
87 cb_bip39 = QCheckBox(_('BIP39 seed'))
88 cb_bip39.toggled.connect(f)
89 cb_bip39.setChecked(self.is_bip39)
90 vbox.addWidget(cb_bip39)
91 vbox.addLayout(Buttons(OkButton(dialog)))
92 if not dialog.exec_():
93 return None
94 self.is_ext = cb_ext.isChecked() if 'ext' in self.options else False
95 self.is_bip39 = cb_bip39.isChecked() if 'bip39' in self.options else False
96
97 def __init__(
98 self,
99 seed=None,
100 title=None,
101 icon=True,
102 msg=None,
103 options=None,
104 is_seed=None,
105 passphrase=None,
106 parent=None,
107 for_seed_words=True,
108 *,
109 config: 'SimpleConfig',
110 ):
111 QVBoxLayout.__init__(self)
112 self.parent = parent
113 self.options = options
114 self.config = config
115 if title:
116 self.addWidget(WWLabel(title))
117 if seed: # "read only", we already have the text
118 if for_seed_words:
119 self.seed_e = ButtonsTextEdit()
120 else: # e.g. xpub
121 self.seed_e = ShowQRTextEdit(config=self.config)
122 self.seed_e.setReadOnly(True)
123 self.seed_e.setText(seed)
124 else: # we expect user to enter text
125 assert for_seed_words
126 self.seed_e = CompletionTextEdit()
127 self.seed_e.setTabChangesFocus(False) # so that tab auto-completes
128 self.is_seed = is_seed
129 self.saved_is_seed = self.is_seed
130 self.seed_e.textChanged.connect(self.on_edit)
131 self.initialize_completer()
132
133 self.seed_e.setMaximumHeight(75)
134 hbox = QHBoxLayout()
135 if icon:
136 logo = QLabel()
137 logo.setPixmap(QPixmap(icon_path("seed.png"))
138 .scaledToWidth(64, mode=Qt.SmoothTransformation))
139 logo.setMaximumWidth(60)
140 hbox.addWidget(logo)
141 hbox.addWidget(self.seed_e)
142 self.addLayout(hbox)
143 hbox = QHBoxLayout()
144 hbox.addStretch(1)
145 self.seed_type_label = QLabel('')
146 hbox.addWidget(self.seed_type_label)
147
148 # options
149 self.is_bip39 = False
150 self.is_ext = False
151 if options:
152 opt_button = EnterButton(_('Options'), self.seed_options)
153 hbox.addWidget(opt_button)
154 self.addLayout(hbox)
155 if passphrase:
156 hbox = QHBoxLayout()
157 passphrase_e = QLineEdit()
158 passphrase_e.setText(passphrase)
159 passphrase_e.setReadOnly(True)
160 hbox.addWidget(QLabel(_("Your seed extension is") + ':'))
161 hbox.addWidget(passphrase_e)
162 self.addLayout(hbox)
163 self.addStretch(1)
164 self.seed_warning = WWLabel('')
165 if msg:
166 self.seed_warning.setText(seed_warning_msg(seed))
167 self.addWidget(self.seed_warning)
168
169 def initialize_completer(self):
170 bip39_english_list = Mnemonic('en').wordlist
171 old_list = old_mnemonic.wordlist
172 only_old_list = set(old_list) - set(bip39_english_list)
173 self.wordlist = list(bip39_english_list) + list(only_old_list) # concat both lists
174 self.wordlist.sort()
175
176 class CompleterDelegate(QStyledItemDelegate):
177 def initStyleOption(self, option, index):
178 super().initStyleOption(option, index)
179 # Some people complained that due to merging the two word lists,
180 # it is difficult to restore from a metal backup, as they planned
181 # to rely on the "4 letter prefixes are unique in bip39 word list" property.
182 # So we color words that are only in old list.
183 if option.text in only_old_list:
184 # yellow bg looks ~ok on both light/dark theme, regardless if (un)selected
185 option.backgroundBrush = ColorScheme.YELLOW.as_color(background=True)
186
187 self.completer = QCompleter(self.wordlist)
188 delegate = CompleterDelegate(self.seed_e)
189 self.completer.popup().setItemDelegate(delegate)
190 self.seed_e.set_completer(self.completer)
191
192 def get_seed(self):
193 text = self.seed_e.text()
194 return ' '.join(text.split())
195
196 def on_edit(self):
197 s = self.get_seed()
198 b = self.is_seed(s)
199 if not self.is_bip39:
200 t = seed_type(s)
201 label = _('Seed Type') + ': ' + t if t else ''
202 else:
203 from electrum.keystore import bip39_is_checksum_valid
204 is_checksum, is_wordlist = bip39_is_checksum_valid(s)
205 status = ('checksum: ' + ('ok' if is_checksum else 'failed')) if is_wordlist else 'unknown wordlist'
206 label = 'BIP39' + ' (%s)'%status
207 self.seed_type_label.setText(label)
208 self.parent.next_button.setEnabled(b)
209
210 # disable suggestions if user already typed an unknown word
211 for word in self.get_seed().split(" ")[:-1]:
212 if word not in self.wordlist:
213 self.seed_e.disable_suggestions()
214 return
215 self.seed_e.enable_suggestions()
216
217 class KeysLayout(QVBoxLayout):
218 def __init__(
219 self,
220 parent=None,
221 header_layout=None,
222 is_valid=None,
223 allow_multi=False,
224 *,
225 config: 'SimpleConfig',
226 ):
227 QVBoxLayout.__init__(self)
228 self.parent = parent
229 self.is_valid = is_valid
230 self.text_e = ScanQRTextEdit(allow_multi=allow_multi, config=config)
231 self.text_e.textChanged.connect(self.on_edit)
232 if isinstance(header_layout, str):
233 self.addWidget(WWLabel(header_layout))
234 else:
235 self.addLayout(header_layout)
236 self.addWidget(self.text_e)
237
238 def get_text(self):
239 return self.text_e.text()
240
241 def on_edit(self):
242 valid = False
243 try:
244 valid = self.is_valid(self.get_text())
245 except Exception as e:
246 self.parent.next_button.setToolTip(f'{_("Error")}: {str(e)}')
247 else:
248 self.parent.next_button.setToolTip('')
249 self.parent.next_button.setEnabled(valid)
250
251
252 class SeedDialog(WindowModalDialog):
253
254 def __init__(self, parent, seed, passphrase, *, config: 'SimpleConfig'):
255 WindowModalDialog.__init__(self, parent, ('Electrum - ' + _('Seed')))
256 self.setMinimumWidth(400)
257 vbox = QVBoxLayout(self)
258 title = _("Your wallet generation seed is:")
259 slayout = SeedLayout(
260 title=title,
261 seed=seed,
262 msg=True,
263 passphrase=passphrase,
264 config=config,
265 )
266 vbox.addLayout(slayout)
267 vbox.addLayout(Buttons(CloseButton(self)))