def test_256bit_passphrase(client: Client):
    """
    BIP32 Root Key for passphrase TREZOR:
    provided by Andrew, address calculated via https://iancoleman.io/bip39/
    xprv9s21ZrQH143K2UspC9FRPfQC9NcDB4HPkx1XG9UEtuceYtpcCZ6ypNZWdgfxQ9dAFVeD1F4Zg4roY7nZm2LB7THPD6kaCege3M7EuS8v85c
    """
    assert client.features.passphrase_protection is True
    client.use_passphrase("TREZOR")
    address = get_test_address(client)
    assert address == "mxVtGxUJ898WLzPMmy6PT1FDHD1GUCWGm7"

    client.clear_session()
    client.use_passphrase("ROZERT")
    address_compare = get_test_address(client)
    assert address != address_compare
def test_128bit_passphrase(client: Client):
    """
    BIP32 Root Key for passphrase TREZOR:
    provided by Andrew, address calculated via https://iancoleman.io/bip39/
    xprv9s21ZrQH143K3dzDLfeY3cMp23u5vDeFYftu5RPYZPucKc99mNEddU4w99GxdgUGcSfMpVDxhnR1XpJzZNXRN1m6xNgnzFS5MwMP6QyBRKV
    """
    assert client.features.passphrase_protection is True
    client.use_passphrase("TREZOR")
    address = get_test_address(client)
    assert address == "mkKDUMRR1CcK8eLAzCZAjKnNbCquPoWPxN"

    client.clear_session()
    client.use_passphrase("ROZERT")
    address_compare = get_test_address(client)
    assert address != address_compare
예제 #3
0
def _get_xpub(client: Client, passphrase=None):
    """Get XPUB and check that the appropriate passphrase flow has happened."""
    if passphrase is not None:
        expected_responses = [
            messages.PassphraseRequest,
            messages.ButtonRequest,
            messages.ButtonRequest,
            messages.PublicKey,
        ]
    else:
        expected_responses = [messages.PublicKey]

    with client:
        client.use_passphrase(passphrase or "")
        client.set_expected_responses(expected_responses)
        result = client.call(XPUB_REQUEST)
        return result.xpub
예제 #4
0
def test_load_device_2(client: Client):
    debuglink.load_device(
        client,
        mnemonic=MNEMONIC12,
        pin="1234",
        passphrase_protection=True,
        label="test",
    )
    client.use_passphrase("passphrase")
    state = client.debug.state()
    assert state.mnemonic_secret == MNEMONIC12.encode()

    if client.features.model == "1":
        # we do not send PIN in DebugLinkState in Core
        assert state.pin == "1234"
    assert state.passphrase_protection is True

    address = get_test_address(client)
    assert address == "mx77VZjTVixVsU7nCtAKHnGFdsyNCnsWWw"
예제 #5
0
def test_session_recycling(client: Client):
    session_id_orig = client.session_id
    with client:
        client.set_expected_responses([
            messages.PassphraseRequest,
            messages.ButtonRequest,
            messages.ButtonRequest,
            messages.Address,
        ])
        client.use_passphrase("TREZOR")
        address = get_test_address(client)

    # create and close 100 sessions - more than the session limit
    for _ in range(100):
        client.init_device(new_session=True)
        client.end_session()

    # it should still be possible to resume the original session
    with client:
        # passphrase should still be cached
        client.set_expected_responses([messages.Features, messages.Address])
        client.use_passphrase("TREZOR")
        client.init_device(session_id=session_id_orig)
        assert address == get_test_address(client)
