1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-05 14:59:44 +00:00

core/recovery: rework arguments

This commit is contained in:
Tomas Susanka 2019-12-27 18:46:22 +00:00
parent faa9078c2b
commit f4e11a9176
2 changed files with 20 additions and 25 deletions

View File

@ -16,10 +16,7 @@ NOK_THRESHOLD_REACHED = const(3)
def check( def check(
current_index: int, backup_type: Optional[EnumTypeBackupType], partial_mnemonic: List[str]
current_word: str,
backup_type: Optional[EnumTypeBackupType],
previous_words: List[str],
) -> int: ) -> int:
# we can't perform any checks if the backup type was not yet decided # we can't perform any checks if the backup type was not yet decided
if backup_type is None: if backup_type is None:
@ -34,22 +31,22 @@ def check(
raise RuntimeError raise RuntimeError
if backup_type == BackupType.Slip39_Basic: if backup_type == BackupType.Slip39_Basic:
return check_slip39_basic(current_index, current_word, previous_mnemonics) return check_slip39_basic(partial_mnemonic, previous_mnemonics)
if backup_type == BackupType.Slip39_Advanced: if backup_type == BackupType.Slip39_Advanced:
return check_slip39_advanced( return check_slip39_advanced(partial_mnemonic, previous_mnemonics)
current_index, current_word, previous_words, previous_mnemonics
)
# there are no other backup types # there are no other backup types
raise RuntimeError raise RuntimeError
def check_slip39_basic( def check_slip39_basic(
current_index: int, current_word: str, previous_mnemonics: List[List[str]] partial_mnemonic: List[str], previous_mnemonics: List[List[str]]
) -> int: ) -> int:
# check if first 3 words of mnemonic match # check if first 3 words of mnemonic match
# we can check against the first one, others were checked already # we can check against the first one, others were checked already
current_index = len(partial_mnemonic) - 1
current_word = partial_mnemonic[-1]
if current_index < 3: if current_index < 3:
share_list = previous_mnemonics[0][0].split(" ") share_list = previous_mnemonics[0][0].split(" ")
if share_list[current_index] != current_word: if share_list[current_index] != current_word:
@ -65,11 +62,10 @@ def check_slip39_basic(
def check_slip39_advanced( def check_slip39_advanced(
current_index: int, partial_mnemonic: List[str], previous_mnemonics: List[List[str]]
current_word: str,
previous_words: List[str],
previous_mnemonics: List[List[str]],
) -> int: ) -> int:
current_index = len(partial_mnemonic) - 1
current_word = partial_mnemonic[-1]
if current_index < 2: if current_index < 2:
share_list = next(s for s in previous_mnemonics if s)[0].split(" ") share_list = next(s for s in previous_mnemonics if s)[0].split(" ")
if share_list[current_index] != current_word: if share_list[current_index] != current_word:
@ -87,7 +83,7 @@ def check_slip39_advanced(
# check if share was already added for group # check if share was already added for group
elif current_index == 3: elif current_index == 3:
# we use the 3rd word from previously entered shares to find the group id # we use the 3rd word from previously entered shares to find the group id
group_identifier_word = previous_words[2] group_identifier_word = partial_mnemonic[2]
group_index = None group_index = None
for i, group in enumerate(previous_mnemonics): for i, group in enumerate(previous_mnemonics):
if len(group) > 0: if len(group) > 0:

View File

@ -4,6 +4,8 @@ from mock_storage import mock_storage
import storage import storage
import storage.recovery import storage.recovery
from apps.management.recovery_device.recover import process_slip39 from apps.management.recovery_device.recover import process_slip39
from trezor.messages import BackupType
from apps.management.recovery_device.word_validity import check, OK, NOK_IDENTIFIER_MISMATCH, NOK_ALREADY_ADDED, NOK_THRESHOLD_REACHED
MNEMONIC_SLIP39_BASIC_20_3of6 = [ MNEMONIC_SLIP39_BASIC_20_3of6 = [
"extra extend academic bishop cricket bundle tofu goat apart victim enlarge program behavior permit course armed jerky faint language modern", "extra extend academic bishop cricket bundle tofu goat apart victim enlarge program behavior permit course armed jerky faint language modern",
@ -142,21 +144,18 @@ class TestSlip39(unittest.TestCase):
@mock_storage @mock_storage
def test_check_word_validity(self): def test_check_word_validity(self):
from trezor.messages import BackupType
from apps.management.recovery_device.word_validity import check, OK, NOK_IDENTIFIER_MISMATCH, NOK_ALREADY_ADDED, NOK_THRESHOLD_REACHED
storage.recovery.set_in_progress(True) storage.recovery.set_in_progress(True)
# nothing is stored -> should raise # We claim to know the backup type, but nothing is stored. That is an invalid state.
with self.assertRaises(RuntimeError): with self.assertRaises(RuntimeError):
check(0, "ocean", BackupType.Slip39_Advanced, []) check(BackupType.Slip39_Advanced, ["ocean"])
# if backup type is not set we can not do any checks # if backup type is not set we can not do any checks
result = check(0, "ocean", None, []) result = check(None, ["ocean"])
self.assertIs(result, OK) self.assertIs(result, OK)
# BIP-39 has no "on-the-fly" checks # BIP-39 has no "on-the-fly" checks
result = check(0, "ocean", BackupType.Bip39, []) result = check(BackupType.Bip39, ["ocean"])
self.assertIs(result, OK) self.assertIs(result, OK)
# let's store two shares in the storage # let's store two shares in the storage
@ -166,15 +165,15 @@ class TestSlip39(unittest.TestCase):
self.assertIsNone(secret) self.assertIsNone(secret)
# different identifier # different identifier
result = check(0, "slush", BackupType.Slip39_Advanced, []) result = check(BackupType.Slip39_Advanced, ["slush"])
self.assertIs(result, NOK_IDENTIFIER_MISMATCH) self.assertIs(result, NOK_IDENTIFIER_MISMATCH)
# same first word but still a different identifier # same first word but still a different identifier
result = check(1, "slush", BackupType.Slip39_Advanced, ["trash"]) result = check(BackupType.Slip39_Advanced, ["trash", "slush"])
self.assertIs(result, NOK_IDENTIFIER_MISMATCH) self.assertIs(result, NOK_IDENTIFIER_MISMATCH)
# same mnemonic found out using the index # same mnemonic found out using the index
result = check(3, "ambition", BackupType.Slip39_Advanced, ["trash", "smug", "adjust"]) result = check(BackupType.Slip39_Advanced, ["trash", "smug", "adjust", "ambition"])
self.assertIs(result, NOK_ALREADY_ADDED) self.assertIs(result, NOK_ALREADY_ADDED)
# Let's store two more. The group is 4/6 so this group is now complete. # Let's store two more. The group is 4/6 so this group is now complete.
@ -184,7 +183,7 @@ class TestSlip39(unittest.TestCase):
self.assertIsNone(secret) self.assertIsNone(secret)
# If trying to add another one from this group we get a warning. # If trying to add another one from this group we get a warning.
result = check(2, "adjust", BackupType.Slip39_Advanced, ["trash", "smug"]) result = check(BackupType.Slip39_Advanced, ["trash", "smug", "adjust"])
self.assertIs(result, NOK_THRESHOLD_REACHED) self.assertIs(result, NOK_THRESHOLD_REACHED)