예제 #1
0
def enumerate(password: str = "") -> List[Dict[str, Any]]:
    results = []
    devs = hid.HidTransport.enumerate()
    devs.extend(webusb.WebUsbTransport.enumerate())
    devs.extend(udp.UdpTransport.enumerate())
    for dev in devs:
        d_data: Dict[str, Any] = {}

        d_data["type"] = "trezor"
        d_data["path"] = dev.get_path()

        client = None
        with handle_errors(common_err_msgs["enumerate"], d_data):
            client = TrezorClient(d_data["path"], password)
            try:
                client._prepare_device()
            except TypeError:
                continue
            if "trezor" not in client.client.features.vendor:
                continue

            d_data["model"] = "trezor_" + client.client.features.model.lower()
            if d_data["path"] == "udp:127.0.0.1:21324":
                d_data["model"] += "_simulator"

            d_data["needs_pin_sent"] = (
                client.client.features.pin_protection
                and not client.client.features.unlocked
            )
            if client.client.features.model == "1":
                d_data[
                    "needs_passphrase_sent"
                ] = (
                    client.client.features.passphrase_protection
                )  # always need the passphrase sent for Trezor One if it has passphrase protection enabled
            else:
                d_data["needs_passphrase_sent"] = False
            if d_data["needs_pin_sent"]:
                raise DeviceNotReadyError(
                    "Trezor is locked. Unlock by using 'promptpin' and then 'sendpin'."
                )
            if d_data["needs_passphrase_sent"] and not password:
                raise DeviceNotReadyError(
                    "Passphrase needs to be specified before the fingerprint information can be retrieved"
                )
            if client.client.features.initialized:
                d_data["fingerprint"] = client.get_master_fingerprint().hex()
                d_data[
                    "needs_passphrase_sent"
                ] = False  # Passphrase is always needed for the above to have worked, so it's already sent
            else:
                d_data["error"] = "Not initialized"
                d_data["code"] = DEVICE_NOT_INITIALIZED

        if client:
            client.close()

        results.append(d_data)
    return results
예제 #2
0
def enumerate(password: str = "") -> List[Dict[str, object]]:
    """
    Enumerate all BitBox02 devices. Bootloaders excluded.
    """
    result = []
    for device_info in devices.get_any_bitbox02s():
        path = device_info["path"].decode()
        client = Bitbox02Client(path)
        client.set_noise_config(SilentNoiseConfig())
        d_data: Dict[str, object] = {}
        bb02 = None
        with handle_errors(common_err_msgs["enumerate"], d_data):
            bb02 = client.init(expect_initialized=None)
        version, platform, edition, unlocked = bitbox02.BitBox02.get_info(
            client.transport
        )
        if platform != Platform.BITBOX02:
            client.close()
            continue
        if edition not in (BitBox02Edition.MULTI, BitBox02Edition.BTCONLY):
            client.close()
            continue

        assert isinstance(edition, BitBox02Edition)

        d_data.update(
            {
                "type": "bitbox02",
                "path": path,
                "model": {
                    BitBox02Edition.MULTI: "bitbox02_multi",
                    BitBox02Edition.BTCONLY: "bitbox02_btconly",
                }[edition],
                "needs_pin_sent": False,
                "needs_passphrase_sent": False,
            }
        )

        if bb02 is not None:
            with handle_errors(common_err_msgs["enumerate"], d_data):
                if not bb02.device_info()["initialized"]:
                    raise DeviceNotReadyError(
                        "BitBox02 is not initialized. Please initialize it using the BitBoxApp."
                    )
                elif not unlocked:
                    raise DeviceNotReadyError(
                        "Please load wallet to unlock."
                        if _using_external_gui
                        else "Please use any subcommand to unlock"
                    )
                d_data["fingerprint"] = client.get_master_fingerprint_hex()

        result.append(d_data)

        client.close()
    return result
예제 #3
0
def enumerate(password=""):
    results = []
    for dev in enumerate_devices():
        # enumerate_devices filters to Trezors and Keepkeys.
        # Only allow Trezors and unknowns. Unknown devices will reach the check for vendor later
        if dev.get_usb_vendor_id() not in TREZOR_VENDOR_IDS | {-1}:
            continue
        d_data = {}

        d_data["type"] = "trezor"
        d_data["path"] = dev.get_path()

        client = None
        with handle_errors(common_err_msgs["enumerate"], d_data):
            client = TrezorClient(d_data["path"], password)
            client.client.init_device()
            if "trezor" not in client.client.features.vendor:
                continue

            d_data["model"] = "trezor_" + client.client.features.model.lower()
            if d_data["path"] == "udp:127.0.0.1:21324":
                d_data["model"] += "_simulator"

            d_data["needs_pin_sent"] = (client.client.features.pin_protection
                                        and
                                        not client.client.features.pin_cached)
            if client.client.features.model == "1":
                d_data["needs_passphrase_sent"] = (
                    client.client.features.passphrase_protection
                )  # always need the passphrase sent for Trezor One if it has passphrase protection enabled
            else:
                d_data["needs_passphrase_sent"] = False
            if d_data["needs_pin_sent"]:
                raise DeviceNotReadyError(
                    "Trezor is locked. Unlock by using 'promptpin' and then 'sendpin'."
                )
            if d_data["needs_passphrase_sent"] and not password:
                raise DeviceNotReadyError(
                    "Passphrase needs to be specified before the fingerprint information can be retrieved"
                )
            if client.client.features.initialized:
                d_data["fingerprint"] = client.get_master_fingerprint_hex()
                # Passphrase is always needed for the above to have worked,
                # so it's already sent
                d_data["needs_passphrase_sent"] = False
            else:
                d_data["error"] = "Not initialized"
                d_data["code"] = DEVICE_NOT_INITIALIZED

        if client:
            client.close()

        results.append(d_data)
    return results
