Browse Source

Merge pull request #7923 from SomberNight/202208_wallet_labels

wallet: add get_label_for_address, and make get_label private
patch-4
ghost43 2 years ago
committed by GitHub
parent
commit
3c964c9ea0
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      electrum/commands.py
  2. 2
      electrum/gui/kivy/main_window.py
  3. 2
      electrum/gui/kivy/uix/dialogs/addresses.py
  4. 2
      electrum/gui/qml/qeaddressdetails.py
  5. 10
      electrum/gui/qml/qeaddresslistmodel.py
  6. 8
      electrum/gui/qml/qewallet.py
  7. 2
      electrum/gui/qt/address_list.py
  8. 2
      electrum/gui/qt/main_window.py
  9. 2
      electrum/gui/qt/utxo_list.py
  10. 2
      electrum/gui/stdio.py
  11. 4
      electrum/gui/text.py
  12. 2
      electrum/plugins/labels/labels.py
  13. 14
      electrum/wallet.py

2
electrum/commands.py

@ -836,7 +836,7 @@ class Commands:
if balance:
item += (format_satoshis(sum(wallet.get_addr_balance(addr))),)
if labels:
item += (repr(wallet.get_label(addr)),)
item += (repr(wallet.get_label_for_address(addr)),)
out.append(item)
return out

2
electrum/gui/kivy/main_window.py

@ -304,7 +304,7 @@ class ElectrumWindow(App, Logger, EventListener):
def on_event_payment_succeeded(self, wallet, key):
if wallet != self.wallet:
return
description = self.wallet.get_label(key)
description = self.wallet.get_label_for_rhash(key)
self.show_info(_('Payment succeeded') + '\n\n' + description)
self._trigger_update_history()

2
electrum/gui/kivy/uix/dialogs/addresses.py

@ -262,7 +262,7 @@ class AddressesDialog(Factory.Popup):
n = 0
cards = []
for address in _list:
label = wallet.get_label(address)
label = wallet.get_label_for_address(address)
balance = sum(wallet.get_addr_balance(address))
is_used_and_empty = wallet.adb.is_used(address) and balance == 0
if self.show_used == 1 and (balance or is_used_and_empty):

2
electrum/gui/qml/qeaddressdetails.py

@ -117,7 +117,7 @@ class QEAddressDetails(QObject):
self.frozenChanged.emit()
self._scriptType = self._wallet.wallet.get_txin_type(self._address)
self._label = self._wallet.wallet.get_label(self._address)
self._label = self._wallet.wallet.get_label_for_address(self._address)
c, u, x = self._wallet.wallet.get_addr_balance(self._address)
self._balance = QEAmount(amount_sat=c + u + x)
self._pubkeys = self._wallet.wallet.get_public_keys(self._address)

10
electrum/gui/qml/qeaddresslistmodel.py

@ -1,3 +1,5 @@
from typing import TYPE_CHECKING
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject
from PyQt5.QtCore import Qt, QAbstractListModel, QModelIndex
@ -6,8 +8,12 @@ from electrum.util import Satoshis
from .qetypes import QEAmount
if TYPE_CHECKING:
from electrum.wallet import Abstract_Wallet
class QEAddressListModel(QAbstractListModel):
def __init__(self, wallet, parent=None):
def __init__(self, wallet: 'Abstract_Wallet', parent=None):
super().__init__(parent)
self.wallet = wallet
self.init_model()
@ -48,7 +54,7 @@ class QEAddressListModel(QAbstractListModel):
item = {}
item['address'] = address
item['numtx'] = self.wallet.adb.get_address_history_len(address)
item['label'] = self.wallet.get_label(address)
item['label'] = self.wallet.get_label_for_address(address)
c, u, x = self.wallet.get_addr_balance(address)
item['balance'] = QEAmount(amount_sat=c + u + x)
item['held'] = self.wallet.is_frozen_address(address)

8
electrum/gui/qml/qewallet.py

@ -2,7 +2,7 @@ import asyncio
import queue
import threading
import time
from typing import Optional
from typing import Optional, TYPE_CHECKING
from PyQt5.QtCore import pyqtProperty, pyqtSignal, pyqtSlot, QObject, QTimer
@ -22,6 +22,10 @@ from .qetransactionlistmodel import QETransactionListModel
from .qetypes import QEAmount
from .util import QtEventListener, qt_event_listener
if TYPE_CHECKING:
from electrum.wallet import Abstract_Wallet
class QEWallet(AuthMixin, QObject, QtEventListener):
__instances = []
@ -62,7 +66,7 @@ class QEWallet(AuthMixin, QObject, QtEventListener):
_synchronizing = False
_synchronizing_progress = ''
def __init__(self, wallet, parent=None):
def __init__(self, wallet: 'Abstract_Wallet', parent=None):
super().__init__(parent)
self.wallet = wallet

2
electrum/gui/qt/address_list.py

@ -217,7 +217,7 @@ class AddressList(MyTreeView):
def refresh_row(self, key, row):
assert row is not None
address = key
label = self.wallet.get_label(address)
label = self.wallet.get_label_for_address(address)
num = self.wallet.adb.get_address_history_len(address)
c, u, x = self.wallet.get_addr_balance(address)
balance = c + u + x

