Ejemplo n.º 1
0
def get_address_and_pubkey(main_ui, bip32_path):
    client = main_ui.hw_client
    if client:
        bip32_path.strip()
        if bip32_path.lower().find('m/') >= 0:
            # removing m/ prefix because of keepkey library
            bip32_path = bip32_path[2:]

        if main_ui.config.hw_type in (HWType.trezor, HWType.keepkey):
            if isinstance(bip32_path, str):
                # trezor/keepkey require bip32 path argument as an array of integers
                bip32_path = client.expand_path(bip32_path)

            return {
                'address': client.get_address('Dash', bip32_path, False),
                'publicKey': client.get_public_node(bip32_path).node.public_key
            }

        elif main_ui.config.hw_type == HWType.ledger_nano_s:
            import hw_intf_ledgernano as ledger

            if isinstance(bip32_path, list):
                # ledger requires bip32 path argument as a string
                bip32_path = bip32_path_n_to_string(bip32_path)

            return ledger.get_address_and_pubkey(client, bip32_path)
        else:
            raise Exception('Unknown hwardware wallet type: ' +
                            main_ui.config.hw_type)
Ejemplo n.º 2
0
def get_address(hw_session: HwSessionInfo, bip32_path):
    client = hw_session.hw_client
    if client:
        if isinstance(bip32_path, str):
            bip32_path.strip()
            if bip32_path.lower().find('m/') >= 0:
                # removing m/ prefix because of keepkey library
                bip32_path = bip32_path[2:]

        if hw_session.app_config.hw_type in (HWType.trezor, HWType.keepkey):
            if isinstance(bip32_path, str):
                # trezor/keepkey require bip32 path argument as an array of integers
                bip32_path = client.expand_path(bip32_path)

            return client.get_address(hw_session.app_config.hw_coin_name,
                                      bip32_path, False)

        elif hw_session.app_config.hw_type == HWType.ledger_nano_s:
            import hw_intf_ledgernano as ledger

            if isinstance(bip32_path, list):
                # ledger requires bip32 path argument as a string
                bip32_path = bip32_path_n_to_string(bip32_path)

            adr_pubkey = ledger.get_address_and_pubkey(client, bip32_path)
            return adr_pubkey.get('address')
        else:
            raise Exception('Unknown hwardware wallet type: ' +
                            hw_session.app_config.hw_type)
    else:
        raise Exception('HW client not open.')
Ejemplo n.º 3
0
    def _get_address(ctrl, hw_session: HwSessionInfo, bip32_path: str, show_display: bool = False,
                     message_to_display: str = None):
        if ctrl:
            ctrl.dlg_config_fun(dlg_title=DEFAULT_HW_BUSY_TITLE, show_progress_bar=False)
            if message_to_display:
                ctrl.display_msg_fun(message_to_display)
            else:
                ctrl.display_msg_fun('<b>Click the confirmation button on your hardware wallet to exit...</b>')

        client = hw_session.hw_client
        if client:
            if isinstance(bip32_path, str):
                bip32_path.strip()
                if bip32_path.lower().find('m/') >= 0:
                    # removing m/ prefix because of keepkey library
                    bip32_path = bip32_path[2:]

            if hw_session.app_config.hw_type == HWType.trezor:

                from trezorlib import btc
                from trezorlib import exceptions

                try:
                    if isinstance(bip32_path, str):
                        bip32_path = gewel_utils.bip32_path_string_to_n(bip32_path)
                    ret = btc.get_address(client, hw_session.app_config.hw_coin_name, bip32_path, show_display)
                    return ret
                except (CancelException, exceptions.Cancelled):
                    raise CancelException()

            elif hw_session.app_config.hw_type == HWType.keepkey:

                from keepkeylib.client import CallException

                try:
                    if isinstance(bip32_path, str):
                        bip32_path = gewel_utils.bip32_path_string_to_n(bip32_path)
                    return client.get_address(hw_session.app_config.hw_coin_name, bip32_path, show_display)
                except CallException as e:
                    if isinstance(e.args, tuple) and len(e.args) >= 2 and isinstance(e.args[1], str) and \
                            e.args[1].find('cancel') >= 0:
                        raise CancelException('Cancelled')

            elif hw_session.app_config.hw_type == HWType.ledger_nano_s:
                import hw_intf_ledgernano as ledger

                if isinstance(bip32_path, list):
                    # ledger requires bip32 path argument as a string
                    bip32_path = bip32_path_n_to_string(bip32_path)

                adr_pubkey = ledger.get_address_and_pubkey(client, bip32_path, show_display)
                return adr_pubkey.get('address')
            else:
                raise Exception('Unknown hardware wallet type: ' + hw_session.app_config.hw_type)
        else:
            raise Exception('HW client not open.')
