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 = 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=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.')
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)
def restore_cache_settings(self): app_cache.restore_window_size(self) if self.initial_mn_sel is None: mode = app_cache.get_value(CACHE_ITEM_UTXO_SOURCE_MODE, 2, int) if mode in (1, 2, 3): self.utxo_src_mode = mode # activated bip32 account numbers: nrs = app_cache.get_value(CACHE_ITEM_HW_ACCOUNT_NUMBERS, [0, 1, 2, 3, 4], list) nrs.sort() self.hw_account_numbers.clear() self.hw_account_numbers.extend(nrs) # base bip32 path: path = app_cache.get_value(CACHE_ITEM_HW_ACCOUNT_BASE_PATH.replace('%NETWORK%', self.app_config.dash_network), dash_utils.get_default_bip32_base_path(self.app_config.dash_network), str) if dash_utils.validate_bip32_path(path): self.hw_account_base_bip32_path = path else: self.hw_account_base_bip32_path = dash_utils.get_default_bip32_base_path(self.app_config.dash_network) # account number: nr = app_cache.get_value(CACHE_ITEM_HW_ACCOUNT_NUMBER, 0, int) if nr < 0: nr = 0 self.hw_account_number = nr # bip32 path (utxo_src_mode 3) path = app_cache.get_value(CACHE_ITEM_HW_SRC_BIP32_PATH.replace('%NETWORK%', self.app_config.dash_network), dash_utils.get_default_bip32_path(self.app_config.dash_network), str) if dash_utils.validate_bip32_path(path): self.hw_src_bip32_path = path else: self.hw_src_bip32_path = dash_utils.get_default_bip32_path(self.app_config.dash_network) self.grid_column_widths = app_cache.get_value(CACHE_ITEM_COL_WIDTHS, self.table_model.getDefaultColWidths(), list) sel_nasternode = app_cache.get_value( CACHE_ITEM_UTXO_SRC_MASTRNODE.replace('%NETWORK%', self.app_config.dash_network), '', str) if sel_nasternode: if sel_nasternode == '<ALL>': self.mn_src_index = len(self.masternodes) else: for idx, mn in enumerate(self.masternodes): if mn.name == sel_nasternode: self.mn_src_index = idx break # restore last list of used addresses enc_json_str = app_cache.get_value(CACHE_ITEM_LAST_RECIPIENTS.replace('%NETWORK%', self.app_config.dash_network), None, str) if enc_json_str: try: # hw encryption key may be not available so use the generated key to not save addresses as plain text self.encryption_key = base64.urlsafe_b64encode(self.app_config.hw_generated_key) fernet = Fernet(self.encryption_key) enc_json_str = bytes(enc_json_str, 'ascii') json_str = fernet.decrypt(enc_json_str) json_str = json_str.decode('ascii') self.recipient_list_from_cache = simplejson.loads(json_str) except Exception: logging.exception('Cannot restore data from cache.')
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)
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))
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 = 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=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 = 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 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))