예제 #6
0
def test_load_device_utf(client: Client):
    words_nfkd = u"Pr\u030ci\u0301s\u030cerne\u030c z\u030clut\u030couc\u030cky\u0301 ku\u030an\u030c u\u0301pe\u030cl d\u030ca\u0301belske\u0301 o\u0301dy za\u0301ker\u030cny\u0301 uc\u030cen\u030c be\u030cz\u030ci\u0301 pode\u0301l zo\u0301ny u\u0301lu\u030a"
    words_nfc = u"P\u0159\xed\u0161ern\u011b \u017elu\u0165ou\u010dk\xfd k\u016f\u0148 \xfap\u011bl \u010f\xe1belsk\xe9 \xf3dy z\xe1ke\u0159n\xfd u\u010de\u0148 b\u011b\u017e\xed pod\xe9l z\xf3ny \xfal\u016f"
    words_nfkc = u"P\u0159\xed\u0161ern\u011b \u017elu\u0165ou\u010dk\xfd k\u016f\u0148 \xfap\u011bl \u010f\xe1belsk\xe9 \xf3dy z\xe1ke\u0159n\xfd u\u010de\u0148 b\u011b\u017e\xed pod\xe9l z\xf3ny \xfal\u016f"
    words_nfd = u"Pr\u030ci\u0301s\u030cerne\u030c z\u030clut\u030couc\u030cky\u0301 ku\u030an\u030c u\u0301pe\u030cl d\u030ca\u0301belske\u0301 o\u0301dy za\u0301ker\u030cny\u0301 uc\u030cen\u030c be\u030cz\u030ci\u0301 pode\u0301l zo\u0301ny u\u0301lu\u030a"

    passphrase_nfkd = (
        u"Neuve\u030cr\u030citelne\u030c bezpec\u030cne\u0301 hesli\u0301c\u030cko"
    )
    passphrase_nfc = u"Neuv\u011b\u0159iteln\u011b bezpe\u010dn\xe9 hesl\xed\u010dko"
    passphrase_nfkc = u"Neuv\u011b\u0159iteln\u011b bezpe\u010dn\xe9 hesl\xed\u010dko"
    passphrase_nfd = (
        u"Neuve\u030cr\u030citelne\u030c bezpec\u030cne\u0301 hesli\u0301c\u030cko"
    )

    debuglink.load_device(
        client,
        mnemonic=words_nfkd,
        pin="",
        passphrase_protection=True,
        label="test",
        language="en-US",
        skip_checksum=True,
    )
    client.use_passphrase(passphrase_nfkd)
    address_nfkd = get_test_address(client)

    device.wipe(client)
    debuglink.load_device(
        client,
        mnemonic=words_nfc,
        pin="",
        passphrase_protection=True,
        label="test",
        language="en-US",
        skip_checksum=True,
    )
    client.use_passphrase(passphrase_nfc)
    address_nfc = get_test_address(client)

    device.wipe(client)
    debuglink.load_device(
        client,
        mnemonic=words_nfkc,
        pin="",
        passphrase_protection=True,
        label="test",
        language="en-US",
        skip_checksum=True,
    )
    client.use_passphrase(passphrase_nfkc)
    address_nfkc = get_test_address(client)

    device.wipe(client)
    debuglink.load_device(
        client,
        mnemonic=words_nfd,
        pin="",
        passphrase_protection=True,
        label="test",
        language="en-US",
        skip_checksum=True,
    )
    client.use_passphrase(passphrase_nfd)
    address_nfd = get_test_address(client)

    assert address_nfkd == address_nfc
    assert address_nfkd == address_nfkc
    assert address_nfkd == address_nfd
예제 #7
0
def client(request: pytest.FixtureRequest,
           _raw_client: Client) -> Generator[Client, None, None]:
    """Client fixture.

    Every test function that requires a client instance will get it from here.
    If we can't connect to a debuggable device, the test will fail.
    If 'skip_t2' is used and TT is connected, the test is skipped. Vice versa with T1
    and 'skip_t1'.

    The client instance is wiped and preconfigured with "all all all..." mnemonic, no
    password and no pin. It is possible to customize this with the `setup_client`
    marker.

    To specify a custom mnemonic and/or custom pin and/or enable passphrase:

    @pytest.mark.setup_client(mnemonic=MY_MNEMONIC, pin="9999", passphrase=True)

    To receive a client instance that was not initialized:

    @pytest.mark.setup_client(uninitialized=True)
    """
    if request.node.get_closest_marker(
            "skip_t2") and _raw_client.features.model == "T":
        pytest.skip("Test excluded on Trezor T")
    if request.node.get_closest_marker(
            "skip_t1") and _raw_client.features.model == "1":
        pytest.skip("Test excluded on Trezor 1")

    sd_marker = request.node.get_closest_marker("sd_card")
    if sd_marker and not _raw_client.features.sd_card_present:
        raise RuntimeError("This test requires SD card.\n"
                           "To skip all such tests, run:\n"
                           "  pytest -m 'not sd_card' <test path>")

    test_ui = request.config.getoption("ui")

    _raw_client.reset_debug_features()
    _raw_client.open()
    try:
        _raw_client.init_device()
    except Exception:
        request.session.shouldstop = "Failed to communicate with Trezor"
        pytest.fail("Failed to communicate with Trezor")

    if test_ui:
        # we need to reseed before the wipe
        _raw_client.debug.reseed(0)

    if sd_marker:
        should_format = sd_marker.kwargs.get("formatted", True)
        _raw_client.debug.erase_sd_card(format=should_format)

    wipe_device(_raw_client)

    setup_params = dict(
        uninitialized=False,
        mnemonic=" ".join(["all"] * 12),
        pin=None,
        passphrase=False,
        needs_backup=False,
        no_backup=False,
    )

    marker = request.node.get_closest_marker("setup_client")
    if marker:
        setup_params.update(marker.kwargs)

    use_passphrase = setup_params["passphrase"] is True or isinstance(
        setup_params["passphrase"], str)

    if not setup_params["uninitialized"]:
        debuglink.load_device(
            _raw_client,
            mnemonic=setup_params["mnemonic"],
            pin=setup_params["pin"],
            passphrase_protection=use_passphrase,
            label="test",
            language="en-US",
            needs_backup=setup_params["needs_backup"],
            no_backup=setup_params["no_backup"],
        )

        if _raw_client.features.model == "T":
            apply_settings(_raw_client, experimental_features=True)

        if use_passphrase and isinstance(setup_params["passphrase"], str):
            _raw_client.use_passphrase(setup_params["passphrase"])

        _raw_client.clear_session()

    if test_ui:
        with ui_tests.screen_recording(_raw_client, request):
            yield _raw_client
    else:
        yield _raw_client

    _raw_client.close()