def connect_hw(hw_type, ask_for_pin_fun, ask_for_pass_fun):
    try:
        if hw_type == 'TREZOR':
            import hw_intf_trezor as trezor
            import trezorlib.client as client
            try:
                return trezor.connect_trezor(ask_for_pin_fun, ask_for_pass_fun)
            except client.PinException as e:
                raise HardwareWalletPinException(e.args[1])
        elif hw_type == 'KEEPKEY':
            import hw_intf_keepkey as keepkey
            import keepkeylib.client as client
            try:
                return keepkey.connect_keepkey(ask_for_pin_fun,
                                               ask_for_pass_fun)
            except client.PinException as e:
                raise HardwareWalletPinException(e.args[1])
        else:
            logging.error('Unsupported HW type: ' + str(hw_type))
    except:
        logging.exception('Exception occurred')
示例#2
0
def connect_hw(hw_type, ask_for_pin_fun, ask_for_pass_fun):
    if hw_type == HWType.trezor:
        import hw_intf_trezor as trezor
        import trezorlib.client as client
        try:
            return trezor.connect_trezor(ask_for_pin_fun, ask_for_pass_fun)
        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.keepkey:
        import hw_intf_keepkey as keepkey
        import keepkeylib.client as client
        try:
            return keepkey.connect_keepkey(ask_for_pin_fun, ask_for_pass_fun)
        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.ledger_nano_s:
        import hw_intf_ledgernano as ledger
        return ledger.connect_ledgernano()

    else:
        raise Exception('Unsupported HW type: ' + str(hw_type))
def connect_hw(hw_type):
    control_trezor_keepkey_libs(hw_type)
    if hw_type == HWType.trezor:
        import hw_intf_trezor as trezor
        import trezorlib.client as client
        try:
            return trezor.connect_trezor()
        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.keepkey:
        import hw_intf_keepkey as keepkey
        import keepkeylib.client as client
        try:
            return keepkey.connect_keepkey()
        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.ledger_nano_s:
        import hw_intf_ledgernano as ledger
        return ledger.connect_ledgernano()

    else:
        raise Exception('Invalid HW type: ' + str(hw_type))
示例#4
0
def connect_hw(hw_session: Optional[HwSessionInfo],
               hw_type: HWType,
               device_id: Optional[str] = 'NFC',
               passphrase_encoding: Optional[str] = None):
    """
    Initializes connection with a hardware wallet.
    :param hw_type: symbol of the hardware wallet type
    :param passphrase_encoding: (for Keepkey only) it allows forcing the passphrase encoding compatible with BIP-39
        standard (NFKD), which is used by Trezor devices; by default Keepkey uses non-standard encoding (NFC).
    :return:
    """
    def get_session_info_trezor(cli, hw_session: HwSessionInfo):
        path = dash_utils.get_default_bip32_base_path(
            hw_session.app_config.dash_network)
        path_n = dash_utils.bip32_path_string_to_n(path)
        pub = cli.get_public_node(path_n).node.public_key
        hw_session.set_base_info(path, pub)

    control_trezor_keepkey_libs(hw_type)
    if hw_type == HWType.trezor:
        import hw_intf_trezor as trezor
        import trezorlib.client as client
        try:
            cli = trezor.connect_trezor(device_id=device_id)
            if cli and hw_session:
                try:
                    get_session_info_trezor(cli, hw_session)
                except Exception:
                    # in the case of error close the session
                    disconnect_hw(cli)
                    raise
            return cli
        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.keepkey:
        import hw_intf_keepkey as keepkey
        import keepkeylib.client as client
        try:
            cli = keepkey.connect_keepkey(
                passphrase_encoding=passphrase_encoding, device_id=device_id)
            if cli and hw_session:
                try:
                    get_session_info_trezor(cli, hw_session)
                except Exception:
                    # in the case of error close the session
                    disconnect_hw(cli)
                    raise
            return cli

        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.ledger_nano_s:
        import hw_intf_ledgernano as ledger
        cli = ledger.connect_ledgernano()
        if cli and hw_session:
            try:
                path = dash_utils.get_default_bip32_base_path(
                    hw_session.app_config.dash_network)
                ap = ledger.get_address_and_pubkey(cli, path)
                hw_session.set_base_info(path, ap['publicKey'])
            except Exception:
                # in the case of error close the session
                disconnect_hw(cli)
                raise
        return cli

    else:
        raise Exception('Invalid HW type: ' + str(hw_type))
