Example #1
0
    def _event_create_menu(self, position):
        account_id = self._account_id

        menu = QMenu()

        # What the user clicked on.
        menu_index = self.indexAt(position)
        menu_source_index = get_source_index(menu_index, _ItemModel)

        if menu_source_index.row() != -1:
            menu_line = self._data[menu_source_index.row()]
            menu_column = menu_source_index.column()
            column_title = self._headers[menu_column]
            if menu_column == 0:
                copy_text = get_key_text(menu_line)
            else:
                copy_text = str(menu_source_index.model().data(
                    menu_source_index, Qt.DisplayRole)).strip()
            menu.addAction(
                _("Copy {}").format(column_title),
                lambda: self._main_window.app.clipboard().setText(copy_text))

        # The row selection.
        selected_indexes = self.selectedIndexes()
        if len(selected_indexes):
            # This is an index on the sort/filter model, translate it to the base model.
            selected = []
            for selected_index in selected_indexes:
                base_index = get_source_index(selected_index, _ItemModel)

                row = base_index.row()
                column = base_index.column()
                line = self._data[row]
                selected.append(
                    (row, column, line, selected_index, base_index))

            is_multisig = isinstance(self._account, MultisigAccount)

            rows = set(v[0] for v in selected)
            multi_select = len(rows) > 1

            if not multi_select:
                row, column, line, selected_index, base_index = selected[0]
                key_id = line.keyinstance_id
                menu.addAction(
                    _('Details'),
                    lambda: self._main_window.show_key(self._account, key_id))
                if column == LABEL_COLUMN:
                    menu.addAction(
                        _("Edit {}").format(column_title),
                        lambda: self.edit(selected_index))
                menu.addAction(
                    _("Request payment"), lambda: self._main_window.
                    _receive_view.receive_at_id(key_id))
                if self._account.can_export():
                    menu.addAction(
                        _("Private key"),
                        lambda: self._main_window.show_private_key(
                            self._account, key_id))
                if not is_multisig and not self._account.is_watching_only():
                    menu.addAction(
                        _("Sign/verify message"),
                        lambda: self._main_window.sign_verify_message(
                            self._account, key_id))
                    menu.addAction(
                        _("Encrypt/decrypt message"),
                        lambda: self._main_window.encrypt_message(
                            self._account, key_id))

                explore_menu = menu.addMenu(_("View on block explorer"))

                keyinstance = self._account.get_keyinstance(key_id)
                addr_URL = script_URL = None
                if keyinstance.script_type != ScriptType.NONE:
                    script_template = self._account.get_script_template_for_id(
                        key_id)
                    if isinstance(script_template, Address):
                        addr_URL = web.BE_URL(self._main_window.config, 'addr',
                                              script_template)

                    scripthash = scripthash_hex(
                        script_template.to_script_bytes())
                    script_URL = web.BE_URL(self._main_window.config, 'script',
                                            scripthash)

                addr_action = explore_menu.addAction(
                    _("By address"), partial(webbrowser.open, addr_URL))
                if not addr_URL:
                    addr_action.setEnabled(False)
                script_action = explore_menu.addAction(
                    _("By script"), partial(webbrowser.open, script_URL))
                if not script_URL:
                    script_action.setEnabled(False)

                for script_type, script in self._account.get_possible_scripts_for_id(
                        key_id):
                    scripthash = scripthash_hex(bytes(script))
                    script_URL = web.BE_URL(self._main_window.config, 'script',
                                            scripthash)
                    if script_URL:
                        explore_menu.addAction(
                            _("As {scripttype}").format(
                                scripttype=script_type.name),
                            partial(webbrowser.open, script_URL))

                if isinstance(self._account, StandardAccount):
                    keystore = self._account.get_keystore()
                    if isinstance(keystore, Hardware_KeyStore):

                        def show_key():
                            self._main_window.run_in_thread(
                                keystore.plugin.show_key, self._account,
                                key_id)

                        menu.addAction(
                            _("Show on {}").format(keystore.plugin.device),
                            show_key)

            # freeze = self._main_window.set_frozen_state
            key_ids = [
                line.keyinstance_id
                for (row, column, line, selected_index, base_index) in selected
            ]
            # if any(self._account.is_frozen_address(addr) for addr in addrs):
            #     menu.addAction(_("Unfreeze"), partial(freeze, self._account, addrs, False))
            # if not all(self._account.is_frozen_address(addr) for addr in addrs):
            #     menu.addAction(_("Freeze"), partial(freeze, self._account, addrs, True))

            coins = self._account.get_spendable_coins(
                domain=key_ids, config=self._main_window.config)
            if coins:
                menu.addAction(_("Spend from"),
                               partial(self._main_window.spend_coins, coins))

        menu.exec_(self.viewport().mapToGlobal(position))
Example #2
0
 def test_address_to_scripthash(self):
     for priv_details in self.priv_pub_addr:
         sh = scripthash_hex(address_from_string(priv_details['address']))
         self.assertEqual(priv_details['scripthash'], sh)