Skip to content

Commit

Permalink
add BIP85 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
odudex committed Jun 13, 2024
1 parent 20561fe commit 48bfa69
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/krux/pages/home_pages/bip85.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from embit import bip85
from embit.bip32 import HARDENED_INDEX
from ...display import BOTTOM_PROMPT_LINE
from ...krux_settings import t
from ..settings_page import DIGITS
Expand All @@ -31,8 +32,6 @@
choose_len_mnemonic,
)

MAX_BIP85_CHILD_INDEX = 2**31 - 1


class Bip85(Page):
"""UI to export and load BIP85 entropy"""
Expand All @@ -49,15 +48,16 @@ def export(self):
return None
try:
child_index = int(child)
if child_index > MAX_BIP85_CHILD_INDEX:
raise ValueError
break
except ValueError:
except: # Empty input
continue

if child_index >= HARDENED_INDEX:
self.flash_error(
t("Value %s out of range: [%s, %s]")
% (child_index, 0, MAX_BIP85_CHILD_INDEX)
% (child_index, 0, HARDENED_INDEX - 1)
)
continue
break
bip85_words = bip85.derive_mnemonic(
self.ctx.wallet.key.root,
num_words,
Expand Down
169 changes: 169 additions & 0 deletions tests/pages/home_pages/test_bip85.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
from .test_home import tdata, create_ctx


def test_bip85_wallet_creation(mocker, amigo, tdata):
from krux.krux_settings import Settings
from krux.pages.home_pages.bip85 import Bip85
from krux.wallet import Wallet
from krux.input import BUTTON_ENTER, BUTTON_PAGE, BUTTON_PAGE_PREV

cases = [
# case
# (
# wallet
# Button presses
# BIP85 child mnemonic
# BIP85 child fingerprint
# Loaded?
# )
# 0 - give up at beginning
(
tdata.SINGLESIG_12_WORD_KEY,
[
BUTTON_PAGE_PREV, # Move to "Back"
BUTTON_ENTER, # Confirm
],
None,
None,
False,
),
# 1 - 12w, load
(
tdata.SINGLESIG_12_WORD_KEY,
[
BUTTON_ENTER, # 12 words
BUTTON_ENTER, # Child index 0
BUTTON_PAGE_PREV, # Move to "Go"
BUTTON_ENTER, # Go
BUTTON_ENTER, # Load? Yes
],
"wheat upgrade bind cover echo govern sorry seminar swallow evil gather horse",
"2ddff806",
True,
),
# 2 - 24w in, 12w out, load
(
tdata.SINGLESIG_24_WORD_KEY,
[
BUTTON_ENTER, # 12 words out
BUTTON_ENTER, # Child index 0
BUTTON_PAGE_PREV, # Move to "Go"
BUTTON_ENTER, # Go
BUTTON_ENTER, # Load? Yes
],
"fury deposit tackle carry minimum cool video choice armor frown age space",
"1b6c9237",
True,
),
# 3 - 12w in, 24w out, load
(
tdata.SINGLESIG_12_WORD_KEY,
[
BUTTON_PAGE, # Move to 24 words
BUTTON_ENTER, # Choose 24 words
BUTTON_ENTER, # Child index 0
BUTTON_PAGE_PREV, # Move to "Go"
BUTTON_ENTER, # Go
BUTTON_ENTER, # Load? Yes
],
"vendor autumn garlic peace excite blur powder room delay pink tattoo debate fabric spare donate employ effort accident remove drift page begin same modify",
"263b15ef",
True,
),
# 4 - 12w, don't load
(
tdata.SINGLESIG_12_WORD_KEY,
[
BUTTON_ENTER, # 12 words
BUTTON_ENTER, # Child index 0
BUTTON_PAGE_PREV, # Move to "Go"
BUTTON_ENTER, # Go
BUTTON_PAGE, # Move to Cancel
BUTTON_ENTER, # Cancel
],
"wheat upgrade bind cover echo govern sorry seminar swallow evil gather horse",
"2ddff806",
False,
),
# 12w, ESC
(
tdata.SINGLESIG_12_WORD_KEY,
[
BUTTON_ENTER, # 12 words
*([BUTTON_PAGE_PREV] * 2), # Move to "Esc"
BUTTON_ENTER, # Press "Esc"
BUTTON_ENTER, # Confirm
],
None,
None,
False,
),
# Assert if no index is assigned it stays in the loop
(
tdata.SINGLESIG_12_WORD_KEY,
[
BUTTON_ENTER, # 12 words
BUTTON_PAGE_PREV, # Move to "Go" with no index assigned
BUTTON_ENTER, # Go
# Loop back to initial state
BUTTON_ENTER, # this time assign 0 for index
BUTTON_PAGE_PREV, # Move to "Go" with no index assigned
BUTTON_ENTER, # Go
BUTTON_ENTER, # Load? Yes
],
"wheat upgrade bind cover echo govern sorry seminar swallow evil gather horse",
"2ddff806",
True,
),
# Too big index
(
tdata.SINGLESIG_12_WORD_KEY,
[
BUTTON_ENTER, # 12 words
BUTTON_PAGE, # Move to 1
*([BUTTON_ENTER] * 12), # Type 111111111111
*([BUTTON_PAGE_PREV] * 2), # Move to "Go"
BUTTON_ENTER, # Go
# Invalid value message
BUTTON_ENTER, # Assign index 0
BUTTON_PAGE_PREV, # Move to "Go"
BUTTON_ENTER, # Go
BUTTON_ENTER, # Load? Yes
],
"wheat upgrade bind cover echo govern sorry seminar swallow evil gather horse",
"2ddff806",
True,
),
]

case_num = 0
for case in cases:
print(case_num)
wallet = Wallet(case[0])
ctx = create_ctx(mocker, case[1], wallet)
bip85_ui = Bip85(ctx)
mocker.spy(bip85_ui, "display_mnemonic")
bip85_ui.export()

assert ctx.input.wait_for_button.call_count == len(case[1])

if case[2]:
bip85_ui.display_mnemonic.assert_called_with(
case[2], suffix="Words" + "\n⊚ %s" % case[3]
)
else:
bip85_ui.display_mnemonic.assert_not_called()

if case[4]:
assert ctx.wallet.key.mnemonic == case[2]
else:
assert ctx.wallet.key.mnemonic == case[0].mnemonic
case_num += 1

# Test with hidden mnemonic setting enabled
Settings().security.hide_mnemonic = True
ctx = create_ctx(mocker, cases[1][1], wallet)
bip85_ui = Bip85(ctx)
mocker.spy(bip85_ui, "display_mnemonic")
bip85_ui.export()
ctx.display.draw_centered_text.assert_called_with("⊚ %s" % case[3])

0 comments on commit 48bfa69

Please sign in to comment.