예제 #4
0
def enumerate(password=''):
    results = []
    for dev in enumerate_devices():
        # enumerate_devices filters to Trezors and Keepkeys.
        # Only allow Trezors and unknowns. Unknown devices will reach the check for vendor later
        if dev.get_usb_vendor_id() not in TREZOR_VENDOR_IDS | {-1}:
            continue
        d_data = {}

        d_data['type'] = 'trezor'
        d_data['path'] = dev.get_path()

        client = None
        with handle_errors(common_err_msgs["enumerate"], d_data):
            client = TrezorClient(d_data['path'], password)
            client.client.init_device()
            if 'trezor' not in client.client.features.vendor:
                continue

            d_data['model'] = 'trezor_' + client.client.features.model.lower()
            if d_data['path'] == 'udp:127.0.0.1:21324':
                d_data['model'] += '_simulator'

            d_data[
                'needs_pin_sent'] = client.client.features.pin_protection and not client.client.features.pin_cached
            if client.client.features.model == '1':
                d_data[
                    'needs_passphrase_sent'] = client.client.features.passphrase_protection  # always need the passphrase sent for Trezor One if it has passphrase protection enabled
            else:
                d_data['needs_passphrase_sent'] = False
            if d_data['needs_pin_sent']:
                raise DeviceNotReadyError(
                    'Trezor is locked. Unlock by using \'promptpin\' and then \'sendpin\'.'
                )
            if d_data['needs_passphrase_sent'] and not password:
                raise DeviceNotReadyError(
                    "Passphrase needs to be specified before the fingerprint information can be retrieved"
                )
            if client.client.features.initialized:
                d_data['fingerprint'] = client.get_master_fingerprint_hex()
                d_data[
                    'needs_passphrase_sent'] = False  # Passphrase is always needed for the above to have worked, so it's already sent
            else:
                d_data['error'] = 'Not initialized'
                d_data['code'] = DEVICE_NOT_INITIALIZED

        if client:
            client.close()

        results.append(d_data)
    return results
예제 #5
0
 def _check_unlocked(self):
     self.coin_name = 'Testnet' if self.is_testnet else 'Bitcoin'
     self.client.init_device()
     if self.client.features.model == 'T':
         self.client.ui.disallow_passphrase()
     if self.client.features.pin_protection and not self.client.features.pin_cached:
         raise DeviceNotReadyError('{} is locked. Unlock by using \'promptpin\' and then \'sendpin\'.'.format(self.type))
예제 #6
0
 def _check_unlocked(self):
     self.coin_name = "Testnet" if self.is_testnet else "Bitcoin"
     self.client.init_device()
     if self.client.features.model == "T":
         self.client.ui.disallow_passphrase()
     if self.client.features.pin_protection and not self.client.features.pin_cached:
         raise DeviceNotReadyError(
             "{} is locked. Unlock by using 'promptpin' and then 'sendpin'."
             .format(self.type))
예제 #7
0
 def _check_unlocked(self) -> None:
     self._prepare_device()
     if self.client.features.model == "T" and isinstance(
         self.client.ui, PassphraseUI
     ):
         self.client.ui.disallow_passphrase()
     if self.client.features.pin_protection and not self.client.features.unlocked:
         raise DeviceNotReadyError(
             "{} is locked. Unlock by using 'promptpin' and then 'sendpin'.".format(
                 self.type
             )
         )
예제 #8
0
 def func(*args, **kwargs):  # type: ignore
     """ Wraps f, mapping exceptions. """
     try:
         return f(*args, **kwargs)
     except UserAbortException:
         raise ActionCanceledError("{} canceled".format(f.__name__))
     except Bitbox02Exception as exc:
         if exc.code in (ERR_GENERIC, ERR_INVALID_INPUT):
             raise BitBox02Error(str(exc))
         raise exc
     except FirmwareVersionOutdatedException as exc:
         raise DeviceNotReadyError(str(exc))
예제 #9
0
    def show_pairing(self, code: str, device_response: Callable[[], bool]) -> bool:
        if _using_external_gui:
            # The user can't see the pairing in the terminal. The
            # output format is also not appropriate for parsing by
            # external tools doing inter process communication using
            # stdin/stdout. For now, we direct the user to pair in the
            # BitBoxApp instead.
            raise DeviceNotReadyError(_unpaired_errmsg)

        print("Please compare and confirm the pairing code on your BitBox02:")
        print(code)
        if not device_response():
            return False
        return input("Accept pairing? [y]/n: ").strip() != "n"
예제 #10
0
 def show_pairing(self, code: str, device_response: Callable[[], bool]) -> bool:
     raise DeviceNotReadyError(_unpaired_errmsg)