Merge pull request #6917 from andrewkozlik/slip39
SLIP-0039 wallet recovery
This commit is contained in:
@@ -989,7 +989,8 @@ class RestoreSeedDialog(WizardDialog):
|
||||
tis.focus = False
|
||||
|
||||
def get_params(self, b):
|
||||
return (self.get_text(), self.is_bip39, self.is_ext)
|
||||
seed_type = 'bip39' if self.is_bip39 else 'electrum'
|
||||
return (self.get_text(), seed_type, self.is_ext)
|
||||
|
||||
|
||||
class ConfirmSeedDialog(RestoreSeedDialog):
|
||||
|
||||
@@ -465,7 +465,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
|
||||
config=self.config,
|
||||
)
|
||||
self.exec_layout(slayout, title, next_enabled=False)
|
||||
return slayout.get_seed(), slayout.is_bip39, slayout.is_ext
|
||||
return slayout.get_seed(), slayout.seed_type, slayout.is_ext
|
||||
|
||||
@wizard_dialog
|
||||
def add_xpub_dialog(self, title, message, is_valid, run_next, allow_multi=False, show_wif_help=False):
|
||||
@@ -493,6 +493,8 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
|
||||
options.append('ext')
|
||||
if self.opt_bip39:
|
||||
options.append('bip39')
|
||||
if self.opt_slip39:
|
||||
options.append('slip39')
|
||||
title = _('Enter Seed')
|
||||
message = _('Please enter your seed phrase in order to restore your wallet.')
|
||||
return self.seed_input(title, message, test, options)
|
||||
@@ -506,7 +508,7 @@ class InstallWizard(QDialog, MessageBoxMixin, BaseWizard):
|
||||
_('If you lose your seed, your money will be permanently lost.'),
|
||||
_('To make sure that you have properly saved your seed, please retype it here.')
|
||||
])
|
||||
seed, is_bip39, is_ext = self.seed_input(title, message, test, None)
|
||||
seed, seed_type, is_ext = self.seed_input(title, message, test, None)
|
||||
return seed
|
||||
|
||||
@wizard_dialog
|
||||
|
||||
@@ -28,14 +28,17 @@ from typing import TYPE_CHECKING
|
||||
from PyQt5.QtCore import Qt
|
||||
from PyQt5.QtGui import QPixmap
|
||||
from PyQt5.QtWidgets import (QVBoxLayout, QCheckBox, QHBoxLayout, QLineEdit,
|
||||
QLabel, QCompleter, QDialog, QStyledItemDelegate)
|
||||
QLabel, QCompleter, QDialog, QStyledItemDelegate,
|
||||
QScrollArea, QWidget, QPushButton)
|
||||
|
||||
from electrum.i18n import _
|
||||
from electrum.mnemonic import Mnemonic, seed_type
|
||||
from electrum import old_mnemonic
|
||||
from electrum import slip39
|
||||
|
||||
from .util import (Buttons, OkButton, WWLabel, ButtonsTextEdit, icon_path,
|
||||
EnterButton, CloseButton, WindowModalDialog, ColorScheme)
|
||||
EnterButton, CloseButton, WindowModalDialog, ColorScheme,
|
||||
ChoicesLayout)
|
||||
from .qrtextedit import ShowQRTextEdit, ScanQRTextEdit
|
||||
from .completion_text_edit import CompletionTextEdit
|
||||
|
||||
@@ -64,16 +67,29 @@ class SeedLayout(QVBoxLayout):
|
||||
def seed_options(self):
|
||||
dialog = QDialog()
|
||||
vbox = QVBoxLayout(dialog)
|
||||
|
||||
seed_types = [
|
||||
(value, title) for value, title in (
|
||||
('electrum', _('Electrum')),
|
||||
('bip39', _('BIP39 seed')),
|
||||
('slip39', _('SLIP39 seed')),
|
||||
)
|
||||
if value in self.options or value == 'electrum'
|
||||
]
|
||||
seed_type_values = [t[0] for t in seed_types]
|
||||
|
||||
if 'ext' in self.options:
|
||||
cb_ext = QCheckBox(_('Extend this seed with custom words'))
|
||||
cb_ext.setChecked(self.is_ext)
|
||||
vbox.addWidget(cb_ext)
|
||||
if 'bip39' in self.options:
|
||||
def f(b):
|
||||
self.is_seed = (lambda x: bool(x)) if b else self.saved_is_seed
|
||||
self.is_bip39 = b
|
||||
if len(seed_types) >= 2:
|
||||
def f(choices_layout):
|
||||
self.seed_type = seed_type_values[choices_layout.selected_index()]
|
||||
self.is_seed = (lambda x: bool(x)) if self.seed_type != 'electrum' else self.saved_is_seed
|
||||
self.slip39_current_mnemonic_invalid = None
|
||||
self.seed_status.setText('')
|
||||
self.on_edit()
|
||||
if b:
|
||||
if self.seed_type == 'bip39':
|
||||
msg = ' '.join([
|
||||
'<b>' + _('Warning') + ':</b> ',
|
||||
_('BIP39 seeds can be imported in Electrum, so that users can access funds locked in other wallets.'),
|
||||
@@ -81,18 +97,28 @@ class SeedLayout(QVBoxLayout):
|
||||
_('BIP39 seeds do not include a version number, which compromises compatibility with future software.'),
|
||||
_('We do not guarantee that BIP39 imports will always be supported in Electrum.'),
|
||||
])
|
||||
elif self.seed_type == 'slip39':
|
||||
msg = ' '.join([
|
||||
'<b>' + _('Warning') + ':</b> ',
|
||||
_('SLIP39 seeds can be imported in Electrum, so that users can access funds locked in other wallets.'),
|
||||
_('However, we do not generate SLIP39 seeds.'),
|
||||
])
|
||||
else:
|
||||
msg = ''
|
||||
self.update_share_buttons()
|
||||
self.initialize_completer()
|
||||
self.seed_warning.setText(msg)
|
||||
cb_bip39 = QCheckBox(_('BIP39 seed'))
|
||||
cb_bip39.toggled.connect(f)
|
||||
cb_bip39.setChecked(self.is_bip39)
|
||||
vbox.addWidget(cb_bip39)
|
||||
|
||||
checked_index = seed_type_values.index(self.seed_type)
|
||||
titles = [t[1] for t in seed_types]
|
||||
clayout = ChoicesLayout(_('Seed type'), titles, on_clicked=f, checked_index=checked_index)
|
||||
vbox.addLayout(clayout.layout())
|
||||
|
||||
vbox.addLayout(Buttons(OkButton(dialog)))
|
||||
if not dialog.exec_():
|
||||
return None
|
||||
self.is_ext = cb_ext.isChecked() if 'ext' in self.options else False
|
||||
self.is_bip39 = cb_bip39.isChecked() if 'bip39' in self.options else False
|
||||
self.seed_type = seed_type_values[clayout.selected_index()] if len(seed_types) >= 2 else 'electrum'
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -112,6 +138,7 @@ class SeedLayout(QVBoxLayout):
|
||||
self.parent = parent
|
||||
self.options = options
|
||||
self.config = config
|
||||
self.seed_type = 'electrum'
|
||||
if title:
|
||||
self.addWidget(WWLabel(title))
|
||||
if seed: # "read only", we already have the text
|
||||
@@ -146,7 +173,6 @@ class SeedLayout(QVBoxLayout):
|
||||
hbox.addWidget(self.seed_type_label)
|
||||
|
||||
# options
|
||||
self.is_bip39 = False
|
||||
self.is_ext = False
|
||||
if options:
|
||||
opt_button = EnterButton(_('Options'), self.seed_options)
|
||||
@@ -160,60 +186,150 @@ class SeedLayout(QVBoxLayout):
|
||||
hbox.addWidget(QLabel(_("Your seed extension is") + ':'))
|
||||
hbox.addWidget(passphrase_e)
|
||||
self.addLayout(hbox)
|
||||
|
||||
# slip39 shares
|
||||
self.slip39_mnemonic_index = 0
|
||||
self.slip39_mnemonics = [""]
|
||||
self.slip39_seed = None
|
||||
self.slip39_current_mnemonic_invalid = None
|
||||
hbox = QHBoxLayout()
|
||||
hbox.addStretch(1)
|
||||
self.prev_share_btn = QPushButton(_("Previous share"))
|
||||
self.prev_share_btn.clicked.connect(self.on_prev_share)
|
||||
hbox.addWidget(self.prev_share_btn)
|
||||
self.next_share_btn = QPushButton(_("Next share"))
|
||||
self.next_share_btn.clicked.connect(self.on_next_share)
|
||||
hbox.addWidget(self.next_share_btn)
|
||||
self.update_share_buttons()
|
||||
self.addLayout(hbox)
|
||||
|
||||
self.addStretch(1)
|
||||
self.seed_status = WWLabel('')
|
||||
self.addWidget(self.seed_status)
|
||||
self.seed_warning = WWLabel('')
|
||||
if msg:
|
||||
self.seed_warning.setText(seed_warning_msg(seed))
|
||||
self.addWidget(self.seed_warning)
|
||||
|
||||
def initialize_completer(self):
|
||||
bip39_english_list = Mnemonic('en').wordlist
|
||||
old_list = old_mnemonic.wordlist
|
||||
only_old_list = set(old_list) - set(bip39_english_list)
|
||||
self.wordlist = list(bip39_english_list) + list(only_old_list) # concat both lists
|
||||
self.wordlist.sort()
|
||||
if self.seed_type != 'slip39':
|
||||
bip39_english_list = Mnemonic('en').wordlist
|
||||
old_list = old_mnemonic.wordlist
|
||||
only_old_list = set(old_list) - set(bip39_english_list)
|
||||
self.wordlist = list(bip39_english_list) + list(only_old_list) # concat both lists
|
||||
self.wordlist.sort()
|
||||
|
||||
class CompleterDelegate(QStyledItemDelegate):
|
||||
def initStyleOption(self, option, index):
|
||||
super().initStyleOption(option, index)
|
||||
# Some people complained that due to merging the two word lists,
|
||||
# it is difficult to restore from a metal backup, as they planned
|
||||
# to rely on the "4 letter prefixes are unique in bip39 word list" property.
|
||||
# So we color words that are only in old list.
|
||||
if option.text in only_old_list:
|
||||
# yellow bg looks ~ok on both light/dark theme, regardless if (un)selected
|
||||
option.backgroundBrush = ColorScheme.YELLOW.as_color(background=True)
|
||||
class CompleterDelegate(QStyledItemDelegate):
|
||||
def initStyleOption(self, option, index):
|
||||
super().initStyleOption(option, index)
|
||||
# Some people complained that due to merging the two word lists,
|
||||
# it is difficult to restore from a metal backup, as they planned
|
||||
# to rely on the "4 letter prefixes are unique in bip39 word list" property.
|
||||
# So we color words that are only in old list.
|
||||
if option.text in only_old_list:
|
||||
# yellow bg looks ~ok on both light/dark theme, regardless if (un)selected
|
||||
option.backgroundBrush = ColorScheme.YELLOW.as_color(background=True)
|
||||
|
||||
delegate = CompleterDelegate(self.seed_e)
|
||||
else:
|
||||
self.wordlist = list(slip39.get_wordlist())
|
||||
delegate = None
|
||||
|
||||
self.completer = QCompleter(self.wordlist)
|
||||
delegate = CompleterDelegate(self.seed_e)
|
||||
self.completer.popup().setItemDelegate(delegate)
|
||||
if delegate:
|
||||
self.completer.popup().setItemDelegate(delegate)
|
||||
self.seed_e.set_completer(self.completer)
|
||||
|
||||
def get_seed_words(self):
|
||||
return self.seed_e.text().split()
|
||||
|
||||
def get_seed(self):
|
||||
text = self.seed_e.text()
|
||||
return ' '.join(text.split())
|
||||
if self.seed_type != 'slip39':
|
||||
return ' '.join(self.get_seed_words())
|
||||
else:
|
||||
return self.slip39_seed
|
||||
|
||||
def on_edit(self):
|
||||
s = self.get_seed()
|
||||
s = ' '.join(self.get_seed_words())
|
||||
b = self.is_seed(s)
|
||||
if not self.is_bip39:
|
||||
t = seed_type(s)
|
||||
label = _('Seed Type') + ': ' + t if t else ''
|
||||
else:
|
||||
if self.seed_type == 'bip39':
|
||||
from electrum.keystore import bip39_is_checksum_valid
|
||||
is_checksum, is_wordlist = bip39_is_checksum_valid(s)
|
||||
status = ('checksum: ' + ('ok' if is_checksum else 'failed')) if is_wordlist else 'unknown wordlist'
|
||||
label = 'BIP39' + ' (%s)'%status
|
||||
elif self.seed_type == 'slip39':
|
||||
self.slip39_mnemonics[self.slip39_mnemonic_index] = s
|
||||
try:
|
||||
slip39.decode_mnemonic(s)
|
||||
except slip39.Slip39Error as e:
|
||||
share_status = str(e)
|
||||
current_mnemonic_invalid = True
|
||||
else:
|
||||
share_status = _('Valid.')
|
||||
current_mnemonic_invalid = False
|
||||
|
||||
label = _('SLIP39 share') + ' #%d: %s' % (self.slip39_mnemonic_index + 1, share_status)
|
||||
|
||||
# No need to process mnemonics if the current mnemonic remains invalid after editing.
|
||||
if not (self.slip39_current_mnemonic_invalid and current_mnemonic_invalid):
|
||||
self.slip39_seed, seed_status = slip39.process_mnemonics(self.slip39_mnemonics)
|
||||
self.seed_status.setText(seed_status)
|
||||
self.slip39_current_mnemonic_invalid = current_mnemonic_invalid
|
||||
|
||||
b = self.slip39_seed is not None
|
||||
self.update_share_buttons()
|
||||
else:
|
||||
t = seed_type(s)
|
||||
label = _('Seed Type') + ': ' + t if t else ''
|
||||
|
||||
self.seed_type_label.setText(label)
|
||||
self.parent.next_button.setEnabled(b)
|
||||
|
||||
# disable suggestions if user already typed an unknown word
|
||||
for word in self.get_seed().split(" ")[:-1]:
|
||||
for word in self.get_seed_words()[:-1]:
|
||||
if word not in self.wordlist:
|
||||
self.seed_e.disable_suggestions()
|
||||
return
|
||||
self.seed_e.enable_suggestions()
|
||||
|
||||
def update_share_buttons(self):
|
||||
if self.seed_type != 'slip39':
|
||||
self.prev_share_btn.hide()
|
||||
self.next_share_btn.hide()
|
||||
return
|
||||
|
||||
finished = self.slip39_seed is not None
|
||||
self.prev_share_btn.show()
|
||||
self.next_share_btn.show()
|
||||
self.prev_share_btn.setEnabled(self.slip39_mnemonic_index != 0)
|
||||
self.next_share_btn.setEnabled(
|
||||
# already pressed "prev" and undoing that:
|
||||
self.slip39_mnemonic_index < len(self.slip39_mnemonics) - 1
|
||||
# finished entering latest share and starting new one:
|
||||
or (bool(self.seed_e.text().strip()) and not self.slip39_current_mnemonic_invalid and not finished)
|
||||
)
|
||||
|
||||
def on_prev_share(self):
|
||||
if not self.slip39_mnemonics[self.slip39_mnemonic_index]:
|
||||
del self.slip39_mnemonics[self.slip39_mnemonic_index]
|
||||
|
||||
self.slip39_mnemonic_index -= 1
|
||||
self.seed_e.setText(self.slip39_mnemonics[self.slip39_mnemonic_index])
|
||||
self.slip39_current_mnemonic_invalid = None
|
||||
|
||||
def on_next_share(self):
|
||||
if not self.slip39_mnemonics[self.slip39_mnemonic_index]:
|
||||
del self.slip39_mnemonics[self.slip39_mnemonic_index]
|
||||
else:
|
||||
self.slip39_mnemonic_index += 1
|
||||
|
||||
if len(self.slip39_mnemonics) <= self.slip39_mnemonic_index:
|
||||
self.slip39_mnemonics.append("")
|
||||
self.seed_e.setFocus()
|
||||
self.seed_e.setText(self.slip39_mnemonics[self.slip39_mnemonic_index])
|
||||
self.slip39_current_mnemonic_invalid = None
|
||||
|
||||
|
||||
class KeysLayout(QVBoxLayout):
|
||||
def __init__(
|
||||
self,
|
||||
|
||||
Reference in New Issue
Block a user