Ejemplo n.º 4
0
def get_address_and_pubkey(hw_session: HwSessionInfo, bip32_path):
    client = hw_session.hw_client
    if client:
        if isinstance(bip32_path, str):
            bip32_path.strip()
            if bip32_path.lower().find('m/') >= 0:
                # removing m/ prefix because of keepkey library
                bip32_path = bip32_path[2:]

        if hw_session.app_config.hw_type == HWType.trezor:

            from trezorlib import btc
            if isinstance(bip32_path, str):
                bip32_path = fix_utils.bip32_path_string_to_n(bip32_path)
            return {
                'address':
                btc.get_address(client, hw_session.app_config.hw_coin_name,
                                bip32_path, False),
                'publicKey':
                btc.get_public_node(client, bip32_path).node.public_key
            }

        elif hw_session.app_config.hw_type == HWType.keepkey:
            if isinstance(bip32_path, str):
                bip32_path = fix_utils.bip32_path_string_to_n(bip32_path)
            return {
                'address':
                client.get_address(hw_session.app_config.hw_coin_name,
                                   bip32_path, False),
                'publicKey':
                client.get_public_node(bip32_path).node.public_key
            }

        elif hw_session.app_config.hw_type == HWType.ledger_nano_s:
            import hw_intf_ledgernano as ledger

            if isinstance(bip32_path, list):
                # ledger requires bip32 path argument as a string
                bip32_path = bip32_path_n_to_string(bip32_path)

            return ledger.get_address_and_pubkey(client, bip32_path)
        else:
            raise Exception('Unknown hardware wallet type: ' +
                            hw_session.app_config.hw_type)
Ejemplo n.º 5
0
    def _get_address(ctrl, hw_session: HwSessionInfo, bip32_path: str, show_display: bool = False,
                     message_to_display: str = None):
        if ctrl:
            ctrl.dlg_config_fun(dlg_title="Please confirm", show_progress_bar=False)
            if message_to_display:
                ctrl.display_msg_fun(message_to_display)
            else:
                ctrl.display_msg_fun('<b>Click the confirmation button on your hardware wallet to exit...</b>')

        client = hw_session.hw_client
        if client:
            if isinstance(bip32_path, str):
                bip32_path.strip()
                if bip32_path.lower().find('m/') >= 0:
                    # removing m/ prefix because of keepkey library
                    bip32_path = bip32_path[2:]

            if hw_session.app_config.hw_type == HWType.trezor:

                from trezorlib import btc
                if isinstance(bip32_path, str):
                    bip32_path = dash_utils.bip32_path_string_to_n(bip32_path)
                return btc.get_address(client, hw_session.app_config.hw_coin_name, bip32_path, show_display)

            elif hw_session.app_config.hw_type == HWType.keepkey:

                if isinstance(bip32_path, str):
                    bip32_path = dash_utils.bip32_path_string_to_n(bip32_path)
                return client.get_address(hw_session.app_config.hw_coin_name, bip32_path, show_display)

            elif hw_session.app_config.hw_type == HWType.ledger_nano_s:
                import hw_intf_ledgernano as ledger

                if isinstance(bip32_path, list):
                    # ledger requires bip32 path argument as a string
                    bip32_path = bip32_path_n_to_string(bip32_path)

                adr_pubkey = ledger.get_address_and_pubkey(client, bip32_path)
                return adr_pubkey.get('address')
            else:
                raise Exception('Unknown hardware wallet type: ' + hw_session.app_config.hw_type)
        else:
            raise Exception('HW client not open.')
Ejemplo n.º 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(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))
Ejemplo n.º 7
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):
        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))
Ejemplo n.º 8
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))