def connect_hw(hw_session: Optional[HwSessionInfo], hw_type: HWType, device_id: Optional[str] = 'NFC',
               passphrase_encoding: Optional[str] = None):
    """
    Initializes connection with a hardware wallet.
    :param hw_type: symbol of the hardware wallet type
    :param passphrase_encoding: (for Keepkey only) it allows forcing the passphrase encoding compatible with BIP-39
        standard (NFKD), which is used by Trezor devices; by default Keepkey uses non-standard encoding (NFC).
    :return:
    """
    def get_session_info_trezor(get_public_node_fun, hw_session: HwSessionInfo):
        nonlocal hw_type

        def call_get_public_node(ctrl, get_public_node_fun, path_n):
            pk = get_public_node_fun(path_n).node.public_key
            return pk

        path = dash_utils.get_default_bip32_base_path(hw_session.app_config.dash_network)
        path_n = dash_utils.bip32_path_string_to_n(path)

        # show message for Trezor T device while waiting for the user to choose the passphrase input method
        pub = WndUtils.run_thread_dialog(call_get_public_node, (get_public_node_fun, path_n), title='Confirm',
                                         text='<b>Complete the action on your hardware wallet device</b>',
                                         show_window_delay_ms=1000)

        hw_session.set_base_info(path, pub)

    control_trezor_keepkey_libs(hw_type)
    if hw_type == HWType.trezor:
        import hw_intf_trezor as trezor
        import trezorlib.client as client
        from trezorlib import btc
        try:
            cli = trezor.connect_trezor(device_id=device_id)
            if cli and hw_session:
                try:
                    get_public_node_fun = partial(btc.get_public_node, cli)
                    get_session_info_trezor(get_public_node_fun, hw_session)
                except Exception:
                    # in the case of error close the session
                    disconnect_hw(cli)
                    raise
            return cli
        except exceptions.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.keepkey:
        import hw_intf_keepkey as keepkey
        import keepkeylib.client as client
        try:
            cli = keepkey.connect_keepkey(passphrase_encoding=passphrase_encoding, device_id=device_id)
            if cli and hw_session:
                try:
                    get_session_info_trezor(cli.get_public_node, hw_session)
                except Exception:
                    # in the case of error close the session
                    disconnect_hw(cli)
                    raise
            return cli

        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.ledger_nano_s:
        import hw_intf_ledgernano as ledger
        cli = ledger.connect_ledgernano()
        if cli and hw_session:
            try:
                path = dash_utils.get_default_bip32_base_path(hw_session.app_config.dash_network)
                ap = ledger.get_address_and_pubkey(cli, path)
                hw_session.set_base_info(path, ap['publicKey'])
            except Exception:
                # in the case of error close the session
                disconnect_hw(cli)
                raise
        return cli

    else:
        raise Exception('Invalid HW type: ' + str(hw_type))
示例#6
0
def connect_hw(hw_session: Optional[HwSessionInfo],
               hw_type: HWType,
               device_id: Optional[str] = 'NFC',
               passphrase_encoding: Optional[str] = None):
    """
    Initializes connection with a hardware wallet.
    :param hw_type: symbol of the hardware wallet type
    :param passphrase_encoding: (for Keepkey only) it allows forcing the passphrase encoding compatible with BIP-39
        standard (NFKD), which is used by Trezor devices; by default Keepkey uses non-standard encoding (NFC).
    :return:
    """
    def get_session_info_trezor(get_public_node_fun, hw_session: HwSessionInfo,
                                hw_client):
        nonlocal hw_type

        def call_get_public_node(ctrl, get_public_node_fun, path_n):
            pk = get_public_node_fun(path_n).node.public_key
            return pk

        path = fix_utils.get_default_bip32_base_path(
            hw_session.app_config.fix_network)
        path_n = fix_utils.bip32_path_string_to_n(path)

        # show message for Trezor T device while waiting for the user to choose the passphrase input method
        pub = WndUtils.run_thread_dialog(call_get_public_node,
                                         (get_public_node_fun, path_n),
                                         title=DEFAULT_HW_BUSY_TITLE,
                                         text=DEFAULT_HW_BUSY_MESSAGE,
                                         force_close_dlg_callback=partial(
                                             cancel_hw_thread_dialog,
                                             hw_client),
                                         show_window_delay_ms=1000)

        if pub:
            hw_session.set_base_info(path, pub)
        else:
            raise Exception('Couldn\'t read data from the hardware wallet.')

    control_trezor_keepkey_libs(hw_type)
    if hw_type == HWType.trezor:
        import hw_intf_trezor as trezor
        import trezorlib.client as client
        from trezorlib import btc, exceptions
        try:
            if hw_session and hw_session.app_config:
                use_webusb = hw_session.app_config.trezor_webusb
                use_bridge = hw_session.app_config.trezor_bridge
                use_udp = hw_session.app_config.trezor_udp
                use_hid = hw_session.app_config.trezor_hid
            else:
                use_webusb = True
                use_bridge = True
                use_udp = True
                use_hid = True

            cli = trezor.connect_trezor(device_id=device_id,
                                        use_webusb=use_webusb,
                                        use_bridge=use_bridge,
                                        use_udp=use_udp,
                                        use_hid=use_hid)
            if cli and hw_session:
                try:
                    get_public_node_fun = partial(btc.get_public_node, cli)
                    get_session_info_trezor(get_public_node_fun, hw_session,
                                            cli)
                except (CancelException, exceptions.Cancelled):
                    # cancel_hw_operation(cli)
                    disconnect_hw(cli)
                    raise CancelException()
                except Exception as e:
                    # in the case of error close the session
                    disconnect_hw(cli)
                    raise
            return cli
        except exceptions.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.keepkey:
        import hw_intf_keepkey as keepkey
        import keepkeylib.client as client
        try:
            cli = keepkey.connect_keepkey(
                passphrase_encoding=passphrase_encoding, device_id=device_id)
            if cli and hw_session:
                try:
                    get_session_info_trezor(cli.get_public_node, hw_session,
                                            cli)
                except CancelException:
                    cancel_hw_operation(cli)
                    disconnect_hw(cli)
                    raise
                except Exception:
                    # in the case of error close the session
                    disconnect_hw(cli)
                    raise
            return cli

        except client.PinException as e:
            raise HardwareWalletPinException(e.args[1])

    elif hw_type == HWType.ledger_nano_s:
        import hw_intf_ledgernano as ledger
        cli = ledger.connect_ledgernano()
        if cli and hw_session:
            try:
                path = fix_utils.get_default_bip32_base_path(
                    hw_session.app_config.fix_network)
                ap = ledger.get_address_and_pubkey(cli, path)
                hw_session.set_base_info(path, ap['publicKey'])
            except CancelException:
                cancel_hw_operation(cli)
                disconnect_hw(cli)
                raise
            except Exception:
                # in the case of error close the session
                disconnect_hw(cli)
                raise
        return cli

    else:
        raise Exception('Invalid HW type: ' + str(hw_type))