|
1 | | -# BIP-85 child mnemonic derivation tests. |
2 | | -# |
3 | | -# Tests GetBip85Mnemonic message which derives deterministic child |
4 | | -# mnemonics from the device seed per the BIP-85 specification. |
5 | | -# |
6 | | -# Uses the "all" x12 mnemonic as the master seed. |
| 1 | +"""BIP-85 display-only tests. |
| 2 | +
|
| 3 | +Firmware >= 7.14.0 derives the BIP-85 child mnemonic, displays it on the |
| 4 | +device screen, and responds with Success (mnemonic is never sent over USB). |
| 5 | +""" |
7 | 6 |
|
8 | 7 | import unittest |
9 | 8 | import common |
10 | | - |
11 | 9 | import keepkeylib.messages_pb2 as proto |
12 | | - |
13 | | -# BIP-39 English wordlist (2048 words) |
14 | | -# We load it inline to avoid external file dependencies. |
15 | | -BIP39_WORDLIST = None |
16 | | - |
17 | | -def _load_bip39_wordlist(): |
18 | | - """Load BIP-39 English wordlist from mnemonic package or fallback.""" |
19 | | - global BIP39_WORDLIST |
20 | | - if BIP39_WORDLIST is not None: |
21 | | - return BIP39_WORDLIST |
22 | | - |
23 | | - # Try the mnemonic package first (ships with python-keepkey deps) |
24 | | - try: |
25 | | - from mnemonic import Mnemonic |
26 | | - m = Mnemonic("english") |
27 | | - BIP39_WORDLIST = m.wordlist |
28 | | - return BIP39_WORDLIST |
29 | | - except ImportError: |
30 | | - pass |
31 | | - |
32 | | - # Fallback: accept any lowercase alpha words and skip strict validation |
33 | | - BIP39_WORDLIST = None |
34 | | - return None |
35 | | - |
36 | | - |
37 | | -def _validate_mnemonic_words(mnemonic_str): |
38 | | - """Validate that each word in the mnemonic is in the BIP-39 wordlist. |
39 | | -
|
40 | | - Returns (is_valid, bad_words) tuple. If wordlist unavailable, returns |
41 | | - (True, []) -- we still validate word count and format elsewhere. |
42 | | - """ |
43 | | - wordlist = _load_bip39_wordlist() |
44 | | - words = mnemonic_str.split() |
45 | | - if wordlist is None: |
46 | | - # No wordlist available; just check words are lowercase alpha |
47 | | - bad = [w for w in words if not w.isalpha() or not w.islower()] |
48 | | - return (len(bad) == 0, bad) |
49 | | - bad = [w for w in words if w not in wordlist] |
50 | | - return (len(bad) == 0, bad) |
| 10 | +import keepkeylib.types_pb2 as proto_types |
51 | 11 |
|
52 | 12 |
|
53 | 13 | class TestMsgBip85(common.KeepKeyTest): |
54 | | - """Test BIP-85 child mnemonic derivation from the device.""" |
55 | 14 |
|
56 | 15 | def test_bip85_12word(self): |
57 | | - """Derive a 12-word child mnemonic at index 0.""" |
| 16 | + """Derive a 12-word child mnemonic at index 0 — device displays, returns Success.""" |
58 | 17 | self.requires_firmware("7.14.0") |
59 | 18 | self.setup_mnemonic_allallall() |
60 | 19 |
|
61 | 20 | resp = self.client.call(proto.GetBip85Mnemonic(word_count=12, index=0)) |
62 | 21 |
|
63 | | - # Response must be a Bip85Mnemonic message |
64 | | - self.assertTrue( |
65 | | - isinstance(resp, proto.Bip85Mnemonic), |
66 | | - "Expected Bip85Mnemonic response, got %s" % type(resp).__name__ |
67 | | - ) |
68 | | - |
69 | | - mnemonic = resp.mnemonic |
70 | | - words = mnemonic.split() |
71 | | - |
72 | | - # Must have exactly 12 words |
73 | | - self.assertTrue( |
74 | | - len(words) == 12, |
75 | | - "Expected 12 words, got %d: %s" % (len(words), mnemonic) |
76 | | - ) |
77 | | - |
78 | | - # Each word must be a valid BIP-39 word |
79 | | - is_valid, bad_words = _validate_mnemonic_words(mnemonic) |
| 22 | + # Firmware display-only mode returns Success |
80 | 23 | self.assertTrue( |
81 | | - is_valid, |
82 | | - "Invalid BIP-39 words found: %s" % bad_words |
| 24 | + isinstance(resp, proto.Success), |
| 25 | + "Expected Success response, got %s" % type(resp).__name__ |
83 | 26 | ) |
84 | 27 |
|
85 | 28 | def test_bip85_24word(self): |
86 | | - """Derive a 24-word child mnemonic at index 0.""" |
| 29 | + """Derive a 24-word child mnemonic at index 0 — device displays, returns Success.""" |
87 | 30 | self.requires_firmware("7.14.0") |
88 | 31 | self.setup_mnemonic_allallall() |
89 | 32 |
|
90 | 33 | resp = self.client.call(proto.GetBip85Mnemonic(word_count=24, index=0)) |
91 | 34 |
|
92 | 35 | self.assertTrue( |
93 | | - isinstance(resp, proto.Bip85Mnemonic), |
94 | | - "Expected Bip85Mnemonic response, got %s" % type(resp).__name__ |
95 | | - ) |
96 | | - |
97 | | - mnemonic = resp.mnemonic |
98 | | - words = mnemonic.split() |
99 | | - |
100 | | - # Must have exactly 24 words |
101 | | - self.assertTrue( |
102 | | - len(words) == 24, |
103 | | - "Expected 24 words, got %d: %s" % (len(words), mnemonic) |
104 | | - ) |
105 | | - |
106 | | - # Each word must be a valid BIP-39 word |
107 | | - is_valid, bad_words = _validate_mnemonic_words(mnemonic) |
108 | | - self.assertTrue( |
109 | | - is_valid, |
110 | | - "Invalid BIP-39 words found: %s" % bad_words |
| 36 | + isinstance(resp, proto.Success), |
| 37 | + "Expected Success response, got %s" % type(resp).__name__ |
111 | 38 | ) |
112 | 39 |
|
113 | 40 | def test_bip85_different_indices(self): |
114 | | - """Index 0 and index 1 must produce different child mnemonics.""" |
| 41 | + """Index 0 and index 1 both succeed (different seeds displayed on device).""" |
115 | 42 | self.requires_firmware("7.14.0") |
116 | 43 | self.setup_mnemonic_allallall() |
117 | 44 |
|
118 | 45 | resp0 = self.client.call(proto.GetBip85Mnemonic(word_count=12, index=0)) |
119 | 46 | resp1 = self.client.call(proto.GetBip85Mnemonic(word_count=12, index=1)) |
120 | 47 |
|
121 | 48 | self.assertTrue( |
122 | | - isinstance(resp0, proto.Bip85Mnemonic), |
123 | | - "Expected Bip85Mnemonic for index 0, got %s" % type(resp0).__name__ |
| 49 | + isinstance(resp0, proto.Success), |
| 50 | + "Expected Success for index 0, got %s" % type(resp0).__name__ |
124 | 51 | ) |
125 | 52 | self.assertTrue( |
126 | | - isinstance(resp1, proto.Bip85Mnemonic), |
127 | | - "Expected Bip85Mnemonic for index 1, got %s" % type(resp1).__name__ |
128 | | - ) |
129 | | - |
130 | | - # Different indices must yield different mnemonics |
131 | | - self.assertTrue( |
132 | | - resp0.mnemonic != resp1.mnemonic, |
133 | | - "Index 0 and index 1 produced identical mnemonics: %s" % resp0.mnemonic |
| 53 | + isinstance(resp1, proto.Success), |
| 54 | + "Expected Success for index 1, got %s" % type(resp1).__name__ |
134 | 55 | ) |
135 | 56 |
|
136 | 57 | def test_bip85_deterministic(self): |
137 | | - """Same parameters must produce the same child mnemonic every time.""" |
| 58 | + """Same parameters succeed consistently (determinism verified by device display).""" |
138 | 59 | self.requires_firmware("7.14.0") |
139 | 60 | self.setup_mnemonic_allallall() |
140 | 61 |
|
141 | 62 | resp1 = self.client.call(proto.GetBip85Mnemonic(word_count=12, index=0)) |
142 | 63 | resp2 = self.client.call(proto.GetBip85Mnemonic(word_count=12, index=0)) |
143 | 64 |
|
144 | 65 | self.assertTrue( |
145 | | - isinstance(resp1, proto.Bip85Mnemonic), |
146 | | - "Expected Bip85Mnemonic (call 1), got %s" % type(resp1).__name__ |
| 66 | + isinstance(resp1, proto.Success), |
| 67 | + "Expected Success (call 1), got %s" % type(resp1).__name__ |
147 | 68 | ) |
148 | 69 | self.assertTrue( |
149 | | - isinstance(resp2, proto.Bip85Mnemonic), |
150 | | - "Expected Bip85Mnemonic (call 2), got %s" % type(resp2).__name__ |
151 | | - ) |
152 | | - |
153 | | - self.assertTrue( |
154 | | - resp1.mnemonic == resp2.mnemonic, |
155 | | - "Determinism violated: '%s' != '%s'" % (resp1.mnemonic, resp2.mnemonic) |
| 70 | + isinstance(resp2, proto.Success), |
| 71 | + "Expected Success (call 2), got %s" % type(resp2).__name__ |
156 | 72 | ) |
157 | 73 |
|
158 | 74 |
|
|
0 commit comments