2
electrum/gui/qt/main_window.py

@ -1169,7 +1169,7 @@ class ElectrumWindow(QMainWindow, MessageBoxMixin, Logger, QtEventListener):
# sent by lnworker, redundant with invoice_status
if wallet != self.wallet:
return
description = self.wallet.get_label(key)
description = self.wallet.get_label_for_rhash(key)
self.notify(_('Payment sent') + '\n\n' + description)
self.need_update.set()

2
electrum/gui/qt/utxo_list.py

@ -118,7 +118,7 @@ class UTXOList(MyTreeView):
utxo = self._utxo_dict[key]
utxo_item = [self.std_model.item(row, col) for col in self.Columns]
address = utxo.address
label = self.wallet.get_label_for_txid(utxo.prevout.txid.hex()) or self.wallet.get_label(address)
label = self.wallet.get_label_for_txid(utxo.prevout.txid.hex()) or self.wallet.get_label_for_address(address)
utxo_item[self.Columns.LABEL].setText(label)
SELECTED_TO_SPEND_TOOLTIP = _('Coin selected to be spent')
if key in (self._spend_set or set()):

2
electrum/gui/stdio.py

@ -146,7 +146,7 @@ class ElectrumGui(BaseElectrumGui, EventListener):
self.print_list(messages, "%19s %25s "%("Key", "Value"))
def print_addresses(self):
messages = map(lambda addr: "%30s %30s "%(addr, self.wallet.get_label(addr)), self.wallet.get_addresses())
messages = map(lambda addr: "%30s %30s "%(addr, self.wallet.get_label_for_address(addr)), self.wallet.get_addresses())
self.print_list(messages, "%19s %25s "%("Address", "Label"))
def print_order(self):

4
electrum/gui/text.py

@ -310,7 +310,7 @@ class ElectrumGui(BaseElectrumGui, EventListener):
fmt = self.format_column_width(x, [-50, '*', 15])
messages = [ fmt % (
addr,
self.wallet.get_label(addr),
self.wallet.get_label_for_address(addr),
self.config.format_amount(sum(self.wallet.get_addr_balance(addr)), whitespaces=True)
) for addr in self.wallet.get_addresses() ]
self.print_list(2, x, messages, fmt % ("Address", "Description", "Balance"))
@ -321,7 +321,7 @@ class ElectrumGui(BaseElectrumGui, EventListener):
utxos = self.wallet.get_utxos()
messages = [ fmt % (
utxo.prevout.to_str(),
self.wallet.get_label(utxo.prevout.txid.hex()),
self.wallet.get_label_for_txid(utxo.prevout.txid.hex()),
self.config.format_amount(utxo.value_sats(), whitespaces=True)
) for utxo in utxos]
self.print_list(2, x, sorted(messages), fmt % ("Outpoint", "Description", "Balance"))

2
electrum/plugins/labels/labels.py

@ -154,7 +154,7 @@ class LabelsPlugin(BasePlugin):
result[key] = value
for key, value in result.items():
if force or not wallet.get_label(key):
if force or not wallet._get_label(key):
wallet._set_label(key, value)
self.logger.info(f"received {len(response)} labels")

14
electrum/wallet.py

@ -1337,10 +1337,16 @@ class Abstract_Wallet(ABC, Logger, EventListener):
item['capital_gain'] = Fiat(cg, fx.ccy)
return item
def get_label(self, key: str) -> str:
def _get_label(self, key: str) -> str:
# key is typically: address / txid / LN-payment-hash-hex
return self._labels.get(key) or ''
def get_label_for_address(self, addr: str) -> str:
label = self._labels.get(addr) or ''
if not label and (request := self.get_request_by_address(addr)):
label = request.get_message()
return label
def get_label_for_txid(self, tx_hash: str) -> str:
return self._labels.get(tx_hash) or self._get_default_label_for_txid(tx_hash)
@ -1349,7 +1355,7 @@ class Abstract_Wallet(ABC, Logger, EventListener):
if not self.db.get_txi_addresses(tx_hash):
labels = []
for addr in self.db.get_txo_addresses(tx_hash):
label = self._labels.get(addr)
label = self.get_label_for_address(addr)
if label:
labels.append(label)
return ', '.join(labels)
@ -1357,7 +1363,7 @@ class Abstract_Wallet(ABC, Logger, EventListener):
def _get_default_label_for_rhash(self, rhash: str) -> str:
req = self.get_request(rhash)
return req.message if req else ''
return req.get_message() if req else ''
def get_label_for_rhash(self, rhash: str) -> str:
return self._labels.get(rhash) or self._get_default_label_for_rhash(rhash)
@ -2287,7 +2293,7 @@ class Abstract_Wallet(ABC, Logger, EventListener):
def get_request_URI(self, req: Invoice) -> str:
# todo: should be a method of invoice?
addr = req.get_address()
message = self.get_label(addr)
message = self.get_label_for_address(addr)
amount = req.get_amount_sat()
extra_query_params = {}
if req.time and req.exp:

Loading…
Cancel
Save